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