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