protobufjs 8.0.1 → 8.0.2

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