protobufjs 8.0.1-experimental → 8.0.2

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