protobufjs 6.8.8 → 6.8.9

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 (104) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +879 -879
  3. package/cli/LICENSE +33 -33
  4. package/cli/README.md +11 -11
  5. package/cli/index.d.ts +3 -3
  6. package/cli/index.js +3 -3
  7. package/cli/lib/tsd-jsdoc/LICENSE +20 -20
  8. package/cli/lib/tsd-jsdoc/README.md +23 -23
  9. package/cli/lib/tsd-jsdoc/plugin.js +21 -21
  10. package/cli/lib/tsd-jsdoc/publish.js +693 -693
  11. package/cli/lib/tsd-jsdoc.json +18 -18
  12. package/cli/package.standalone.json +31 -31
  13. package/cli/pbjs.d.ts +9 -9
  14. package/cli/pbjs.js +329 -329
  15. package/cli/pbts.d.ts +9 -9
  16. package/cli/pbts.js +197 -197
  17. package/cli/targets/json-module.js +38 -38
  18. package/cli/targets/json.js +8 -8
  19. package/cli/targets/proto.js +326 -326
  20. package/cli/targets/proto2.js +10 -10
  21. package/cli/targets/proto3.js +10 -10
  22. package/cli/targets/static-module.js +29 -29
  23. package/cli/targets/static.js +702 -702
  24. package/cli/util.js +183 -183
  25. package/cli/wrappers/amd.js +7 -7
  26. package/cli/wrappers/closure.js +7 -7
  27. package/cli/wrappers/commonjs.js +7 -7
  28. package/cli/wrappers/default.js +15 -15
  29. package/cli/wrappers/es6.js +5 -5
  30. package/dist/README.md +31 -31
  31. package/dist/light/README.md +31 -31
  32. package/dist/light/protobuf.js +6030 -6030
  33. package/dist/light/protobuf.js.map +1 -1
  34. package/dist/light/protobuf.min.js +1 -1
  35. package/dist/light/protobuf.min.js.map +1 -1
  36. package/dist/minimal/README.md +31 -31
  37. package/dist/minimal/protobuf.js +1876 -1876
  38. package/dist/minimal/protobuf.js.map +1 -1
  39. package/dist/minimal/protobuf.min.js +1 -1
  40. package/dist/minimal/protobuf.min.js.map +1 -1
  41. package/dist/protobuf.js +7595 -7595
  42. package/dist/protobuf.js.map +1 -1
  43. package/dist/protobuf.min.js +1 -1
  44. package/dist/protobuf.min.js.map +1 -1
  45. package/ext/debug/README.md +4 -4
  46. package/ext/debug/index.js +71 -71
  47. package/ext/descriptor/README.md +72 -72
  48. package/ext/descriptor/index.js +1052 -1052
  49. package/ext/descriptor/test.js +54 -54
  50. package/google/LICENSE +27 -27
  51. package/google/README.md +1 -1
  52. package/google/api/annotations.proto +10 -10
  53. package/google/protobuf/descriptor.proto +286 -286
  54. package/google/protobuf/source_context.proto +7 -7
  55. package/index.js +4 -4
  56. package/light.d.ts +2 -2
  57. package/light.js +3 -3
  58. package/minimal.d.ts +2 -2
  59. package/minimal.js +4 -4
  60. package/package-lock.json +1748 -642
  61. package/package.json +119 -122
  62. package/scripts/changelog.js +150 -150
  63. package/scripts/postinstall.js +35 -35
  64. package/src/common.js +399 -399
  65. package/src/converter.js +293 -293
  66. package/src/decoder.js +106 -106
  67. package/src/encoder.js +99 -99
  68. package/src/enum.js +181 -181
  69. package/src/field.js +371 -371
  70. package/src/index-light.js +104 -104
  71. package/src/index-minimal.js +36 -36
  72. package/src/index.js +12 -12
  73. package/src/mapfield.js +126 -126
  74. package/src/message.js +138 -138
  75. package/src/method.js +151 -151
  76. package/src/namespace.js +433 -433
  77. package/src/object.js +200 -200
  78. package/src/oneof.js +203 -203
  79. package/src/parse.js +758 -758
  80. package/src/reader.js +405 -405
  81. package/src/reader_buffer.js +44 -44
  82. package/src/root.js +351 -351
  83. package/src/roots.js +18 -18
  84. package/src/rpc/service.js +142 -142
  85. package/src/rpc.js +36 -36
  86. package/src/service.js +167 -167
  87. package/src/tokenize.js +397 -397
  88. package/src/type.js +589 -589
  89. package/src/types.js +196 -196
  90. package/src/typescript.jsdoc +15 -15
  91. package/src/util/longbits.js +200 -200
  92. package/src/util/minimal.js +414 -414
  93. package/src/util.js +178 -178
  94. package/src/verifier.js +176 -176
  95. package/src/wrappers.js +83 -83
  96. package/src/writer.js +459 -459
  97. package/src/writer_buffer.js +81 -81
  98. package/tsconfig.json +6 -6
  99. package/cli/node_modules/os-tmpdir/index.js +0 -25
  100. package/cli/node_modules/os-tmpdir/license +0 -21
  101. package/cli/node_modules/os-tmpdir/readme.md +0 -32
  102. package/cli/node_modules/tmp/LICENSE +0 -21
  103. package/cli/node_modules/tmp/README.md +0 -314
  104. package/cli/node_modules/tmp/lib/tmp.js +0 -611
