protobufjs 6.8.8 → 6.9.0

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 (108) hide show
  1. package/CHANGELOG.md +941 -921
  2. package/LICENSE +39 -39
  3. package/README.md +901 -879
  4. package/cli/LICENSE +33 -33
  5. package/cli/README.md +11 -11
  6. package/cli/index.d.ts +3 -3
  7. package/cli/index.js +3 -3
  8. package/cli/lib/tsd-jsdoc/LICENSE +20 -20
  9. package/cli/lib/tsd-jsdoc/README.md +23 -23
  10. package/cli/lib/tsd-jsdoc/plugin.js +21 -21
  11. package/cli/lib/tsd-jsdoc/publish.js +705 -693
  12. package/cli/lib/tsd-jsdoc.json +18 -18
  13. package/cli/package.json +1 -1
  14. package/cli/package.standalone.json +32 -32
  15. package/cli/pbjs.d.ts +9 -9
  16. package/cli/pbjs.js +329 -329
  17. package/cli/pbts.d.ts +9 -9
  18. package/cli/pbts.js +197 -197
  19. package/cli/targets/json-module.js +38 -38
  20. package/cli/targets/json.js +8 -8
  21. package/cli/targets/proto.js +326 -326
  22. package/cli/targets/proto2.js +10 -10
  23. package/cli/targets/proto3.js +10 -10
  24. package/cli/targets/static-module.js +29 -29
  25. package/cli/targets/static.js +702 -702
  26. package/cli/util.js +183 -183
  27. package/cli/wrappers/amd.js +7 -7
  28. package/cli/wrappers/closure.js +7 -7
  29. package/cli/wrappers/commonjs.js +7 -7
  30. package/cli/wrappers/default.js +15 -15
  31. package/cli/wrappers/es6.js +5 -5
  32. package/dist/README.md +31 -31
  33. package/dist/light/README.md +31 -31
  34. package/dist/light/protobuf.js +6058 -6032
  35. package/dist/light/protobuf.js.map +1 -1
  36. package/dist/light/protobuf.min.js +3 -3
  37. package/dist/light/protobuf.min.js.map +1 -1
  38. package/dist/minimal/README.md +31 -31
  39. package/dist/minimal/protobuf.js +1900 -1877
  40. package/dist/minimal/protobuf.js.map +1 -1
  41. package/dist/minimal/protobuf.min.js +3 -3
  42. package/dist/minimal/protobuf.min.js.map +1 -1
  43. package/dist/protobuf.js +7625 -7596
  44. package/dist/protobuf.js.map +1 -1
  45. package/dist/protobuf.min.js +3 -3
  46. package/dist/protobuf.min.js.map +1 -1
  47. package/ext/debug/README.md +4 -4
  48. package/ext/debug/index.js +71 -71
  49. package/ext/descriptor/README.md +72 -72
  50. package/ext/descriptor/index.js +1052 -1052
  51. package/ext/descriptor/test.js +54 -54
  52. package/google/LICENSE +27 -27
  53. package/google/README.md +1 -1
  54. package/google/api/annotations.proto +10 -10
  55. package/google/protobuf/descriptor.proto +286 -286
  56. package/google/protobuf/source_context.proto +7 -7
  57. package/index.d.ts +6 -6
  58. package/index.js +4 -4
  59. package/light.d.ts +2 -2
  60. package/light.js +3 -3
  61. package/minimal.d.ts +2 -2
  62. package/minimal.js +4 -4
  63. package/package-lock.json +4223 -3171
  64. package/package.json +122 -122
  65. package/scripts/changelog.js +150 -150
  66. package/scripts/postinstall.js +35 -35
  67. package/src/common.js +399 -399
  68. package/src/converter.js +293 -293
  69. package/src/decoder.js +106 -106
  70. package/src/encoder.js +100 -100
  71. package/src/enum.js +181 -181
  72. package/src/field.js +371 -371
  73. package/src/index-light.js +104 -104
  74. package/src/index-minimal.js +36 -36
  75. package/src/index.js +12 -12
  76. package/src/mapfield.js +126 -126
  77. package/src/message.js +138 -138
  78. package/src/method.js +151 -151
  79. package/src/namespace.js +433 -433
  80. package/src/object.js +200 -200
  81. package/src/oneof.js +203 -203
  82. package/src/parse.js +761 -758
  83. package/src/reader.js +411 -405
  84. package/src/reader_buffer.js +51 -44
  85. package/src/root.js +353 -351
  86. package/src/roots.js +18 -18
  87. package/src/rpc/service.js +142 -142
  88. package/src/rpc.js +36 -36
  89. package/src/service.js +167 -167
  90. package/src/tokenize.js +397 -397
  91. package/src/type.js +589 -589
  92. package/src/types.js +196 -196
  93. package/src/typescript.jsdoc +15 -15
  94. package/src/util/longbits.js +200 -200
  95. package/src/util/minimal.js +414 -414
  96. package/src/util.js +178 -178
  97. package/src/verifier.js +176 -176
  98. package/src/wrappers.js +83 -83
  99. package/src/writer.js +465 -459
  100. package/src/writer_buffer.js +85 -81
  101. package/tsconfig.json +6 -6
  102. package/cli/node_modules/os-tmpdir/index.js +0 -25
  103. package/cli/node_modules/os-tmpdir/license +0 -21
  104. package/cli/node_modules/os-tmpdir/readme.md +0 -32
  105. package/cli/node_modules/tmp/LICENSE +0 -21
  106. package/cli/node_modules/tmp/README.md +0 -314
  107. package/cli/node_modules/tmp/lib/tmp.js +0 -611
  108. package/cli/package-lock.json +0 -20
