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/cli/pbjs.js CHANGED
@@ -1,329 +1,329 @@
1
- "use strict";
2
- var path = require("path"),
3
- fs = require("fs"),
4
- pkg = require("./package.json"),
5
- util = require("./util");
6
-
7
- util.setup();
8
-
9
- var protobuf = require(util.pathToProtobufJs),
10
- minimist = require("minimist"),
11
- chalk = require("chalk"),
12
- glob = require("glob");
13
-
14
- var targets = util.requireAll("./targets");
15
-
16
- /**
17
- * Runs pbjs programmatically.
18
- * @param {string[]} args Command line arguments
19
- * @param {function(?Error, string=)} [callback] Optional completion callback
20
- * @returns {number|undefined} Exit code, if known
21
- */
22
- exports.main = function main(args, callback) {
23
- var lintDefault = "eslint-disable " + [
24
- "block-scoped-var",
25
- "id-length",
26
- "no-control-regex",
27
- "no-magic-numbers",
28
- "no-prototype-builtins",
29
- "no-redeclare",
30
- "no-shadow",
31
- "no-var",
32
- "sort-vars"
33
- ].join(", ");
34
- var argv = minimist(args, {
35
- alias: {
36
- target: "t",
37
- out: "o",
38
- path: "p",
39
- wrap: "w",
40
- root: "r",
41
- lint: "l",
42
- // backward compatibility:
43
- "force-long": "strict-long",
44
- "force-message": "strict-message"
45
- },
46
- string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
47
- boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
48
- default: {
49
- target: "json",
50
- create: true,
51
- encode: true,
52
- decode: true,
53
- verify: true,
54
- convert: true,
55
- delimited: true,
56
- beautify: true,
57
- comments: true,
58
- es6: null,
59
- lint: lintDefault,
60
- "keep-case": false,
61
- "force-long": false,
62
- "force-number": false,
63
- "force-enum-string": false,
64
- "force-message": false
65
- }
66
- });
67
-
68
- var target = targets[argv.target],
69
- files = argv._,
70
- paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
71
-
72
- // alias hyphen args in camel case
73
- Object.keys(argv).forEach(function(key) {
74
- var camelKey = key.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
75
- if (camelKey !== key)
76
- argv[camelKey] = argv[key];
77
- });
78
-
79
- // protobuf.js package directory contains additional, otherwise non-bundled google types
80
- paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || ".");
81
-
82
- if (!files.length) {
83
- var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
84
- return " " + util.pad(key, 14, true) + targets[key].description;
85
- });
86
- if (callback)
87
- callback(Error("usage")); // eslint-disable-line callback-return
88
- else
89
- process.stderr.write([
90
- "protobuf.js v" + pkg.version + " CLI for JavaScript",
91
- "",
92
- chalk.bold.white("Translates between file formats and generates static code."),
93
- "",
94
- " -t, --target Specifies the target format. Also accepts a path to require a custom target.",
95
- "",
96
- descs.join("\n"),
97
- "",
98
- " -p, --path Adds a directory to the include path.",
99
- "",
100
- " -o, --out Saves to a file instead of writing to stdout.",
101
- "",
102
- " --sparse Exports only those types referenced from a main file (experimental).",
103
- "",
104
- chalk.bold.gray(" Module targets only:"),
105
- "",
106
- " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
107
- "",
108
- " default Default wrapper supporting both CommonJS and AMD",
109
- " commonjs CommonJS wrapper",
110
- " amd AMD wrapper",
111
- " es6 ES6 wrapper (implies --es6)",
112
- " closure A closure adding to protobuf.roots where protobuf is a global",
113
- "",
114
- " --dependency Specifies which version of protobuf to require. Accepts any valid module id",
115
- "",
116
- " -r, --root Specifies an alternative protobuf.roots name.",
117
- "",
118
- " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
119
- "",
120
- " " + lintDefault,
121
- "",
122
- " --es6 Enables ES6 syntax (const/let instead of var)",
123
- "",
124
- chalk.bold.gray(" Proto sources only:"),
125
- "",
126
- " --keep-case Keeps field casing instead of converting to camel case.",
127
- "",
128
- chalk.bold.gray(" Static targets only:"),
129
- "",
130
- " --no-create Does not generate create functions used for reflection compatibility.",
131
- " --no-encode Does not generate encode functions.",
132
- " --no-decode Does not generate decode functions.",
133
- " --no-verify Does not generate verify functions.",
134
- " --no-convert Does not generate convert functions like from/toObject",
135
- " --no-delimited Does not generate delimited encode/decode functions.",
136
- " --no-beautify Does not beautify generated code.",
137
- " --no-comments Does not output any JSDoc comments.",
138
- "",
139
- " --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
140
- " --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
141
- " --force-message Enfores the use of message instances instead of plain objects.",
142
- "",
143
- "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
144
- ""
145
- ].join("\n"));
146
- return 1;
147
- }
148
-
149
- if (typeof argv["strict-long"] === "boolean")
150
- argv["force-long"] = argv["strict-long"];
151
-
152
- // Resolve glob expressions
153
- for (var i = 0; i < files.length;) {
154
- if (glob.hasMagic(files[i])) {
155
- var matches = glob.sync(files[i]);
156
- Array.prototype.splice.apply(files, [i, 1].concat(matches));
157
- i += matches.length;
158
- } else
159
- ++i;
160
- }
161
-
162
- // Require custom target
163
- if (!target)
164
- target = require(path.resolve(process.cwd(), argv.target));
165
-
166
- var root = new protobuf.Root();
167
-
168
- var mainFiles = [];
169
-
170
- // Search include paths when resolving imports
171
- root.resolvePath = function pbjsResolvePath(origin, target) {
172
- var normOrigin = protobuf.util.path.normalize(origin),
173
- normTarget = protobuf.util.path.normalize(target);
174
- if (!normOrigin)
175
- mainFiles.push(normTarget);
176
-
177
- var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
178
- var idx = resolved.lastIndexOf("google/protobuf/");
179
- if (idx > -1) {
180
- var altname = resolved.substring(idx);
181
- if (altname in protobuf.common)
182
- resolved = altname;
183
- }
184
-
185
- if (fs.existsSync(resolved))
186
- return resolved;
187
-
188
- for (var i = 0; i < paths.length; ++i) {
189
- var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
190
- if (fs.existsSync(iresolved))
191
- return iresolved;
192
- }
193
-
194
- return resolved;
195
- };
196
-
197
- // Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
198
- if (argv.wrap === "es6" || argv.es6) {
199
- argv.wrap = "es6";
200
- argv.es6 = true;
201
- }
202
-
203
- var parseOptions = {
204
- "keepCase": argv["keep-case"] || false
205
- };
206
-
207
- // Read from stdin
208
- if (files.length === 1 && files[0] === "-") {
209
- var data = [];
210
- process.stdin.on("data", function(chunk) {
211
- data.push(chunk);
212
- });
213
- process.stdin.on("end", function() {
214
- var source = Buffer.concat(data).toString("utf8");
215
- try {
216
- if (source.charAt(0) !== "{") {
217
- protobuf.parse.filename = "-";
218
- protobuf.parse(source, root, parseOptions);
219
- } else {
220
- var json = JSON.parse(source);
221
- root.setOptions(json.options).addJSON(json);
222
- }
223
- callTarget();
224
- } catch (err) {
225
- if (callback) {
226
- callback(err);
227
- return;
228
- }
229
- throw err;
230
- }
231
- });
232
-
233
- // Load from disk
234
- } else {
235
- try {
236
- root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
237
- if (argv.sparse)
238
- sparsify(root);
239
- callTarget();
240
- } catch (err) {
241
- if (callback) {
242
- callback(err);
243
- return undefined;
244
- }
245
- throw err;
246
- }
247
- }
248
-
249
- function markReferenced(tobj) {
250
- tobj.referenced = true;
251
- // also mark a type's fields and oneofs
252
- if (tobj.fieldsArray)
253
- tobj.fieldsArray.forEach(function(fobj) {
254
- fobj.referenced = true;
255
- });
256
- if (tobj.oneofsArray)
257
- tobj.oneofsArray.forEach(function(oobj) {
258
- oobj.referenced = true;
259
- });
260
- // also mark an extension field's extended type, but not its (other) fields
261
- if (tobj.extensionField)
262
- tobj.extensionField.parent.referenced = true;
263
- }
264
-
265
- function sparsify(root) {
266
-
267
- // 1. mark directly or indirectly referenced objects
268
- util.traverse(root, function(obj) {
269
- if (!obj.filename)
270
- return;
271
- if (mainFiles.indexOf(obj.filename) > -1)
272
- util.traverseResolved(obj, markReferenced);
273
- });
274
-
275
- // 2. empty unreferenced objects
276
- util.traverse(root, function(obj) {
277
- var parent = obj.parent;
278
- if (!parent || obj.referenced) // root or referenced
279
- return;
280
- // remove unreferenced namespaces
281
- if (obj instanceof protobuf.Namespace) {
282
- var hasReferenced = false;
283
- util.traverse(obj, function(iobj) {
284
- if (iobj.referenced)
285
- hasReferenced = true;
286
- });
287
- if (hasReferenced) { // replace with plain namespace if a namespace subclass
288
- if (obj instanceof protobuf.Type || obj instanceof protobuf.Service) {
289
- var robj = new protobuf.Namespace(obj.name, obj.options);
290
- robj.nested = obj.nested;
291
- parent.add(robj);
292
- }
293
- } else // remove completely if nothing inside is referenced
294
- parent.remove(obj);
295
-
296
- // remove everything else unreferenced
297
- } else if (!(obj instanceof protobuf.Namespace))
298
- parent.remove(obj);
299
- });
300
-
301
- // 3. validate that everything is fine
302
- root.resolveAll();
303
- }
304
-
305
- function callTarget() {
306
- target(root, argv, function targetCallback(err, output) {
307
- if (err) {
308
- if (callback)
309
- return callback(err);
310
- throw err;
311
- }
312
- try {
313
- if (argv.out)
314
- fs.writeFileSync(argv.out, output, { encoding: "utf8" });
315
- else if (!callback)
316
- process.stdout.write(output, "utf8");
317
- return callback
318
- ? callback(null, output)
319
- : undefined;
320
- } catch (err) {
321
- if (callback)
322
- return callback(err);
323
- throw err;
324
- }
325
- });
326
- }
327
-
328
- return undefined;
329
- };
1
+ "use strict";
2
+ var path = require("path"),
3
+ fs = require("fs"),
4
+ pkg = require("./package.json"),
5
+ util = require("./util");
6
+
7
+ util.setup();
8
+
9
+ var protobuf = require(util.pathToProtobufJs),
10
+ minimist = require("minimist"),
11
+ chalk = require("chalk"),
12
+ glob = require("glob");
13
+
14
+ var targets = util.requireAll("./targets");
15
+
16
+ /**
17
+ * Runs pbjs programmatically.
18
+ * @param {string[]} args Command line arguments
19
+ * @param {function(?Error, string=)} [callback] Optional completion callback
20
+ * @returns {number|undefined} Exit code, if known
21
+ */
22
+ exports.main = function main(args, callback) {
23
+ var lintDefault = "eslint-disable " + [
24
+ "block-scoped-var",
25
+ "id-length",
26
+ "no-control-regex",
27
+ "no-magic-numbers",
28
+ "no-prototype-builtins",
29
+ "no-redeclare",
30
+ "no-shadow",
31
+ "no-var",
32
+ "sort-vars"
33
+ ].join(", ");
34
+ var argv = minimist(args, {
35
+ alias: {
36
+ target: "t",
37
+ out: "o",
38
+ path: "p",
39
+ wrap: "w",
40
+ root: "r",
41
+ lint: "l",
42
+ // backward compatibility:
43
+ "force-long": "strict-long",
44
+ "force-message": "strict-message"
45
+ },
46
+ string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
47
+ boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
48
+ default: {
49
+ target: "json",
50
+ create: true,
51
+ encode: true,
52
+ decode: true,
53
+ verify: true,
54
+ convert: true,
55
+ delimited: true,
56
+ beautify: true,
57
+ comments: true,
58
+ es6: null,
59
+ lint: lintDefault,
60
+ "keep-case": false,
61
+ "force-long": false,
62
+ "force-number": false,
63
+ "force-enum-string": false,
64
+ "force-message": false
65
+ }
66
+ });
67
+
68
+ var target = targets[argv.target],
69
+ files = argv._,
70
+ paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
71
+
72
+ // alias hyphen args in camel case
73
+ Object.keys(argv).forEach(function(key) {
74
+ var camelKey = key.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
75
+ if (camelKey !== key)
76
+ argv[camelKey] = argv[key];
77
+ });
78
+
79
+ // protobuf.js package directory contains additional, otherwise non-bundled google types
80
+ paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || ".");
81
+
82
+ if (!files.length) {
83
+ var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
84
+ return " " + util.pad(key, 14, true) + targets[key].description;
85
+ });
86
+ if (callback)
87
+ callback(Error("usage")); // eslint-disable-line callback-return
88
+ else
89
+ process.stderr.write([
90
+ "protobuf.js v" + pkg.version + " CLI for JavaScript",
91
+ "",
92
+ chalk.bold.white("Translates between file formats and generates static code."),
93
+ "",
94
+ " -t, --target Specifies the target format. Also accepts a path to require a custom target.",
95
+ "",
96
+ descs.join("\n"),
97
+ "",
98
+ " -p, --path Adds a directory to the include path.",
99
+ "",
100
+ " -o, --out Saves to a file instead of writing to stdout.",
101
+ "",
102
+ " --sparse Exports only those types referenced from a main file (experimental).",
103
+ "",
104
+ chalk.bold.gray(" Module targets only:"),
105
+ "",
106
+ " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
107
+ "",
108
+ " default Default wrapper supporting both CommonJS and AMD",
109
+ " commonjs CommonJS wrapper",
110
+ " amd AMD wrapper",
111
+ " es6 ES6 wrapper (implies --es6)",
112
+ " closure A closure adding to protobuf.roots where protobuf is a global",
113
+ "",
114
+ " --dependency Specifies which version of protobuf to require. Accepts any valid module id",
115
+ "",
116
+ " -r, --root Specifies an alternative protobuf.roots name.",
117
+ "",
118
+ " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
119
+ "",
120
+ " " + lintDefault,
121
+ "",
122
+ " --es6 Enables ES6 syntax (const/let instead of var)",
123
+ "",
124
+ chalk.bold.gray(" Proto sources only:"),
125
+ "",
126
+ " --keep-case Keeps field casing instead of converting to camel case.",
127
+ "",
128
+ chalk.bold.gray(" Static targets only:"),
129
+ "",
130
+ " --no-create Does not generate create functions used for reflection compatibility.",
131
+ " --no-encode Does not generate encode functions.",
132
+ " --no-decode Does not generate decode functions.",
133
+ " --no-verify Does not generate verify functions.",
134
+ " --no-convert Does not generate convert functions like from/toObject",
135
+ " --no-delimited Does not generate delimited encode/decode functions.",
136
+ " --no-beautify Does not beautify generated code.",
137
+ " --no-comments Does not output any JSDoc comments.",
138
+ "",
139
+ " --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
140
+ " --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
141
+ " --force-message Enfores the use of message instances instead of plain objects.",
142
+ "",
143
+ "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
144
+ ""
145
+ ].join("\n"));
146
+ return 1;
147
+ }
148
+
149
+ if (typeof argv["strict-long"] === "boolean")
150
+ argv["force-long"] = argv["strict-long"];
151
+
152
+ // Resolve glob expressions
153
+ for (var i = 0; i < files.length;) {
154
+ if (glob.hasMagic(files[i])) {
155
+ var matches = glob.sync(files[i]);
156
+ Array.prototype.splice.apply(files, [i, 1].concat(matches));
157
+ i += matches.length;
158
+ } else
159
+ ++i;
160
+ }
161
+
162
+ // Require custom target
163
+ if (!target)
164
+ target = require(path.resolve(process.cwd(), argv.target));
165
+
166
+ var root = new protobuf.Root();
167
+
168
+ var mainFiles = [];
169
+
170
+ // Search include paths when resolving imports
171
+ root.resolvePath = function pbjsResolvePath(origin, target) {
172
+ var normOrigin = protobuf.util.path.normalize(origin),
173
+ normTarget = protobuf.util.path.normalize(target);
174
+ if (!normOrigin)
175
+ mainFiles.push(normTarget);
176
+
177
+ var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
178
+ var idx = resolved.lastIndexOf("google/protobuf/");
179
+ if (idx > -1) {
180
+ var altname = resolved.substring(idx);
181
+ if (altname in protobuf.common)
182
+ resolved = altname;
183
+ }
184
+
185
+ if (fs.existsSync(resolved))
186
+ return resolved;
187
+
188
+ for (var i = 0; i < paths.length; ++i) {
189
+ var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
190
+ if (fs.existsSync(iresolved))
191
+ return iresolved;
192
+ }
193
+
194
+ return resolved;
195
+ };
196
+
197
+ // Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
198
+ if (argv.wrap === "es6" || argv.es6) {
199
+ argv.wrap = "es6";
200
+ argv.es6 = true;
201
+ }
202
+
203
+ var parseOptions = {
204
+ "keepCase": argv["keep-case"] || false
205
+ };
206
+
207
+ // Read from stdin
208
+ if (files.length === 1 && files[0] === "-") {
209
+ var data = [];
210
+ process.stdin.on("data", function(chunk) {
211
+ data.push(chunk);
212
+ });
213
+ process.stdin.on("end", function() {
214
+ var source = Buffer.concat(data).toString("utf8");
215
+ try {
216
+ if (source.charAt(0) !== "{") {
217
+ protobuf.parse.filename = "-";
218
+ protobuf.parse(source, root, parseOptions);
219
+ } else {
220
+ var json = JSON.parse(source);
221
+ root.setOptions(json.options).addJSON(json);
222
+ }
223
+ callTarget();
224
+ } catch (err) {
225
+ if (callback) {
226
+ callback(err);
227
+ return;
228
+ }
229
+ throw err;
230
+ }
231
+ });
232
+
233
+ // Load from disk
234
+ } else {
235
+ try {
236
+ root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
237
+ if (argv.sparse)
238
+ sparsify(root);
239
+ callTarget();
240
+ } catch (err) {
241
+ if (callback) {
242
+ callback(err);
243
+ return undefined;
244
+ }
245
+ throw err;
246
+ }
247
+ }
248
+
249
+ function markReferenced(tobj) {
250
+ tobj.referenced = true;
251
+ // also mark a type's fields and oneofs
252
+ if (tobj.fieldsArray)
253
+ tobj.fieldsArray.forEach(function(fobj) {
254
+ fobj.referenced = true;
255
+ });
256
+ if (tobj.oneofsArray)
257
+ tobj.oneofsArray.forEach(function(oobj) {
258
+ oobj.referenced = true;
259
+ });
260
+ // also mark an extension field's extended type, but not its (other) fields
261
+ if (tobj.extensionField)
262
+ tobj.extensionField.parent.referenced = true;
263
+ }
264
+
265
+ function sparsify(root) {
266
+
267
+ // 1. mark directly or indirectly referenced objects
268
+ util.traverse(root, function(obj) {
269
+ if (!obj.filename)
270
+ return;
271
+ if (mainFiles.indexOf(obj.filename) > -1)
272
+ util.traverseResolved(obj, markReferenced);
273
+ });
274
+
275
+ // 2. empty unreferenced objects
276
+ util.traverse(root, function(obj) {
277
+ var parent = obj.parent;
278
+ if (!parent || obj.referenced) // root or referenced
279
+ return;
280
+ // remove unreferenced namespaces
281
+ if (obj instanceof protobuf.Namespace) {
282
+ var hasReferenced = false;
283
+ util.traverse(obj, function(iobj) {
284
+ if (iobj.referenced)
285
+ hasReferenced = true;
286
+ });
287
+ if (hasReferenced) { // replace with plain namespace if a namespace subclass
288
+ if (obj instanceof protobuf.Type || obj instanceof protobuf.Service) {
289
+ var robj = new protobuf.Namespace(obj.name, obj.options);
290
+ robj.nested = obj.nested;
291
+ parent.add(robj);
292
+ }
293
+ } else // remove completely if nothing inside is referenced
294
+ parent.remove(obj);
295
+
296
+ // remove everything else unreferenced
297
+ } else if (!(obj instanceof protobuf.Namespace))
298
+ parent.remove(obj);
299
+ });
300
+
301
+ // 3. validate that everything is fine
302
+ root.resolveAll();
303
+ }
304
+
305
+ function callTarget() {
306
+ target(root, argv, function targetCallback(err, output) {
307
+ if (err) {
308
+ if (callback)
309
+ return callback(err);
310
+ throw err;
311
+ }
312
+ try {
313
+ if (argv.out)
314
+ fs.writeFileSync(argv.out, output, { encoding: "utf8" });
315
+ else if (!callback)
316
+ process.stdout.write(output, "utf8");
317
+ return callback
318
+ ? callback(null, output)
319
+ : undefined;
320
+ } catch (err) {
321
+ if (callback)
322
+ return callback(err);
323
+ throw err;
324
+ }
325
+ });
326
+ }
327
+
328
+ return undefined;
329
+ };