protobufjs 8.0.1-experimental → 8.0.2

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