package/src/namespace.js CHANGED
@@ -1,433 +1,433 @@
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
-
11
- var Type, // cyclic
12
- Service,
13
- Enum;
14
-
15
- /**
16
- * Constructs a new namespace instance.
17
- * @name Namespace
18
- * @classdesc Reflected namespace.
19
- * @extends NamespaceBase
20
- * @constructor
21
- * @param {string} name Namespace name
22
- * @param {Object.<string,*>} [options] Declared options
23
- */
24
-
25
- /**
26
- * Constructs a namespace from JSON.
27
- * @memberof Namespace
28
- * @function
29
- * @param {string} name Namespace name
30
- * @param {Object.<string,*>} json JSON object
31
- * @returns {Namespace} Created namespace
32
- * @throws {TypeError} If arguments are invalid
33
- */
34
- Namespace.fromJSON = function fromJSON(name, json) {
35
- return new Namespace(name, json.options).addJSON(json.nested);
36
- };
37
-
38
- /**
39
- * Converts an array of reflection objects to JSON.
40
- * @memberof Namespace
41
- * @param {ReflectionObject[]} array Object array
42
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
43
- * @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
44
- */
45
- function arrayToJSON(array, toJSONOptions) {
46
- if (!(array && array.length))
47
- return undefined;
48
- var obj = {};
49
- for (var i = 0; i < array.length; ++i)
50
- obj[array[i].name] = array[i].toJSON(toJSONOptions);
51
- return obj;
52
- }
53
-
54
- Namespace.arrayToJSON = arrayToJSON;
55
-
56
- /**
57
- * Tests if the specified id is reserved.
58
- * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
59
- * @param {number} id Id to test
60
- * @returns {boolean} `true` if reserved, otherwise `false`
61
- */
62
- Namespace.isReservedId = function isReservedId(reserved, id) {
63
- if (reserved)
64
- for (var i = 0; i < reserved.length; ++i)
65
- if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] >= id)
66
- return true;
67
- return false;
68
- };
69
-
70
- /**
71
- * Tests if the specified name is reserved.
72
- * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
73
- * @param {string} name Name to test
74
- * @returns {boolean} `true` if reserved, otherwise `false`
75
- */
76
- Namespace.isReservedName = function isReservedName(reserved, name) {
77
- if (reserved)
78
- for (var i = 0; i < reserved.length; ++i)
79
- if (reserved[i] === name)
80
- return true;
81
- return false;
82
- };
83
-
84
- /**
85
- * Not an actual constructor. Use {@link Namespace} instead.
86
- * @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.
87
- * @exports NamespaceBase
88
- * @extends ReflectionObject
89
- * @abstract
90
- * @constructor
91
- * @param {string} name Namespace name
92
- * @param {Object.<string,*>} [options] Declared options
93
- * @see {@link Namespace}
94
- */
95
- function Namespace(name, options) {
96
- ReflectionObject.call(this, name, options);
97
-
98
- /**
99
- * Nested objects by name.
100
- * @type {Object.<string,ReflectionObject>|undefined}
101
- */
102
- this.nested = undefined; // toJSON
103
-
104
- /**
105
- * Cached nested objects as an array.
106
- * @type {ReflectionObject[]|null}
107
- * @private
108
- */
109
- this._nestedArray = null;
110
- }
111
-
112
- function clearCache(namespace) {
113
- namespace._nestedArray = null;
114
- return namespace;
115
- }
116
-
117
- /**
118
- * Nested objects of this namespace as an array for iteration.
119
- * @name NamespaceBase#nestedArray
120
- * @type {ReflectionObject[]}
121
- * @readonly
122
- */
123
- Object.defineProperty(Namespace.prototype, "nestedArray", {
124
- get: function() {
125
- return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
126
- }
127
- });
128
-
129
- /**
130
- * Namespace descriptor.
131
- * @interface INamespace
132
- * @property {Object.<string,*>} [options] Namespace options
133
- * @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
134
- */
135
-
136
- /**
137
- * Any extension field descriptor.
138
- * @typedef AnyExtensionField
139
- * @type {IExtensionField|IExtensionMapField}
140
- */
141
-
142
- /**
143
- * Any nested object descriptor.
144
- * @typedef AnyNestedObject
145
- * @type {IEnum|IType|IService|AnyExtensionField|INamespace}
146
- */
147
- // ^ BEWARE: VSCode hangs forever when using more than 5 types (that's why AnyExtensionField exists in the first place)
148
-
149
- /**
150
- * Converts this namespace to a namespace descriptor.
151
- * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
152
- * @returns {INamespace} Namespace descriptor
153
- */
154
- Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
155
- return util.toObject([
156
- "options" , this.options,
157
- "nested" , arrayToJSON(this.nestedArray, toJSONOptions)
158
- ]);
159
- };
160
-
161
- /**
162
- * Adds nested objects to this namespace from nested object descriptors.
163
- * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
164
- * @returns {Namespace} `this`
165
- */
166
- Namespace.prototype.addJSON = function addJSON(nestedJson) {
167
- var ns = this;
168
- /* istanbul ignore else */
169
- if (nestedJson) {
170
- for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
171
- nested = nestedJson[names[i]];
172
- ns.add( // most to least likely
173
- ( nested.fields !== undefined
174
- ? Type.fromJSON
175
- : nested.values !== undefined
176
- ? Enum.fromJSON
177
- : nested.methods !== undefined
178
- ? Service.fromJSON
179
- : nested.id !== undefined
180
- ? Field.fromJSON
181
- : Namespace.fromJSON )(names[i], nested)
182
- );
183
- }
184
- }
185
- return this;
186
- };
187
-
188
- /**
189
- * Gets the nested object of the specified name.
190
- * @param {string} name Nested object name
191
- * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
192
- */
193
- Namespace.prototype.get = function get(name) {
194
- return this.nested && this.nested[name]
195
- || null;
196
- };
197
-
198
- /**
199
- * Gets the values of the nested {@link Enum|enum} of the specified name.
200
- * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
201
- * @param {string} name Nested enum name
202
- * @returns {Object.<string,number>} Enum values
203
- * @throws {Error} If there is no such enum
204
- */
205
- Namespace.prototype.getEnum = function getEnum(name) {
206
- if (this.nested && this.nested[name] instanceof Enum)
207
- return this.nested[name].values;
208
- throw Error("no such enum: " + name);
209
- };
210
-
211
- /**
212
- * Adds a nested object to this namespace.
213
- * @param {ReflectionObject} object Nested object to add
214
- * @returns {Namespace} `this`
215
- * @throws {TypeError} If arguments are invalid
216
- * @throws {Error} If there is already a nested object with this name
217
- */
218
- Namespace.prototype.add = function add(object) {
219
-
220
- if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof Enum || object instanceof Service || object instanceof Namespace))
221
- throw TypeError("object must be a valid nested object");
222
-
223
- if (!this.nested)
224
- this.nested = {};
225
- else {
226
- var prev = this.get(object.name);
227
- if (prev) {
228
- if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
229
- // replace plain namespace but keep existing nested elements and options
230
- var nested = prev.nestedArray;
231
- for (var i = 0; i < nested.length; ++i)
232
- object.add(nested[i]);
233
- this.remove(prev);
234
- if (!this.nested)
235
- this.nested = {};
236
- object.setOptions(prev.options, true);
237
-
238
- } else
239
- throw Error("duplicate name '" + object.name + "' in " + this);
240
- }
241
- }
242
- this.nested[object.name] = object;
243
- object.onAdd(this);
244
- return clearCache(this);
245
- };
246
-
247
- /**
248
- * Removes a nested object from this namespace.
249
- * @param {ReflectionObject} object Nested object to remove
250
- * @returns {Namespace} `this`
251
- * @throws {TypeError} If arguments are invalid
252
- * @throws {Error} If `object` is not a member of this namespace
253
- */
254
- Namespace.prototype.remove = function remove(object) {
255
-
256
- if (!(object instanceof ReflectionObject))
257
- throw TypeError("object must be a ReflectionObject");
258
- if (object.parent !== this)
259
- throw Error(object + " is not a member of " + this);
260
-
261
- delete this.nested[object.name];
262
- if (!Object.keys(this.nested).length)
263
- this.nested = undefined;
264
-
265
- object.onRemove(this);
266
- return clearCache(this);
267
- };
268
-
269
- /**
270
- * Defines additial namespaces within this one if not yet existing.
271
- * @param {string|string[]} path Path to create
272
- * @param {*} [json] Nested types to create from JSON
273
- * @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
274
- */
275
- Namespace.prototype.define = function define(path, json) {
276
-
277
- if (util.isString(path))
278
- path = path.split(".");
279
- else if (!Array.isArray(path))
280
- throw TypeError("illegal path");
281
- if (path && path.length && path[0] === "")
282
- throw Error("path must be relative");
283
-
284
- var ptr = this;
285
- while (path.length > 0) {
286
- var part = path.shift();
287
- if (ptr.nested && ptr.nested[part]) {
288
- ptr = ptr.nested[part];
289
- if (!(ptr instanceof Namespace))
290
- throw Error("path conflicts with non-namespace objects");
291
- } else
292
- ptr.add(ptr = new Namespace(part));
293
- }
294
- if (json)
295
- ptr.addJSON(json);
296
- return ptr;
297
- };
298
-
299
- /**
300
- * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
301
- * @returns {Namespace} `this`
302
- */
303
- Namespace.prototype.resolveAll = function resolveAll() {
304
- var nested = this.nestedArray, i = 0;
305
- while (i < nested.length)
306
- if (nested[i] instanceof Namespace)
307
- nested[i++].resolveAll();
308
- else
309
- nested[i++].resolve();
310
- return this.resolve();
311
- };
312
-
313
- /**
314
- * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
315
- * @param {string|string[]} path Path to look up
316
- * @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
317
- * @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
318
- * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
319
- */
320
- Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
321
-
322
- /* istanbul ignore next */
323
- if (typeof filterTypes === "boolean") {
324
- parentAlreadyChecked = filterTypes;
325
- filterTypes = undefined;
326
- } else if (filterTypes && !Array.isArray(filterTypes))
327
- filterTypes = [ filterTypes ];
328
-
329
- if (util.isString(path) && path.length) {
330
- if (path === ".")
331
- return this.root;
332
- path = path.split(".");
333
- } else if (!path.length)
334
- return this;
335
-
336
- // Start at root if path is absolute
337
- if (path[0] === "")
338
- return this.root.lookup(path.slice(1), filterTypes);
339
-
340
- // Test if the first part matches any nested object, and if so, traverse if path contains more
341
- var found = this.get(path[0]);
342
- if (found) {
343
- if (path.length === 1) {
344
- if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
345
- return found;
346
- } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
347
- return found;
348
-
349
- // Otherwise try each nested namespace
350
- } else
351
- for (var i = 0; i < this.nestedArray.length; ++i)
352
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
353
- return found;
354
-
355
- // If there hasn't been a match, try again at the parent
356
- if (this.parent === null || parentAlreadyChecked)
357
- return null;
358
- return this.parent.lookup(path, filterTypes);
359
- };
360
-
361
- /**
362
- * Looks up the reflection object at the specified path, relative to this namespace.
363
- * @name NamespaceBase#lookup
364
- * @function
365
- * @param {string|string[]} path Path to look up
366
- * @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
367
- * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
368
- * @variation 2
369
- */
370
- // lookup(path: string, [parentAlreadyChecked: boolean])
371
-
372
- /**
373
- * Looks up the {@link Type|type} at the specified path, relative to this namespace.
374
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
375
- * @param {string|string[]} path Path to look up
376
- * @returns {Type} Looked up type
377
- * @throws {Error} If `path` does not point to a type
378
- */
379
- Namespace.prototype.lookupType = function lookupType(path) {
380
- var found = this.lookup(path, [ Type ]);
381
- if (!found)
382
- throw Error("no such type: " + path);
383
- return found;
384
- };
385
-
386
- /**
387
- * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
388
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
389
- * @param {string|string[]} path Path to look up
390
- * @returns {Enum} Looked up enum
391
- * @throws {Error} If `path` does not point to an enum
392
- */
393
- Namespace.prototype.lookupEnum = function lookupEnum(path) {
394
- var found = this.lookup(path, [ Enum ]);
395
- if (!found)
396
- throw Error("no such Enum '" + path + "' in " + this);
397
- return found;
398
- };
399
-
400
- /**
401
- * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
402
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
403
- * @param {string|string[]} path Path to look up
404
- * @returns {Type} Looked up type or enum
405
- * @throws {Error} If `path` does not point to a type or enum
406
- */
407
- Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
408
- var found = this.lookup(path, [ Type, Enum ]);
409
- if (!found)
410
- throw Error("no such Type or Enum '" + path + "' in " + this);
411
- return found;
412
- };
413
-
414
- /**
415
- * Looks up the {@link Service|service} at the specified path, relative to this namespace.
416
- * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
417
- * @param {string|string[]} path Path to look up
418
- * @returns {Service} Looked up service
419
- * @throws {Error} If `path` does not point to a service
420
- */
421
- Namespace.prototype.lookupService = function lookupService(path) {
422
- var found = this.lookup(path, [ Service ]);
423
- if (!found)
424
- throw Error("no such Service '" + path + "' in " + this);
425
- return found;
426
- };
427
-
428
- // Sets up cyclic dependencies (called in index-light)
429
- Namespace._configure = function(Type_, Service_, Enum_) {
430
- Type = Type_;
431
- Service = Service_;
432
- Enum = Enum_;
433
- };
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
+
11
+ var Type, // cyclic
12
+ Service,
13
+ Enum;
14
+
15
+ /**
16
+ * Constructs a new namespace instance.
17
+ * @name Namespace
18
+ * @classdesc Reflected namespace.
19
+ * @extends NamespaceBase
20
+ * @constructor
21
+ * @param {string} name Namespace name
22
+ * @param {Object.<string,*>} [options] Declared options
23
+ */
24
+
25
+ /**
26
+ * Constructs a namespace from JSON.
27
+ * @memberof Namespace
28
+ * @function
29
+ * @param {string} name Namespace name
30
+ * @param {Object.<string,*>} json JSON object
31
+ * @returns {Namespace} Created namespace
32
+ * @throws {TypeError} If arguments are invalid
33
+ */
34
+ Namespace.fromJSON = function fromJSON(name, json) {
35
+ return new Namespace(name, json.options).addJSON(json.nested);
36
+ };
37
+
38
+ /**
39
+ * Converts an array of reflection objects to JSON.
40
+ * @memberof Namespace
41
+ * @param {ReflectionObject[]} array Object array
42
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
43
+ * @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
44
+ */
45
+ function arrayToJSON(array, toJSONOptions) {
46
+ if (!(array && array.length))
47
+ return undefined;
48
+ var obj = {};
49
+ for (var i = 0; i < array.length; ++i)
50
+ obj[array[i].name] = array[i].toJSON(toJSONOptions);
51
+ return obj;
52
+ }
53
+
54
+ Namespace.arrayToJSON = arrayToJSON;
55
+
56
+ /**
57
+ * Tests if the specified id is reserved.
58
+ * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
59
+ * @param {number} id Id to test
60
+ * @returns {boolean} `true` if reserved, otherwise `false`
61
+ */
62
+ Namespace.isReservedId = function isReservedId(reserved, id) {
63
+ if (reserved)
64
+ for (var i = 0; i < reserved.length; ++i)
65
+ if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] > id)
66
+ return true;
67
+ return false;
68
+ };
69
+
70
+ /**
71
+ * Tests if the specified name is reserved.
72
+ * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
73
+ * @param {string} name Name to test
74
+ * @returns {boolean} `true` if reserved, otherwise `false`
75
+ */
76
+ Namespace.isReservedName = function isReservedName(reserved, name) {
77
+ if (reserved)
78
+ for (var i = 0; i < reserved.length; ++i)
79
+ if (reserved[i] === name)
80
+ return true;
81
+ return false;
82
+ };
83
+
84
+ /**
85
+ * Not an actual constructor. Use {@link Namespace} instead.
86
+ * @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.
87
+ * @exports NamespaceBase
88
+ * @extends ReflectionObject
89
+ * @abstract
90
+ * @constructor
91
+ * @param {string} name Namespace name
92
+ * @param {Object.<string,*>} [options] Declared options
93
+ * @see {@link Namespace}
94
+ */
95
+ function Namespace(name, options) {
96
+ ReflectionObject.call(this, name, options);
97
+
98
+ /**
99
+ * Nested objects by name.
100
+ * @type {Object.<string,ReflectionObject>|undefined}
101
+ */
102
+ this.nested = undefined; // toJSON
103
+
104
+ /**
105
+ * Cached nested objects as an array.
106
+ * @type {ReflectionObject[]|null}
107
+ * @private
108
+ */
109
+ this._nestedArray = null;
110
+ }
111
+
112
+ function clearCache(namespace) {
113
+ namespace._nestedArray = null;
114
+ return namespace;
115
+ }
116
+
117
+ /**
118
+ * Nested objects of this namespace as an array for iteration.
119
+ * @name NamespaceBase#nestedArray
120
+ * @type {ReflectionObject[]}
121
+ * @readonly
122
+ */
123
+ Object.defineProperty(Namespace.prototype, "nestedArray", {
124
+ get: function() {
125
+ return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
126
+ }
127
+ });
128
+
129
+ /**
130
+ * Namespace descriptor.
131
+ * @interface INamespace
132
+ * @property {Object.<string,*>} [options] Namespace options
133
+ * @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
134
+ */
135
+
136
+ /**
137
+ * Any extension field descriptor.
138
+ * @typedef AnyExtensionField
139
+ * @type {IExtensionField|IExtensionMapField}
140
+ */
141
+
142
+ /**
143
+ * Any nested object descriptor.
144
+ * @typedef AnyNestedObject
145
+ * @type {IEnum|IType|IService|AnyExtensionField|INamespace}
146
+ */
147
+ // ^ BEWARE: VSCode hangs forever when using more than 5 types (that's why AnyExtensionField exists in the first place)
148
+
149
+ /**
150
+ * Converts this namespace to a namespace descriptor.
151
+ * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
152
+ * @returns {INamespace} Namespace descriptor
153
+ */
154
+ Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
155
+ return util.toObject([
156
+ "options" , this.options,
157
+ "nested" , arrayToJSON(this.nestedArray, toJSONOptions)
158
+ ]);
159
+ };
160
+
161
+ /**
162
+ * Adds nested objects to this namespace from nested object descriptors.
163
+ * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
164
+ * @returns {Namespace} `this`
165
+ */
166
+ Namespace.prototype.addJSON = function addJSON(nestedJson) {
167
+ var ns = this;
168
+ /* istanbul ignore else */
169
+ if (nestedJson) {
170
+ for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
171
+ nested = nestedJson[names[i]];
172
+ ns.add( // most to least likely
173
+ ( nested.fields !== undefined
174
+ ? Type.fromJSON
175
+ : nested.values !== undefined
176
+ ? Enum.fromJSON
177
+ : nested.methods !== undefined
178
+ ? Service.fromJSON
179
+ : nested.id !== undefined
180
+ ? Field.fromJSON
181
+ : Namespace.fromJSON )(names[i], nested)
182
+ );
183
+ }
184
+ }
185
+ return this;
186
+ };
187
+
188
+ /**
189
+ * Gets the nested object of the specified name.
190
+ * @param {string} name Nested object name
191
+ * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
192
+ */
193
+ Namespace.prototype.get = function get(name) {
194
+ return this.nested && this.nested[name]
195
+ || null;
196
+ };
197
+
198
+ /**
199
+ * Gets the values of the nested {@link Enum|enum} of the specified name.
200
+ * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
201
+ * @param {string} name Nested enum name
202
+ * @returns {Object.<string,number>} Enum values
203
+ * @throws {Error} If there is no such enum
204
+ */
205
+ Namespace.prototype.getEnum = function getEnum(name) {
206
+ if (this.nested && this.nested[name] instanceof Enum)
207
+ return this.nested[name].values;
208
+ throw Error("no such enum: " + name);
209
+ };
210
+
211
+ /**
212
+ * Adds a nested object to this namespace.
213
+ * @param {ReflectionObject} object Nested object to add
214
+ * @returns {Namespace} `this`
215
+ * @throws {TypeError} If arguments are invalid
216
+ * @throws {Error} If there is already a nested object with this name
217
+ */
218
+ Namespace.prototype.add = function add(object) {
219
+
220
+ if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof Enum || object instanceof Service || object instanceof Namespace))
221
+ throw TypeError("object must be a valid nested object");
222
+
223
+ if (!this.nested)
224
+ this.nested = {};
225
+ else {
226
+ var prev = this.get(object.name);
227
+ if (prev) {
228
+ if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
229
+ // replace plain namespace but keep existing nested elements and options
230
+ var nested = prev.nestedArray;
231
+ for (var i = 0; i < nested.length; ++i)
232
+ object.add(nested[i]);
233
+ this.remove(prev);
234
+ if (!this.nested)
235
+ this.nested = {};
236
+ object.setOptions(prev.options, true);
237
+
238
+ } else
239
+ throw Error("duplicate name '" + object.name + "' in " + this);
240
+ }
241
+ }
242
+ this.nested[object.name] = object;
243
+ object.onAdd(this);
244
+ return clearCache(this);
245
+ };
246
+
247
+ /**
248
+ * Removes a nested object from this namespace.
249
+ * @param {ReflectionObject} object Nested object to remove
250
+ * @returns {Namespace} `this`
251
+ * @throws {TypeError} If arguments are invalid
252
+ * @throws {Error} If `object` is not a member of this namespace
253
+ */
254
+ Namespace.prototype.remove = function remove(object) {
255
+
256
+ if (!(object instanceof ReflectionObject))
257
+ throw TypeError("object must be a ReflectionObject");
258
+ if (object.parent !== this)
259
+ throw Error(object + " is not a member of " + this);
260
+
261
+ delete this.nested[object.name];
262
+ if (!Object.keys(this.nested).length)
263
+ this.nested = undefined;
264
+
265
+ object.onRemove(this);
266
+ return clearCache(this);
267
+ };
268
+
269
+ /**
270
+ * Defines additial namespaces within this one if not yet existing.
271
+ * @param {string|string[]} path Path to create
272
+ * @param {*} [json] Nested types to create from JSON
273
+ * @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
274
+ */
275
+ Namespace.prototype.define = function define(path, json) {
276
+
277
+ if (util.isString(path))
278
+ path = path.split(".");
279
+ else if (!Array.isArray(path))
280
+ throw TypeError("illegal path");
281
+ if (path && path.length && path[0] === "")
282
+ throw Error("path must be relative");
283
+
284
+ var ptr = this;
285
+ while (path.length > 0) {
286
+ var part = path.shift();
287
+ if (ptr.nested && ptr.nested[part]) {
288
+ ptr = ptr.nested[part];
289
+ if (!(ptr instanceof Namespace))
290
+ throw Error("path conflicts with non-namespace objects");
291
+ } else
292
+ ptr.add(ptr = new Namespace(part));
293
+ }
294
+ if (json)
295
+ ptr.addJSON(json);
296
+ return ptr;
297
+ };
298
+
299
+ /**
300
+ * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
301
+ * @returns {Namespace} `this`
302
+ */
303
+ Namespace.prototype.resolveAll = function resolveAll() {
304
+ var nested = this.nestedArray, i = 0;
305
+ while (i < nested.length)
306
+ if (nested[i] instanceof Namespace)
307
+ nested[i++].resolveAll();
308
+ else
309
+ nested[i++].resolve();
310
+ return this.resolve();
311
+ };
312
+
313
+ /**
314
+ * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
315
+ * @param {string|string[]} path Path to look up
316
+ * @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
317
+ * @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
318
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
319
+ */
320
+ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
321
+
322
+ /* istanbul ignore next */
323
+ if (typeof filterTypes === "boolean") {
324
+ parentAlreadyChecked = filterTypes;
325
+ filterTypes = undefined;
326
+ } else if (filterTypes && !Array.isArray(filterTypes))
327
+ filterTypes = [ filterTypes ];
328
+
329
+ if (util.isString(path) && path.length) {
330
+ if (path === ".")
331
+ return this.root;
332
+ path = path.split(".");
333
+ } else if (!path.length)
334
+ return this;
335
+
336
+ // Start at root if path is absolute
337
+ if (path[0] === "")
338
+ return this.root.lookup(path.slice(1), filterTypes);
339
+
340
+ // Test if the first part matches any nested object, and if so, traverse if path contains more
341
+ var found = this.get(path[0]);
342
+ if (found) {
343
+ if (path.length === 1) {
344
+ if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
345
+ return found;
346
+ } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
347
+ return found;
348
+
349
+ // Otherwise try each nested namespace
350
+ } else
351
+ for (var i = 0; i < this.nestedArray.length; ++i)
352
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
353
+ return found;
354
+
355
+ // If there hasn't been a match, try again at the parent
356
+ if (this.parent === null || parentAlreadyChecked)
357
+ return null;
358
+ return this.parent.lookup(path, filterTypes);
359
+ };
360
+
361
+ /**
362
+ * Looks up the reflection object at the specified path, relative to this namespace.
363
+ * @name NamespaceBase#lookup
364
+ * @function
365
+ * @param {string|string[]} path Path to look up
366
+ * @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
367
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
368
+ * @variation 2
369
+ */
370
+ // lookup(path: string, [parentAlreadyChecked: boolean])
371
+
372
+ /**
373
+ * Looks up the {@link Type|type} at the specified path, relative to this namespace.
374
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
375
+ * @param {string|string[]} path Path to look up
376
+ * @returns {Type} Looked up type
377
+ * @throws {Error} If `path` does not point to a type
378
+ */
379
+ Namespace.prototype.lookupType = function lookupType(path) {
380
+ var found = this.lookup(path, [ Type ]);
381
+ if (!found)
382
+ throw Error("no such type: " + path);
383
+ return found;
384
+ };
385
+
386
+ /**
387
+ * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
388
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
389
+ * @param {string|string[]} path Path to look up
390
+ * @returns {Enum} Looked up enum
391
+ * @throws {Error} If `path` does not point to an enum
392
+ */
393
+ Namespace.prototype.lookupEnum = function lookupEnum(path) {
394
+ var found = this.lookup(path, [ Enum ]);
395
+ if (!found)
396
+ throw Error("no such Enum '" + path + "' in " + this);
397
+ return found;
398
+ };
399
+
400
+ /**
401
+ * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
402
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
403
+ * @param {string|string[]} path Path to look up
404
+ * @returns {Type} Looked up type or enum
405
+ * @throws {Error} If `path` does not point to a type or enum
406
+ */
407
+ Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
408
+ var found = this.lookup(path, [ Type, Enum ]);
409
+ if (!found)
410
+ throw Error("no such Type or Enum '" + path + "' in " + this);
411
+ return found;
412
+ };
413
+
414
+ /**
415
+ * Looks up the {@link Service|service} at the specified path, relative to this namespace.
416
+ * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
417
+ * @param {string|string[]} path Path to look up
418
+ * @returns {Service} Looked up service
419
+ * @throws {Error} If `path` does not point to a service
420
+ */
421
+ Namespace.prototype.lookupService = function lookupService(path) {
422
+ var found = this.lookup(path, [ Service ]);
423
+ if (!found)
424
+ throw Error("no such Service '" + path + "' in " + this);
425
+ return found;
426
+ };
427
+
428
+ // Sets up cyclic dependencies (called in index-light)
429
+ Namespace._configure = function(Type_, Service_, Enum_) {
430
+ Type = Type_;
431
+ Service = Service_;
432
+ Enum = Enum_;
433
+ };