2 module.exports = Message;
4 var util = require("./util/minimal");
7 * Constructs a new message instance.
8 * @classdesc Abstract runtime message.
10 * @param {Properties<T>} [properties] Properties to set
11 * @template T extends object = object
13 function Message(properties) {
14 // not used internally
16 for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
17 this[keys[i]] = properties[keys[i]];
21 * Reference to the reflected type.
28 * Reference to the reflected type.
34 /*eslint-disable valid-jsdoc*/
37 * Creates a new message of this type using the specified properties.
38 * @param {Object.<string,*>} [properties] Properties to set
39 * @returns {Message<T>} Message instance
40 * @template T extends Message<T>
41 * @this Constructor<T>
43 Message.create = function create(properties) {
44 return this.$type.create(properties);
48 * Encodes a message of this type.
49 * @param {T|Object.<string,*>} message Message to encode
50 * @param {Writer} [writer] Writer to use
51 * @returns {Writer} Writer
52 * @template T extends Message<T>
53 * @this Constructor<T>
55 Message.encode = function encode(message, writer) {
56 return this.$type.encode(message, writer);
60 * Encodes a message of this type preceeded by its length as a varint.
61 * @param {T|Object.<string,*>} message Message to encode
62 * @param {Writer} [writer] Writer to use
63 * @returns {Writer} Writer
64 * @template T extends Message<T>
65 * @this Constructor<T>
67 Message.encodeDelimited = function encodeDelimited(message, writer) {
68 return this.$type.encodeDelimited(message, writer);
72 * Decodes a message of this type.
73 * @name Message.decode
75 * @param {Reader|Uint8Array} reader Reader or buffer to decode
76 * @returns {T} Decoded message
77 * @template T extends Message<T>
78 * @this Constructor<T>
80 Message.decode = function decode(reader) {
81 return this.$type.decode(reader);
85 * Decodes a message of this type preceeded by its length as a varint.
86 * @name Message.decodeDelimited
88 * @param {Reader|Uint8Array} reader Reader or buffer to decode
89 * @returns {T} Decoded message
90 * @template T extends Message<T>
91 * @this Constructor<T>
93 Message.decodeDelimited = function decodeDelimited(reader) {
94 return this.$type.decodeDelimited(reader);
98 * Verifies a message of this type.
99 * @name Message.verify
101 * @param {Object.<string,*>} message Plain object to verify
102 * @returns {string|null} `null` if valid, otherwise the reason why it is not
104 Message.verify = function verify(message) {
105 return this.$type.verify(message);
109 * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
110 * @param {Object.<string,*>} object Plain object
111 * @returns {T} Message instance
112 * @template T extends Message<T>
113 * @this Constructor<T>
115 Message.fromObject = function fromObject(object) {
116 return this.$type.fromObject(object);
120 * Creates a plain object from a message of this type. Also converts values to other types if specified.
121 * @param {T} message Message instance
122 * @param {IConversionOptions} [options] Conversion options
123 * @returns {Object.<string,*>} Plain object
124 * @template T extends Message<T>
125 * @this Constructor<T>
127 Message.toObject = function toObject(message, options) {
128 return this.$type.toObject(message, options);
132 * Converts this message to JSON.
133 * @returns {Object.<string,*>} JSON object
135 Message.prototype.toJSON = function toJSON() {
136 return this.$type.toObject(this, util.toJSONOptions);
139 /*eslint-enable valid-jsdoc*/