protobufjs 8.6.0 → 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 (108) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8473 -8516
  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 +3018 -3061
  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 +10376 -10419
  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 -86
  22. package/ext/descriptor.generated.d.ts +14 -6
  23. package/ext/descriptor.js +1354 -1303
  24. package/ext/protojson.LICENSE +201 -201
  25. package/ext/protojson.d.ts +20 -20
  26. package/ext/protojson.js +925 -903
  27. package/ext/textformat.d.ts +19 -19
  28. package/ext/textformat.js +1256 -1256
  29. package/google/LICENSE +27 -27
  30. package/google/README.md +1 -1
  31. package/google/api/annotations.json +82 -82
  32. package/google/api/annotations.proto +10 -10
  33. package/google/api/http.json +85 -85
  34. package/google/api/http.proto +30 -30
  35. package/google/protobuf/api.json +117 -117
  36. package/google/protobuf/api.proto +33 -33
  37. package/google/protobuf/compiler/plugin.json +126 -126
  38. package/google/protobuf/compiler/plugin.proto +47 -47
  39. package/google/protobuf/descriptor.json +1381 -1381
  40. package/google/protobuf/descriptor.proto +534 -534
  41. package/google/protobuf/source_context.json +19 -19
  42. package/google/protobuf/source_context.proto +7 -7
  43. package/google/protobuf/type.json +201 -201
  44. package/google/protobuf/type.proto +89 -89
  45. package/index.d.ts +10 -18
  46. package/index.js +4 -4
  47. package/light.d.ts +2 -2
  48. package/light.js +3 -3
  49. package/minimal.d.ts +2 -2
  50. package/minimal.js +4 -4
  51. package/package.json +93 -93
  52. package/src/common.js +399 -399
  53. package/src/converter.js +344 -344
  54. package/src/decoder.js +208 -208
  55. package/src/encoder.js +111 -111
  56. package/src/enum.js +231 -231
  57. package/src/field.js +497 -497
  58. package/src/index-light.js +104 -104
  59. package/src/index-minimal.js +36 -36
  60. package/src/index.js +12 -12
  61. package/src/mapfield.js +136 -136
  62. package/src/message.js +137 -137
  63. package/src/method.js +175 -175
  64. package/src/namespace.js +565 -565
  65. package/src/object.js +382 -382
  66. package/src/oneof.js +225 -225
  67. package/src/parse.js +1068 -1068
  68. package/src/reader.js +543 -543
  69. package/src/reader_buffer.js +72 -72
  70. package/src/root.js +416 -416
  71. package/src/roots.js +18 -18
  72. package/src/rpc/service.js +148 -148
  73. package/src/rpc.js +36 -36
  74. package/src/service.js +198 -198
  75. package/src/tokenize.js +421 -421
  76. package/src/type.js +655 -655
  77. package/src/types.js +196 -196
  78. package/src/typescript.js +25 -25
  79. package/src/util/aspromise.d.ts +13 -13
  80. package/src/util/aspromise.js +52 -52
  81. package/src/util/base64.d.ts +32 -32
  82. package/src/util/base64.js +146 -146
  83. package/src/util/codegen.d.ts +31 -31
  84. package/src/util/codegen.js +113 -113
  85. package/src/util/eventemitter.d.ts +45 -45
  86. package/src/util/eventemitter.js +86 -86
  87. package/src/util/fetch.d.ts +56 -56
  88. package/src/util/fetch.js +112 -112
  89. package/src/util/float.d.ts +83 -83
  90. package/src/util/float.js +335 -335
  91. package/src/util/fs.js +11 -11
  92. package/src/util/longbits.js +200 -200
  93. package/src/util/minimal.js +515 -518
  94. package/src/util/path.d.ts +22 -22
  95. package/src/util/path.js +72 -72
  96. package/src/util/patterns.js +7 -7
  97. package/src/util/pool.d.ts +32 -32
  98. package/src/util/pool.js +48 -48
  99. package/src/util/utf8.d.ts +24 -24
  100. package/src/util/utf8.js +130 -130
  101. package/src/util.js +242 -242
  102. package/src/verifier.js +180 -180
  103. package/src/wrappers.js +106 -106
  104. package/src/writer.js +495 -495
  105. package/src/writer_buffer.js +102 -102
  106. package/tsconfig.json +6 -6
  107. package/src/util/inquire.d.ts +0 -10
  108. package/src/util/inquire.js +0 -38
