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