protobufjs 8.6.2 → 8.6.3

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 (105) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8575 -8454
  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 +3064 -2999
  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 +10479 -10358
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/README.md +70 -70
  16. package/ext/debug/README.md +4 -4
  17. package/ext/debug/index.js +71 -71
  18. package/ext/descriptor/README.md +5 -5
  19. package/ext/descriptor/index.d.ts +2 -2
  20. package/ext/descriptor/index.js +2 -2
  21. package/ext/descriptor.d.ts +87 -87
  22. package/ext/descriptor.js +1354 -1354
  23. package/ext/protojson.LICENSE +201 -201
  24. package/ext/protojson.d.ts +20 -20
  25. package/ext/protojson.js +925 -925
  26. package/ext/textformat.d.ts +19 -19
  27. package/ext/textformat.js +1256 -1256
  28. package/google/LICENSE +27 -27
  29. package/google/README.md +1 -1
  30. package/google/api/annotations.json +82 -82
  31. package/google/api/annotations.proto +10 -10
  32. package/google/api/http.json +85 -85
  33. package/google/api/http.proto +30 -30
  34. package/google/protobuf/api.json +117 -117
  35. package/google/protobuf/api.proto +33 -33
  36. package/google/protobuf/compiler/plugin.json +126 -126
  37. package/google/protobuf/compiler/plugin.proto +47 -47
  38. package/google/protobuf/descriptor.json +1381 -1381
  39. package/google/protobuf/descriptor.proto +534 -534
  40. package/google/protobuf/source_context.json +19 -19
  41. package/google/protobuf/source_context.proto +7 -7
  42. package/google/protobuf/type.json +201 -201
  43. package/google/protobuf/type.proto +89 -89
  44. package/index.d.ts +30 -0
  45. package/index.js +4 -4
  46. package/light.d.ts +2 -2
  47. package/light.js +3 -3
  48. package/minimal.d.ts +2 -2
  49. package/minimal.js +4 -4
  50. package/package.json +93 -93
  51. package/src/common.js +399 -399
  52. package/src/converter.js +344 -344
  53. package/src/decoder.js +214 -208
  54. package/src/encoder.js +111 -111
  55. package/src/enum.js +231 -231
  56. package/src/field.js +497 -497
  57. package/src/index-light.js +104 -104
  58. package/src/index-minimal.js +35 -36
  59. package/src/index.js +12 -12
  60. package/src/mapfield.js +136 -136
  61. package/src/message.js +137 -137
  62. package/src/method.js +175 -175
  63. package/src/namespace.js +566 -565
  64. package/src/object.js +382 -382
  65. package/src/oneof.js +225 -225
  66. package/src/parse.js +1068 -1068
  67. package/src/reader.js +558 -543
  68. package/src/reader_buffer.js +72 -72
  69. package/src/root.js +416 -416
  70. package/src/roots.js +18 -18
  71. package/src/rpc/service.js +148 -148
  72. package/src/rpc.js +36 -36
  73. package/src/service.js +198 -198
  74. package/src/tokenize.js +421 -421
  75. package/src/type.js +654 -655
  76. package/src/types.js +196 -196
  77. package/src/typescript.js +25 -25
  78. package/src/util/aspromise.d.ts +13 -13
  79. package/src/util/aspromise.js +52 -52
  80. package/src/util/base64.d.ts +32 -32
  81. package/src/util/base64.js +146 -146
  82. package/src/util/codegen.d.ts +31 -31
  83. package/src/util/codegen.js +113 -113
  84. package/src/util/eventemitter.d.ts +45 -45
  85. package/src/util/eventemitter.js +86 -86
  86. package/src/util/fetch.d.ts +56 -56
  87. package/src/util/fetch.js +112 -112
  88. package/src/util/float.d.ts +83 -83
  89. package/src/util/float.js +335 -335
  90. package/src/util/fs.js +11 -11
  91. package/src/util/longbits.js +200 -200
  92. package/src/util/minimal.js +515 -515
  93. package/src/util/path.d.ts +22 -22
  94. package/src/util/path.js +100 -72
  95. package/src/util/patterns.js +7 -7
  96. package/src/util/pool.d.ts +32 -32
  97. package/src/util/pool.js +48 -48
  98. package/src/util/utf8.d.ts +24 -24
  99. package/src/util/utf8.js +181 -130
  100. package/src/util.js +264 -242
  101. package/src/verifier.js +180 -180
  102. package/src/wrappers.js +106 -106
  103. package/src/writer.js +495 -495
  104. package/src/writer_buffer.js +102 -102
  105. package/tsconfig.json +6 -6