package/src/field.js CHANGED
@@ -1,371 +1,371 @@
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
- this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
86
-
87
- /**
88
- * Field type.
89
- * @type {string}
90
- */
91
- this.type = type; // toJSON
92
-
93
- /**
94
- * Unique field id.
95
- * @type {number}
96
- */
97
- this.id = id; // toJSON, marker
98
-
99
- /**
100
- * Extended type if different from parent.
101
- * @type {string|undefined}
102
- */
103
- this.extend = extend || undefined; // toJSON
104
-
105
- /**
106
- * Whether this field is required.
107
- * @type {boolean}
108
- */
109
- this.required = rule === "required";
110
-
111
- /**
112
- * Whether this field is optional.
113
- * @type {boolean}
114
- */
115
- this.optional = !this.required;
116
-
117
- /**
118
- * Whether this field is repeated.
119
- * @type {boolean}
120
- */
121
- this.repeated = rule === "repeated";
122
-
123
- /**
124
- * Whether this field is a map or not.
125
- * @type {boolean}
126
- */
127
- this.map = false;
128
-
129
- /**
130
- * Message this field belongs to.
131
- * @type {Type|null}
132
- */
133
- this.message = null;
134
-
135
- /**
136
- * OneOf this field belongs to, if any,
137
- * @type {OneOf|null}
138
- */
139
- this.partOf = null;
140
-
141
- /**
142
- * The field type's default value.
143
- * @type {*}
144
- */
145
- this.typeDefault = null;
146
-
147
- /**
148
- * The field's default value on prototypes.
149
- * @type {*}
150
- */
151
- this.defaultValue = null;
152
-
153
- /**
154
- * Whether this field's value should be treated as a long.
155
- * @type {boolean}
156
- */
157
- this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
158
-
159
- /**
160
- * Whether this field's value is a buffer.
161
- * @type {boolean}
162
- */
163
- this.bytes = type === "bytes";
164
-
165
- /**
166
- * Resolved type if not a basic type.
167
- * @type {Type|Enum|null}
168
- */
169
- this.resolvedType = null;
170
-
171
- /**
172
- * Sister-field within the extended type if a declaring extension field.
173
- * @type {Field|null}
174
- */
175
- this.extensionField = null;
176
-
177
- /**
178
- * Sister-field within the declaring namespace if an extended field.
179
- * @type {Field|null}
180
- */
181
- this.declaringField = null;
182
-
183
- /**
184
- * Internally remembers whether this field is packed.
185
- * @type {boolean|null}
186
- * @private
187
- */
188
- this._packed = null;
189
-
190
- /**
191
- * Comment for this field.
192
- * @type {string|null}
193
- */
194
- this.comment = comment;
195
- }
196
-
197
- /**
198
- * Determines whether this field is packed. Only relevant when repeated and working with proto2.
199
- * @name Field#packed
200
- * @type {boolean}
201
- * @readonly
202
- */
203
- Object.defineProperty(Field.prototype, "packed", {
204
- get: function() {
205
- // defaults to packed=true if not explicity set to false
206
- if (this._packed === null)
207
- this._packed = this.getOption("packed") !== false;
208
- return this._packed;
209
- }
210
- });
211
-
212
- /**
213
- * @override
214
- */
215
- Field.prototype.setOption = function setOption(name, value, ifNotSet) {
216
- if (name === "packed") // clear cached before setting
217
- this._packed = null;
218
- return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
219
- };
220
-
221
- /**
222
- * Field descriptor.
223
- * @interface IField
224
- * @property {string} [rule="optional"] Field rule
225
- * @property {string} type Field type
226
- * @property {number} id Field id
227
- * @property {Object.<string,*>} [options] Field options
228
- */
229
-
230
- /**
231
- * Extension field descriptor.
232
- * @interface IExtensionField
233
- * @extends IField
234
- * @property {string} extend Extended type
235
- */
236
-
237
- /**
238
- * Converts this field to a field descriptor.
239
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
240
- * @returns {IField} Field descriptor
241
- */
242
- Field.prototype.toJSON = function toJSON(toJSONOptions) {
243
- var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
244
- return util.toObject([
245
- "rule" , this.rule !== "optional" && this.rule || undefined,
246
- "type" , this.type,
247
- "id" , this.id,
248
- "extend" , this.extend,
249
- "options" , this.options,
250
- "comment" , keepComments ? this.comment : undefined
251
- ]);
252
- };
253
-
254
- /**
255
- * Resolves this field's type references.
256
- * @returns {Field} `this`
257
- * @throws {Error} If any reference cannot be resolved
258
- */
259
- Field.prototype.resolve = function resolve() {
260
-
261
- if (this.resolved)
262
- return this;
263
-
264
- if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
265
- this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
266
- if (this.resolvedType instanceof Type)
267
- this.typeDefault = null;
268
- else // instanceof Enum
269
- this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
270
- }
271
-
272
- // use explicitly set default value if present
273
- if (this.options && this.options["default"] != null) {
274
- this.typeDefault = this.options["default"];
275
- if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
276
- this.typeDefault = this.resolvedType.values[this.typeDefault];
277
- }
278
-
279
- // remove unnecessary options
280
- if (this.options) {
281
- if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
282
- delete this.options.packed;
283
- if (!Object.keys(this.options).length)
284
- this.options = undefined;
285
- }
286
-
287
- // convert to internal data type if necesssary
288
- if (this.long) {
289
- this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
290
-
291
- /* istanbul ignore else */
292
- if (Object.freeze)
293
- Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
294
-
295
- } else if (this.bytes && typeof this.typeDefault === "string") {
296
- var buf;
297
- if (util.base64.test(this.typeDefault))
298
- util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
299
- else
300
- util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
301
- this.typeDefault = buf;
302
- }
303
-
304
- // take special care of maps and repeated fields
305
- if (this.map)
306
- this.defaultValue = util.emptyObject;
307
- else if (this.repeated)
308
- this.defaultValue = util.emptyArray;
309
- else
310
- this.defaultValue = this.typeDefault;
311
-
312
- // ensure proper value on prototype
313
- if (this.parent instanceof Type)
314
- this.parent.ctor.prototype[this.name] = this.defaultValue;
315
-
316
- return ReflectionObject.prototype.resolve.call(this);
317
- };
318
-
319
- /**
320
- * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
321
- * @typedef FieldDecorator
322
- * @type {function}
323
- * @param {Object} prototype Target prototype
324
- * @param {string} fieldName Field name
325
- * @returns {undefined}
326
- */
327
-
328
- /**
329
- * Field decorator (TypeScript).
330
- * @name Field.d
331
- * @function
332
- * @param {number} fieldId Field id
333
- * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
334
- * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
335
- * @param {T} [defaultValue] Default value
336
- * @returns {FieldDecorator} Decorator function
337
- * @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
338
- */
339
- Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
340
-
341
- // submessage: decorate the submessage and use its name as the type
342
- if (typeof fieldType === "function")
343
- fieldType = util.decorateType(fieldType).name;
344
-
345
- // enum reference: create a reflected copy of the enum and keep reuseing it
346
- else if (fieldType && typeof fieldType === "object")
347
- fieldType = util.decorateEnum(fieldType).name;
348
-
349
- return function fieldDecorator(prototype, fieldName) {
350
- util.decorateType(prototype.constructor)
351
- .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
352
- };
353
- };
354
-
355
- /**
356
- * Field decorator (TypeScript).
357
- * @name Field.d
358
- * @function
359
- * @param {number} fieldId Field id
360
- * @param {Constructor<T>|string} fieldType Field type
361
- * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
362
- * @returns {FieldDecorator} Decorator function
363
- * @template T extends Message<T>
364
- * @variation 2
365
- */
366
- // like Field.d but without a default value
367
-
368
- // Sets up cyclic dependencies (called in index-light)
369
- Field._configure = function configure(Type_) {
370
- Type = Type_;
371
- };
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
+ this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
86
+
87
+ /**
88
+ * Field type.
89
+ * @type {string}
90
+ */
91
+ this.type = type; // toJSON
92
+
93
+ /**
94
+ * Unique field id.
95
+ * @type {number}
96
+ */
97
+ this.id = id; // toJSON, marker
98
+
99
+ /**
100
+ * Extended type if different from parent.
101
+ * @type {string|undefined}
102
+ */
103
+ this.extend = extend || undefined; // toJSON
104
+
105
+ /**
106
+ * Whether this field is required.
107
+ * @type {boolean}
108
+ */
109
+ this.required = rule === "required";
110
+
111
+ /**
112
+ * Whether this field is optional.
113
+ * @type {boolean}
114
+ */
115
+ this.optional = !this.required;
116
+
117
+ /**
118
+ * Whether this field is repeated.
119
+ * @type {boolean}
120
+ */
121
+ this.repeated = rule === "repeated";
122
+
123
+ /**
124
+ * Whether this field is a map or not.
125
+ * @type {boolean}
126
+ */
127
+ this.map = false;
128
+
129
+ /**
130
+ * Message this field belongs to.
131
+ * @type {Type|null}
132
+ */
133
+ this.message = null;
134
+
135
+ /**
136
+ * OneOf this field belongs to, if any,
137
+ * @type {OneOf|null}
138
+ */
139
+ this.partOf = null;
140
+
141
+ /**
142
+ * The field type's default value.
143
+ * @type {*}
144
+ */
145
+ this.typeDefault = null;
146
+
147
+ /**
148
+ * The field's default value on prototypes.
149
+ * @type {*}
150
+ */
151
+ this.defaultValue = null;
152
+
153
+ /**
154
+ * Whether this field's value should be treated as a long.
155
+ * @type {boolean}
156
+ */
157
+ this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
158
+
159
+ /**
160
+ * Whether this field's value is a buffer.
161
+ * @type {boolean}
162
+ */
163
+ this.bytes = type === "bytes";
164
+
165
+ /**
166
+ * Resolved type if not a basic type.
167
+ * @type {Type|Enum|null}
168
+ */
169
+ this.resolvedType = null;
170
+
171
+ /**
172
+ * Sister-field within the extended type if a declaring extension field.
173
+ * @type {Field|null}
174
+ */
175
+ this.extensionField = null;
176
+
177
+ /**
178
+ * Sister-field within the declaring namespace if an extended field.
179
+ * @type {Field|null}
180
+ */
181
+ this.declaringField = null;
182
+
183
+ /**
184
+ * Internally remembers whether this field is packed.
185
+ * @type {boolean|null}
186
+ * @private
187
+ */
188
+ this._packed = null;
189
+
190
+ /**
191
+ * Comment for this field.
192
+ * @type {string|null}
193
+ */
194
+ this.comment = comment;
195
+ }
196
+
197
+ /**
198
+ * Determines whether this field is packed. Only relevant when repeated and working with proto2.
199
+ * @name Field#packed
200
+ * @type {boolean}
201
+ * @readonly
202
+ */
203
+ Object.defineProperty(Field.prototype, "packed", {
204
+ get: function() {
205
+ // defaults to packed=true if not explicity set to false
206
+ if (this._packed === null)
207
+ this._packed = this.getOption("packed") !== false;
208
+ return this._packed;
209
+ }
210
+ });
211
+
212
+ /**
213
+ * @override
214
+ */
215
+ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
216
+ if (name === "packed") // clear cached before setting
217
+ this._packed = null;
218
+ return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
219
+ };
220
+
221
+ /**
222
+ * Field descriptor.
223
+ * @interface IField
224
+ * @property {string} [rule="optional"] Field rule
225
+ * @property {string} type Field type
226
+ * @property {number} id Field id
227
+ * @property {Object.<string,*>} [options] Field options
228
+ */
229
+
230
+ /**
231
+ * Extension field descriptor.
232
+ * @interface IExtensionField
233
+ * @extends IField
234
+ * @property {string} extend Extended type
235
+ */
236
+
237
+ /**
238
+ * Converts this field to a field descriptor.
239
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
240
+ * @returns {IField} Field descriptor
241
+ */
242
+ Field.prototype.toJSON = function toJSON(toJSONOptions) {
243
+ var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
244
+ return util.toObject([
245
+ "rule" , this.rule !== "optional" && this.rule || undefined,
246
+ "type" , this.type,
247
+ "id" , this.id,
248
+ "extend" , this.extend,
249
+ "options" , this.options,
250
+ "comment" , keepComments ? this.comment : undefined
251
+ ]);
252
+ };
253
+
254
+ /**
255
+ * Resolves this field's type references.
256
+ * @returns {Field} `this`
257
+ * @throws {Error} If any reference cannot be resolved
258
+ */
259
+ Field.prototype.resolve = function resolve() {
260
+
261
+ if (this.resolved)
262
+ return this;
263
+
264
+ if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
265
+ this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
266
+ if (this.resolvedType instanceof Type)
267
+ this.typeDefault = null;
268
+ else // instanceof Enum
269
+ this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
270
+ }
271
+
272
+ // use explicitly set default value if present
273
+ if (this.options && this.options["default"] != null) {
274
+ this.typeDefault = this.options["default"];
275
+ if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
276
+ this.typeDefault = this.resolvedType.values[this.typeDefault];
277
+ }
278
+
279
+ // remove unnecessary options
280
+ if (this.options) {
281
+ if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
282
+ delete this.options.packed;
283
+ if (!Object.keys(this.options).length)
284
+ this.options = undefined;
285
+ }
286
+
287
+ // convert to internal data type if necesssary
288
+ if (this.long) {
289
+ this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
290
+
291
+ /* istanbul ignore else */
292
+ if (Object.freeze)
293
+ Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
294
+
295
+ } else if (this.bytes && typeof this.typeDefault === "string") {
296
+ var buf;
297
+ if (util.base64.test(this.typeDefault))
298
+ util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
299
+ else
300
+ util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
301
+ this.typeDefault = buf;
302
+ }
303
+
304
+ // take special care of maps and repeated fields
305
+ if (this.map)
306
+ this.defaultValue = util.emptyObject;
307
+ else if (this.repeated)
308
+ this.defaultValue = util.emptyArray;
309
+ else
310
+ this.defaultValue = this.typeDefault;
311
+
312
+ // ensure proper value on prototype
313
+ if (this.parent instanceof Type)
314
+ this.parent.ctor.prototype[this.name] = this.defaultValue;
315
+
316
+ return ReflectionObject.prototype.resolve.call(this);
317
+ };
318
+
319
+ /**
320
+ * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
321
+ * @typedef FieldDecorator
322
+ * @type {function}
323
+ * @param {Object} prototype Target prototype
324
+ * @param {string} fieldName Field name
325
+ * @returns {undefined}
326
+ */
327
+
328
+ /**
329
+ * Field decorator (TypeScript).
330
+ * @name Field.d
331
+ * @function
332
+ * @param {number} fieldId Field id
333
+ * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
334
+ * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
335
+ * @param {T} [defaultValue] Default value
336
+ * @returns {FieldDecorator} Decorator function
337
+ * @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
338
+ */
339
+ Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
340
+
341
+ // submessage: decorate the submessage and use its name as the type
342
+ if (typeof fieldType === "function")
343
+ fieldType = util.decorateType(fieldType).name;
344
+
345
+ // enum reference: create a reflected copy of the enum and keep reuseing it
346
+ else if (fieldType && typeof fieldType === "object")
347
+ fieldType = util.decorateEnum(fieldType).name;
348
+
349
+ return function fieldDecorator(prototype, fieldName) {
350
+ util.decorateType(prototype.constructor)
351
+ .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
352
+ };
353
+ };
354
+
355
+ /**
356
+ * Field decorator (TypeScript).
357
+ * @name Field.d
358
+ * @function
359
+ * @param {number} fieldId Field id
360
+ * @param {Constructor<T>|string} fieldType Field type
361
+ * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
362
+ * @returns {FieldDecorator} Decorator function
363
+ * @template T extends Message<T>
364
+ * @variation 2
365
+ */
366
+ // like Field.d but without a default value
367
+
368
+ // Sets up cyclic dependencies (called in index-light)
369
+ Field._configure = function configure(Type_) {
370
+ Type = Type_;
371
+ };