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.
- package/LICENSE +39 -39
- package/README.md +727 -727
- package/dist/light/protobuf.js +7724 -7352
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +2606 -2546
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +9593 -9132
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/debug/README.md +4 -4
- package/ext/debug/index.js +71 -71
- package/ext/descriptor/README.md +72 -72
- package/ext/descriptor/index.d.ts +195 -191
- package/ext/descriptor/index.js +1181 -1052
- package/ext/descriptor/test.js +54 -54
- package/google/LICENSE +27 -27
- package/google/README.md +1 -1
- package/google/api/annotations.json +82 -82
- package/google/api/annotations.proto +10 -10
- package/google/api/http.json +85 -85
- package/google/api/http.proto +30 -30
- package/google/protobuf/api.json +117 -117
- package/google/protobuf/api.proto +33 -33
- package/google/protobuf/descriptor.json +1381 -738
- package/google/protobuf/descriptor.proto +535 -286
- package/google/protobuf/source_context.json +19 -19
- package/google/protobuf/source_context.proto +7 -7
- package/google/protobuf/type.json +201 -201
- package/google/protobuf/type.proto +89 -89
- package/index.d.ts +2817 -2792
- package/index.js +4 -4
- package/light.d.ts +2 -2
- package/light.js +3 -3
- package/minimal.d.ts +2 -2
- package/minimal.js +4 -4
- package/package.json +96 -112
- package/scripts/postinstall.js +32 -32
- package/src/common.js +399 -399
- package/src/converter.js +310 -301
- package/src/decoder.js +135 -127
- package/src/encoder.js +100 -100
- package/src/enum.js +226 -241
- package/src/field.js +453 -432
- package/src/index-light.js +104 -104
- package/src/index-minimal.js +36 -36
- package/src/index.js +12 -12
- package/src/mapfield.js +126 -126
- package/src/message.js +139 -139
- package/src/method.js +160 -160
- package/src/namespace.js +550 -434
- package/src/object.js +385 -330
- package/src/oneof.js +222 -222
- package/src/parse.js +1032 -948
- package/src/reader.js +426 -416
- package/src/reader_buffer.js +51 -51
- package/src/root.js +404 -384
- package/src/roots.js +17 -17
- package/src/rpc/service.js +142 -142
- package/src/rpc.js +36 -36
- package/src/service.js +193 -168
- package/src/tokenize.js +421 -416
- package/src/type.js +625 -590
- package/src/types.js +196 -196
- package/src/typescript.jsdoc +15 -15
- package/src/util/aspromise.d.ts +13 -0
- package/src/util/aspromise.js +52 -0
- package/src/util/base64.d.ts +32 -0
- package/src/util/base64.js +139 -0
- package/src/util/codegen.d.ts +31 -0
- package/src/util/codegen.js +113 -0
- package/src/util/eventemitter.d.ts +45 -0
- package/src/util/eventemitter.js +84 -0
- package/src/util/fetch.d.ts +56 -0
- package/src/util/fetch.js +114 -0
- package/src/util/float.d.ts +83 -0
- package/src/util/float.js +335 -0
- package/src/util/inquire.d.ts +9 -0
- package/src/util/inquire.js +37 -0
- package/src/util/longbits.js +200 -200
- package/src/util/minimal.js +461 -438
- package/src/util/path.d.ts +22 -0
- package/src/util/path.js +72 -0
- package/src/util/patterns.js +8 -0
- package/src/util/pool.d.ts +32 -0
- package/src/util/pool.js +48 -0
- package/src/util/utf8.d.ts +24 -0
- package/src/util/utf8.js +104 -0
- package/src/util.js +215 -213
- package/src/verifier.js +180 -177
- package/src/wrappers.js +103 -102
- package/src/writer.js +465 -465
- package/src/writer_buffer.js +85 -85
- 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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
* @
|
|
47
|
-
* @
|
|
48
|
-
* @
|
|
49
|
-
* @
|
|
50
|
-
* @
|
|
51
|
-
* @param {string
|
|
52
|
-
* @param {
|
|
53
|
-
* @param {string}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
comment =
|
|
63
|
-
options =
|
|
64
|
-
extend = undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
*
|
|
261
|
-
* @
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
*
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
*
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
"
|
|
284
|
-
"
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
this.typeDefault =
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
this.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
features.
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
* @
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
*
|
|
418
|
-
* @
|
|
419
|
-
* @
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
//
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|