protobufjs 6.8.5 → 6.8.9

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