package/src/namespace.js CHANGED
@@ -1,565 +1,566 @@
1
- "use strict";
2
- module.exports = Namespace;
3
-
4
- // extends ReflectionObject
5
- var ReflectionObject = require("./object");
6
- ((Namespace.prototype = Object.create(ReflectionObject.prototype)).constructor = Namespace).className = "Namespace";
7
-
8
- var Field = require("./field"),
9
- util = require("./util"),
10
- OneOf = require("./oneof");
11
-
12
- var Type, // cyclic
13
- Service,
14
- Enum;
15
-
16
- /**
17
- * Constructs a new namespace instance.
18
- * @name Namespace
19
- * @classdesc Reflected namespace.
20
- * @extends NamespaceBase
21
- * @constructor
22
- * @param {string} name Namespace name
23
- * @param {Object.<string,*>} [options] Declared options
24
- */
25
-
26
- /**
27
- * Constructs a namespace from JSON.
28
- * @memberof Namespace
29
- * @function
30
- * @param {string} name Namespace name
31
- * @param {Object.<string,*>} json JSON object
32
- * @param {number} [depth] Current nesting depth, defaults to `0`
33
- * @returns {Namespace} Created namespace
34
- * @throws {TypeError} If arguments are invalid
35
- */
36
- Namespace.fromJSON = function fromJSON(name, json, depth) {
37
- if (depth === undefined)
38
- depth = 0;
39
- if (depth > util.recursionLimit)
40
- throw Error("max depth exceeded");
41
- return new Namespace(name, json.options).addJSON(json.nested, depth);
42
- };
43
-
44
- /**
45
- * Converts an array of reflection objects to JSON.
46
- * @memberof Namespace
47
- * @param {ReflectionObject[]} array Object array
48
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
49
- * @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
50
- */
51
- function arrayToJSON(array, toJSONOptions) {
52
- if (!(array && array.length))
53
- return undefined;
54
- var obj = {};
55
- for (var i = 0; i < array.length; ++i)
56
- obj[array[i].name] = array[i].toJSON(toJSONOptions);
57
- return obj;
58
- }
59
-
60
- Namespace.arrayToJSON = arrayToJSON;
61
-
62
- /**
63
- * Tests if the specified id is reserved.
64
- * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
65
- * @param {number} id Id to test
66
- * @returns {boolean} `true` if reserved, otherwise `false`
67
- */
68
- Namespace.isReservedId = function isReservedId(reserved, id) {
69
- if (reserved)
70
- for (var i = 0; i < reserved.length; ++i)
71
- if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] >= id)
72
- return true;
73
- return false;
74
- };
75
-
76
- /**
77
- * Tests if the specified name is reserved.
78
- * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
79
- * @param {string} name Name to test
80
- * @returns {boolean} `true` if reserved, otherwise `false`
81
- */
82
- Namespace.isReservedName = function isReservedName(reserved, name) {
83
- if (reserved)
84
- for (var i = 0; i < reserved.length; ++i)
85
- if (reserved[i] === name)
86
- return true;
87
- return false;
88
- };
89
-
90
- /**
91
- * Not an actual constructor. Use {@link Namespace} instead.
92
- * @classdesc Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions.
93
- * @exports NamespaceBase
94
- * @extends ReflectionObject
95
- * @abstract
96
- * @constructor
97
- * @param {string} name Namespace name
98
- * @param {Object.<string,*>} [options] Declared options
99
- * @see {@link Namespace}
100
- */
101
- function Namespace(name, options) {
102
- ReflectionObject.call(this, name, options);
103
-
104
- /**
105
- * Nested objects by name.
106
- * @type {Object.<string,ReflectionObject>|undefined}
107
- */
108
- this.nested = undefined; // toJSON
109
-
110
- /**
111
- * Cached nested objects as an array.
112
- * @type {ReflectionObject[]|null}
113
- * @private
114
- */
115
- this._nestedArray = null;
116
-
117
- /**
118
- * Cache lookup calls for any objects contains anywhere under this namespace.
119
- * This drastically speeds up resolve for large cross-linked protos where the same
120
- * types are looked up repeatedly.
121
- * @type {Object.<string,ReflectionObject|null>}
122
- * @private
123
- */
124
- this._lookupCache = Object.create(null);
125
-
126
- /**
127
- * Whether or not objects contained in this namespace need feature resolution.
128
- * @type {boolean}
129
- * @protected
130
- */
131
- this._needsRecursiveFeatureResolution = true;
132
-
133
- /**
134
- * Whether or not objects contained in this namespace need a resolve.
135
- * @type {boolean}
136
- * @protected
137
- */
138
- this._needsRecursiveResolve = true;
139
- }
140
-
141
- function clearCache(namespace) {
142
- namespace._nestedArray = null;
143
- namespace._lookupCache = Object.create(null);
144
-
145
- // Also clear parent caches, since they include nested lookups.
146
- var parent = namespace;
147
- while(parent = parent.parent) {
148
- parent._lookupCache = Object.create(null);
149
- }
150
- return namespace;
151
- }
152
-
153
- /**
154
- * Nested objects of this namespace as an array for iteration.
155
- * @name NamespaceBase#nestedArray
156
- * @type {ReflectionObject[]}
157
- * @readonly
158
- */
159
- Object.defineProperty(Namespace.prototype, "nestedArray", {
160
- get: function() {
161
- return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
162
- }
163
- });
164
-
165
- /**
166
- * Namespace descriptor.
167
- * @interface INamespace
168
- * @property {Object.<string,*>} [options] Namespace options
169
- * @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
170
- */
171
-
172
- /**
173
- * Any extension field descriptor.
174
- * @typedef AnyExtensionField
175
- * @type {IExtensionField|IExtensionMapField}
176
- */
177
-
178
- /**
179
- * Any nested object descriptor.
180
- * @typedef AnyNestedObject
181
- * @type {IEnum|IType|IService|AnyExtensionField|INamespace|IOneOf}
182
- */
183
-
184
- /**
185
- * Converts this namespace to a namespace descriptor.
186
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
187
- * @returns {INamespace} Namespace descriptor
188
- */
189
- Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
190
- return util.toObject([
191
- "options" , this.options,
192
- "nested" , arrayToJSON(this.nestedArray, toJSONOptions)
193
- ]);
194
- };
195
-
196
- /**
197
- * Adds nested objects to this namespace from nested object descriptors.
198
- * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
199
- * @param {number} [depth] Current nesting depth, defaults to `0`
200
- * @returns {Namespace} `this`
201
- */
202
- Namespace.prototype.addJSON = function addJSON(nestedJson, depth) {
203
- if (depth === undefined)
204
- depth = 0;
205
- if (depth > util.recursionLimit)
206
- throw Error("max depth exceeded");
207
- var ns = this;
208
- /* istanbul ignore else */
209
- if (nestedJson) {
210
- for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
211
- nested = nestedJson[names[i]];
212
- ns.add( // most to least likely
213
- ( nested.fields !== undefined
214
- ? Type.fromJSON
215
- : nested.values !== undefined
216
- ? Enum.fromJSON
217
- : nested.methods !== undefined
218
- ? Service.fromJSON
219
- : nested.id !== undefined
220
- ? Field.fromJSON
221
- : Namespace.fromJSON )(names[i], nested, depth + 1)
222
- );
223
- }
224
- }
225
- return this;
226
- };
227
-
228
- /**
229
- * Gets the nested object of the specified name.
230
- * @param {string} name Nested object name
231
- * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
232
- */
233
- Namespace.prototype.get = function get(name) {
234
- return this.nested && Object.prototype.hasOwnProperty.call(this.nested, name)
235
- ? this.nested[name]
236
- : null;
237
- };
238
-
239
- /**
240
- * Gets the values of the nested {@link Enum|enum} of the specified name.
241
- * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
242
- * @param {string} name Nested enum name
243
- * @returns {Object.<string,number>} Enum values
244
- * @throws {Error} If there is no such enum
245
- */
246
- Namespace.prototype.getEnum = function getEnum(name) {
247
- if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name) && this.nested[name] instanceof Enum)
248
- return this.nested[name].values;
249
- throw Error("no such enum: " + name);
250
- };
251
-
252
- /**
253
- * Adds a nested object to this namespace.
254
- * @param {ReflectionObject} object Nested object to add
255
- * @returns {Namespace} `this`
256
- * @throws {TypeError} If arguments are invalid
257
- * @throws {Error} If there is already a nested object with this name
258
- */
259
- Namespace.prototype.add = function add(object) {
260
-
261
- if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof OneOf || object instanceof Enum || object instanceof Service || object instanceof Namespace))
262
- throw TypeError("object must be a valid nested object");
263
-
264
- if (object.name === "__proto__")
265
- return this;
266
-
267
- if (!this.nested)
268
- this.nested = {};
269
- else {
270
- var prev = this.get(object.name);
271
- if (prev) {
272
- if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
273
- // replace plain namespace but keep existing nested elements and options
274
- var nested = prev.nestedArray;
275
- for (var i = 0; i < nested.length; ++i)
276
- object.add(nested[i]);
277
- this.remove(prev);
278
- if (!this.nested)
279
- this.nested = {};
280
- object.setOptions(prev.options, true);
281
-
282
- } else
283
- throw Error("duplicate name '" + object.name + "' in " + this);
284
- }
285
- }
286
- this.nested[object.name] = object;
287
-
288
- if (!(this instanceof Type || this instanceof Service || this instanceof Enum || this instanceof Field)) {
289
- // This is a package or a root namespace.
290
- if (!object._edition) {
291
- // Make sure that some edition is set if it hasn't already been specified.
292
- object._edition = object._defaultEdition;
293
- }
294
- }
295
-
296
- this._needsRecursiveFeatureResolution = true;
297
- this._needsRecursiveResolve = true;
298
-
299
- // Also clear parent caches, since they need to recurse down.
300
- var parent = this;
301
- while(parent = parent.parent) {
302
- parent._needsRecursiveFeatureResolution = true;
303
- parent._needsRecursiveResolve = true;
304
- }
305
-
306
- object.onAdd(this);
307
- return clearCache(this);
308
- };
309
-
310
- /**
311
- * Removes a nested object from this namespace.
312
- * @param {ReflectionObject} object Nested object to remove
313
- * @returns {Namespace} `this`
314
- * @throws {TypeError} If arguments are invalid
315
- * @throws {Error} If `object` is not a member of this namespace
316
- */
317
- Namespace.prototype.remove = function remove(object) {
318
-
319
- if (!(object instanceof ReflectionObject))
320
- throw TypeError("object must be a ReflectionObject");
321
- if (object.parent !== this)
322
- throw Error(object + " is not a member of " + this);
323
-
324
- delete this.nested[object.name];
325
- if (!Object.keys(this.nested).length)
326
- this.nested = undefined;
327
-
328
- object.onRemove(this);
329
- return clearCache(this);
330
- };
331
-
332
- /**
333
- * Defines additial namespaces within this one if not yet existing.
334
- * @param {string|string[]} path Path to create
335
- * @param {*} [json] Nested types to create from JSON
336
- * @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
337
- */
338
- Namespace.prototype.define = function define(path, json) {
339
-
340
- if (util.isString(path))
341
- path = path.split(".");
342
- else if (!Array.isArray(path))
343
- throw TypeError("illegal path");
344
- if (path && path.length && path[0] === "")
345
- throw Error("path must be relative");
346
- if (path.length > util.recursionLimit)
347
- throw Error("max depth exceeded");
348
-
349
- var ptr = this;
350
- while (path.length > 0) {
351
- var part = path.shift();
352
- if (ptr.nested && ptr.nested[part]) {
353
- ptr = ptr.nested[part];
354
- if (!(ptr instanceof Namespace))
355
- throw Error("path conflicts with non-namespace objects");
356
- } else
357
- ptr.add(ptr = new Namespace(part));
358
- }
359
- if (json)
360
- ptr.addJSON(json);
361
- return ptr;
362
- };
363
-
364
- /**
365
- * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
366
- * @returns {Namespace} `this`
367
- */
368
- Namespace.prototype.resolveAll = function resolveAll() {
369
- if (!this._needsRecursiveResolve) return this;
370
-
371
- if (this._needsRecursiveFeatureResolution)
372
- this._resolveFeaturesRecursive(this._edition);
373
-
374
- var nested = this.nestedArray, i = 0;
375
- this.resolve();
376
- while (i < nested.length)
377
- if (nested[i] instanceof Namespace)
378
- nested[i++].resolveAll();
379
- else
380
- nested[i++].resolve();
381
- this._needsRecursiveResolve = false;
382
- return this;
383
- };
384
-
385
- /**
386
- * @override
387
- */
388
- Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
389
- if (!this._needsRecursiveFeatureResolution) return this;
390
- this._needsRecursiveFeatureResolution = false;
391
-
392
- edition = this._edition || edition;
393
-
394
- ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
395
- this.nestedArray.forEach(nested => {
396
- nested._resolveFeaturesRecursive(edition);
397
- });
398
- return this;
399
- };
400
-
401
- /**
402
- * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
403
- * @param {string|string[]} path Path to look up
404
- * @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
405
- * @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
406
- * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
407
- */
408
- Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
409
- /* istanbul ignore next */
410
- if (typeof filterTypes === "boolean") {
411
- parentAlreadyChecked = filterTypes;
412
- filterTypes = undefined;
413
- } else if (filterTypes && !Array.isArray(filterTypes))
414
- filterTypes = [ filterTypes ];
415
-
416
- if (util.isString(path) && path.length) {
417
- if (path === ".")
418
- return this.root;
419
- path = path.split(".");
420
- } else if (!path.length)
421
- return this;
422
-
423
- var flatPath = path.join(".");
424
-
425
- // Start at root if path is absolute
426
- if (path[0] === "")
427
- return this.root.lookup(path.slice(1), filterTypes);
428
-
429
- // Lookup at this namespace and below
430
- var found = this._lookupImpl(path, flatPath);
431
- if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
432
- return found;
433
- }
434
-
435
- // Fall back to respective absolute path once relative scope has been checked (non-standard)
436
- found = this.root._fullyQualifiedObjects && this.root._fullyQualifiedObjects["." + flatPath];
437
- if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
438
- return found;
439
- }
440
-
441
- if (parentAlreadyChecked)
442
- return null;
443
-
444
- // If there hasn't been a match, walk up the tree and look more broadly
445
- var current = this;
446
- while (current.parent) {
447
- found = current.parent._lookupImpl(path, flatPath);
448
- if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
449
- return found;
450
- }
451
- current = current.parent;
452
- }
453
- return null;
454
- };
455
-
456
- /**
457
- * Internal helper for lookup that handles searching just at this namespace and below along with caching.
458
- * @param {string[]} path Path to look up
459
- * @param {string} flatPath Flattened version of the path to use as a cache key
460
- * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
461
- * @private
462
- */
463
- Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
464
- if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
465
- return this._lookupCache[flatPath];
466
- }
467
-
468
- // Test if the first part matches any nested object, and if so, traverse if path contains more
469
- var found = this.get(path[0]);
470
- var exact = null;
471
- if (found) {
472
- if (path.length === 1) {
473
- exact = found;
474
- } else if (found instanceof Namespace) {
475
- path = path.slice(1);
476
- exact = found._lookupImpl(path, path.join("."));
477
- }
478
-
479
- // Otherwise try each nested namespace
480
- } else {
481
- for (var i = 0; i < this.nestedArray.length; ++i)
482
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath))) {
483
- exact = found;
484
- break;
485
- }
486
- }
487
-
488
- // Set this even when null, so that when we walk up the tree we can quickly bail on repeated checks back down.
489
- this._lookupCache[flatPath] = exact;
490
- return exact;
491
- };
492
-
493
- /**
494
- * Looks up the reflection object at the specified path, relative to this namespace.
495
- * @name NamespaceBase#lookup
496
- * @function
497
- * @param {string|string[]} path Path to look up
498
- * @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
499
- * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
500
- * @variation 2
501
- */
502
- // lookup(path: string, [parentAlreadyChecked: boolean])
503
-
504
- /**
505
- * Looks up the {@link Type|type} at the specified path, relative to this namespace.
506
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
507
- * @param {string|string[]} path Path to look up
508
- * @returns {Type} Looked up type
509
- * @throws {Error} If `path` does not point to a type
510
- */
511
- Namespace.prototype.lookupType = function lookupType(path) {
512
- var found = this.lookup(path, [ Type ]);
513
- if (!found)
514
- throw Error("no such type: " + path);
515
- return found;
516
- };
517
-
518
- /**
519
- * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
520
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
521
- * @param {string|string[]} path Path to look up
522
- * @returns {Enum} Looked up enum
523
- * @throws {Error} If `path` does not point to an enum
524
- */
525
- Namespace.prototype.lookupEnum = function lookupEnum(path) {
526
- var found = this.lookup(path, [ Enum ]);
527
- if (!found)
528
- throw Error("no such Enum '" + path + "' in " + this);
529
- return found;
530
- };
531
-
532
- /**
533
- * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
534
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
535
- * @param {string|string[]} path Path to look up
536
- * @returns {Type} Looked up type or enum
537
- * @throws {Error} If `path` does not point to a type or enum
538
- */
539
- Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
540
- var found = this.lookup(path, [ Type, Enum ]);
541
- if (!found)
542
- throw Error("no such Type or Enum '" + path + "' in " + this);
543
- return found;
544
- };
545
-
546
- /**
547
- * Looks up the {@link Service|service} at the specified path, relative to this namespace.
548
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
549
- * @param {string|string[]} path Path to look up
550
- * @returns {Service} Looked up service
551
- * @throws {Error} If `path` does not point to a service
552
- */
553
- Namespace.prototype.lookupService = function lookupService(path) {
554
- var found = this.lookup(path, [ Service ]);
555
- if (!found)
556
- throw Error("no such Service '" + path + "' in " + this);
557
- return found;
558
- };
559
-
560
- // Sets up cyclic dependencies (called in index-light)
561
- Namespace._configure = function(Type_, Service_, Enum_) {
562
- Type = Type_;
563
- Service = Service_;
564
- Enum = Enum_;
565
- };
1
+ "use strict";
2
+ module.exports = Namespace;
3
+
4
+ // extends ReflectionObject
5
+ var ReflectionObject = require("./object");
6
+ ((Namespace.prototype = Object.create(ReflectionObject.prototype)).constructor = Namespace).className = "Namespace";
7
+
8
+ var Field = require("./field"),
9
+ util = require("./util"),
10
+ OneOf = require("./oneof");
11
+
12
+ var Type, // cyclic
13
+ Service,
14
+ Enum;
15
+
16
+ /**
17
+ * Constructs a new namespace instance.
18
+ * @name Namespace
19
+ * @classdesc Reflected namespace.
20
+ * @extends NamespaceBase
21
+ * @constructor
22
+ * @param {string} name Namespace name
23
+ * @param {Object.<string,*>} [options] Declared options
24
+ */
25
+
26
+ /**
27
+ * Constructs a namespace from JSON.
28
+ * @memberof Namespace
29
+ * @function
30
+ * @param {string} name Namespace name
31
+ * @param {Object.<string,*>} json JSON object
32
+ * @param {number} [depth] Current nesting depth, defaults to `0`
33
+ * @returns {Namespace} Created namespace
34
+ * @throws {TypeError} If arguments are invalid
35
+ */
36
+ Namespace.fromJSON = function fromJSON(name, json, depth) {
37
+ if (depth === undefined)
38
+ depth = 0;
39
+ if (depth > util.recursionLimit)
40
+ throw Error("max depth exceeded");
41
+ return new Namespace(name, json.options).addJSON(json.nested, depth);
42
+ };
43
+
44
+ /**
45
+ * Converts an array of reflection objects to JSON.
46
+ * @memberof Namespace
47
+ * @param {ReflectionObject[]} array Object array
48
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
49
+ * @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
50
+ */
51
+ function arrayToJSON(array, toJSONOptions) {
52
+ if (!(array && array.length))
53
+ return undefined;
54
+ var obj = {};
55
+ for (var i = 0; i < array.length; ++i)
56
+ obj[array[i].name] = array[i].toJSON(toJSONOptions);
57
+ return obj;
58
+ }
59
+
60
+ Namespace.arrayToJSON = arrayToJSON;
61
+
62
+ /**
63
+ * Tests if the specified id is reserved.
64
+ * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
65
+ * @param {number} id Id to test
66
+ * @returns {boolean} `true` if reserved, otherwise `false`
67
+ */
68
+ Namespace.isReservedId = function isReservedId(reserved, id) {
69
+ if (reserved)
70
+ for (var i = 0; i < reserved.length; ++i)
71
+ if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] >= id)
72
+ return true;
73
+ return false;
74
+ };
75
+
76
+ /**
77
+ * Tests if the specified name is reserved.
78
+ * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
79
+ * @param {string} name Name to test
80
+ * @returns {boolean} `true` if reserved, otherwise `false`
81
+ */
82
+ Namespace.isReservedName = function isReservedName(reserved, name) {
83
+ if (reserved)
84
+ for (var i = 0; i < reserved.length; ++i)
85
+ if (reserved[i] === name)
86
+ return true;
87
+ return false;
88
+ };
89
+
90
+ /**
91
+ * Not an actual constructor. Use {@link Namespace} instead.
92
+ * @classdesc Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions.
93
+ * @exports NamespaceBase
94
+ * @extends ReflectionObject
95
+ * @abstract
96
+ * @constructor
97
+ * @param {string} name Namespace name
98
+ * @param {Object.<string,*>} [options] Declared options
99
+ * @see {@link Namespace}
100
+ */
101
+ function Namespace(name, options) {
102
+ ReflectionObject.call(this, name, options);
103
+
104
+ /**
105
+ * Nested objects by name.
106
+ * @type {Object.<string,ReflectionObject>|undefined}
107
+ */
108
+ this.nested = undefined; // toJSON
109
+
110
+ /**
111
+ * Cached nested objects as an array.
112
+ * @type {ReflectionObject[]|null}
113
+ * @private
114
+ */
115
+ this._nestedArray = null;
116
+
117
+ /**
118
+ * Cache lookup calls for any objects contains anywhere under this namespace.
119
+ * This drastically speeds up resolve for large cross-linked protos where the same
120
+ * types are looked up repeatedly.
121
+ * @type {Object.<string,ReflectionObject|null>}
122
+ * @private
123
+ */
124
+ this._lookupCache = Object.create(null);
125
+
126
+ /**
127
+ * Whether or not objects contained in this namespace need feature resolution.
128
+ * @type {boolean}
129
+ * @protected
130
+ */
131
+ this._needsRecursiveFeatureResolution = true;
132
+
133
+ /**
134
+ * Whether or not objects contained in this namespace need a resolve.
135
+ * @type {boolean}
136
+ * @protected
137
+ */
138
+ this._needsRecursiveResolve = true;
139
+ }
140
+
141
+ function clearCache(namespace) {
142
+ namespace._nestedArray = null;
143
+ namespace._lookupCache = Object.create(null);
144
+
145
+ // Also clear parent caches, since they include nested lookups.
146
+ var parent = namespace;
147
+ while(parent = parent.parent) {
148
+ parent._lookupCache = Object.create(null);
149
+ }
150
+ return namespace;
151
+ }
152
+
153
+ /**
154
+ * Nested objects of this namespace as an array for iteration.
155
+ * @name NamespaceBase#nestedArray
156
+ * @type {ReflectionObject[]}
157
+ * @readonly
158
+ */
159
+ Object.defineProperty(Namespace.prototype, "nestedArray", {
160
+ get: function() {
161
+ return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
162
+ }
163
+ });
164
+
165
+ /**
166
+ * Namespace descriptor.
167
+ * @interface INamespace
168
+ * @property {Object.<string,*>} [options] Namespace options
169
+ * @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
170
+ */
171
+
172
+ /**
173
+ * Any extension field descriptor.
174
+ * @typedef AnyExtensionField
175
+ * @type {IExtensionField|IExtensionMapField}
176
+ */
177
+
178
+ /**
179
+ * Any nested object descriptor.
180
+ * @typedef AnyNestedObject
181
+ * @type {IEnum|IType|IService|AnyExtensionField|INamespace|IOneOf}
182
+ */
183
+
184
+ /**
185
+ * Converts this namespace to a namespace descriptor.
186
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
187
+ * @returns {INamespace} Namespace descriptor
188
+ */
189
+ Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
190
+ return util.toObject([
191
+ "options" , this.options,
192
+ "nested" , arrayToJSON(this.nestedArray, toJSONOptions)
193
+ ]);
194
+ };
195
+
196
+ /**
197
+ * Adds nested objects to this namespace from nested object descriptors.
198
+ * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
199
+ * @param {number} [depth] Current nesting depth, defaults to `0`
200
+ * @returns {Namespace} `this`
201
+ */
202
+ Namespace.prototype.addJSON = function addJSON(nestedJson, depth) {
203
+ if (depth === undefined)
204
+ depth = 0;
205
+ if (depth > util.recursionLimit)
206
+ throw Error("max depth exceeded");
207
+ var ns = this;
208
+ /* istanbul ignore else */
209
+ if (nestedJson) {
210
+ for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
211
+ nested = nestedJson[names[i]];
212
+ ns.add( // most to least likely
213
+ ( nested.fields !== undefined
214
+ ? Type.fromJSON
215
+ : nested.values !== undefined
216
+ ? Enum.fromJSON
217
+ : nested.methods !== undefined
218
+ ? Service.fromJSON
219
+ : nested.id !== undefined
220
+ ? Field.fromJSON
221
+ : Namespace.fromJSON )(names[i], nested, depth + 1)
222
+ );
223
+ }
224
+ }
225
+ return this;
226
+ };
227
+
228
+ /**
229
+ * Gets the nested object of the specified name.
230
+ * @param {string} name Nested object name
231
+ * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
232
+ */
233
+ Namespace.prototype.get = function get(name) {
234
+ return this.nested && Object.prototype.hasOwnProperty.call(this.nested, name)
235
+ ? this.nested[name]
236
+ : null;
237
+ };
238
+
239
+ /**
240
+ * Gets the values of the nested {@link Enum|enum} of the specified name.
241
+ * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
242
+ * @param {string} name Nested enum name
243
+ * @returns {Object.<string,number>} Enum values
244
+ * @throws {Error} If there is no such enum
245
+ */
246
+ Namespace.prototype.getEnum = function getEnum(name) {
247
+ if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name) && this.nested[name] instanceof Enum)
248
+ return this.nested[name].values;
249
+ throw Error("no such enum: " + name);
250
+ };
251
+
252
+ /**
253
+ * Adds a nested object to this namespace.
254
+ * @param {ReflectionObject} object Nested object to add
255
+ * @returns {Namespace} `this`
256
+ * @throws {TypeError} If arguments are invalid
257
+ * @throws {Error} If there is already a nested object with this name
258
+ */
259
+ Namespace.prototype.add = function add(object) {
260
+
261
+ if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof OneOf || object instanceof Enum || object instanceof Service || object instanceof Namespace))
262
+ throw TypeError("object must be a valid nested object");
263
+
264
+ if (object.name === "__proto__")
265
+ return this;
266
+
267
+ if (!this.nested)
268
+ this.nested = {};
269
+ else {
270
+ var prev = this.get(object.name);
271
+ if (prev) {
272
+ if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
273
+ // replace plain namespace but keep existing nested elements and options
274
+ var nested = prev.nestedArray;
275
+ for (var i = 0; i < nested.length; ++i)
276
+ object.add(nested[i]);
277
+ this.remove(prev);
278
+ if (!this.nested)
279
+ this.nested = {};
280
+ object.setOptions(prev.options, true);
281
+
282
+ } else
283
+ throw Error("duplicate name '" + object.name + "' in " + this);
284
+ }
285
+ }
286
+ this.nested[object.name] = object;
287
+
288
+ if (!(this instanceof Type || this instanceof Service || this instanceof Enum || this instanceof Field)) {
289
+ // This is a package or a root namespace.
290
+ if (!object._edition) {
291
+ // Make sure that some edition is set if it hasn't already been specified.
292
+ object._edition = object._defaultEdition;
293
+ }
294
+ }
295
+
296
+ this._needsRecursiveFeatureResolution = true;
297
+ this._needsRecursiveResolve = true;
298
+
299
+ // Also clear parent caches, since they need to recurse down.
300
+ var parent = this;
301
+ while(parent = parent.parent) {
302
+ parent._needsRecursiveFeatureResolution = true;
303
+ parent._needsRecursiveResolve = true;
304
+ }
305
+
306
+ object.onAdd(this);
307
+ return clearCache(this);
308
+ };
309
+
310
+ /**
311
+ * Removes a nested object from this namespace.
312
+ * @param {ReflectionObject} object Nested object to remove
313
+ * @returns {Namespace} `this`
314
+ * @throws {TypeError} If arguments are invalid
315
+ * @throws {Error} If `object` is not a member of this namespace
316
+ */
317
+ Namespace.prototype.remove = function remove(object) {
318
+
319
+ if (!(object instanceof ReflectionObject))
320
+ throw TypeError("object must be a ReflectionObject");
321
+ if (object.parent !== this)
322
+ throw Error(object + " is not a member of " + this);
323
+
324
+ if (!util.remove(this.nested, object, object.name))
325
+ throw Error(object + " is not a member of " + this);
326
+ if (!Object.keys(this.nested).length)
327
+ this.nested = undefined;
328
+
329
+ object.onRemove(this);
330
+ return clearCache(this);
331
+ };
332
+
333
+ /**
334
+ * Defines additial namespaces within this one if not yet existing.
335
+ * @param {string|string[]} path Path to create
336
+ * @param {*} [json] Nested types to create from JSON
337
+ * @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
338
+ */
339
+ Namespace.prototype.define = function define(path, json) {
340
+
341
+ if (util.isString(path))
342
+ path = path.split(".");
343
+ else if (!Array.isArray(path))
344
+ throw TypeError("illegal path");
345
+ if (path && path.length && path[0] === "")
346
+ throw Error("path must be relative");
347
+ if (path.length > util.recursionLimit)
348
+ throw Error("max depth exceeded");
349
+
350
+ var ptr = this;
351
+ while (path.length > 0) {
352
+ var part = path.shift();
353
+ if (ptr.nested && ptr.nested[part]) {
354
+ ptr = ptr.nested[part];
355
+ if (!(ptr instanceof Namespace))
356
+ throw Error("path conflicts with non-namespace objects");
357
+ } else
358
+ ptr.add(ptr = new Namespace(part));
359
+ }
360
+ if (json)
361
+ ptr.addJSON(json);
362
+ return ptr;
363
+ };
364
+
365
+ /**
366
+ * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
367
+ * @returns {Namespace} `this`
368
+ */
369
+ Namespace.prototype.resolveAll = function resolveAll() {
370
+ if (!this._needsRecursiveResolve) return this;
371
+
372
+ if (this._needsRecursiveFeatureResolution)
373
+ this._resolveFeaturesRecursive(this._edition);
374
+
375
+ var nested = this.nestedArray, i = 0;
376
+ this.resolve();
377
+ while (i < nested.length)
378
+ if (nested[i] instanceof Namespace)
379
+ nested[i++].resolveAll();
380
+ else
381
+ nested[i++].resolve();
382
+ this._needsRecursiveResolve = false;
383
+ return this;
384
+ };
385
+
386
+ /**
387
+ * @override
388
+ */
389
+ Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
390
+ if (!this._needsRecursiveFeatureResolution) return this;
391
+ this._needsRecursiveFeatureResolution = false;
392
+
393
+ edition = this._edition || edition;
394
+
395
+ ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
396
+ this.nestedArray.forEach(nested => {
397
+ nested._resolveFeaturesRecursive(edition);
398
+ });
399
+ return this;
400
+ };
401
+
402
+ /**
403
+ * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
404
+ * @param {string|string[]} path Path to look up
405
+ * @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
406
+ * @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
407
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
408
+ */
409
+ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
410
+ /* istanbul ignore next */
411
+ if (typeof filterTypes === "boolean") {
412
+ parentAlreadyChecked = filterTypes;
413
+ filterTypes = undefined;
414
+ } else if (filterTypes && !Array.isArray(filterTypes))
415
+ filterTypes = [ filterTypes ];
416
+
417
+ if (util.isString(path) && path.length) {
418
+ if (path === ".")
419
+ return this.root;
420
+ path = path.split(".");
421
+ } else if (!path.length)
422
+ return this;
423
+
424
+ var flatPath = path.join(".");
425
+
426
+ // Start at root if path is absolute
427
+ if (path[0] === "")
428
+ return this.root.lookup(path.slice(1), filterTypes);
429
+
430
+ // Lookup at this namespace and below
431
+ var found = this._lookupImpl(path, flatPath);
432
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
433
+ return found;
434
+ }
435
+
436
+ // Fall back to respective absolute path once relative scope has been checked (non-standard)
437
+ found = this.root._fullyQualifiedObjects && this.root._fullyQualifiedObjects["." + flatPath];
438
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
439
+ return found;
440
+ }
441
+
442
+ if (parentAlreadyChecked)
443
+ return null;
444
+
445
+ // If there hasn't been a match, walk up the tree and look more broadly
446
+ var current = this;
447
+ while (current.parent) {
448
+ found = current.parent._lookupImpl(path, flatPath);
449
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
450
+ return found;
451
+ }
452
+ current = current.parent;
453
+ }
454
+ return null;
455
+ };
456
+
457
+ /**
458
+ * Internal helper for lookup that handles searching just at this namespace and below along with caching.
459
+ * @param {string[]} path Path to look up
460
+ * @param {string} flatPath Flattened version of the path to use as a cache key
461
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
462
+ * @private
463
+ */
464
+ Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
465
+ if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
466
+ return this._lookupCache[flatPath];
467
+ }
468
+
469
+ // Test if the first part matches any nested object, and if so, traverse if path contains more
470
+ var found = this.get(path[0]);
471
+ var exact = null;
472
+ if (found) {
473
+ if (path.length === 1) {
474
+ exact = found;
475
+ } else if (found instanceof Namespace) {
476
+ path = path.slice(1);
477
+ exact = found._lookupImpl(path, path.join("."));
478
+ }
479
+
480
+ // Otherwise try each nested namespace
481
+ } else {
482
+ for (var i = 0; i < this.nestedArray.length; ++i)
483
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath))) {
484
+ exact = found;
485
+ break;
486
+ }
487
+ }
488
+
489
+ // Set this even when null, so that when we walk up the tree we can quickly bail on repeated checks back down.
490
+ this._lookupCache[flatPath] = exact;
491
+ return exact;
492
+ };
493
+
494
+ /**
495
+ * Looks up the reflection object at the specified path, relative to this namespace.
496
+ * @name NamespaceBase#lookup
497
+ * @function
498
+ * @param {string|string[]} path Path to look up
499
+ * @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
500
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
501
+ * @variation 2
502
+ */
503
+ // lookup(path: string, [parentAlreadyChecked: boolean])
504
+
505
+ /**
506
+ * Looks up the {@link Type|type} at the specified path, relative to this namespace.
507
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
508
+ * @param {string|string[]} path Path to look up
509
+ * @returns {Type} Looked up type
510
+ * @throws {Error} If `path` does not point to a type
511
+ */
512
+ Namespace.prototype.lookupType = function lookupType(path) {
513
+ var found = this.lookup(path, [ Type ]);
514
+ if (!found)
515
+ throw Error("no such type: " + path);
516
+ return found;
517
+ };
518
+
519
+ /**
520
+ * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
521
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
522
+ * @param {string|string[]} path Path to look up
523
+ * @returns {Enum} Looked up enum
524
+ * @throws {Error} If `path` does not point to an enum
525
+ */
526
+ Namespace.prototype.lookupEnum = function lookupEnum(path) {
527
+ var found = this.lookup(path, [ Enum ]);
528
+ if (!found)
529
+ throw Error("no such Enum '" + path + "' in " + this);
530
+ return found;
531
+ };
532
+
533
+ /**
534
+ * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
535
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
536
+ * @param {string|string[]} path Path to look up
537
+ * @returns {Type} Looked up type or enum
538
+ * @throws {Error} If `path` does not point to a type or enum
539
+ */
540
+ Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
541
+ var found = this.lookup(path, [ Type, Enum ]);
542
+ if (!found)
543
+ throw Error("no such Type or Enum '" + path + "' in " + this);
544
+ return found;
545
+ };
546
+
547
+ /**
548
+ * Looks up the {@link Service|service} at the specified path, relative to this namespace.
549
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
550
+ * @param {string|string[]} path Path to look up
551
+ * @returns {Service} Looked up service
552
+ * @throws {Error} If `path` does not point to a service
553
+ */
554
+ Namespace.prototype.lookupService = function lookupService(path) {
555
+ var found = this.lookup(path, [ Service ]);
556
+ if (!found)
557
+ throw Error("no such Service '" + path + "' in " + this);
558
+ return found;
559
+ };
560
+
561
+ // Sets up cyclic dependencies (called in index-light)
562
+ Namespace._configure = function(Type_, Service_, Enum_) {
563
+ Type = Type_;
564
+ Service = Service_;
565
+ Enum = Enum_;
566
+ };