protobufjs 8.0.1 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +727 -727
  3. package/dist/light/protobuf.js +7724 -7591
  4. package/dist/light/protobuf.js.map +1 -1
  5. package/dist/light/protobuf.min.js +3 -3
  6. package/dist/light/protobuf.min.js.map +1 -1
  7. package/dist/minimal/protobuf.js +2606 -2546
  8. package/dist/minimal/protobuf.js.map +1 -1
  9. package/dist/minimal/protobuf.min.js +3 -3
  10. package/dist/minimal/protobuf.min.js.map +1 -1
  11. package/dist/protobuf.js +9593 -9436
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/debug/README.md +4 -4
  16. package/ext/debug/index.js +71 -71
  17. package/ext/descriptor/README.md +72 -72
  18. package/ext/descriptor/index.d.ts +195 -191
  19. package/ext/descriptor/index.js +1181 -1163
  20. package/ext/descriptor/test.js +54 -54
  21. package/google/LICENSE +27 -27
  22. package/google/README.md +1 -1
  23. package/google/api/annotations.json +82 -82
  24. package/google/api/annotations.proto +10 -10
  25. package/google/api/http.json +85 -85
  26. package/google/api/http.proto +30 -30
  27. package/google/protobuf/api.json +117 -117
  28. package/google/protobuf/api.proto +33 -33
  29. package/google/protobuf/descriptor.json +1381 -1381
  30. package/google/protobuf/descriptor.proto +534 -534
  31. package/google/protobuf/source_context.json +19 -19
  32. package/google/protobuf/source_context.proto +7 -7
  33. package/google/protobuf/type.json +201 -201
  34. package/google/protobuf/type.proto +89 -89
  35. package/index.d.ts +2817 -2799
  36. package/index.js +4 -4
  37. package/light.d.ts +2 -2
  38. package/light.js +3 -3
  39. package/minimal.d.ts +2 -2
  40. package/minimal.js +4 -4
  41. package/package.json +96 -114
  42. package/scripts/postinstall.js +32 -32
  43. package/src/common.js +399 -399
  44. package/src/converter.js +310 -301
  45. package/src/decoder.js +135 -127
  46. package/src/encoder.js +100 -100
  47. package/src/enum.js +226 -223
  48. package/src/field.js +453 -453
  49. package/src/index-light.js +104 -104
  50. package/src/index-minimal.js +36 -36
  51. package/src/index.js +12 -12
  52. package/src/mapfield.js +126 -126
  53. package/src/message.js +139 -143
  54. package/src/method.js +160 -160
  55. package/src/namespace.js +550 -546
  56. package/src/object.js +385 -381
  57. package/src/oneof.js +222 -222
  58. package/src/parse.js +1032 -1013
  59. package/src/reader.js +426 -416
  60. package/src/reader_buffer.js +51 -51
  61. package/src/root.js +403 -403
  62. package/src/roots.js +17 -17
  63. package/src/rpc/service.js +142 -142
  64. package/src/rpc.js +36 -36
  65. package/src/service.js +193 -189
  66. package/src/tokenize.js +421 -416
  67. package/src/type.js +625 -615
  68. package/src/types.js +196 -196
  69. package/src/typescript.jsdoc +15 -15
  70. package/src/util/aspromise.d.ts +13 -0
  71. package/src/util/aspromise.js +52 -0
  72. package/src/util/base64.d.ts +32 -0
  73. package/src/util/base64.js +139 -0
  74. package/src/util/codegen.d.ts +31 -0
  75. package/src/util/codegen.js +113 -0
  76. package/src/util/eventemitter.d.ts +45 -0
  77. package/src/util/eventemitter.js +84 -0
  78. package/src/util/fetch.d.ts +56 -0
  79. package/src/util/fetch.js +114 -0
  80. package/src/util/float.d.ts +83 -0
  81. package/src/util/float.js +335 -0
  82. package/src/util/inquire.d.ts +9 -0
  83. package/src/util/inquire.js +37 -0
  84. package/src/util/longbits.js +200 -200
  85. package/src/util/minimal.js +461 -438
  86. package/src/util/path.d.ts +22 -0
  87. package/src/util/path.js +72 -0
  88. package/src/util/patterns.js +8 -0
  89. package/src/util/pool.d.ts +32 -0
  90. package/src/util/pool.js +48 -0
  91. package/src/util/utf8.d.ts +24 -0
  92. package/src/util/utf8.js +104 -0
  93. package/src/util.js +215 -215
  94. package/src/verifier.js +180 -177
  95. package/src/wrappers.js +103 -102
  96. package/src/writer.js +465 -465
  97. package/src/writer_buffer.js +85 -85
  98. package/tsconfig.json +8 -8
