protobufjs 8.6.2 → 8.6.4

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 +8582 -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 +3071 -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 +10486 -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 +945 -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 +188 -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/root.js CHANGED
@@ -1,416 +1,416 @@
1
- "use strict";
2
- module.exports = Root;
3
-
4
- // extends Namespace
5
- var Namespace = require("./namespace");
6
- ((Root.prototype = Object.create(Namespace.prototype)).constructor = Root).className = "Root";
7
-
8
- var Field = require("./field"),
9
- Enum = require("./enum"),
10
- OneOf = require("./oneof"),
11
- util = require("./util");
12
-
13
- var Type, // cyclic
14
- parse, // might be excluded
15
- common; // "
16
-
17
- /**
18
- * Constructs a new root namespace instance.
19
- * @classdesc Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together.
20
- * @extends NamespaceBase
21
- * @constructor
22
- * @param {Object.<string,*>} [options] Top level options
23
- */
24
- function Root(options) {
25
- Namespace.call(this, "", options);
26
-
27
- /**
28
- * Deferred extension fields.
29
- * @type {Field[]}
30
- */
31
- this.deferred = [];
32
-
33
- /**
34
- * Resolved file names of loaded files.
35
- * @type {string[]}
36
- */
37
- this.files = [];
38
-
39
- /**
40
- * Edition, defaults to proto2 if unspecified.
41
- * @type {string}
42
- * @private
43
- */
44
- this._edition = "proto2";
45
-
46
- /**
47
- * Global lookup cache of fully qualified names.
48
- * @type {Object.<string,ReflectionObject>}
49
- * @private
50
- */
51
- this._fullyQualifiedObjects = {};
52
- }
53
-
54
- /**
55
- * Loads a namespace descriptor into a root namespace.
56
- * @param {INamespace} json Namespace descriptor
57
- * @param {Root} [root] Root namespace, defaults to create a new one if omitted
58
- * @param {number} [depth] Current nesting depth, defaults to `0`
59
- * @returns {Root} Root namespace
60
- */
61
- Root.fromJSON = function fromJSON(json, root, depth) {
62
- if (depth === undefined)
63
- depth = 0;
64
- if (depth > util.recursionLimit)
65
- throw Error("max depth exceeded");
66
- if (!root)
67
- root = new Root();
68
- if (json.options)
69
- root.setOptions(json.options);
70
- return root.addJSON(json.nested, depth).resolveAll();
71
- };
72
-
73
- /**
74
- * Resolves the path of an imported file, relative to the importing origin.
75
- * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
76
- * @function
77
- * @param {string} origin The file name of the importing file
78
- * @param {string} target The file name being imported
79
- * @returns {string|null} Resolved path to `target` or `null` to skip the file
80
- */
81
- Root.prototype.resolvePath = util.path.resolve;
82
-
83
- /**
84
- * Fetch content from file path or url
85
- * This method exists so you can override it with your own logic.
86
- * @function
87
- * @param {string} path File path or url
88
- * @param {FetchCallback} callback Callback function
89
- * @returns {undefined}
90
- */
91
- Root.prototype.fetch = util.fetch;
92
-
93
- // A symbol-like function to safely signal synchronous loading
94
- /* istanbul ignore next */
95
- function SYNC() {} // eslint-disable-line no-empty-function
96
-
97
- /**
98
- * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
99
- * @param {string|string[]} filename Names of one or multiple files to load
100
- * @param {IParseOptions} options Parse options
101
- * @param {LoadCallback} callback Callback function
102
- * @returns {undefined}
103
- */
104
- Root.prototype.load = function load(filename, options, callback) {
105
- if (typeof options === "function") {
106
- callback = options;
107
- options = undefined;
108
- }
109
- var self = this;
110
- if (!callback) {
111
- return util.asPromise(load, self, filename, options);
112
- }
113
-
114
- var sync = callback === SYNC; // undocumented
115
-
116
- // Finishes loading by calling the callback (exactly once)
117
- function finish(err, root) {
118
- /* istanbul ignore if */
119
- if (!callback) {
120
- return;
121
- }
122
- if (sync) {
123
- throw err;
124
- }
125
- if (root) {
126
- root.resolveAll();
127
- }
128
- var cb = callback;
129
- callback = null;
130
- cb(err, root);
131
- }
132
-
133
- // Bundled definition existence checking
134
- function getBundledFileName(filename) {
135
- var idx = filename.lastIndexOf("google/protobuf/");
136
- if (idx > -1) {
137
- var altname = filename.substring(idx);
138
- if (Object.prototype.hasOwnProperty.call(common, altname)) return altname;
139
- }
140
- if (Object.prototype.hasOwnProperty.call(common, filename)) return filename;
141
- return null;
142
- }
143
-
144
- // Processes a single file
145
- function process(filename, source, depth) {
146
- if (depth === undefined)
147
- depth = 0;
148
- try {
149
- if (depth > util.recursionLimit)
150
- throw Error("max depth exceeded");
151
- if (util.isString(source) && source.charAt(0) === "{")
152
- source = JSON.parse(source);
153
- if (!util.isString(source))
154
- self.setOptions(source.options).addJSON(source.nested);
155
- else {
156
- parse.filename = filename;
157
- var parsed = parse(source, self, options),
158
- resolved,
159
- i = 0;
160
- if (parsed.imports)
161
- for (; i < parsed.imports.length; ++i)
162
- if (resolved = getBundledFileName(parsed.imports[i]) || self.resolvePath(filename, parsed.imports[i]))
163
- fetch(resolved, false, depth + 1);
164
- if (parsed.weakImports)
165
- for (i = 0; i < parsed.weakImports.length; ++i)
166
- if (resolved = getBundledFileName(parsed.weakImports[i]) || self.resolvePath(filename, parsed.weakImports[i]))
167
- fetch(resolved, true, depth + 1);
168
- }
169
- } catch (err) {
170
- finish(err);
171
- }
172
- if (!sync && !queued) {
173
- finish(null, self); // only once anyway
174
- }
175
- }
176
-
177
- // Fetches a single file
178
- function fetch(filename, weak, depth) {
179
- if (depth === undefined)
180
- depth = 0;
181
- filename = getBundledFileName(filename) || filename;
182
-
183
- // Skip if already loaded / attempted
184
- if (self.files.indexOf(filename) > -1) {
185
- return;
186
- }
187
- self.files.push(filename);
188
-
189
- // Shortcut bundled definitions
190
- if (Object.prototype.hasOwnProperty.call(common, filename)) {
191
- if (sync) {
192
- process(filename, common[filename], depth);
193
- } else {
194
- ++queued;
195
- setTimeout(function() {
196
- --queued;
197
- process(filename, common[filename], depth);
198
- });
199
- }
200
- return;
201
- }
202
-
203
- // Otherwise fetch from disk or network
204
- if (sync) {
205
- var source;
206
- try {
207
- source = util.fs.readFileSync(filename).toString("utf8");
208
- } catch (err) {
209
- if (!weak)
210
- finish(err);
211
- return;
212
- }
213
- process(filename, source, depth);
214
- } else {
215
- ++queued;
216
- self.fetch(filename, function(err, source) {
217
- --queued;
218
- /* istanbul ignore if */
219
- if (!callback) {
220
- return; // terminated meanwhile
221
- }
222
- if (err) {
223
- /* istanbul ignore else */
224
- if (!weak)
225
- finish(err);
226
- else if (!queued) // can't be covered reliably
227
- finish(null, self);
228
- return;
229
- }
230
- process(filename, source, depth);
231
- });
232
- }
233
- }
234
- var queued = 0;
235
-
236
- // Assembling the root namespace doesn't require working type
237
- // references anymore, so we can load everything in parallel
238
- if (util.isString(filename)) {
239
- filename = [ filename ];
240
- }
241
- for (var i = 0, resolved; i < filename.length; ++i)
242
- if (resolved = self.resolvePath("", filename[i]))
243
- fetch(resolved);
244
- if (sync) {
245
- self.resolveAll();
246
- return self;
247
- }
248
- if (!queued) {
249
- finish(null, self);
250
- }
251
-
252
- return self;
253
- };
254
- // function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
255
-
256
- /**
257
- * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
258
- * @function Root#load
259
- * @param {string|string[]} filename Names of one or multiple files to load
260
- * @param {LoadCallback} callback Callback function
261
- * @returns {undefined}
262
- * @variation 2
263
- */
264
- // function load(filename:string, callback:LoadCallback):undefined
265
-
266
- /**
267
- * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
268
- * @function Root#load
269
- * @param {string|string[]} filename Names of one or multiple files to load
270
- * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
271
- * @returns {Promise<Root>} Promise
272
- * @variation 3
273
- */
274
- // function load(filename:string, [options:IParseOptions]):Promise<Root>
275
-
276
- /**
277
- * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
278
- * @function Root#loadSync
279
- * @param {string|string[]} filename Names of one or multiple files to load
280
- * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
281
- * @returns {Root} Root namespace
282
- * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
283
- */
284
- Root.prototype.loadSync = function loadSync(filename, options) {
285
- if (!util.isNode)
286
- throw Error("not supported");
287
- return this.load(filename, options, SYNC);
288
- };
289
-
290
- /**
291
- * @override
292
- */
293
- Root.prototype.resolveAll = function resolveAll() {
294
- if (!this._needsRecursiveResolve) return this;
295
-
296
- if (this.deferred.length)
297
- throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
298
- return "'extend " + field.extend + "' in " + field.parent.fullName;
299
- }).join(", "));
300
- return Namespace.prototype.resolveAll.call(this);
301
- };
302
-
303
- // only uppercased (and thus conflict-free) children are exposed, see below
304
- var exposeRe = /^[A-Z]/;
305
-
306
- /**
307
- * Handles a deferred declaring extension field by creating a sister field to represent it within its extended type.
308
- * @param {Root} root Root instance
309
- * @param {Field} field Declaring extension field witin the declaring type
310
- * @returns {boolean} `true` if successfully added to the extended type, `false` otherwise
311
- * @inner
312
- * @ignore
313
- */
314
- function tryHandleExtension(root, field) {
315
- var extendedType = field.parent.lookup(field.extend);
316
- if (extendedType) {
317
- var sisterField = new Field(field.fullName, field.id, field.type, field.rule, undefined, field.options);
318
- //do not allow to extend same field twice to prevent the error
319
- if (extendedType.get(sisterField.name)) {
320
- return true;
321
- }
322
- sisterField.declaringField = field;
323
- field.extensionField = sisterField;
324
- extendedType.add(sisterField);
325
- return true;
326
- }
327
- return false;
328
- }
329
-
330
- /**
331
- * Called when any object is added to this root or its sub-namespaces.
332
- * @param {ReflectionObject} object Object added
333
- * @returns {undefined}
334
- * @private
335
- */
336
- Root.prototype._handleAdd = function _handleAdd(object) {
337
- if (object instanceof Field) {
338
-
339
- if (/* an extension field (implies not part of a oneof) */ object.extend !== undefined && /* not already handled */ !object.extensionField)
340
- if (!tryHandleExtension(this, object))
341
- this.deferred.push(object);
342
-
343
- } else if (object instanceof Enum) {
344
-
345
- if (exposeRe.test(object.name))
346
- object.parent[object.name] = object.values; // expose enum values as property of its parent
347
-
348
- } else if (!(object instanceof OneOf)) /* everything else is a namespace */ {
349
-
350
- if (object instanceof Type) // Try to handle any deferred extensions
351
- for (var i = 0; i < this.deferred.length;)
352
- if (tryHandleExtension(this, this.deferred[i]))
353
- this.deferred.splice(i, 1);
354
- else
355
- ++i;
356
- for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
357
- this._handleAdd(object._nestedArray[j]);
358
- if (exposeRe.test(object.name))
359
- object.parent[object.name] = object; // expose namespace as property of its parent
360
- }
361
-
362
- if (object instanceof Type || object instanceof Enum || object instanceof Field) {
363
- // Only store types and enums for quick lookup during resolve.
364
- this._fullyQualifiedObjects[object.fullName] = object;
365
- }
366
-
367
- // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
368
- // properties of namespaces just like static code does. This allows using a .d.ts generated for
369
- // a static module with reflection-based solutions where the condition is met.
370
- };
371
-
372
- /**
373
- * Called when any object is removed from this root or its sub-namespaces.
374
- * @param {ReflectionObject} object Object removed
375
- * @returns {undefined}
376
- * @private
377
- */
378
- Root.prototype._handleRemove = function _handleRemove(object) {
379
- if (object instanceof Field) {
380
-
381
- if (/* an extension field */ object.extend !== undefined) {
382
- if (/* already handled */ object.extensionField) { // remove its sister field
383
- object.extensionField.parent.remove(object.extensionField);
384
- object.extensionField = null;
385
- } else { // cancel the extension
386
- var index = this.deferred.indexOf(object);
387
- /* istanbul ignore else */
388
- if (index > -1)
389
- this.deferred.splice(index, 1);
390
- }
391
- }
392
-
393
- } else if (object instanceof Enum) {
394
-
395
- if (exposeRe.test(object.name))
396
- delete object.parent[object.name]; // unexpose enum values
397
-
398
- } else if (object instanceof Namespace) {
399
-
400
- for (var i = 0; i < /* initializes */ object.nestedArray.length; ++i) // recurse into the namespace
401
- this._handleRemove(object._nestedArray[i]);
402
-
403
- if (exposeRe.test(object.name))
404
- delete object.parent[object.name]; // unexpose namespaces
405
-
406
- }
407
-
408
- delete this._fullyQualifiedObjects[object.fullName];
409
- };
410
-
411
- // Sets up cyclic dependencies (called in index-light)
412
- Root._configure = function(Type_, parse_, common_) {
413
- Type = Type_;
414
- parse = parse_;
415
- common = common_;
416
- };
1
+ "use strict";
2
+ module.exports = Root;
3
+
4
+ // extends Namespace
5
+ var Namespace = require("./namespace");
6
+ ((Root.prototype = Object.create(Namespace.prototype)).constructor = Root).className = "Root";
7
+
8
+ var Field = require("./field"),
9
+ Enum = require("./enum"),
10
+ OneOf = require("./oneof"),
11
+ util = require("./util");
12
+
13
+ var Type, // cyclic
14
+ parse, // might be excluded
15
+ common; // "
16
+
17
+ /**
18
+ * Constructs a new root namespace instance.
19
+ * @classdesc Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together.
20
+ * @extends NamespaceBase
21
+ * @constructor
22
+ * @param {Object.<string,*>} [options] Top level options
23
+ */
24
+ function Root(options) {
25
+ Namespace.call(this, "", options);
26
+
27
+ /**
28
+ * Deferred extension fields.
29
+ * @type {Field[]}
30
+ */
31
+ this.deferred = [];
32
+
33
+ /**
34
+ * Resolved file names of loaded files.
35
+ * @type {string[]}
36
+ */
37
+ this.files = [];
38
+
39
+ /**
40
+ * Edition, defaults to proto2 if unspecified.
41
+ * @type {string}
42
+ * @private
43
+ */
44
+ this._edition = "proto2";
45
+
46
+ /**
47
+ * Global lookup cache of fully qualified names.
48
+ * @type {Object.<string,ReflectionObject>}
49
+ * @private
50
+ */
51
+ this._fullyQualifiedObjects = {};
52
+ }
53
+
54
+ /**
55
+ * Loads a namespace descriptor into a root namespace.
56
+ * @param {INamespace} json Namespace descriptor
57
+ * @param {Root} [root] Root namespace, defaults to create a new one if omitted
58
+ * @param {number} [depth] Current nesting depth, defaults to `0`
59
+ * @returns {Root} Root namespace
60
+ */
61
+ Root.fromJSON = function fromJSON(json, root, depth) {
62
+ if (depth === undefined)
63
+ depth = 0;
64
+ if (depth > util.recursionLimit)
65
+ throw Error("max depth exceeded");
66
+ if (!root)
67
+ root = new Root();
68
+ if (json.options)
69
+ root.setOptions(json.options);
70
+ return root.addJSON(json.nested, depth).resolveAll();
71
+ };
72
+
73
+ /**
74
+ * Resolves the path of an imported file, relative to the importing origin.
75
+ * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
76
+ * @function
77
+ * @param {string} origin The file name of the importing file
78
+ * @param {string} target The file name being imported
79
+ * @returns {string|null} Resolved path to `target` or `null` to skip the file
80
+ */
81
+ Root.prototype.resolvePath = util.path.resolve;
82
+
83
+ /**
84
+ * Fetch content from file path or url
85
+ * This method exists so you can override it with your own logic.
86
+ * @function
87
+ * @param {string} path File path or url
88
+ * @param {FetchCallback} callback Callback function
89
+ * @returns {undefined}
90
+ */
91
+ Root.prototype.fetch = util.fetch;
92
+
93
+ // A symbol-like function to safely signal synchronous loading
94
+ /* istanbul ignore next */
95
+ function SYNC() {} // eslint-disable-line no-empty-function
96
+
97
+ /**
98
+ * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
99
+ * @param {string|string[]} filename Names of one or multiple files to load
100
+ * @param {IParseOptions} options Parse options
101
+ * @param {LoadCallback} callback Callback function
102
+ * @returns {undefined}
103
+ */
104
+ Root.prototype.load = function load(filename, options, callback) {
105
+ if (typeof options === "function") {
106
+ callback = options;
107
+ options = undefined;
108
+ }
109
+ var self = this;
110
+ if (!callback) {
111
+ return util.asPromise(load, self, filename, options);
112
+ }
113
+
114
+ var sync = callback === SYNC; // undocumented
115
+
116
+ // Finishes loading by calling the callback (exactly once)
117
+ function finish(err, root) {
118
+ /* istanbul ignore if */
119
+ if (!callback) {
120
+ return;
121
+ }
122
+ if (sync) {
123
+ throw err;
124
+ }
125
+ if (root) {
126
+ root.resolveAll();
127
+ }
128
+ var cb = callback;
129
+ callback = null;
130
+ cb(err, root);
131
+ }
132
+
133
+ // Bundled definition existence checking
134
+ function getBundledFileName(filename) {
135
+ var idx = filename.lastIndexOf("google/protobuf/");
136
+ if (idx > -1) {
137
+ var altname = filename.substring(idx);
138
+ if (Object.prototype.hasOwnProperty.call(common, altname)) return altname;
139
+ }
140
+ if (Object.prototype.hasOwnProperty.call(common, filename)) return filename;
141
+ return null;
142
+ }
143
+
144
+ // Processes a single file
145
+ function process(filename, source, depth) {
146
+ if (depth === undefined)
147
+ depth = 0;
148
+ try {
149
+ if (depth > util.recursionLimit)
150
+ throw Error("max depth exceeded");
151
+ if (util.isString(source) && source.charAt(0) === "{")
152
+ source = JSON.parse(source);
153
+ if (!util.isString(source))
154
+ self.setOptions(source.options).addJSON(source.nested);
155
+ else {
156
+ parse.filename = filename;
157
+ var parsed = parse(source, self, options),
158
+ resolved,
159
+ i = 0;
160
+ if (parsed.imports)
161
+ for (; i < parsed.imports.length; ++i)
162
+ if (resolved = getBundledFileName(parsed.imports[i]) || self.resolvePath(filename, parsed.imports[i]))
163
+ fetch(resolved, false, depth + 1);
164
+ if (parsed.weakImports)
165
+ for (i = 0; i < parsed.weakImports.length; ++i)
166
+ if (resolved = getBundledFileName(parsed.weakImports[i]) || self.resolvePath(filename, parsed.weakImports[i]))
167
+ fetch(resolved, true, depth + 1);
168
+ }
169
+ } catch (err) {
170
+ finish(err);
171
+ }
172
+ if (!sync && !queued) {
173
+ finish(null, self); // only once anyway
174
+ }
175
+ }
176
+
177
+ // Fetches a single file
178
+ function fetch(filename, weak, depth) {
179
+ if (depth === undefined)
180
+ depth = 0;
181
+ filename = getBundledFileName(filename) || filename;
182
+
183
+ // Skip if already loaded / attempted
184
+ if (self.files.indexOf(filename) > -1) {
185
+ return;
186
+ }
187
+ self.files.push(filename);
188
+
189
+ // Shortcut bundled definitions
190
+ if (Object.prototype.hasOwnProperty.call(common, filename)) {
191
+ if (sync) {
192
+ process(filename, common[filename], depth);
193
+ } else {
194
+ ++queued;
195
+ setTimeout(function() {
196
+ --queued;
197
+ process(filename, common[filename], depth);
198
+ });
199
+ }
200
+ return;
201
+ }
202
+
203
+ // Otherwise fetch from disk or network
204
+ if (sync) {
205
+ var source;
206
+ try {
207
+ source = util.fs.readFileSync(filename).toString("utf8");
208
+ } catch (err) {
209
+ if (!weak)
210
+ finish(err);
211
+ return;
212
+ }
213
+ process(filename, source, depth);
214
+ } else {
215
+ ++queued;
216
+ self.fetch(filename, function(err, source) {
217
+ --queued;
218
+ /* istanbul ignore if */
219
+ if (!callback) {
220
+ return; // terminated meanwhile
221
+ }
222
+ if (err) {
223
+ /* istanbul ignore else */
224
+ if (!weak)
225
+ finish(err);
226
+ else if (!queued) // can't be covered reliably
227
+ finish(null, self);
228
+ return;
229
+ }
230
+ process(filename, source, depth);
231
+ });
232
+ }
233
+ }
234
+ var queued = 0;
235
+
236
+ // Assembling the root namespace doesn't require working type
237
+ // references anymore, so we can load everything in parallel
238
+ if (util.isString(filename)) {
239
+ filename = [ filename ];
240
+ }
241
+ for (var i = 0, resolved; i < filename.length; ++i)
242
+ if (resolved = self.resolvePath("", filename[i]))
243
+ fetch(resolved);
244
+ if (sync) {
245
+ self.resolveAll();
246
+ return self;
247
+ }
248
+ if (!queued) {
249
+ finish(null, self);
250
+ }
251
+
252
+ return self;
253
+ };
254
+ // function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
255
+
256
+ /**
257
+ * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
258
+ * @function Root#load
259
+ * @param {string|string[]} filename Names of one or multiple files to load
260
+ * @param {LoadCallback} callback Callback function
261
+ * @returns {undefined}
262
+ * @variation 2
263
+ */
264
+ // function load(filename:string, callback:LoadCallback):undefined
265
+
266
+ /**
267
+ * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
268
+ * @function Root#load
269
+ * @param {string|string[]} filename Names of one or multiple files to load
270
+ * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
271
+ * @returns {Promise<Root>} Promise
272
+ * @variation 3
273
+ */
274
+ // function load(filename:string, [options:IParseOptions]):Promise<Root>
275
+
276
+ /**
277
+ * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
278
+ * @function Root#loadSync
279
+ * @param {string|string[]} filename Names of one or multiple files to load
280
+ * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
281
+ * @returns {Root} Root namespace
282
+ * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
283
+ */
284
+ Root.prototype.loadSync = function loadSync(filename, options) {
285
+ if (!util.isNode)
286
+ throw Error("not supported");
287
+ return this.load(filename, options, SYNC);
288
+ };
289
+
290
+ /**
291
+ * @override
292
+ */
293
+ Root.prototype.resolveAll = function resolveAll() {
294
+ if (!this._needsRecursiveResolve) return this;
295
+
296
+ if (this.deferred.length)
297
+ throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
298
+ return "'extend " + field.extend + "' in " + field.parent.fullName;
299
+ }).join(", "));
300
+ return Namespace.prototype.resolveAll.call(this);
301
+ };
302
+
303
+ // only uppercased (and thus conflict-free) children are exposed, see below
304
+ var exposeRe = /^[A-Z]/;
305
+
306
+ /**
307
+ * Handles a deferred declaring extension field by creating a sister field to represent it within its extended type.
308
+ * @param {Root} root Root instance
309
+ * @param {Field} field Declaring extension field witin the declaring type
310
+ * @returns {boolean} `true` if successfully added to the extended type, `false` otherwise
311
+ * @inner
312
+ * @ignore
313
+ */
314
+ function tryHandleExtension(root, field) {
315
+ var extendedType = field.parent.lookup(field.extend);
316
+ if (extendedType) {
317
+ var sisterField = new Field(field.fullName, field.id, field.type, field.rule, undefined, field.options);
318
+ //do not allow to extend same field twice to prevent the error
319
+ if (extendedType.get(sisterField.name)) {
320
+ return true;
321
+ }
322
+ sisterField.declaringField = field;
323
+ field.extensionField = sisterField;
324
+ extendedType.add(sisterField);
325
+ return true;
326
+ }
327
+ return false;
328
+ }
329
+
330
+ /**
331
+ * Called when any object is added to this root or its sub-namespaces.
332
+ * @param {ReflectionObject} object Object added
333
+ * @returns {undefined}
334
+ * @private
335
+ */
336
+ Root.prototype._handleAdd = function _handleAdd(object) {
337
+ if (object instanceof Field) {
338
+
339
+ if (/* an extension field (implies not part of a oneof) */ object.extend !== undefined && /* not already handled */ !object.extensionField)
340
+ if (!tryHandleExtension(this, object))
341
+ this.deferred.push(object);
342
+
343
+ } else if (object instanceof Enum) {
344
+
345
+ if (exposeRe.test(object.name))
346
+ object.parent[object.name] = object.values; // expose enum values as property of its parent
347
+
348
+ } else if (!(object instanceof OneOf)) /* everything else is a namespace */ {
349
+
350
+ if (object instanceof Type) // Try to handle any deferred extensions
351
+ for (var i = 0; i < this.deferred.length;)
352
+ if (tryHandleExtension(this, this.deferred[i]))
353
+ this.deferred.splice(i, 1);
354
+ else
355
+ ++i;
356
+ for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
357
+ this._handleAdd(object._nestedArray[j]);
358
+ if (exposeRe.test(object.name))
359
+ object.parent[object.name] = object; // expose namespace as property of its parent
360
+ }
361
+
362
+ if (object instanceof Type || object instanceof Enum || object instanceof Field) {
363
+ // Only store types and enums for quick lookup during resolve.
364
+ this._fullyQualifiedObjects[object.fullName] = object;
365
+ }
366
+
367
+ // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
368
+ // properties of namespaces just like static code does. This allows using a .d.ts generated for
369
+ // a static module with reflection-based solutions where the condition is met.
370
+ };
371
+
372
+ /**
373
+ * Called when any object is removed from this root or its sub-namespaces.
374
+ * @param {ReflectionObject} object Object removed
375
+ * @returns {undefined}
376
+ * @private
377
+ */
378
+ Root.prototype._handleRemove = function _handleRemove(object) {
379
+ if (object instanceof Field) {
380
+
381
+ if (/* an extension field */ object.extend !== undefined) {
382
+ if (/* already handled */ object.extensionField) { // remove its sister field
383
+ object.extensionField.parent.remove(object.extensionField);
384
+ object.extensionField = null;
385
+ } else { // cancel the extension
386
+ var index = this.deferred.indexOf(object);
387
+ /* istanbul ignore else */
388
+ if (index > -1)
389
+ this.deferred.splice(index, 1);
390
+ }
391
+ }
392
+
393
+ } else if (object instanceof Enum) {
394
+
395
+ if (exposeRe.test(object.name))
396
+ delete object.parent[object.name]; // unexpose enum values
397
+
398
+ } else if (object instanceof Namespace) {
399
+
400
+ for (var i = 0; i < /* initializes */ object.nestedArray.length; ++i) // recurse into the namespace
401
+ this._handleRemove(object._nestedArray[i]);
402
+
403
+ if (exposeRe.test(object.name))
404
+ delete object.parent[object.name]; // unexpose namespaces
405
+
406
+ }
407
+
408
+ delete this._fullyQualifiedObjects[object.fullName];
409
+ };
410
+
411
+ // Sets up cyclic dependencies (called in index-light)
412
+ Root._configure = function(Type_, parse_, common_) {
413
+ Type = Type_;
414
+ parse = parse_;
415
+ common = common_;
416
+ };