package/src/field.js CHANGED
@@ -1,497 +1,497 @@
1
- "use strict";
2
- module.exports = Field;
3
-
4
- // extends ReflectionObject
5
- var ReflectionObject = require("./object");
6
- ((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
7
-
8
- var Enum = require("./enum"),
9
- types = require("./types"),
10
- util = require("./util");
11
-
12
- var Type; // cyclic
13
-
14
- var ruleRe = /^(?:required|optional|repeated)$/;
15
-
16
- /**
17
- * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
18
- * @name Field
19
- * @classdesc Reflected message field.
20
- * @extends FieldBase
21
- * @constructor
22
- * @param {string} name Unique name within its namespace
23
- * @param {number} id Unique id within its namespace
24
- * @param {string} type Value type
25
- * @param {string|Object.<string,*>} [rule="optional"] Field rule
26
- * @param {string|Object.<string,*>} [extend] Extended type if different from parent
27
- * @param {Object.<string,*>} [options] Declared options
28
- */
29
-
30
- /**
31
- * Constructs a field from a field descriptor.
32
- * @param {string} name Field name
33
- * @param {IField} json Field descriptor
34
- * @returns {Field} Created field
35
- * @throws {TypeError} If arguments are invalid
36
- */
37
- Field.fromJSON = function fromJSON(name, json) {
38
- var field = new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
39
- if (json.edition)
40
- field._edition = json.edition;
41
- if (json.protoName)
42
- field.protoName = json.protoName;
43
- if (json.jsonName !== undefined)
44
- field.jsonName = json.jsonName;
45
- else if (json.options && json.options.json_name !== undefined)
46
- field.jsonName = json.options.json_name;
47
- field._defaultEdition = "proto3"; // For backwards-compatibility.
48
- return field;
49
- };
50
-
51
- /**
52
- * Not an actual constructor. Use {@link Field} instead.
53
- * @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
54
- * @exports FieldBase
55
- * @extends ReflectionObject
56
- * @constructor
57
- * @param {string} name Unique name within its namespace
58
- * @param {number} id Unique id within its namespace
59
- * @param {string} type Value type
60
- * @param {string|Object.<string,*>} [rule="optional"] Field rule
61
- * @param {string|Object.<string,*>} [extend] Extended type if different from parent
62
- * @param {Object.<string,*>} [options] Declared options
63
- * @param {string} [comment] Comment associated with this field
64
- */
65
- function Field(name, id, type, rule, extend, options, comment) {
66
-
67
- if (util.isObject(rule)) {
68
- comment = extend;
69
- options = rule;
70
- rule = extend = undefined;
71
- } else if (util.isObject(extend)) {
72
- comment = options;
73
- options = extend;
74
- extend = undefined;
75
- }
76
-
77
- ReflectionObject.call(this, name, options);
78
-
79
- if (!util.isInteger(id) || id < 0)
80
- throw TypeError("id must be a non-negative integer");
81
-
82
- if (!util.isString(type))
83
- throw TypeError("type must be a string");
84
-
85
- if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
86
- throw TypeError("rule must be a string rule");
87
-
88
- if (extend !== undefined && !util.isString(extend))
89
- throw TypeError("extend must be a string");
90
-
91
- /**
92
- * Field rule, if any.
93
- * @type {string|undefined}
94
- */
95
- this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
96
-
97
- /**
98
- * Field type.
99
- * @type {string}
100
- */
101
- this.type = type; // toJSON
102
-
103
- /**
104
- * Unique field id.
105
- * @type {number}
106
- */
107
- this.id = id; // toJSON, marker
108
-
109
- /**
110
- * Extended type if different from parent.
111
- * @type {string|undefined}
112
- */
113
- this.extend = extend || undefined; // toJSON
114
-
115
- /**
116
- * Whether this field is repeated.
117
- * @type {boolean}
118
- */
119
- this.repeated = rule === "repeated";
120
-
121
- /**
122
- * Whether this field is a map or not.
123
- * @type {boolean}
124
- */
125
- this.map = false;
126
-
127
- /**
128
- * Message this field belongs to.
129
- * @type {Type|null}
130
- */
131
- this.message = null;
132
-
133
- /**
134
- * OneOf this field belongs to, if any,
135
- * @type {OneOf|null}
136
- */
137
- this.partOf = null;
138
-
139
- /**
140
- * The field type's default value.
141
- * @type {*}
142
- */
143
- this.typeDefault = null;
144
-
145
- /**
146
- * The field's default value on prototypes.
147
- * @type {*}
148
- */
149
- this.defaultValue = null;
150
-
151
- /**
152
- * Whether this field's value should be treated as a long.
153
- * @type {boolean}
154
- */
155
- this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
156
-
157
- /**
158
- * Whether this field's value is a buffer.
159
- * @type {boolean}
160
- */
161
- this.bytes = type === "bytes";
162
-
163
- /**
164
- * Resolved type if not a basic type.
165
- * @type {Type|Enum|null}
166
- */
167
- this.resolvedType = null;
168
-
169
- /**
170
- * Sister-field within the extended type if a declaring extension field.
171
- * @type {Field|null}
172
- */
173
- this.extensionField = null;
174
-
175
- /**
176
- * Sister-field within the declaring namespace if an extended field.
177
- * @type {Field|null}
178
- */
179
- this.declaringField = null;
180
-
181
- /**
182
- * Comment for this field.
183
- * @type {string|null}
184
- */
185
- this.comment = comment;
186
-
187
- /**
188
- * Field name as declared in the .proto source, if different from `name`.
189
- * @type {string|undefined}
190
- */
191
- this.protoName = undefined;
192
-
193
- /**
194
- * JSON name, if different from the derived default.
195
- * @type {string|undefined}
196
- */
197
- this.jsonName = undefined;
198
- }
199
-
200
- /**
201
- * Determines whether this field is required.
202
- * @name Field#required
203
- * @type {boolean}
204
- * @readonly
205
- */
206
- Object.defineProperty(Field.prototype, "required", {
207
- get: function() {
208
- return this._features.field_presence === "LEGACY_REQUIRED";
209
- }
210
- });
211
-
212
- /**
213
- * Determines whether this field is not required.
214
- * @name Field#optional
215
- * @type {boolean}
216
- * @readonly
217
- */
218
- Object.defineProperty(Field.prototype, "optional", {
219
- get: function() {
220
- return !this.required;
221
- }
222
- });
223
-
224
- /**
225
- * Determines whether this field uses tag-delimited encoding. In proto2 this
226
- * corresponded to group syntax.
227
- * @name Field#delimited
228
- * @type {boolean}
229
- * @readonly
230
- */
231
- Object.defineProperty(Field.prototype, "delimited", {
232
- get: function() {
233
- return this.resolvedType instanceof Type &&
234
- this._features.message_encoding === "DELIMITED";
235
- }
236
- });
237
-
238
- /**
239
- * Determines whether this field is packed. Only relevant when repeated.
240
- * @name Field#packed
241
- * @type {boolean}
242
- * @readonly
243
- */
244
- Object.defineProperty(Field.prototype, "packed", {
245
- get: function() {
246
- return this._features.repeated_field_encoding === "PACKED";
247
- }
248
- });
249
-
250
- /**
251
- * Determines whether this field tracks presence.
252
- * @name Field#hasPresence
253
- * @type {boolean}
254
- * @readonly
255
- */
256
- Object.defineProperty(Field.prototype, "hasPresence", {
257
- get: function() {
258
- if (this.repeated || this.map) {
259
- return false;
260
- }
261
- return this.partOf || // oneofs
262
- this.declaringField || this.extensionField || // extensions
263
- this._features.field_presence !== "IMPLICIT";
264
- }
265
- });
266
-
267
- /**
268
- * The field name as declared in the .proto source (snake_case). Populated on resolve,
269
- * falling back to `name`. Mirrors `FieldDescriptorProto.name`.
270
- * @name Field#protoName
271
- * @type {string}
272
- * @readonly
273
- */
274
-
275
- /**
276
- * The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
277
- * explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
278
- * @name Field#jsonName
279
- * @type {string}
280
- * @readonly
281
- */
282
-
283
- /**
284
- * @override
285
- */
286
- Field.prototype.setOption = function setOption(name, value, ifNotSet) {
287
- return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
288
- };
289
-
290
- /**
291
- * Field descriptor.
292
- * @interface IField
293
- * @property {string} [edition] Edition
294
- * @property {string} [rule="optional"] Field rule
295
- * @property {string} type Field type
296
- * @property {number} id Field id
297
- * @property {Object.<string,*>} [options] Field options
298
- * @property {string|null} [comment] Field comment
299
- */
300
-
301
- /**
302
- * Extension field descriptor.
303
- * @interface IExtensionField
304
- * @extends IField
305
- * @property {string} extend Extended type
306
- */
307
-
308
- /**
309
- * Converts this field to a field descriptor.
310
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
311
- * @returns {IField} Field descriptor
312
- */
313
- Field.prototype.toJSON = function toJSON(toJSONOptions) {
314
- var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
315
- return util.toObject([
316
- "edition" , this._editionToJSON(),
317
- "rule" , this.rule !== "optional" && this.rule || undefined,
318
- "type" , this.type,
319
- "id" , this.id,
320
- "extend" , this.extend,
321
- "protoName" , this.protoName !== this.name ? this.protoName : undefined,
322
- "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
323
- "options" , this.options,
324
- "comment" , keepComments ? this.comment : undefined
325
- ]);
326
- };
327
-
328
- /**
329
- * Resolves this field's type references.
330
- * @returns {Field} `this`
331
- * @throws {Error} If any reference cannot be resolved
332
- */
333
- Field.prototype.resolve = function resolve() {
334
-
335
- if (this.resolved)
336
- return this;
337
-
338
- if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
339
- this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
340
- if (this.resolvedType instanceof Type)
341
- this.typeDefault = null;
342
- else // instanceof Enum
343
- this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
344
- } else if (this.options && this.options.proto3_optional) {
345
- // proto3 scalar value marked optional; should default to null
346
- this.typeDefault = null;
347
- }
348
-
349
- // use explicitly set default value if present
350
- if (this.options && this.options["default"] != null) {
351
- this.typeDefault = this.options["default"];
352
- if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
353
- this.typeDefault = this.resolvedType.values[this.typeDefault];
354
- }
355
-
356
- // remove unnecessary options
357
- if (this.options) {
358
- if (this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
359
- delete this.options.packed;
360
- if (!Object.keys(this.options).length)
361
- this.options = undefined;
362
- }
363
-
364
- // convert to internal data type if necesssary
365
- if (this.long) {
366
- this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type === "uint64" || this.type === "fixed64");
367
-
368
- /* istanbul ignore else */
369
- if (Object.freeze)
370
- Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
371
-
372
- } else if (this.bytes && typeof this.typeDefault === "string") {
373
- var buf;
374
- if (util.base64.test(this.typeDefault))
375
- util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
376
- else
377
- util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
378
- this.typeDefault = buf;
379
- }
380
-
381
- // take special care of maps and repeated fields
382
- if (this.map)
383
- this.defaultValue = util.emptyObject;
384
- else if (this.repeated)
385
- this.defaultValue = util.emptyArray;
386
- else
387
- this.defaultValue = this.typeDefault;
388
-
389
- // ensure proper value on prototype
390
- if (this.parent instanceof Type && this.parent._ctor)
391
- this.parent._ctor.prototype[this.name] = this.defaultValue;
392
-
393
- // derive the proto/JSON names
394
- if (this.protoName === undefined)
395
- this.protoName = this.name;
396
- if (this.jsonName === undefined)
397
- this.jsonName = util.jsonName(this.protoName);
398
-
399
- return ReflectionObject.prototype.resolve.call(this);
400
- };
401
-
402
- /**
403
- * Infers field features from legacy syntax that may have been specified differently.
404
- * in older editions.
405
- * @param {string|undefined} edition The edition this proto is on, or undefined if pre-editions
406
- * @returns {object} The feature values to override
407
- */
408
- Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(edition) {
409
- if (edition !== "proto2" && edition !== "proto3") {
410
- return {};
411
- }
412
-
413
- var features = {};
414
-
415
- if (this.rule === "required") {
416
- features.field_presence = "LEGACY_REQUIRED";
417
- }
418
- if (this.parent && types.defaults[this.type] === undefined) {
419
- // We can't use resolvedType because types may not have been resolved yet. However,
420
- // legacy groups are always in the same scope as the field so we don't have to do a
421
- // full scan of the tree.
422
- var type = this.parent.get(this.type.split(".").pop());
423
- if (type && type instanceof Type && type.group) {
424
- features.message_encoding = "DELIMITED";
425
- }
426
- }
427
- if (this.getOption("packed") === true) {
428
- features.repeated_field_encoding = "PACKED";
429
- } else if (this.getOption("packed") === false) {
430
- features.repeated_field_encoding = "EXPANDED";
431
- }
432
- return features;
433
- };
434
-
435
- /**
436
- * @override
437
- */
438
- Field.prototype._resolveFeatures = function _resolveFeatures(edition) {
439
- return ReflectionObject.prototype._resolveFeatures.call(this, this._edition || edition);
440
- };
441
-
442
- /**
443
- * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
444
- * @typedef FieldDecorator
445
- * @type {function}
446
- * @param {Object} prototype Target prototype
447
- * @param {string} fieldName Field name
448
- * @returns {undefined}
449
- * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
450
- */
451
-
452
- /**
453
- * Field decorator (TypeScript).
454
- * @name Field.d
455
- * @function
456
- * @param {number} fieldId Field id
457
- * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
458
- * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
459
- * @param {T} [defaultValue] Default value
460
- * @returns {FieldDecorator} Decorator function
461
- * @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
462
- * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
463
- */
464
- Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
465
-
466
- // submessage: decorate the submessage and use its name as the type
467
- if (typeof fieldType === "function")
468
- fieldType = util.decorateType(fieldType).name;
469
-
470
- // enum reference: create a reflected copy of the enum and keep reuseing it
471
- else if (fieldType && typeof fieldType === "object")
472
- fieldType = util.decorateEnum(fieldType).name;
473
-
474
- return function fieldDecorator(prototype, fieldName) {
475
- util.decorateType(prototype.constructor)
476
- .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
477
- };
478
- };
479
-
480
- // Sets up cyclic dependencies (called in index-light)
481
- Field._configure = function configure(Type_) {
482
- Type = Type_;
483
- };
484
-
485
- /**
486
- * Field decorator (TypeScript).
487
- * @name Field.d
488
- * @function
489
- * @param {number} fieldId Field id
490
- * @param {Constructor<T>|string} fieldType Field type
491
- * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
492
- * @returns {FieldDecorator} Decorator function
493
- * @template T extends Message<T>
494
- * @variation 2
495
- * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
496
- */
497
- // like Field.d but without a default value
1
+ "use strict";
2
+ module.exports = Field;
3
+
4
+ // extends ReflectionObject
5
+ var ReflectionObject = require("./object");
6
+ ((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
7
+
8
+ var Enum = require("./enum"),
9
+ types = require("./types"),
10
+ util = require("./util");
11
+
12
+ var Type; // cyclic
13
+
14
+ var ruleRe = /^(?:required|optional|repeated)$/;
15
+
16
+ /**
17
+ * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
18
+ * @name Field
19
+ * @classdesc Reflected message field.
20
+ * @extends FieldBase
21
+ * @constructor
22
+ * @param {string} name Unique name within its namespace
23
+ * @param {number} id Unique id within its namespace
24
+ * @param {string} type Value type
25
+ * @param {string|Object.<string,*>} [rule="optional"] Field rule
26
+ * @param {string|Object.<string,*>} [extend] Extended type if different from parent
27
+ * @param {Object.<string,*>} [options] Declared options
28
+ */
29
+
30
+ /**
31
+ * Constructs a field from a field descriptor.
32
+ * @param {string} name Field name
33
+ * @param {IField} json Field descriptor
34
+ * @returns {Field} Created field
35
+ * @throws {TypeError} If arguments are invalid
36
+ */
37
+ Field.fromJSON = function fromJSON(name, json) {
38
+ var field = new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
39
+ if (json.edition)
40
+ field._edition = json.edition;
41
+ if (json.protoName)
42
+ field.protoName = json.protoName;
43
+ if (json.jsonName !== undefined)
44
+ field.jsonName = json.jsonName;
45
+ else if (json.options && json.options.json_name !== undefined)
46
+ field.jsonName = json.options.json_name;
47
+ field._defaultEdition = "proto3"; // For backwards-compatibility.
48
+ return field;
49
+ };
50
+
51
+ /**
52
+ * Not an actual constructor. Use {@link Field} instead.
53
+ * @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
54
+ * @exports FieldBase
55
+ * @extends ReflectionObject
56
+ * @constructor
57
+ * @param {string} name Unique name within its namespace
58
+ * @param {number} id Unique id within its namespace
59
+ * @param {string} type Value type
60
+ * @param {string|Object.<string,*>} [rule="optional"] Field rule
61
+ * @param {string|Object.<string,*>} [extend] Extended type if different from parent
62
+ * @param {Object.<string,*>} [options] Declared options
63
+ * @param {string} [comment] Comment associated with this field
64
+ */
65
+ function Field(name, id, type, rule, extend, options, comment) {
66
+
67
+ if (util.isObject(rule)) {
68
+ comment = extend;
69
+ options = rule;
70
+ rule = extend = undefined;
71
+ } else if (util.isObject(extend)) {
72
+ comment = options;
73
+ options = extend;
74
+ extend = undefined;
75
+ }
76
+
77
+ ReflectionObject.call(this, name, options);
78
+
79
+ if (!util.isInteger(id) || id < 0)
80
+ throw TypeError("id must be a non-negative integer");
81
+
82
+ if (!util.isString(type))
83
+ throw TypeError("type must be a string");
84
+
85
+ if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
86
+ throw TypeError("rule must be a string rule");
87
+
88
+ if (extend !== undefined && !util.isString(extend))
89
+ throw TypeError("extend must be a string");
90
+
91
+ /**
92
+ * Field rule, if any.
93
+ * @type {string|undefined}
94
+ */
95
+ this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
96
+
97
+ /**
98
+ * Field type.
99
+ * @type {string}
100
+ */
101
+ this.type = type; // toJSON
102
+
103
+ /**
104
+ * Unique field id.
105
+ * @type {number}
106
+ */
107
+ this.id = id; // toJSON, marker
108
+
109
+ /**
110
+ * Extended type if different from parent.
111
+ * @type {string|undefined}
112
+ */
113
+ this.extend = extend || undefined; // toJSON
114
+
115
+ /**
116
+ * Whether this field is repeated.
117
+ * @type {boolean}
118
+ */
119
+ this.repeated = rule === "repeated";
120
+
121
+ /**
122
+ * Whether this field is a map or not.
123
+ * @type {boolean}
124
+ */
125
+ this.map = false;
126
+
127
+ /**
128
+ * Message this field belongs to.
129
+ * @type {Type|null}
130
+ */
131
+ this.message = null;
132
+
133
+ /**
134
+ * OneOf this field belongs to, if any,
135
+ * @type {OneOf|null}
136
+ */
137
+ this.partOf = null;
138
+
139
+ /**
140
+ * The field type's default value.
141
+ * @type {*}
142
+ */
143
+ this.typeDefault = null;
144
+
145
+ /**
146
+ * The field's default value on prototypes.
147
+ * @type {*}
148
+ */
149
+ this.defaultValue = null;
150
+
151
+ /**
152
+ * Whether this field's value should be treated as a long.
153
+ * @type {boolean}
154
+ */
155
+ this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
156
+
157
+ /**
158
+ * Whether this field's value is a buffer.
159
+ * @type {boolean}
160
+ */
161
+ this.bytes = type === "bytes";
162
+
163
+ /**
164
+ * Resolved type if not a basic type.
165
+ * @type {Type|Enum|null}
166
+ */
167
+ this.resolvedType = null;
168
+
169
+ /**
170
+ * Sister-field within the extended type if a declaring extension field.
171
+ * @type {Field|null}
172
+ */
173
+ this.extensionField = null;
174
+
175
+ /**
176
+ * Sister-field within the declaring namespace if an extended field.
177
+ * @type {Field|null}
178
+ */
179
+ this.declaringField = null;
180
+
181
+ /**
182
+ * Comment for this field.
183
+ * @type {string|null}
184
+ */
185
+ this.comment = comment;
186
+
187
+ /**
188
+ * Field name as declared in the .proto source, if different from `name`.
189
+ * @type {string|undefined}
190
+ */
191
+ this.protoName = undefined;
192
+
193
+ /**
194
+ * JSON name, if different from the derived default.
195
+ * @type {string|undefined}
196
+ */
197
+ this.jsonName = undefined;
198
+ }
199
+
200
+ /**
201
+ * Determines whether this field is required.
202
+ * @name Field#required
203
+ * @type {boolean}
204
+ * @readonly
205
+ */
206
+ Object.defineProperty(Field.prototype, "required", {
207
+ get: function() {
208
+ return this._features.field_presence === "LEGACY_REQUIRED";
209
+ }
210
+ });
211
+
212
+ /**
213
+ * Determines whether this field is not required.
214
+ * @name Field#optional
215
+ * @type {boolean}
216
+ * @readonly
217
+ */
218
+ Object.defineProperty(Field.prototype, "optional", {
219
+ get: function() {
220
+ return !this.required;
221
+ }
222
+ });
223
+
224
+ /**
225
+ * Determines whether this field uses tag-delimited encoding. In proto2 this
226
+ * corresponded to group syntax.
227
+ * @name Field#delimited
228
+ * @type {boolean}
229
+ * @readonly
230
+ */
231
+ Object.defineProperty(Field.prototype, "delimited", {
232
+ get: function() {
233
+ return this.resolvedType instanceof Type &&
234
+ this._features.message_encoding === "DELIMITED";
235
+ }
236
+ });
237
+
238
+ /**
239
+ * Determines whether this field is packed. Only relevant when repeated.
240
+ * @name Field#packed
241
+ * @type {boolean}
242
+ * @readonly
243
+ */
244
+ Object.defineProperty(Field.prototype, "packed", {
245
+ get: function() {
246
+ return this._features.repeated_field_encoding === "PACKED";
247
+ }
248
+ });
249
+
250
+ /**
251
+ * Determines whether this field tracks presence.
252
+ * @name Field#hasPresence
253
+ * @type {boolean}
254
+ * @readonly
255
+ */
256
+ Object.defineProperty(Field.prototype, "hasPresence", {
257
+ get: function() {
258
+ if (this.repeated || this.map) {
259
+ return false;
260
+ }
261
+ return this.partOf || // oneofs
262
+ this.declaringField || this.extensionField || // extensions
263
+ this._features.field_presence !== "IMPLICIT";
264
+ }
265
+ });
266
+
267
+ /**
268
+ * The field name as declared in the .proto source (snake_case). Populated on resolve,
269
+ * falling back to `name`. Mirrors `FieldDescriptorProto.name`.
270
+ * @name Field#protoName
271
+ * @type {string}
272
+ * @readonly
273
+ */
274
+
275
+ /**
276
+ * The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
277
+ * explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
278
+ * @name Field#jsonName
279
+ * @type {string}
280
+ * @readonly
281
+ */
282
+
283
+ /**
284
+ * @override
285
+ */
286
+ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
287
+ return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
288
+ };
289
+
290
+ /**
291
+ * Field descriptor.
292
+ * @interface IField
293
+ * @property {string} [edition] Edition
294
+ * @property {string} [rule="optional"] Field rule
295
+ * @property {string} type Field type
296
+ * @property {number} id Field id
297
+ * @property {Object.<string,*>} [options] Field options
298
+ * @property {string|null} [comment] Field comment
299
+ */
300
+
301
+ /**
302
+ * Extension field descriptor.
303
+ * @interface IExtensionField
304
+ * @extends IField
305
+ * @property {string} extend Extended type
306
+ */
307
+
308
+ /**
309
+ * Converts this field to a field descriptor.
310
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
311
+ * @returns {IField} Field descriptor
312
+ */
313
+ Field.prototype.toJSON = function toJSON(toJSONOptions) {
314
+ var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
315
+ return util.toObject([
316
+ "edition" , this._editionToJSON(),
317
+ "rule" , this.rule !== "optional" && this.rule || undefined,
318
+ "type" , this.type,
319
+ "id" , this.id,
320
+ "extend" , this.extend,
321
+ "protoName" , this.protoName !== this.name ? this.protoName : undefined,
322
+ "jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
323
+ "options" , this.options,
324
+ "comment" , keepComments ? this.comment : undefined
325
+ ]);
326
+ };
327
+
328
+ /**
329
+ * Resolves this field's type references.
330
+ * @returns {Field} `this`
331
+ * @throws {Error} If any reference cannot be resolved
332
+ */
333
+ Field.prototype.resolve = function resolve() {
334
+
335
+ if (this.resolved)
336
+ return this;
337
+
338
+ if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
339
+ this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
340
+ if (this.resolvedType instanceof Type)
341
+ this.typeDefault = null;
342
+ else // instanceof Enum
343
+ this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
344
+ } else if (this.options && this.options.proto3_optional) {
345
+ // proto3 scalar value marked optional; should default to null
346
+ this.typeDefault = null;
347
+ }
348
+
349
+ // use explicitly set default value if present
350
+ if (this.options && this.options["default"] != null) {
351
+ this.typeDefault = this.options["default"];
352
+ if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
353
+ this.typeDefault = this.resolvedType.values[this.typeDefault];
354
+ }
355
+
356
+ // remove unnecessary options
357
+ if (this.options) {
358
+ if (this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
359
+ delete this.options.packed;
360
+ if (!Object.keys(this.options).length)
361
+ this.options = undefined;
362
+ }
363
+
364
+ // convert to internal data type if necesssary
365
+ if (this.long) {
366
+ this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type === "uint64" || this.type === "fixed64");
367
+
368
+ /* istanbul ignore else */
369
+ if (Object.freeze)
370
+ Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
371
+
372
+ } else if (this.bytes && typeof this.typeDefault === "string") {
373
+ var buf;
374
+ if (util.base64.test(this.typeDefault))
375
+ util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
376
+ else
377
+ util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
378
+ this.typeDefault = buf;
379
+ }
380
+
381
+ // take special care of maps and repeated fields
382
+ if (this.map)
383
+ this.defaultValue = util.emptyObject;
384
+ else if (this.repeated)
385
+ this.defaultValue = util.emptyArray;
386
+ else
387
+ this.defaultValue = this.typeDefault;
388
+
389
+ // ensure proper value on prototype
390
+ if (this.parent instanceof Type && this.parent._ctor)
391
+ this.parent._ctor.prototype[this.name] = this.defaultValue;
392
+
393
+ // derive the proto/JSON names
394
+ if (this.protoName === undefined)
395
+ this.protoName = this.name;
396
+ if (this.jsonName === undefined)
397
+ this.jsonName = util.jsonName(this.protoName);
398
+
399
+ return ReflectionObject.prototype.resolve.call(this);
400
+ };
401
+
402
+ /**
403
+ * Infers field features from legacy syntax that may have been specified differently.
404
+ * in older editions.
405
+ * @param {string|undefined} edition The edition this proto is on, or undefined if pre-editions
406
+ * @returns {object} The feature values to override
407
+ */
408
+ Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(edition) {
409
+ if (edition !== "proto2" && edition !== "proto3") {
410
+ return {};
411
+ }
412
+
413
+ var features = {};
414
+
415
+ if (this.rule === "required") {
416
+ features.field_presence = "LEGACY_REQUIRED";
417
+ }
418
+ if (this.parent && types.defaults[this.type] === undefined) {
419
+ // We can't use resolvedType because types may not have been resolved yet. However,
420
+ // legacy groups are always in the same scope as the field so we don't have to do a
421
+ // full scan of the tree.
422
+ var type = this.parent.get(this.type.split(".").pop());
423
+ if (type && type instanceof Type && type.group) {
424
+ features.message_encoding = "DELIMITED";
425
+ }
426
+ }
427
+ if (this.getOption("packed") === true) {
428
+ features.repeated_field_encoding = "PACKED";
429
+ } else if (this.getOption("packed") === false) {
430
+ features.repeated_field_encoding = "EXPANDED";
431
+ }
432
+ return features;
433
+ };
434
+
435
+ /**
436
+ * @override
437
+ */
438
+ Field.prototype._resolveFeatures = function _resolveFeatures(edition) {
439
+ return ReflectionObject.prototype._resolveFeatures.call(this, this._edition || edition);
440
+ };
441
+
442
+ /**
443
+ * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
444
+ * @typedef FieldDecorator
445
+ * @type {function}
446
+ * @param {Object} prototype Target prototype
447
+ * @param {string} fieldName Field name
448
+ * @returns {undefined}
449
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
450
+ */
451
+
452
+ /**
453
+ * Field decorator (TypeScript).
454
+ * @name Field.d
455
+ * @function
456
+ * @param {number} fieldId Field id
457
+ * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
458
+ * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
459
+ * @param {T} [defaultValue] Default value
460
+ * @returns {FieldDecorator} Decorator function
461
+ * @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
462
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
463
+ */
464
+ Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
465
+
466
+ // submessage: decorate the submessage and use its name as the type
467
+ if (typeof fieldType === "function")
468
+ fieldType = util.decorateType(fieldType).name;
469
+
470
+ // enum reference: create a reflected copy of the enum and keep reuseing it
471
+ else if (fieldType && typeof fieldType === "object")
472
+ fieldType = util.decorateEnum(fieldType).name;
473
+
474
+ return function fieldDecorator(prototype, fieldName) {
475
+ util.decorateType(prototype.constructor)
476
+ .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
477
+ };
478
+ };
479
+
480
+ // Sets up cyclic dependencies (called in index-light)
481
+ Field._configure = function configure(Type_) {
482
+ Type = Type_;
483
+ };
484
+
485
+ /**
486
+ * Field decorator (TypeScript).
487
+ * @name Field.d
488
+ * @function
489
+ * @param {number} fieldId Field id
490
+ * @param {Constructor<T>|string} fieldType Field type
491
+ * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
492
+ * @returns {FieldDecorator} Decorator function
493
+ * @template T extends Message<T>
494
+ * @variation 2
495
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
496
+ */
497
+ // like Field.d but without a default value