package/src/type.js CHANGED
@@ -1,615 +1,625 @@
1
- "use strict";
2
- module.exports = Type;
3
-
4
- // extends Namespace
5
- var Namespace = require("./namespace");
6
- ((Type.prototype = Object.create(Namespace.prototype)).constructor = Type).className = "Type";
7
-
8
- var Enum = require("./enum"),
9
- OneOf = require("./oneof"),
10
- Field = require("./field"),
11
- MapField = require("./mapfield"),
12
- Service = require("./service"),
13
- Message = require("./message"),
14
- Reader = require("./reader"),
15
- Writer = require("./writer"),
16
- util = require("./util"),
17
- encoder = require("./encoder"),
18
- decoder = require("./decoder"),
19
- verifier = require("./verifier"),
20
- converter = require("./converter"),
21
- wrappers = require("./wrappers");
22
-
23
- /**
24
- * Constructs a new reflected message type instance.
25
- * @classdesc Reflected message type.
26
- * @extends NamespaceBase
27
- * @constructor
28
- * @param {string} name Message name
29
- * @param {Object.<string,*>} [options] Declared options
30
- */
31
- function Type(name, options) {
32
- name = name.replace(/\W/g, "");
33
- Namespace.call(this, name, options);
34
-
35
- /**
36
- * Message fields.
37
- * @type {Object.<string,Field>}
38
- */
39
- this.fields = {}; // toJSON, marker
40
-
41
- /**
42
- * Oneofs declared within this namespace, if any.
43
- * @type {Object.<string,OneOf>}
44
- */
45
- this.oneofs = undefined; // toJSON
46
-
47
- /**
48
- * Extension ranges, if any.
49
- * @type {number[][]}
50
- */
51
- this.extensions = undefined; // toJSON
52
-
53
- /**
54
- * Reserved ranges, if any.
55
- * @type {Array.<number[]|string>}
56
- */
57
- this.reserved = undefined; // toJSON
58
-
59
- /*?
60
- * Whether this type is a legacy group.
61
- * @type {boolean|undefined}
62
- */
63
- this.group = undefined; // toJSON
64
-
65
- /**
66
- * Cached fields by id.
67
- * @type {Object.<number,Field>|null}
68
- * @private
69
- */
70
- this._fieldsById = null;
71
-
72
- /**
73
- * Cached fields as an array.
74
- * @type {Field[]|null}
75
- * @private
76
- */
77
- this._fieldsArray = null;
78
-
79
- /**
80
- * Cached oneofs as an array.
81
- * @type {OneOf[]|null}
82
- * @private
83
- */
84
- this._oneofsArray = null;
85
-
86
- /**
87
- * Cached constructor.
88
- * @type {Constructor<{}>}
89
- * @private
90
- */
91
- this._ctor = null;
92
- }
93
-
94
- Object.defineProperties(Type.prototype, {
95
-
96
- /**
97
- * Message fields by id.
98
- * @name Type#fieldsById
99
- * @type {Object.<number,Field>}
100
- * @readonly
101
- */
102
- fieldsById: {
103
- get: function() {
104
-
105
- /* istanbul ignore if */
106
- if (this._fieldsById)
107
- return this._fieldsById;
108
-
109
- this._fieldsById = {};
110
- for (var names = Object.keys(this.fields), i = 0; i < names.length; ++i) {
111
- var field = this.fields[names[i]],
112
- id = field.id;
113
-
114
- /* istanbul ignore if */
115
- if (this._fieldsById[id])
116
- throw Error("duplicate id " + id + " in " + this);
117
-
118
- this._fieldsById[id] = field;
119
- }
120
- return this._fieldsById;
121
- }
122
- },
123
-
124
- /**
125
- * Fields of this message as an array for iteration.
126
- * @name Type#fieldsArray
127
- * @type {Field[]}
128
- * @readonly
129
- */
130
- fieldsArray: {
131
- get: function() {
132
- return this._fieldsArray || (this._fieldsArray = util.toArray(this.fields));
133
- }
134
- },
135
-
136
- /**
137
- * Oneofs of this message as an array for iteration.
138
- * @name Type#oneofsArray
139
- * @type {OneOf[]}
140
- * @readonly
141
- */
142
- oneofsArray: {
143
- get: function() {
144
- return this._oneofsArray || (this._oneofsArray = util.toArray(this.oneofs));
145
- }
146
- },
147
-
148
- /**
149
- * The registered constructor, if any registered, otherwise a generic constructor.
150
- * Assigning a function replaces the internal constructor. If the function does not extend {@link Message} yet, its prototype will be setup accordingly and static methods will be populated. If it already extends {@link Message}, it will just replace the internal constructor.
151
- * @name Type#ctor
152
- * @type {Constructor<{}>}
153
- */
154
- ctor: {
155
- get: function() {
156
- return this._ctor || (this.ctor = Type.generateConstructor(this)());
157
- },
158
- set: function(ctor) {
159
-
160
- // Ensure proper prototype
161
- var prototype = ctor.prototype;
162
- if (!(prototype instanceof Message)) {
163
- (ctor.prototype = new Message()).constructor = ctor;
164
- util.merge(ctor.prototype, prototype);
165
- }
166
-
167
- // Classes and messages reference their reflected type
168
- ctor.$type = ctor.prototype.$type = this;
169
-
170
- // Mix in static methods
171
- util.merge(ctor, Message, true);
172
-
173
- this._ctor = ctor;
174
-
175
- // Messages have non-enumerable default values on their prototype
176
- var i = 0;
177
- for (; i < /* initializes */ this.fieldsArray.length; ++i)
178
- this._fieldsArray[i].resolve(); // ensures a proper value
179
-
180
- // Messages have non-enumerable getters and setters for each virtual oneof field
181
- var ctorProperties = {};
182
- for (i = 0; i < /* initializes */ this.oneofsArray.length; ++i)
183
- ctorProperties[this._oneofsArray[i].resolve().name] = {
184
- get: util.oneOfGetter(this._oneofsArray[i].oneof),
185
- set: util.oneOfSetter(this._oneofsArray[i].oneof)
186
- };
187
- if (i)
188
- Object.defineProperties(ctor.prototype, ctorProperties);
189
- }
190
- }
191
- });
192
-
193
- /**
194
- * Generates a constructor function for the specified type.
195
- * @param {Type} mtype Message type
196
- * @returns {Codegen} Codegen instance
197
- */
198
- Type.generateConstructor = function generateConstructor(mtype) {
199
- /* eslint-disable no-unexpected-multiline */
200
- var gen = util.codegen(["p"], mtype.name);
201
- // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
202
- for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
203
- if ((field = mtype._fieldsArray[i]).map) gen
204
- ("this%s={}", util.safeProp(field.name));
205
- else if (field.repeated) gen
206
- ("this%s=[]", util.safeProp(field.name));
207
- return gen
208
- ("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null)") // omit undefined or null
209
- ("this[ks[i]]=p[ks[i]]");
210
- /* eslint-enable no-unexpected-multiline */
211
- };
212
-
213
- function clearCache(type) {
214
- type._fieldsById = type._fieldsArray = type._oneofsArray = null;
215
- delete type.encode;
216
- delete type.decode;
217
- delete type.verify;
218
- return type;
219
- }
220
-
221
- /**
222
- * Message type descriptor.
223
- * @interface IType
224
- * @extends INamespace
225
- * @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
226
- * @property {Object.<string,IField>} fields Field descriptors
227
- * @property {number[][]} [extensions] Extension ranges
228
- * @property {Array.<number[]|string>} [reserved] Reserved ranges
229
- * @property {boolean} [group=false] Whether a legacy group or not
230
- */
231
-
232
- /**
233
- * Creates a message type from a message type descriptor.
234
- * @param {string} name Message name
235
- * @param {IType} json Message type descriptor
236
- * @returns {Type} Created message type
237
- */
238
- Type.fromJSON = function fromJSON(name, json) {
239
- var type = new Type(name, json.options);
240
- type.extensions = json.extensions;
241
- type.reserved = json.reserved;
242
- var names = Object.keys(json.fields),
243
- i = 0;
244
- for (; i < names.length; ++i)
245
- type.add(
246
- ( typeof json.fields[names[i]].keyType !== "undefined"
247
- ? MapField.fromJSON
248
- : Field.fromJSON )(names[i], json.fields[names[i]])
249
- );
250
- if (json.oneofs)
251
- for (names = Object.keys(json.oneofs), i = 0; i < names.length; ++i)
252
- type.add(OneOf.fromJSON(names[i], json.oneofs[names[i]]));
253
- if (json.nested)
254
- for (names = Object.keys(json.nested), i = 0; i < names.length; ++i) {
255
- var nested = json.nested[names[i]];
256
- type.add( // most to least likely
257
- ( nested.id !== undefined
258
- ? Field.fromJSON
259
- : nested.fields !== undefined
260
- ? Type.fromJSON
261
- : nested.values !== undefined
262
- ? Enum.fromJSON
263
- : nested.methods !== undefined
264
- ? Service.fromJSON
265
- : Namespace.fromJSON )(names[i], nested)
266
- );
267
- }
268
- if (json.extensions && json.extensions.length)
269
- type.extensions = json.extensions;
270
- if (json.reserved && json.reserved.length)
271
- type.reserved = json.reserved;
272
- if (json.group)
273
- type.group = true;
274
- if (json.comment)
275
- type.comment = json.comment;
276
- if (json.edition)
277
- type._edition = json.edition;
278
- type._defaultEdition = "proto3"; // For backwards-compatibility.
279
- return type;
280
- };
281
-
282
- /**
283
- * Converts this message type to a message type descriptor.
284
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
285
- * @returns {IType} Message type descriptor
286
- */
287
- Type.prototype.toJSON = function toJSON(toJSONOptions) {
288
- var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
289
- var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
290
- return util.toObject([
291
- "edition" , this._editionToJSON(),
292
- "options" , inherited && inherited.options || undefined,
293
- "oneofs" , Namespace.arrayToJSON(this.oneofsArray, toJSONOptions),
294
- "fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; }), toJSONOptions) || {},
295
- "extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
296
- "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
297
- "group" , this.group || undefined,
298
- "nested" , inherited && inherited.nested || undefined,
299
- "comment" , keepComments ? this.comment : undefined
300
- ]);
301
- };
302
-
303
- /**
304
- * @override
305
- */
306
- Type.prototype.resolveAll = function resolveAll() {
307
- if (!this._needsRecursiveResolve) return this;
308
-
309
- Namespace.prototype.resolveAll.call(this);
310
- var oneofs = this.oneofsArray; i = 0;
311
- while (i < oneofs.length)
312
- oneofs[i++].resolve();
313
- var fields = this.fieldsArray, i = 0;
314
- while (i < fields.length)
315
- fields[i++].resolve();
316
- return this;
317
- };
318
-
319
- /**
320
- * @override
321
- */
322
- Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
323
- if (!this._needsRecursiveFeatureResolution) return this;
324
-
325
- edition = this._edition || edition;
326
-
327
- Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
328
- this.oneofsArray.forEach(oneof => {
329
- oneof._resolveFeatures(edition);
330
- });
331
- this.fieldsArray.forEach(field => {
332
- field._resolveFeatures(edition);
333
- });
334
- return this;
335
- };
336
-
337
- /**
338
- * @override
339
- */
340
- Type.prototype.get = function get(name) {
341
- return this.fields[name]
342
- || this.oneofs && this.oneofs[name]
343
- || this.nested && this.nested[name]
344
- || null;
345
- };
346
-
347
- /**
348
- * Adds a nested object to this type.
349
- * @param {ReflectionObject} object Nested object to add
350
- * @returns {Type} `this`
351
- * @throws {TypeError} If arguments are invalid
352
- * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id
353
- */
354
- Type.prototype.add = function add(object) {
355
-
356
- if (this.get(object.name))
357
- throw Error("duplicate name '" + object.name + "' in " + this);
358
-
359
- if (object instanceof Field && object.extend === undefined) {
360
- // NOTE: Extension fields aren't actual fields on the declaring type, but nested objects.
361
- // The root object takes care of adding distinct sister-fields to the respective extended
362
- // type instead.
363
-
364
- // avoids calling the getter if not absolutely necessary because it's called quite frequently
365
- if (this._fieldsById ? /* istanbul ignore next */ this._fieldsById[object.id] : this.fieldsById[object.id])
366
- throw Error("duplicate id " + object.id + " in " + this);
367
- if (this.isReservedId(object.id))
368
- throw Error("id " + object.id + " is reserved in " + this);
369
- if (this.isReservedName(object.name))
370
- throw Error("name '" + object.name + "' is reserved in " + this);
371
-
372
- if (object.parent)
373
- object.parent.remove(object);
374
- this.fields[object.name] = object;
375
- object.message = this;
376
- object.onAdd(this);
377
- return clearCache(this);
378
- }
379
- if (object instanceof OneOf) {
380
- if (!this.oneofs)
381
- this.oneofs = {};
382
- this.oneofs[object.name] = object;
383
- object.onAdd(this);
384
- return clearCache(this);
385
- }
386
- return Namespace.prototype.add.call(this, object);
387
- };
388
-
389
- /**
390
- * Removes a nested object from this type.
391
- * @param {ReflectionObject} object Nested object to remove
392
- * @returns {Type} `this`
393
- * @throws {TypeError} If arguments are invalid
394
- * @throws {Error} If `object` is not a member of this type
395
- */
396
- Type.prototype.remove = function remove(object) {
397
- if (object instanceof Field && object.extend === undefined) {
398
- // See Type#add for the reason why extension fields are excluded here.
399
-
400
- /* istanbul ignore if */
401
- if (!this.fields || this.fields[object.name] !== object)
402
- throw Error(object + " is not a member of " + this);
403
-
404
- delete this.fields[object.name];
405
- object.parent = null;
406
- object.onRemove(this);
407
- return clearCache(this);
408
- }
409
- if (object instanceof OneOf) {
410
-
411
- /* istanbul ignore if */
412
- if (!this.oneofs || this.oneofs[object.name] !== object)
413
- throw Error(object + " is not a member of " + this);
414
-
415
- delete this.oneofs[object.name];
416
- object.parent = null;
417
- object.onRemove(this);
418
- return clearCache(this);
419
- }
420
- return Namespace.prototype.remove.call(this, object);
421
- };
422
-
423
- /**
424
- * Tests if the specified id is reserved.
425
- * @param {number} id Id to test
426
- * @returns {boolean} `true` if reserved, otherwise `false`
427
- */
428
- Type.prototype.isReservedId = function isReservedId(id) {
429
- return Namespace.isReservedId(this.reserved, id);
430
- };
431
-
432
- /**
433
- * Tests if the specified name is reserved.
434
- * @param {string} name Name to test
435
- * @returns {boolean} `true` if reserved, otherwise `false`
436
- */
437
- Type.prototype.isReservedName = function isReservedName(name) {
438
- return Namespace.isReservedName(this.reserved, name);
439
- };
440
-
441
- /**
442
- * Creates a new message of this type using the specified properties.
443
- * @param {Object.<string,*>} [properties] Properties to set
444
- * @returns {Message<{}>} Message instance
445
- */
446
- Type.prototype.create = function create(properties) {
447
- return new this.ctor(properties);
448
- };
449
-
450
- /**
451
- * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}.
452
- * @returns {Type} `this`
453
- */
454
- Type.prototype.setup = function setup() {
455
- // Sets up everything at once so that the prototype chain does not have to be re-evaluated
456
- // multiple times (V8, soft-deopt prototype-check).
457
-
458
- var fullName = this.fullName,
459
- types = [];
460
- for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
461
- types.push(this._fieldsArray[i].resolve().resolvedType);
462
-
463
- // Replace setup methods with type-specific generated functions
464
- this.encode = encoder(this)({
465
- Writer : Writer,
466
- types : types,
467
- util : util
468
- });
469
- this.decode = decoder(this)({
470
- Reader : Reader,
471
- types : types,
472
- util : util
473
- });
474
- this.verify = verifier(this)({
475
- types : types,
476
- util : util
477
- });
478
- this.fromObject = converter.fromObject(this)({
479
- types : types,
480
- util : util
481
- });
482
- this.toObject = converter.toObject(this)({
483
- types : types,
484
- util : util
485
- });
486
-
487
- // Inject custom wrappers for common types
488
- var wrapper = wrappers[fullName];
489
- if (wrapper) {
490
- var originalThis = Object.create(this);
491
- // if (wrapper.fromObject) {
492
- originalThis.fromObject = this.fromObject;
493
- this.fromObject = wrapper.fromObject.bind(originalThis);
494
- // }
495
- // if (wrapper.toObject) {
496
- originalThis.toObject = this.toObject;
497
- this.toObject = wrapper.toObject.bind(originalThis);
498
- // }
499
- }
500
-
501
- return this;
502
- };
503
-
504
- /**
505
- * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages.
506
- * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
507
- * @param {Writer} [writer] Writer to encode to
508
- * @returns {Writer} writer
509
- */
510
- Type.prototype.encode = function encode_setup(message, writer) {
511
- return this.setup().encode(message, writer); // overrides this method
512
- };
513
-
514
- /**
515
- * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages.
516
- * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
517
- * @param {Writer} [writer] Writer to encode to
518
- * @returns {Writer} writer
519
- */
520
- Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
521
- return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
522
- };
523
-
524
- /**
525
- * Decodes a message of this type.
526
- * @param {Reader|Uint8Array} reader Reader or buffer to decode from
527
- * @param {number} [length] Length of the message, if known beforehand
528
- * @returns {Message<{}>} Decoded message
529
- * @throws {Error} If the payload is not a reader or valid buffer
530
- * @throws {util.ProtocolError<{}>} If required fields are missing
531
- */
532
- Type.prototype.decode = function decode_setup(reader, length) {
533
- return this.setup().decode(reader, length); // overrides this method
534
- };
535
-
536
- /**
537
- * Decodes a message of this type preceeded by its byte length as a varint.
538
- * @param {Reader|Uint8Array} reader Reader or buffer to decode from
539
- * @returns {Message<{}>} Decoded message
540
- * @throws {Error} If the payload is not a reader or valid buffer
541
- * @throws {util.ProtocolError} If required fields are missing
542
- */
543
- Type.prototype.decodeDelimited = function decodeDelimited(reader) {
544
- if (!(reader instanceof Reader))
545
- reader = Reader.create(reader);
546
- return this.decode(reader, reader.uint32());
547
- };
548
-
549
- /**
550
- * Verifies that field values are valid and that required fields are present.
551
- * @param {Object.<string,*>} message Plain object to verify
552
- * @returns {null|string} `null` if valid, otherwise the reason why it is not
553
- */
554
- Type.prototype.verify = function verify_setup(message) {
555
- return this.setup().verify(message); // overrides this method
556
- };
557
-
558
- /**
559
- * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
560
- * @param {Object.<string,*>} object Plain object to convert
561
- * @returns {Message<{}>} Message instance
562
- */
563
- Type.prototype.fromObject = function fromObject(object) {
564
- return this.setup().fromObject(object);
565
- };
566
-
567
- /**
568
- * Conversion options as used by {@link Type#toObject} and {@link Message.toObject}.
569
- * @interface IConversionOptions
570
- * @property {Function} [longs] Long conversion type.
571
- * Valid values are `String` and `Number` (the global types).
572
- * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
573
- * @property {Function} [enums] Enum value conversion type.
574
- * Only valid value is `String` (the global type).
575
- * Defaults to copy the present value, which is the numeric id.
576
- * @property {Function} [bytes] Bytes value conversion type.
577
- * Valid values are `Array` and (a base64 encoded) `String` (the global types).
578
- * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser.
579
- * @property {boolean} [defaults=false] Also sets default values on the resulting object
580
- * @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
581
- * @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
582
- * @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any
583
- * @property {boolean} [json=false] Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings
584
- */
585
-
586
- /**
587
- * Creates a plain object from a message of this type. Also converts values to other types if specified.
588
- * @param {Message<{}>} message Message instance
589
- * @param {IConversionOptions} [options] Conversion options
590
- * @returns {Object.<string,*>} Plain object
591
- */
592
- Type.prototype.toObject = function toObject(message, options) {
593
- return this.setup().toObject(message, options);
594
- };
595
-
596
- /**
597
- * Decorator function as returned by {@link Type.d} (TypeScript).
598
- * @typedef TypeDecorator
599
- * @type {function}
600
- * @param {Constructor<T>} target Target constructor
601
- * @returns {undefined}
602
- * @template T extends Message<T>
603
- */
604
-
605
- /**
606
- * Type decorator (TypeScript).
607
- * @param {string} [typeName] Type name, defaults to the constructor's name
608
- * @returns {TypeDecorator<T>} Decorator function
609
- * @template T extends Message<T>
610
- */
611
- Type.d = function decorateType(typeName) {
612
- return function typeDecorator(target) {
613
- util.decorateType(target, typeName);
614
- };
615
- };
1
+ "use strict";
2
+ module.exports = Type;
3
+
4
+ // extends Namespace
5
+ var Namespace = require("./namespace");
6
+ ((Type.prototype = Object.create(Namespace.prototype)).constructor = Type).className = "Type";
7
+
8
+ var Enum = require("./enum"),
9
+ OneOf = require("./oneof"),
10
+ Field = require("./field"),
11
+ MapField = require("./mapfield"),
12
+ Service = require("./service"),
13
+ Message = require("./message"),
14
+ Reader = require("./reader"),
15
+ Writer = require("./writer"),
16
+ util = require("./util"),
17
+ encoder = require("./encoder"),
18
+ decoder = require("./decoder"),
19
+ verifier = require("./verifier"),
20
+ converter = require("./converter"),
21
+ wrappers = require("./wrappers");
22
+
23
+ /**
24
+ * Constructs a new reflected message type instance.
25
+ * @classdesc Reflected message type.
26
+ * @extends NamespaceBase
27
+ * @constructor
28
+ * @param {string} name Message name
29
+ * @param {Object.<string,*>} [options] Declared options
30
+ */
31
+ function Type(name, options) {
32
+ name = name.replace(/\W/g, "");
33
+ Namespace.call(this, name, options);
34
+
35
+ /**
36
+ * Message fields.
37
+ * @type {Object.<string,Field>}
38
+ */
39
+ this.fields = {}; // toJSON, marker
40
+
41
+ /**
42
+ * Oneofs declared within this namespace, if any.
43
+ * @type {Object.<string,OneOf>}
44
+ */
45
+ this.oneofs = undefined; // toJSON
46
+
47
+ /**
48
+ * Extension ranges, if any.
49
+ * @type {number[][]}
50
+ */
51
+ this.extensions = undefined; // toJSON
52
+
53
+ /**
54
+ * Reserved ranges, if any.
55
+ * @type {Array.<number[]|string>}
56
+ */
57
+ this.reserved = undefined; // toJSON
58
+
59
+ /*?
60
+ * Whether this type is a legacy group.
61
+ * @type {boolean|undefined}
62
+ */
63
+ this.group = undefined; // toJSON
64
+
65
+ /**
66
+ * Cached fields by id.
67
+ * @type {Object.<number,Field>|null}
68
+ * @private
69
+ */
70
+ this._fieldsById = null;
71
+
72
+ /**
73
+ * Cached fields as an array.
74
+ * @type {Field[]|null}
75
+ * @private
76
+ */
77
+ this._fieldsArray = null;
78
+
79
+ /**
80
+ * Cached oneofs as an array.
81
+ * @type {OneOf[]|null}
82
+ * @private
83
+ */
84
+ this._oneofsArray = null;
85
+
86
+ /**
87
+ * Cached constructor.
88
+ * @type {Constructor<{}>}
89
+ * @private
90
+ */
91
+ this._ctor = null;
92
+ }
93
+
94
+ Object.defineProperties(Type.prototype, {
95
+
96
+ /**
97
+ * Message fields by id.
98
+ * @name Type#fieldsById
99
+ * @type {Object.<number,Field>}
100
+ * @readonly
101
+ */
102
+ fieldsById: {
103
+ get: function() {
104
+
105
+ /* istanbul ignore if */
106
+ if (this._fieldsById)
107
+ return this._fieldsById;
108
+
109
+ this._fieldsById = {};
110
+ for (var names = Object.keys(this.fields), i = 0; i < names.length; ++i) {
111
+ var field = this.fields[names[i]],
112
+ id = field.id;
113
+
114
+ /* istanbul ignore if */
115
+ if (this._fieldsById[id])
116
+ throw Error("duplicate id " + id + " in " + this);
117
+
118
+ this._fieldsById[id] = field;
119
+ }
120
+ return this._fieldsById;
121
+ }
122
+ },
123
+
124
+ /**
125
+ * Fields of this message as an array for iteration.
126
+ * @name Type#fieldsArray
127
+ * @type {Field[]}
128
+ * @readonly
129
+ */
130
+ fieldsArray: {
131
+ get: function() {
132
+ return this._fieldsArray || (this._fieldsArray = util.toArray(this.fields));
133
+ }
134
+ },
135
+
136
+ /**
137
+ * Oneofs of this message as an array for iteration.
138
+ * @name Type#oneofsArray
139
+ * @type {OneOf[]}
140
+ * @readonly
141
+ */
142
+ oneofsArray: {
143
+ get: function() {
144
+ return this._oneofsArray || (this._oneofsArray = util.toArray(this.oneofs));
145
+ }
146
+ },
147
+
148
+ /**
149
+ * The registered constructor, if any registered, otherwise a generic constructor.
150
+ * Assigning a function replaces the internal constructor. If the function does not extend {@link Message} yet, its prototype will be setup accordingly and static methods will be populated. If it already extends {@link Message}, it will just replace the internal constructor.
151
+ * @name Type#ctor
152
+ * @type {Constructor<{}>}
153
+ */
154
+ ctor: {
155
+ get: function() {
156
+ return this._ctor || (this.ctor = Type.generateConstructor(this)());
157
+ },
158
+ set: function(ctor) {
159
+
160
+ // Ensure proper prototype
161
+ var prototype = ctor.prototype;
162
+ if (!(prototype instanceof Message)) {
163
+ (ctor.prototype = new Message()).constructor = ctor;
164
+ util.merge(ctor.prototype, prototype);
165
+ }
166
+
167
+ // Classes and messages reference their reflected type
168
+ ctor.$type = ctor.prototype.$type = this;
169
+
170
+ // Mix in static methods
171
+ util.merge(ctor, Message, true);
172
+
173
+ this._ctor = ctor;
174
+
175
+ // Messages have non-enumerable default values on their prototype
176
+ var i = 0;
177
+ for (; i < /* initializes */ this.fieldsArray.length; ++i)
178
+ this._fieldsArray[i].resolve(); // ensures a proper value
179
+
180
+ // Messages have non-enumerable getters and setters for each virtual oneof field
181
+ var ctorProperties = {};
182
+ for (i = 0; i < /* initializes */ this.oneofsArray.length; ++i)
183
+ ctorProperties[this._oneofsArray[i].resolve().name] = {
184
+ get: util.oneOfGetter(this._oneofsArray[i].oneof),
185
+ set: util.oneOfSetter(this._oneofsArray[i].oneof)
186
+ };
187
+ if (i)
188
+ Object.defineProperties(ctor.prototype, ctorProperties);
189
+ }
190
+ }
191
+ });
192
+
193
+ /**
194
+ * Generates a constructor function for the specified type.
195
+ * @param {Type} mtype Message type
196
+ * @returns {Codegen} Codegen instance
197
+ */
198
+ Type.generateConstructor = function generateConstructor(mtype) {
199
+ /* eslint-disable no-unexpected-multiline */
200
+ var gen = util.codegen(["p"], mtype.name);
201
+ // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
202
+ for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
203
+ if ((field = mtype._fieldsArray[i]).map) gen
204
+ ("this%s={}", util.safeProp(field.name));
205
+ else if (field.repeated) gen
206
+ ("this%s=[]", util.safeProp(field.name));
207
+ return gen
208
+ ("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null&&ks[i]!==\"__proto__\")") // omit undefined or null
209
+ ("this[ks[i]]=p[ks[i]]");
210
+ /* eslint-enable no-unexpected-multiline */
211
+ };
212
+
213
+ function clearCache(type) {
214
+ type._fieldsById = type._fieldsArray = type._oneofsArray = null;
215
+ delete type.encode;
216
+ delete type.decode;
217
+ delete type.verify;
218
+ return type;
219
+ }
220
+
221
+ /**
222
+ * Message type descriptor.
223
+ * @interface IType
224
+ * @extends INamespace
225
+ * @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
226
+ * @property {Object.<string,IField>} fields Field descriptors
227
+ * @property {number[][]} [extensions] Extension ranges
228
+ * @property {Array.<number[]|string>} [reserved] Reserved ranges
229
+ * @property {boolean} [group=false] Whether a legacy group or not
230
+ */
231
+
232
+ /**
233
+ * Creates a message type from a message type descriptor.
234
+ * @param {string} name Message name
235
+ * @param {IType} json Message type descriptor
236
+ * @returns {Type} Created message type
237
+ */
238
+ Type.fromJSON = function fromJSON(name, json) {
239
+ var type = new Type(name, json.options);
240
+ type.extensions = json.extensions;
241
+ type.reserved = json.reserved;
242
+ var names = Object.keys(json.fields),
243
+ i = 0;
244
+ for (; i < names.length; ++i)
245
+ type.add(
246
+ ( typeof json.fields[names[i]].keyType !== "undefined"
247
+ ? MapField.fromJSON
248
+ : Field.fromJSON )(names[i], json.fields[names[i]])
249
+ );
250
+ if (json.oneofs)
251
+ for (names = Object.keys(json.oneofs), i = 0; i < names.length; ++i)
252
+ type.add(OneOf.fromJSON(names[i], json.oneofs[names[i]]));
253
+ if (json.nested)
254
+ for (names = Object.keys(json.nested), i = 0; i < names.length; ++i) {
255
+ var nested = json.nested[names[i]];
256
+ type.add( // most to least likely
257
+ ( nested.id !== undefined
258
+ ? Field.fromJSON
259
+ : nested.fields !== undefined
260
+ ? Type.fromJSON
261
+ : nested.values !== undefined
262
+ ? Enum.fromJSON
263
+ : nested.methods !== undefined
264
+ ? Service.fromJSON
265
+ : Namespace.fromJSON )(names[i], nested)
266
+ );
267
+ }
268
+ if (json.extensions && json.extensions.length)
269
+ type.extensions = json.extensions;
270
+ if (json.reserved && json.reserved.length)
271
+ type.reserved = json.reserved;
272
+ if (json.group)
273
+ type.group = true;
274
+ if (json.comment)
275
+ type.comment = json.comment;
276
+ if (json.edition)
277
+ type._edition = json.edition;
278
+ type._defaultEdition = "proto3"; // For backwards-compatibility.
279
+ return type;
280
+ };
281
+
282
+ /**
283
+ * Converts this message type to a message type descriptor.
284
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
285
+ * @returns {IType} Message type descriptor
286
+ */
287
+ Type.prototype.toJSON = function toJSON(toJSONOptions) {
288
+ var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
289
+ var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
290
+ return util.toObject([
291
+ "edition" , this._editionToJSON(),
292
+ "options" , inherited && inherited.options || undefined,
293
+ "oneofs" , Namespace.arrayToJSON(this.oneofsArray, toJSONOptions),
294
+ "fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; }), toJSONOptions) || {},
295
+ "extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
296
+ "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
297
+ "group" , this.group || undefined,
298
+ "nested" , inherited && inherited.nested || undefined,
299
+ "comment" , keepComments ? this.comment : undefined
300
+ ]);
301
+ };
302
+
303
+ /**
304
+ * @override
305
+ */
306
+ Type.prototype.resolveAll = function resolveAll() {
307
+ if (!this._needsRecursiveResolve) return this;
308
+
309
+ Namespace.prototype.resolveAll.call(this);
310
+ var oneofs = this.oneofsArray; i = 0;
311
+ while (i < oneofs.length)
312
+ oneofs[i++].resolve();
313
+ var fields = this.fieldsArray, i = 0;
314
+ while (i < fields.length)
315
+ fields[i++].resolve();
316
+ return this;
317
+ };
318
+
319
+ /**
320
+ * @override
321
+ */
322
+ Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
323
+ if (!this._needsRecursiveFeatureResolution) return this;
324
+
325
+ edition = this._edition || edition;
326
+
327
+ Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
328
+ this.oneofsArray.forEach(oneof => {
329
+ oneof._resolveFeatures(edition);
330
+ });
331
+ this.fieldsArray.forEach(field => {
332
+ field._resolveFeatures(edition);
333
+ });
334
+ return this;
335
+ };
336
+
337
+ /**
338
+ * @override
339
+ */
340
+ Type.prototype.get = function get(name) {
341
+ if (Object.prototype.hasOwnProperty.call(this.fields, name))
342
+ return this.fields[name];
343
+ if (this.oneofs && Object.prototype.hasOwnProperty.call(this.oneofs, name))
344
+ return this.oneofs[name];
345
+ if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name))
346
+ return this.nested[name];
347
+ return null;
348
+ };
349
+
350
+ /**
351
+ * Adds a nested object to this type.
352
+ * @param {ReflectionObject} object Nested object to add
353
+ * @returns {Type} `this`
354
+ * @throws {TypeError} If arguments are invalid
355
+ * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id
356
+ */
357
+ Type.prototype.add = function add(object) {
358
+ if (this.get(object.name))
359
+ throw Error("duplicate name '" + object.name + "' in " + this);
360
+
361
+ if (object instanceof Field && object.extend === undefined) {
362
+ // NOTE: Extension fields aren't actual fields on the declaring type, but nested objects.
363
+ // The root object takes care of adding distinct sister-fields to the respective extended
364
+ // type instead.
365
+
366
+ // avoids calling the getter if not absolutely necessary because it's called quite frequently
367
+ if (this._fieldsById ? /* istanbul ignore next */ this._fieldsById[object.id] : this.fieldsById[object.id])
368
+ throw Error("duplicate id " + object.id + " in " + this);
369
+ if (this.isReservedId(object.id))
370
+ throw Error("id " + object.id + " is reserved in " + this);
371
+ if (this.isReservedName(object.name))
372
+ throw Error("name '" + object.name + "' is reserved in " + this);
373
+ if (object.name === "__proto__")
374
+ return this;
375
+
376
+ if (object.parent)
377
+ object.parent.remove(object);
378
+ this.fields[object.name] = object;
379
+ object.message = this;
380
+ object.onAdd(this);
381
+ return clearCache(this);
382
+ }
383
+ if (object instanceof OneOf) {
384
+ if (object.name === "__proto__")
385
+ return this;
386
+ if (!this.oneofs)
387
+ this.oneofs = {};
388
+ this.oneofs[object.name] = object;
389
+ object.onAdd(this);
390
+ return clearCache(this);
391
+ }
392
+ return Namespace.prototype.add.call(this, object);
393
+ };
394
+
395
+ /**
396
+ * Removes a nested object from this type.
397
+ * @param {ReflectionObject} object Nested object to remove
398
+ * @returns {Type} `this`
399
+ * @throws {TypeError} If arguments are invalid
400
+ * @throws {Error} If `object` is not a member of this type
401
+ */
402
+ Type.prototype.remove = function remove(object) {
403
+ if (object instanceof Field && object.extend === undefined) {
404
+ // See Type#add for the reason why extension fields are excluded here.
405
+
406
+ /* istanbul ignore if */
407
+ if (!this.fields || this.fields[object.name] !== object)
408
+ throw Error(object + " is not a member of " + this);
409
+
410
+ delete this.fields[object.name];
411
+ object.parent = null;
412
+ object.onRemove(this);
413
+ return clearCache(this);
414
+ }
415
+ if (object instanceof OneOf) {
416
+
417
+ /* istanbul ignore if */
418
+ if (!this.oneofs || this.oneofs[object.name] !== object)
419
+ throw Error(object + " is not a member of " + this);
420
+
421
+ delete this.oneofs[object.name];
422
+ object.parent = null;
423
+ object.onRemove(this);
424
+ return clearCache(this);
425
+ }
426
+ return Namespace.prototype.remove.call(this, object);
427
+ };
428
+
429
+ /**
430
+ * Tests if the specified id is reserved.
431
+ * @param {number} id Id to test
432
+ * @returns {boolean} `true` if reserved, otherwise `false`
433
+ */
434
+ Type.prototype.isReservedId = function isReservedId(id) {
435
+ return Namespace.isReservedId(this.reserved, id);
436
+ };
437
+
438
+ /**
439
+ * Tests if the specified name is reserved.
440
+ * @param {string} name Name to test
441
+ * @returns {boolean} `true` if reserved, otherwise `false`
442
+ */
443
+ Type.prototype.isReservedName = function isReservedName(name) {
444
+ return Namespace.isReservedName(this.reserved, name);
445
+ };
446
+
447
+ /**
448
+ * Creates a new message of this type using the specified properties.
449
+ * @param {Object.<string,*>} [properties] Properties to set
450
+ * @returns {Message<{}>} Message instance
451
+ */
452
+ Type.prototype.create = function create(properties) {
453
+ return new this.ctor(properties);
454
+ };
455
+
456
+ /**
457
+ * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}.
458
+ * @returns {Type} `this`
459
+ */
460
+ Type.prototype.setup = function setup() {
461
+ // Sets up everything at once so that the prototype chain does not have to be re-evaluated
462
+ // multiple times (V8, soft-deopt prototype-check).
463
+
464
+ var fullName = this.fullName,
465
+ types = [];
466
+ for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
467
+ types.push(this._fieldsArray[i].resolve().resolvedType);
468
+
469
+ // Replace setup methods with type-specific generated functions
470
+ this.encode = encoder(this)({
471
+ Writer : Writer,
472
+ types : types,
473
+ util : util
474
+ });
475
+ this.decode = decoder(this)({
476
+ Reader : Reader,
477
+ types : types,
478
+ util : util
479
+ });
480
+ this.verify = verifier(this)({
481
+ types : types,
482
+ util : util
483
+ });
484
+ this.fromObject = converter.fromObject(this)({
485
+ types : types,
486
+ util : util
487
+ });
488
+ this.toObject = converter.toObject(this)({
489
+ types : types,
490
+ util : util
491
+ });
492
+
493
+ // Inject custom wrappers for common types
494
+ var wrapper = wrappers[fullName];
495
+ if (wrapper) {
496
+ var originalThis = Object.create(this);
497
+ // if (wrapper.fromObject) {
498
+ originalThis.fromObject = this.fromObject;
499
+ this.fromObject = wrapper.fromObject.bind(originalThis);
500
+ // }
501
+ // if (wrapper.toObject) {
502
+ originalThis.toObject = this.toObject;
503
+ this.toObject = wrapper.toObject.bind(originalThis);
504
+ // }
505
+ }
506
+
507
+ return this;
508
+ };
509
+
510
+ /**
511
+ * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages.
512
+ * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
513
+ * @param {Writer} [writer] Writer to encode to
514
+ * @returns {Writer} writer
515
+ */
516
+ Type.prototype.encode = function encode_setup(message, writer) {
517
+ return this.setup().encode(message, writer); // overrides this method
518
+ };
519
+
520
+ /**
521
+ * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages.
522
+ * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
523
+ * @param {Writer} [writer] Writer to encode to
524
+ * @returns {Writer} writer
525
+ */
526
+ Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
527
+ return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
528
+ };
529
+
530
+ /**
531
+ * Decodes a message of this type.
532
+ * @param {Reader|Uint8Array} reader Reader or buffer to decode from
533
+ * @param {number} [length] Length of the message, if known beforehand
534
+ * @param {number} [end] Expected group end tag, if decoding a group
535
+ * @param {number} [depth] Current nesting depth
536
+ * @returns {Message<{}>} Decoded message
537
+ * @throws {Error} If the payload is not a reader or valid buffer
538
+ * @throws {util.ProtocolError<{}>} If required fields are missing
539
+ */
540
+ Type.prototype.decode = function decode_setup(reader, length, end, depth) {
541
+ return this.setup().decode(reader, length, end, depth); // overrides this method
542
+ };
543
+
544
+ /**
545
+ * Decodes a message of this type preceeded by its byte length as a varint.
546
+ * @param {Reader|Uint8Array} reader Reader or buffer to decode from
547
+ * @returns {Message<{}>} Decoded message
548
+ * @throws {Error} If the payload is not a reader or valid buffer
549
+ * @throws {util.ProtocolError} If required fields are missing
550
+ */
551
+ Type.prototype.decodeDelimited = function decodeDelimited(reader) {
552
+ if (!(reader instanceof Reader))
553
+ reader = Reader.create(reader);
554
+ return this.decode(reader, reader.uint32());
555
+ };
556
+
557
+ /**
558
+ * Verifies that field values are valid and that required fields are present.
559
+ * @param {Object.<string,*>} message Plain object to verify
560
+ * @param {number} [depth] Current nesting depth
561
+ * @returns {null|string} `null` if valid, otherwise the reason why it is not
562
+ */
563
+ Type.prototype.verify = function verify_setup(message, depth) {
564
+ return this.setup().verify(message, depth); // overrides this method
565
+ };
566
+
567
+ /**
568
+ * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
569
+ * @param {Object.<string,*>} object Plain object to convert
570
+ * @param {number} [depth] Current nesting depth
571
+ * @returns {Message<{}>} Message instance
572
+ */
573
+ Type.prototype.fromObject = function fromObject(object, depth) {
574
+ return this.setup().fromObject(object, depth);
575
+ };
576
+
577
+ /**
578
+ * Conversion options as used by {@link Type#toObject} and {@link Message.toObject}.
579
+ * @interface IConversionOptions
580
+ * @property {Function} [longs] Long conversion type.
581
+ * Valid values are `String` and `Number` (the global types).
582
+ * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
583
+ * @property {Function} [enums] Enum value conversion type.
584
+ * Only valid value is `String` (the global type).
585
+ * Defaults to copy the present value, which is the numeric id.
586
+ * @property {Function} [bytes] Bytes value conversion type.
587
+ * Valid values are `Array` and (a base64 encoded) `String` (the global types).
588
+ * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser.
589
+ * @property {boolean} [defaults=false] Also sets default values on the resulting object
590
+ * @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
591
+ * @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
592
+ * @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any
593
+ * @property {boolean} [json=false] Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings
594
+ */
595
+
596
+ /**
597
+ * Creates a plain object from a message of this type. Also converts values to other types if specified.
598
+ * @param {Message<{}>} message Message instance
599
+ * @param {IConversionOptions} [options] Conversion options
600
+ * @returns {Object.<string,*>} Plain object
601
+ */
602
+ Type.prototype.toObject = function toObject(message, options) {
603
+ return this.setup().toObject(message, options);
604
+ };
605
+
606
+ /**
607
+ * Decorator function as returned by {@link Type.d} (TypeScript).
608
+ * @typedef TypeDecorator
609
+ * @type {function}
610
+ * @param {Constructor<T>} target Target constructor
611
+ * @returns {undefined}
612
+ * @template T extends Message<T>
613
+ */
614
+
615
+ /**
616
+ * Type decorator (TypeScript).
617
+ * @param {string} [typeName] Type name, defaults to the constructor's name
618
+ * @returns {TypeDecorator<T>} Decorator function
619
+ * @template T extends Message<T>
620
+ */
621
+ Type.d = function decorateType(typeName) {
622
+ return function typeDecorator(target) {
623
+ util.decorateType(target, typeName);
624
+ };
625
+ };