message.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. module.exports = Message;
  3. var util = require("./util/minimal");
  4. /**
  5. * Constructs a new message instance.
  6. * @classdesc Abstract runtime message.
  7. * @constructor
  8. * @param {Properties<T>} [properties] Properties to set
  9. * @template T extends object = object
  10. */
  11. function Message(properties) {
  12. // not used internally
  13. if (properties)
  14. for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
  15. this[keys[i]] = properties[keys[i]];
  16. }
  17. /**
  18. * Reference to the reflected type.
  19. * @name Message.$type
  20. * @type {Type}
  21. * @readonly
  22. */
  23. /**
  24. * Reference to the reflected type.
  25. * @name Message#$type
  26. * @type {Type}
  27. * @readonly
  28. */
  29. /*eslint-disable valid-jsdoc*/
  30. /**
  31. * Creates a new message of this type using the specified properties.
  32. * @param {Object.<string,*>} [properties] Properties to set
  33. * @returns {Message<T>} Message instance
  34. * @template T extends Message<T>
  35. * @this Constructor<T>
  36. */
  37. Message.create = function create(properties) {
  38. return this.$type.create(properties);
  39. };
  40. /**
  41. * Encodes a message of this type.
  42. * @param {T|Object.<string,*>} message Message to encode
  43. * @param {Writer} [writer] Writer to use
  44. * @returns {Writer} Writer
  45. * @template T extends Message<T>
  46. * @this Constructor<T>
  47. */
  48. Message.encode = function encode(message, writer) {
  49. return this.$type.encode(message, writer);
  50. };
  51. /**
  52. * Encodes a message of this type preceeded by its length as a varint.
  53. * @param {T|Object.<string,*>} message Message to encode
  54. * @param {Writer} [writer] Writer to use
  55. * @returns {Writer} Writer
  56. * @template T extends Message<T>
  57. * @this Constructor<T>
  58. */
  59. Message.encodeDelimited = function encodeDelimited(message, writer) {
  60. return this.$type.encodeDelimited(message, writer);
  61. };
  62. /**
  63. * Decodes a message of this type.
  64. * @name Message.decode
  65. * @function
  66. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  67. * @returns {T} Decoded message
  68. * @template T extends Message<T>
  69. * @this Constructor<T>
  70. */
  71. Message.decode = function decode(reader) {
  72. return this.$type.decode(reader);
  73. };
  74. /**
  75. * Decodes a message of this type preceeded by its length as a varint.
  76. * @name Message.decodeDelimited
  77. * @function
  78. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  79. * @returns {T} Decoded message
  80. * @template T extends Message<T>
  81. * @this Constructor<T>
  82. */
  83. Message.decodeDelimited = function decodeDelimited(reader) {
  84. return this.$type.decodeDelimited(reader);
  85. };
  86. /**
  87. * Verifies a message of this type.
  88. * @name Message.verify
  89. * @function
  90. * @param {Object.<string,*>} message Plain object to verify
  91. * @returns {string|null} `null` if valid, otherwise the reason why it is not
  92. */
  93. Message.verify = function verify(message) {
  94. return this.$type.verify(message);
  95. };
  96. /**
  97. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  98. * @param {Object.<string,*>} object Plain object
  99. * @returns {T} Message instance
  100. * @template T extends Message<T>
  101. * @this Constructor<T>
  102. */
  103. Message.fromObject = function fromObject(object) {
  104. return this.$type.fromObject(object);
  105. };
  106. /**
  107. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  108. * @param {T} message Message instance
  109. * @param {IConversionOptions} [options] Conversion options
  110. * @returns {Object.<string,*>} Plain object
  111. * @template T extends Message<T>
  112. * @this Constructor<T>
  113. */
  114. Message.toObject = function toObject(message, options) {
  115. return this.$type.toObject(message, options);
  116. };
  117. /**
  118. * Converts this message to JSON.
  119. * @returns {Object.<string,*>} JSON object
  120. */
  121. Message.prototype.toJSON = function toJSON() {
  122. return this.$type.toObject(this, util.toJSONOptions);
  123. };
  124. /*eslint-enable valid-jsdoc*/