JavaScript&&Nodejs

protobufjs typescript 静态模块使用方式

2022-08-29  本文已影响0人  EasyNetCN

message.proto

syntax = "proto3";
package protocol;
option go_package = "/protocol";

message Message {
    string clientId = 1;     // websocket客户端ID
    string fromUsername = 2; // 发送消息用户的用户名
    string from = 3;         // 发送消息用户ID
    string to = 4;           // 发送给对端用户的ID
    string content = 5;      // 文本消息内容
    int32 contentType = 6;   // 消息内容类型:1.文字 2.普通文件 3.图片 4.音频 5.视频 6.语音聊天 7.视频聊天
    string type = 7;         // 消息传输类型:如果是心跳消息,该内容为heatbeat,在线视频或者音频为webrtc
    int32 messageType = 8;   // 消息类型,1.单聊 2.群聊
    string fileKey = 9;      // OSS文件Key
    string fileUrl = 10;     //文件链接地址
}

生成js静态模块文件

pbjs -t static-module -w commonjs -o  src/proto/message.js src/proto/message.proto

生成ts声明文件

pbts -o src/proto/message.d.ts src/proto/message.js

修改生成的js静态模块文件

/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
"use strict";

import $protobuf from "protobufjs/minimal";

// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});

$root.protocol = (function () {

    /**
     * Namespace protocol.
     * @exports protocol
     * @namespace
     */
    var protocol = {};

    protocol.Message = (function () {

        /**
         * Properties of a Message.
         * @memberof protocol
         * @interface IMessage
         * @property {string|null} [clientId] Message clientId
         * @property {string|null} [fromUsername] Message fromUsername
         * @property {string|null} [from] Message from
         * @property {string|null} [to] Message to
         * @property {string|null} [content] Message content
         * @property {number|null} [contentType] Message contentType
         * @property {string|null} [type] Message type
         * @property {number|null} [messageType] Message messageType
         * @property {string|null} [fileKey] Message fileKey
         * @property {string|null} [fileUrl] Message fileUrl
         */

        /**
         * Constructs a new Message.
         * @memberof protocol
         * @classdesc Represents a Message.
         * @implements IMessage
         * @constructor
         * @param {protocol.IMessage=} [properties] Properties to set
         */
        function Message(properties) {
            if (properties)
                for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
                    if (properties[keys[i]] != null)
                        this[keys[i]] = properties[keys[i]];
        }

        /**
         * Message clientId.
         * @member {string} clientId
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.clientId = "";

        /**
         * Message fromUsername.
         * @member {string} fromUsername
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.fromUsername = "";

        /**
         * Message from.
         * @member {string} from
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.from = "";

        /**
         * Message to.
         * @member {string} to
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.to = "";

        /**
         * Message content.
         * @member {string} content
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.content = "";

        /**
         * Message contentType.
         * @member {number} contentType
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.contentType = 0;

        /**
         * Message type.
         * @member {string} type
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.type = "";

        /**
         * Message messageType.
         * @member {number} messageType
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.messageType = 0;

        /**
         * Message fileKey.
         * @member {string} fileKey
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.fileKey = "";

        /**
         * Message fileUrl.
         * @member {string} fileUrl
         * @memberof protocol.Message
         * @instance
         */
        Message.prototype.fileUrl = "";

        /**
         * Creates a new Message instance using the specified properties.
         * @function create
         * @memberof protocol.Message
         * @static
         * @param {protocol.IMessage=} [properties] Properties to set
         * @returns {protocol.Message} Message instance
         */
        Message.create = function create(properties) {
            return new Message(properties);
        };

        /**
         * Encodes the specified Message message. Does not implicitly {@link protocol.Message.verify|verify} messages.
         * @function encode
         * @memberof protocol.Message
         * @static
         * @param {protocol.IMessage} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        Message.encode = function encode(message, writer) {
            if (!writer)
                writer = $Writer.create();
            if (message.clientId != null && Object.hasOwnProperty.call(message, "clientId"))
                writer.uint32(/* id 1, wireType 2 =*/10).string(message.clientId);
            if (message.fromUsername != null && Object.hasOwnProperty.call(message, "fromUsername"))
                writer.uint32(/* id 2, wireType 2 =*/18).string(message.fromUsername);
            if (message.from != null && Object.hasOwnProperty.call(message, "from"))
                writer.uint32(/* id 3, wireType 2 =*/26).string(message.from);
            if (message.to != null && Object.hasOwnProperty.call(message, "to"))
                writer.uint32(/* id 4, wireType 2 =*/34).string(message.to);
            if (message.content != null && Object.hasOwnProperty.call(message, "content"))
                writer.uint32(/* id 5, wireType 2 =*/42).string(message.content);
            if (message.contentType != null && Object.hasOwnProperty.call(message, "contentType"))
                writer.uint32(/* id 6, wireType 0 =*/48).int32(message.contentType);
            if (message.type != null && Object.hasOwnProperty.call(message, "type"))
                writer.uint32(/* id 7, wireType 2 =*/58).string(message.type);
            if (message.messageType != null && Object.hasOwnProperty.call(message, "messageType"))
                writer.uint32(/* id 8, wireType 0 =*/64).int32(message.messageType);
            if (message.fileKey != null && Object.hasOwnProperty.call(message, "fileKey"))
                writer.uint32(/* id 9, wireType 2 =*/74).string(message.fileKey);
            if (message.fileUrl != null && Object.hasOwnProperty.call(message, "fileUrl"))
                writer.uint32(/* id 10, wireType 2 =*/82).string(message.fileUrl);
            return writer;
        };

        /**
         * Encodes the specified Message message, length delimited. Does not implicitly {@link protocol.Message.verify|verify} messages.
         * @function encodeDelimited
         * @memberof protocol.Message
         * @static
         * @param {protocol.IMessage} message Message message or plain object to encode
         * @param {$protobuf.Writer} [writer] Writer to encode to
         * @returns {$protobuf.Writer} Writer
         */
        Message.encodeDelimited = function encodeDelimited(message, writer) {
            return this.encode(message, writer).ldelim();
        };

        /**
         * Decodes a Message message from the specified reader or buffer.
         * @function decode
         * @memberof protocol.Message
         * @static
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @param {number} [length] Message length if known beforehand
         * @returns {protocol.Message} Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        Message.decode = function decode(reader, length) {
            if (!(reader instanceof $Reader))
                reader = $Reader.create(reader);
            var end = length === undefined ? reader.len : reader.pos + length, message = new $root.protocol.Message();
            while (reader.pos < end) {
                var tag = reader.uint32();
                switch (tag >>> 3) {
                    case 1: {
                        message.clientId = reader.string();
                        break;
                    }
                    case 2: {
                        message.fromUsername = reader.string();
                        break;
                    }
                    case 3: {
                        message.from = reader.string();
                        break;
                    }
                    case 4: {
                        message.to = reader.string();
                        break;
                    }
                    case 5: {
                        message.content = reader.string();
                        break;
                    }
                    case 6: {
                        message.contentType = reader.int32();
                        break;
                    }
                    case 7: {
                        message.type = reader.string();
                        break;
                    }
                    case 8: {
                        message.messageType = reader.int32();
                        break;
                    }
                    case 9: {
                        message.fileKey = reader.string();
                        break;
                    }
                    case 10: {
                        message.fileUrl = reader.string();
                        break;
                    }
                    default:
                        reader.skipType(tag & 7);
                        break;
                }
            }
            return message;
        };

        /**
         * Decodes a Message message from the specified reader or buffer, length delimited.
         * @function decodeDelimited
         * @memberof protocol.Message
         * @static
         * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
         * @returns {protocol.Message} Message
         * @throws {Error} If the payload is not a reader or valid buffer
         * @throws {$protobuf.util.ProtocolError} If required fields are missing
         */
        Message.decodeDelimited = function decodeDelimited(reader) {
            if (!(reader instanceof $Reader))
                reader = new $Reader(reader);
            return this.decode(reader, reader.uint32());
        };

        /**
         * Verifies a Message message.
         * @function verify
         * @memberof protocol.Message
         * @static
         * @param {Object.<string,*>} message Plain object to verify
         * @returns {string|null} `null` if valid, otherwise the reason why it is not
         */
        Message.verify = function verify(message) {
            if (typeof message !== "object" || message === null)
                return "object expected";
            if (message.clientId != null && message.hasOwnProperty("clientId"))
                if (!$util.isString(message.clientId))
                    return "clientId: string expected";
            if (message.fromUsername != null && message.hasOwnProperty("fromUsername"))
                if (!$util.isString(message.fromUsername))
                    return "fromUsername: string expected";
            if (message.from != null && message.hasOwnProperty("from"))
                if (!$util.isString(message.from))
                    return "from: string expected";
            if (message.to != null && message.hasOwnProperty("to"))
                if (!$util.isString(message.to))
                    return "to: string expected";
            if (message.content != null && message.hasOwnProperty("content"))
                if (!$util.isString(message.content))
                    return "content: string expected";
            if (message.contentType != null && message.hasOwnProperty("contentType"))
                if (!$util.isInteger(message.contentType))
                    return "contentType: integer expected";
            if (message.type != null && message.hasOwnProperty("type"))
                if (!$util.isString(message.type))
                    return "type: string expected";
            if (message.messageType != null && message.hasOwnProperty("messageType"))
                if (!$util.isInteger(message.messageType))
                    return "messageType: integer expected";
            if (message.fileKey != null && message.hasOwnProperty("fileKey"))
                if (!$util.isString(message.fileKey))
                    return "fileKey: string expected";
            if (message.fileUrl != null && message.hasOwnProperty("fileUrl"))
                if (!$util.isString(message.fileUrl))
                    return "fileUrl: string expected";
            return null;
        };

        /**
         * Creates a Message message from a plain object. Also converts values to their respective internal types.
         * @function fromObject
         * @memberof protocol.Message
         * @static
         * @param {Object.<string,*>} object Plain object
         * @returns {protocol.Message} Message
         */
        Message.fromObject = function fromObject(object) {
            if (object instanceof $root.protocol.Message)
                return object;
            var message = new $root.protocol.Message();
            if (object.clientId != null)
                message.clientId = String(object.clientId);
            if (object.fromUsername != null)
                message.fromUsername = String(object.fromUsername);
            if (object.from != null)
                message.from = String(object.from);
            if (object.to != null)
                message.to = String(object.to);
            if (object.content != null)
                message.content = String(object.content);
            if (object.contentType != null)
                message.contentType = object.contentType | 0;
            if (object.type != null)
                message.type = String(object.type);
            if (object.messageType != null)
                message.messageType = object.messageType | 0;
            if (object.fileKey != null)
                message.fileKey = String(object.fileKey);
            if (object.fileUrl != null)
                message.fileUrl = String(object.fileUrl);
            return message;
        };

        /**
         * Creates a plain object from a Message message. Also converts values to other types if specified.
         * @function toObject
         * @memberof protocol.Message
         * @static
         * @param {protocol.Message} message Message
         * @param {$protobuf.IConversionOptions} [options] Conversion options
         * @returns {Object.<string,*>} Plain object
         */
        Message.toObject = function toObject(message, options) {
            if (!options)
                options = {};
            var object = {};
            if (options.defaults) {
                object.clientId = "";
                object.fromUsername = "";
                object.from = "";
                object.to = "";
                object.content = "";
                object.contentType = 0;
                object.type = "";
                object.messageType = 0;
                object.fileKey = "";
                object.fileUrl = "";
            }
            if (message.clientId != null && message.hasOwnProperty("clientId"))
                object.clientId = message.clientId;
            if (message.fromUsername != null && message.hasOwnProperty("fromUsername"))
                object.fromUsername = message.fromUsername;
            if (message.from != null && message.hasOwnProperty("from"))
                object.from = message.from;
            if (message.to != null && message.hasOwnProperty("to"))
                object.to = message.to;
            if (message.content != null && message.hasOwnProperty("content"))
                object.content = message.content;
            if (message.contentType != null && message.hasOwnProperty("contentType"))
                object.contentType = message.contentType;
            if (message.type != null && message.hasOwnProperty("type"))
                object.type = message.type;
            if (message.messageType != null && message.hasOwnProperty("messageType"))
                object.messageType = message.messageType;
            if (message.fileKey != null && message.hasOwnProperty("fileKey"))
                object.fileKey = message.fileKey;
            if (message.fileUrl != null && message.hasOwnProperty("fileUrl"))
                object.fileUrl = message.fileUrl;
            return object;
        };

        /**
         * Converts this Message to JSON.
         * @function toJSON
         * @memberof protocol.Message
         * @instance
         * @returns {Object.<string,*>} JSON object
         */
        Message.prototype.toJSON = function toJSON() {
            return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
        };

        /**
         * Gets the default type url for Message
         * @function getTypeUrl
         * @memberof protocol.Message
         * @static
         * @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
         * @returns {string} The default type url
         */
        Message.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
            if (typeUrlPrefix === undefined) {
                typeUrlPrefix = "type.googleapis.com";
            }
            return typeUrlPrefix + "/protocol.Message";
        };

        return Message;
    })();

    return protocol;
})();

// 修改调整的内容
const protocol = $root.protocol;

export { protocol };

使用

import * as proto from '@/proto/message'

const messagePB = proto.protocol.Message.create(data)
上一篇下一篇

猜你喜欢

热点阅读