protobufjs 6.11.2 → 7.1.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 (259) hide show
  1. package/README.md +7 -174
  2. package/dist/light/protobuf.js +77 -29
  3. package/dist/light/protobuf.js.map +1 -1
  4. package/dist/light/protobuf.min.js +3 -3
  5. package/dist/light/protobuf.min.js.map +1 -1
  6. package/dist/minimal/protobuf.js +27 -10
  7. package/dist/minimal/protobuf.js.map +1 -1
  8. package/dist/minimal/protobuf.min.js +3 -3
  9. package/dist/minimal/protobuf.min.js.map +1 -1
  10. package/dist/protobuf.js +163 -61
  11. package/dist/protobuf.js.map +1 -1
  12. package/dist/protobuf.min.js +3 -3
  13. package/dist/protobuf.min.js.map +1 -1
  14. package/index.d.ts +9 -7
  15. package/package.json +14 -33
  16. package/scripts/postinstall.js +0 -3
  17. package/src/converter.js +10 -4
  18. package/src/decoder.js +4 -3
  19. package/src/enum.js +24 -7
  20. package/src/field.js +6 -3
  21. package/src/namespace.js +3 -3
  22. package/src/parse.js +55 -16
  23. package/src/roots.js +1 -1
  24. package/src/tokenize.js +31 -16
  25. package/src/util/minimal.js +24 -7
  26. package/src/util.js +3 -0
  27. package/src/wrappers.js +2 -2
  28. package/tsconfig.json +2 -1
  29. package/CHANGELOG.md +0 -1019
  30. package/bin/pbjs +0 -6
  31. package/bin/pbts +0 -6
  32. package/cli/LICENSE +0 -33
  33. package/cli/README.md +0 -174
  34. package/cli/bin/pbjs +0 -6
  35. package/cli/bin/pbts +0 -6
  36. package/cli/index.d.ts +0 -3
  37. package/cli/index.js +0 -3
  38. package/cli/lib/tsd-jsdoc/LICENSE +0 -21
  39. package/cli/lib/tsd-jsdoc/README.md +0 -23
  40. package/cli/lib/tsd-jsdoc/plugin.js +0 -21
  41. package/cli/lib/tsd-jsdoc/publish.js +0 -705
  42. package/cli/lib/tsd-jsdoc.json +0 -18
  43. package/cli/node_modules/.package-lock.json +0 -256
  44. package/cli/node_modules/acorn/CHANGELOG.md +0 -620
  45. package/cli/node_modules/acorn/LICENSE +0 -21
  46. package/cli/node_modules/acorn/README.md +0 -269
  47. package/cli/node_modules/acorn/bin/acorn +0 -4
  48. package/cli/node_modules/acorn/dist/acorn.d.ts +0 -209
  49. package/cli/node_modules/acorn/dist/acorn.js +0 -5186
  50. package/cli/node_modules/acorn/dist/acorn.js.map +0 -1
  51. package/cli/node_modules/acorn/dist/acorn.mjs +0 -5155
  52. package/cli/node_modules/acorn/dist/acorn.mjs.d.ts +0 -2
  53. package/cli/node_modules/acorn/dist/acorn.mjs.map +0 -1
  54. package/cli/node_modules/acorn/dist/bin.js +0 -64
  55. package/cli/node_modules/acorn/package.json +0 -35
  56. package/cli/node_modules/acorn-jsx/LICENSE +0 -19
  57. package/cli/node_modules/acorn-jsx/README.md +0 -40
  58. package/cli/node_modules/acorn-jsx/index.js +0 -488
  59. package/cli/node_modules/acorn-jsx/package.json +0 -27
  60. package/cli/node_modules/acorn-jsx/xhtml.js +0 -255
  61. package/cli/node_modules/balanced-match/.npmignore +0 -5
  62. package/cli/node_modules/balanced-match/LICENSE.md +0 -21
  63. package/cli/node_modules/balanced-match/README.md +0 -91
  64. package/cli/node_modules/balanced-match/index.js +0 -59
  65. package/cli/node_modules/balanced-match/package.json +0 -49
  66. package/cli/node_modules/brace-expansion/LICENSE +0 -21
  67. package/cli/node_modules/brace-expansion/README.md +0 -129
  68. package/cli/node_modules/brace-expansion/index.js +0 -201
  69. package/cli/node_modules/brace-expansion/package.json +0 -47
  70. package/cli/node_modules/concat-map/.travis.yml +0 -4
  71. package/cli/node_modules/concat-map/LICENSE +0 -18
  72. package/cli/node_modules/concat-map/README.markdown +0 -62
  73. package/cli/node_modules/concat-map/example/map.js +0 -6
  74. package/cli/node_modules/concat-map/index.js +0 -13
  75. package/cli/node_modules/concat-map/package.json +0 -43
  76. package/cli/node_modules/concat-map/test/map.js +0 -39
  77. package/cli/node_modules/deep-is/.npmignore +0 -1
  78. package/cli/node_modules/deep-is/.travis.yml +0 -6
  79. package/cli/node_modules/deep-is/LICENSE +0 -22
  80. package/cli/node_modules/deep-is/README.markdown +0 -70
  81. package/cli/node_modules/deep-is/example/cmp.js +0 -11
  82. package/cli/node_modules/deep-is/index.js +0 -102
  83. package/cli/node_modules/deep-is/package.json +0 -61
  84. package/cli/node_modules/deep-is/test/NaN.js +0 -16
  85. package/cli/node_modules/deep-is/test/cmp.js +0 -23
  86. package/cli/node_modules/deep-is/test/neg-vs-pos-0.js +0 -15
  87. package/cli/node_modules/escodegen/LICENSE.BSD +0 -21
  88. package/cli/node_modules/escodegen/README.md +0 -84
  89. package/cli/node_modules/escodegen/bin/escodegen.js +0 -77
  90. package/cli/node_modules/escodegen/bin/esgenerate.js +0 -64
  91. package/cli/node_modules/escodegen/escodegen.js +0 -2647
  92. package/cli/node_modules/escodegen/package.json +0 -62
  93. package/cli/node_modules/eslint-visitor-keys/CHANGELOG.md +0 -25
  94. package/cli/node_modules/eslint-visitor-keys/LICENSE +0 -201
  95. package/cli/node_modules/eslint-visitor-keys/README.md +0 -98
  96. package/cli/node_modules/eslint-visitor-keys/lib/index.js +0 -81
  97. package/cli/node_modules/eslint-visitor-keys/lib/visitor-keys.json +0 -284
  98. package/cli/node_modules/eslint-visitor-keys/package.json +0 -40
  99. package/cli/node_modules/espree/CHANGELOG.md +0 -509
  100. package/cli/node_modules/espree/LICENSE +0 -25
  101. package/cli/node_modules/espree/README.md +0 -233
  102. package/cli/node_modules/espree/espree.js +0 -177
  103. package/cli/node_modules/espree/lib/ast-node-types.js +0 -96
  104. package/cli/node_modules/espree/lib/espree.js +0 -286
  105. package/cli/node_modules/espree/lib/features.js +0 -29
  106. package/cli/node_modules/espree/lib/options.js +0 -106
  107. package/cli/node_modules/espree/lib/token-translator.js +0 -263
  108. package/cli/node_modules/espree/lib/visitor-keys.js +0 -123
  109. package/cli/node_modules/espree/package.json +0 -64
  110. package/cli/node_modules/esprima/ChangeLog +0 -235
  111. package/cli/node_modules/esprima/LICENSE.BSD +0 -21
  112. package/cli/node_modules/esprima/README.md +0 -46
  113. package/cli/node_modules/esprima/bin/esparse.js +0 -139
  114. package/cli/node_modules/esprima/bin/esvalidate.js +0 -236
  115. package/cli/node_modules/esprima/dist/esprima.js +0 -6709
  116. package/cli/node_modules/esprima/package.json +0 -112
  117. package/cli/node_modules/estraverse/.jshintrc +0 -16
  118. package/cli/node_modules/estraverse/LICENSE.BSD +0 -19
  119. package/cli/node_modules/estraverse/README.md +0 -153
  120. package/cli/node_modules/estraverse/estraverse.js +0 -801
  121. package/cli/node_modules/estraverse/gulpfile.js +0 -70
  122. package/cli/node_modules/estraverse/package.json +0 -40
  123. package/cli/node_modules/esutils/LICENSE.BSD +0 -19
  124. package/cli/node_modules/esutils/README.md +0 -174
  125. package/cli/node_modules/esutils/lib/ast.js +0 -144
  126. package/cli/node_modules/esutils/lib/code.js +0 -135
  127. package/cli/node_modules/esutils/lib/keyword.js +0 -165
  128. package/cli/node_modules/esutils/lib/utils.js +0 -33
  129. package/cli/node_modules/esutils/package.json +0 -44
  130. package/cli/node_modules/fast-levenshtein/LICENSE.md +0 -25
  131. package/cli/node_modules/fast-levenshtein/README.md +0 -104
  132. package/cli/node_modules/fast-levenshtein/levenshtein.js +0 -136
  133. package/cli/node_modules/fast-levenshtein/package.json +0 -39
  134. package/cli/node_modules/fs.realpath/LICENSE +0 -43
  135. package/cli/node_modules/fs.realpath/README.md +0 -33
  136. package/cli/node_modules/fs.realpath/index.js +0 -66
  137. package/cli/node_modules/fs.realpath/old.js +0 -303
  138. package/cli/node_modules/fs.realpath/package.json +0 -26
  139. package/cli/node_modules/glob/LICENSE +0 -21
  140. package/cli/node_modules/glob/README.md +0 -375
  141. package/cli/node_modules/glob/changelog.md +0 -67
  142. package/cli/node_modules/glob/common.js +0 -240
  143. package/cli/node_modules/glob/glob.js +0 -790
  144. package/cli/node_modules/glob/package.json +0 -46
  145. package/cli/node_modules/glob/sync.js +0 -486
  146. package/cli/node_modules/inflight/LICENSE +0 -15
  147. package/cli/node_modules/inflight/README.md +0 -37
  148. package/cli/node_modules/inflight/inflight.js +0 -54
  149. package/cli/node_modules/inflight/package.json +0 -29
  150. package/cli/node_modules/inherits/LICENSE +0 -16
  151. package/cli/node_modules/inherits/README.md +0 -42
  152. package/cli/node_modules/inherits/inherits.js +0 -9
  153. package/cli/node_modules/inherits/inherits_browser.js +0 -27
  154. package/cli/node_modules/inherits/package.json +0 -29
  155. package/cli/node_modules/levn/LICENSE +0 -22
  156. package/cli/node_modules/levn/README.md +0 -196
  157. package/cli/node_modules/levn/lib/cast.js +0 -298
  158. package/cli/node_modules/levn/lib/coerce.js +0 -285
  159. package/cli/node_modules/levn/lib/index.js +0 -22
  160. package/cli/node_modules/levn/lib/parse-string.js +0 -113
  161. package/cli/node_modules/levn/lib/parse.js +0 -102
  162. package/cli/node_modules/levn/package.json +0 -47
  163. package/cli/node_modules/minimatch/LICENSE +0 -15
  164. package/cli/node_modules/minimatch/README.md +0 -209
  165. package/cli/node_modules/minimatch/minimatch.js +0 -923
  166. package/cli/node_modules/minimatch/package.json +0 -30
  167. package/cli/node_modules/once/LICENSE +0 -15
  168. package/cli/node_modules/once/README.md +0 -79
  169. package/cli/node_modules/once/once.js +0 -42
  170. package/cli/node_modules/once/package.json +0 -33
  171. package/cli/node_modules/optionator/CHANGELOG.md +0 -56
  172. package/cli/node_modules/optionator/LICENSE +0 -22
  173. package/cli/node_modules/optionator/README.md +0 -238
  174. package/cli/node_modules/optionator/lib/help.js +0 -260
  175. package/cli/node_modules/optionator/lib/index.js +0 -465
  176. package/cli/node_modules/optionator/lib/util.js +0 -54
  177. package/cli/node_modules/optionator/package.json +0 -44
  178. package/cli/node_modules/path-is-absolute/index.js +0 -20
  179. package/cli/node_modules/path-is-absolute/license +0 -21
  180. package/cli/node_modules/path-is-absolute/package.json +0 -43
  181. package/cli/node_modules/path-is-absolute/readme.md +0 -59
  182. package/cli/node_modules/prelude-ls/CHANGELOG.md +0 -99
  183. package/cli/node_modules/prelude-ls/LICENSE +0 -22
  184. package/cli/node_modules/prelude-ls/README.md +0 -15
  185. package/cli/node_modules/prelude-ls/lib/Func.js +0 -65
  186. package/cli/node_modules/prelude-ls/lib/List.js +0 -686
  187. package/cli/node_modules/prelude-ls/lib/Num.js +0 -130
  188. package/cli/node_modules/prelude-ls/lib/Obj.js +0 -154
  189. package/cli/node_modules/prelude-ls/lib/Str.js +0 -92
  190. package/cli/node_modules/prelude-ls/lib/index.js +0 -178
  191. package/cli/node_modules/prelude-ls/package.json +0 -52
  192. package/cli/node_modules/rimraf/CHANGELOG.md +0 -65
  193. package/cli/node_modules/rimraf/LICENSE +0 -15
  194. package/cli/node_modules/rimraf/README.md +0 -101
  195. package/cli/node_modules/rimraf/bin.js +0 -68
  196. package/cli/node_modules/rimraf/package.json +0 -32
  197. package/cli/node_modules/rimraf/rimraf.js +0 -360
  198. package/cli/node_modules/source-map/CHANGELOG.md +0 -301
  199. package/cli/node_modules/source-map/LICENSE +0 -28
  200. package/cli/node_modules/source-map/README.md +0 -742
  201. package/cli/node_modules/source-map/dist/source-map.debug.js +0 -3234
  202. package/cli/node_modules/source-map/dist/source-map.js +0 -3233
  203. package/cli/node_modules/source-map/dist/source-map.min.js +0 -2
  204. package/cli/node_modules/source-map/dist/source-map.min.js.map +0 -1
  205. package/cli/node_modules/source-map/lib/array-set.js +0 -121
  206. package/cli/node_modules/source-map/lib/base64-vlq.js +0 -140
  207. package/cli/node_modules/source-map/lib/base64.js +0 -67
  208. package/cli/node_modules/source-map/lib/binary-search.js +0 -111
  209. package/cli/node_modules/source-map/lib/mapping-list.js +0 -79
  210. package/cli/node_modules/source-map/lib/quick-sort.js +0 -114
  211. package/cli/node_modules/source-map/lib/source-map-consumer.js +0 -1145
  212. package/cli/node_modules/source-map/lib/source-map-generator.js +0 -425
  213. package/cli/node_modules/source-map/lib/source-node.js +0 -413
  214. package/cli/node_modules/source-map/lib/util.js +0 -488
  215. package/cli/node_modules/source-map/package.json +0 -73
  216. package/cli/node_modules/source-map/source-map.d.ts +0 -98
  217. package/cli/node_modules/source-map/source-map.js +0 -8
  218. package/cli/node_modules/tmp/CHANGELOG.md +0 -288
  219. package/cli/node_modules/tmp/LICENSE +0 -21
  220. package/cli/node_modules/tmp/README.md +0 -365
  221. package/cli/node_modules/tmp/lib/tmp.js +0 -780
  222. package/cli/node_modules/tmp/package.json +0 -58
  223. package/cli/node_modules/type-check/LICENSE +0 -22
  224. package/cli/node_modules/type-check/README.md +0 -210
  225. package/cli/node_modules/type-check/lib/check.js +0 -126
  226. package/cli/node_modules/type-check/lib/index.js +0 -16
  227. package/cli/node_modules/type-check/lib/parse-type.js +0 -196
  228. package/cli/node_modules/type-check/package.json +0 -40
  229. package/cli/node_modules/word-wrap/LICENSE +0 -21
  230. package/cli/node_modules/word-wrap/README.md +0 -182
  231. package/cli/node_modules/word-wrap/index.d.ts +0 -50
  232. package/cli/node_modules/word-wrap/index.js +0 -46
  233. package/cli/node_modules/word-wrap/package.json +0 -77
  234. package/cli/node_modules/wrappy/LICENSE +0 -15
  235. package/cli/node_modules/wrappy/README.md +0 -36
  236. package/cli/node_modules/wrappy/package.json +0 -29
  237. package/cli/node_modules/wrappy/wrappy.js +0 -33
  238. package/cli/package.json +0 -8
  239. package/cli/package.standalone.json +0 -32
  240. package/cli/pbjs.d.ts +0 -9
  241. package/cli/pbjs.js +0 -330
  242. package/cli/pbts.d.ts +0 -9
  243. package/cli/pbts.js +0 -197
  244. package/cli/targets/json-module.js +0 -38
  245. package/cli/targets/json.js +0 -8
  246. package/cli/targets/proto.js +0 -326
  247. package/cli/targets/proto2.js +0 -10
  248. package/cli/targets/proto3.js +0 -10
  249. package/cli/targets/static-module.js +0 -29
  250. package/cli/targets/static.js +0 -711
  251. package/cli/util.js +0 -183
  252. package/cli/wrappers/amd.js +0 -7
  253. package/cli/wrappers/closure.js +0 -7
  254. package/cli/wrappers/commonjs.js +0 -7
  255. package/cli/wrappers/default.js +0 -15
  256. package/cli/wrappers/es6.js +0 -5
  257. package/dist/README.md +0 -31
  258. package/dist/light/README.md +0 -31
  259. package/dist/minimal/README.md +0 -31
package/cli/pbjs.js DELETED
@@ -1,330 +0,0 @@
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", "service", "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
- service: true,
59
- es6: null,
60
- lint: lintDefault,
61
- "keep-case": false,
62
- "force-long": false,
63
- "force-number": false,
64
- "force-enum-string": false,
65
- "force-message": false
66
- }
67
- });
68
-
69
- var target = targets[argv.target],
70
- files = argv._,
71
- paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
72
-
73
- // alias hyphen args in camel case
74
- Object.keys(argv).forEach(function(key) {
75
- var camelKey = key.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
76
- if (camelKey !== key)
77
- argv[camelKey] = argv[key];
78
- });
79
-
80
- // protobuf.js package directory contains additional, otherwise non-bundled google types
81
- paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || ".");
82
-
83
- if (!files.length) {
84
- var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
85
- return " " + util.pad(key, 14, true) + targets[key].description;
86
- });
87
- if (callback)
88
- callback(Error("usage")); // eslint-disable-line callback-return
89
- else
90
- process.stderr.write([
91
- "protobuf.js v" + pkg.version + " CLI for JavaScript",
92
- "",
93
- chalk.bold.white("Translates between file formats and generates static code."),
94
- "",
95
- " -t, --target Specifies the target format. Also accepts a path to require a custom target.",
96
- "",
97
- descs.join("\n"),
98
- "",
99
- " -p, --path Adds a directory to the include path.",
100
- "",
101
- " -o, --out Saves to a file instead of writing to stdout.",
102
- "",
103
- " --sparse Exports only those types referenced from a main file (experimental).",
104
- "",
105
- chalk.bold.gray(" Module targets only:"),
106
- "",
107
- " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
108
- "",
109
- " default Default wrapper supporting both CommonJS and AMD",
110
- " commonjs CommonJS wrapper",
111
- " amd AMD wrapper",
112
- " es6 ES6 wrapper (implies --es6)",
113
- " closure A closure adding to protobuf.roots where protobuf is a global",
114
- "",
115
- " --dependency Specifies which version of protobuf to require. Accepts any valid module id",
116
- "",
117
- " -r, --root Specifies an alternative protobuf.roots name.",
118
- "",
119
- " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
120
- "",
121
- " " + lintDefault,
122
- "",
123
- " --es6 Enables ES6 syntax (const/let instead of var)",
124
- "",
125
- chalk.bold.gray(" Proto sources only:"),
126
- "",
127
- " --keep-case Keeps field casing instead of converting to camel case.",
128
- "",
129
- chalk.bold.gray(" Static targets only:"),
130
- "",
131
- " --no-create Does not generate create functions used for reflection compatibility.",
132
- " --no-encode Does not generate encode functions.",
133
- " --no-decode Does not generate decode functions.",
134
- " --no-verify Does not generate verify functions.",
135
- " --no-convert Does not generate convert functions like from/toObject",
136
- " --no-delimited Does not generate delimited encode/decode functions.",
137
- " --no-beautify Does not beautify generated code.",
138
- " --no-comments Does not output any JSDoc comments.",
139
- " --no-service Does not output service classes.",
140
- "",
141
- " --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
142
- " --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
143
- " --force-message Enfores the use of message instances instead of plain objects.",
144
- "",
145
- "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
146
- ""
147
- ].join("\n"));
148
- return 1;
149
- }
150
-
151
- if (typeof argv["strict-long"] === "boolean")
152
- argv["force-long"] = argv["strict-long"];
153
-
154
- // Resolve glob expressions
155
- for (var i = 0; i < files.length;) {
156
- if (glob.hasMagic(files[i])) {
157
- var matches = glob.sync(files[i]);
158
- Array.prototype.splice.apply(files, [i, 1].concat(matches));
159
- i += matches.length;
160
- } else
161
- ++i;
162
- }
163
-
164
- // Require custom target
165
- if (!target)
166
- target = require(path.resolve(process.cwd(), argv.target));
167
-
168
- var root = new protobuf.Root();
169
-
170
- var mainFiles = [];
171
-
172
- // Search include paths when resolving imports
173
- root.resolvePath = function pbjsResolvePath(origin, target) {
174
- var normOrigin = protobuf.util.path.normalize(origin),
175
- normTarget = protobuf.util.path.normalize(target);
176
- if (!normOrigin)
177
- mainFiles.push(normTarget);
178
-
179
- var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
180
- var idx = resolved.lastIndexOf("google/protobuf/");
181
- if (idx > -1) {
182
- var altname = resolved.substring(idx);
183
- if (altname in protobuf.common)
184
- resolved = altname;
185
- }
186
-
187
- if (fs.existsSync(resolved))
188
- return resolved;
189
-
190
- for (var i = 0; i < paths.length; ++i) {
191
- var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
192
- if (fs.existsSync(iresolved))
193
- return iresolved;
194
- }
195
-
196
- return resolved;
197
- };
198
-
199
- // `--wrap es6` implies `--es6` but not the other way around. You can still use e.g. `--es6 --wrap commonjs`
200
- if (argv.wrap === "es6") {
201
- argv.es6 = true;
202
- }
203
-
204
- var parseOptions = {
205
- "keepCase": argv["keep-case"] || false
206
- };
207
-
208
- // Read from stdin
209
- if (files.length === 1 && files[0] === "-") {
210
- var data = [];
211
- process.stdin.on("data", function(chunk) {
212
- data.push(chunk);
213
- });
214
- process.stdin.on("end", function() {
215
- var source = Buffer.concat(data).toString("utf8");
216
- try {
217
- if (source.charAt(0) !== "{") {
218
- protobuf.parse.filename = "-";
219
- protobuf.parse(source, root, parseOptions);
220
- } else {
221
- var json = JSON.parse(source);
222
- root.setOptions(json.options).addJSON(json);
223
- }
224
- callTarget();
225
- } catch (err) {
226
- if (callback) {
227
- callback(err);
228
- return;
229
- }
230
- throw err;
231
- }
232
- });
233
-
234
- // Load from disk
235
- } else {
236
- try {
237
- root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
238
- if (argv.sparse)
239
- sparsify(root);
240
- callTarget();
241
- } catch (err) {
242
- if (callback) {
243
- callback(err);
244
- return undefined;
245
- }
246
- throw err;
247
- }
248
- }
249
-
250
- function markReferenced(tobj) {
251
- tobj.referenced = true;
252
- // also mark a type's fields and oneofs
253
- if (tobj.fieldsArray)
254
- tobj.fieldsArray.forEach(function(fobj) {
255
- fobj.referenced = true;
256
- });
257
- if (tobj.oneofsArray)
258
- tobj.oneofsArray.forEach(function(oobj) {
259
- oobj.referenced = true;
260
- });
261
- // also mark an extension field's extended type, but not its (other) fields
262
- if (tobj.extensionField)
263
- tobj.extensionField.parent.referenced = true;
264
- }
265
-
266
- function sparsify(root) {
267
-
268
- // 1. mark directly or indirectly referenced objects
269
- util.traverse(root, function(obj) {
270
- if (!obj.filename)
271
- return;
272
- if (mainFiles.indexOf(obj.filename) > -1)
273
- util.traverseResolved(obj, markReferenced);
274
- });
275
-
276
- // 2. empty unreferenced objects
277
- util.traverse(root, function(obj) {
278
- var parent = obj.parent;
279
- if (!parent || obj.referenced) // root or referenced
280
- return;
281
- // remove unreferenced namespaces
282
- if (obj instanceof protobuf.Namespace) {
283
- var hasReferenced = false;
284
- util.traverse(obj, function(iobj) {
285
- if (iobj.referenced)
286
- hasReferenced = true;
287
- });
288
- if (hasReferenced) { // replace with plain namespace if a namespace subclass
289
- if (obj instanceof protobuf.Type || obj instanceof protobuf.Service) {
290
- var robj = new protobuf.Namespace(obj.name, obj.options);
291
- robj.nested = obj.nested;
292
- parent.add(robj);
293
- }
294
- } else // remove completely if nothing inside is referenced
295
- parent.remove(obj);
296
-
297
- // remove everything else unreferenced
298
- } else if (!(obj instanceof protobuf.Namespace))
299
- parent.remove(obj);
300
- });
301
-
302
- // 3. validate that everything is fine
303
- root.resolveAll();
304
- }
305
-
306
- function callTarget() {
307
- target(root, argv, function targetCallback(err, output) {
308
- if (err) {
309
- if (callback)
310
- return callback(err);
311
- throw err;
312
- }
313
- try {
314
- if (argv.out)
315
- fs.writeFileSync(argv.out, output, { encoding: "utf8" });
316
- else if (!callback)
317
- process.stdout.write(output, "utf8");
318
- return callback
319
- ? callback(null, output)
320
- : undefined;
321
- } catch (err) {
322
- if (callback)
323
- return callback(err);
324
- throw err;
325
- }
326
- });
327
- }
328
-
329
- return undefined;
330
- };
package/cli/pbts.d.ts DELETED
@@ -1,9 +0,0 @@
1
- type pbtsCallback = (err: Error|null, output?: string) => void;
2
-
3
- /**
4
- * Runs pbts programmatically.
5
- * @param {string[]} args Command line arguments
6
- * @param {function(?Error, string=)} [callback] Optional completion callback
7
- * @returns {number|undefined} Exit code, if known
8
- */
9
- export function main(args: string[], callback?: pbtsCallback): number|undefined;
package/cli/pbts.js DELETED
@@ -1,197 +0,0 @@
1
- "use strict";
2
- var child_process = require("child_process"),
3
- path = require("path"),
4
- fs = require("fs"),
5
- pkg = require("./package.json"),
6
- util = require("./util");
7
-
8
- util.setup();
9
-
10
- var minimist = require("minimist"),
11
- chalk = require("chalk"),
12
- glob = require("glob"),
13
- tmp = require("tmp");
14
-
15
- /**
16
- * Runs pbts programmatically.
17
- * @param {string[]} args Command line arguments
18
- * @param {function(?Error, string=)} [callback] Optional completion callback
19
- * @returns {number|undefined} Exit code, if known
20
- */
21
- exports.main = function(args, callback) {
22
- var argv = minimist(args, {
23
- alias: {
24
- name: "n",
25
- out : "o",
26
- main: "m",
27
- global: "g",
28
- import: "i"
29
- },
30
- string: [ "name", "out", "global", "import" ],
31
- boolean: [ "comments", "main" ],
32
- default: {
33
- comments: true,
34
- main: false
35
- }
36
- });
37
-
38
- var files = argv._;
39
-
40
- if (!files.length) {
41
- if (callback)
42
- callback(Error("usage")); // eslint-disable-line callback-return
43
- else
44
- process.stderr.write([
45
- "protobuf.js v" + pkg.version + " CLI for TypeScript",
46
- "",
47
- chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
48
- "",
49
- " -o, --out Saves to a file instead of writing to stdout.",
50
- "",
51
- " -g, --global Name of the global object in browser environments, if any.",
52
- "",
53
- " -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
54
- "",
55
- " --no-comments Does not output any JSDoc comments.",
56
- "",
57
- chalk.bold.gray(" Internal flags:"),
58
- "",
59
- " -n, --name Wraps everything in a module of the specified name.",
60
- "",
61
- " -m, --main Whether building the main library without any imports.",
62
- "",
63
- "usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -",
64
- ""
65
- ].join("\n"));
66
- return 1;
67
- }
68
-
69
- // Resolve glob expressions
70
- for (var i = 0; i < files.length;) {
71
- if (glob.hasMagic(files[i])) {
72
- var matches = glob.sync(files[i]);
73
- Array.prototype.splice.apply(files, [i, 1].concat(matches));
74
- i += matches.length;
75
- } else
76
- ++i;
77
- }
78
-
79
- var cleanup = [];
80
-
81
- // Read from stdin (to a temporary file)
82
- if (files.length === 1 && files[0] === "-") {
83
- var data = [];
84
- process.stdin.on("data", function(chunk) {
85
- data.push(chunk);
86
- });
87
- process.stdin.on("end", function() {
88
- files[0] = tmp.tmpNameSync() + ".js";
89
- fs.writeFileSync(files[0], Buffer.concat(data));
90
- cleanup.push(files[0]);
91
- callJsdoc();
92
- });
93
-
94
- // Load from disk
95
- } else {
96
- callJsdoc();
97
- }
98
-
99
- function callJsdoc() {
100
-
101
- // There is no proper API for jsdoc, so this executes the CLI and pipes the output
102
- var basedir = path.join(__dirname, ".");
103
- var moduleName = argv.name || "null";
104
- var nodePath = process.execPath;
105
- var cmd = "\"" + nodePath + "\" \"" + require.resolve("jsdoc/jsdoc.js") + "\" -c \"" + path.join(basedir, "lib", "tsd-jsdoc.json") + "\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return "\"" + file + "\""; }).join(" ");
106
- var child = child_process.exec(cmd, {
107
- cwd: process.cwd(),
108
- argv0: "node",
109
- stdio: "pipe",
110
- maxBuffer: 1 << 24 // 16mb
111
- });
112
- var out = [];
113
- var ended = false;
114
- var closed = false;
115
- child.stdout.on("data", function(data) {
116
- out.push(data);
117
- });
118
- child.stdout.on("end", function() {
119
- if (closed) finish();
120
- else ended = true;
121
- });
122
- child.stderr.pipe(process.stderr);
123
- child.on("close", function(code) {
124
- // clean up temporary files, no matter what
125
- try { cleanup.forEach(fs.unlinkSync); } catch(e) {/**/} cleanup = [];
126
-
127
- if (code) {
128
- out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
129
- process.stderr.write(out);
130
- var err = Error("code " + code);
131
- if (callback)
132
- return callback(err);
133
- throw err;
134
- }
135
-
136
- if (ended) return finish();
137
- closed = true;
138
- return undefined;
139
- });
140
-
141
- function getImportName(importItem) {
142
- return path.basename(importItem, ".js").replace(/([-_~.+]\w)/g, function(match) {
143
- return match[1].toUpperCase();
144
- });
145
- }
146
-
147
- function finish() {
148
- var output = [];
149
- if (argv.main)
150
- output.push(
151
- "// DO NOT EDIT! This is a generated file. Edit the JSDoc in src/*.js instead and run 'npm run types'.",
152
- ""
153
- );
154
- if (argv.global)
155
- output.push(
156
- "export as namespace " + argv.global + ";",
157
- ""
158
- );
159
-
160
- if (!argv.main) {
161
- // Ensure we have a usable array of imports
162
- var importArray = typeof argv.import === "string" ? argv.import.split(",") : argv.import || [];
163
-
164
- // Build an object of imports and paths
165
- var imports = {
166
- $protobuf: "protobufjs"
167
- };
168
- importArray.forEach(function(importItem) {
169
- imports[getImportName(importItem)] = importItem;
170
- });
171
-
172
- // Write out the imports
173
- Object.keys(imports).forEach(function(key) {
174
- output.push("import * as " + key + " from \"" + imports[key] + "\";");
175
- });
176
- }
177
-
178
- output = output.join("\n") + "\n" + out.join("");
179
-
180
- try {
181
- if (argv.out)
182
- fs.writeFileSync(argv.out, output, { encoding: "utf8" });
183
- else if (!callback)
184
- process.stdout.write(output, "utf8");
185
- return callback
186
- ? callback(null, output)
187
- : undefined;
188
- } catch (err) {
189
- if (callback)
190
- return callback(err);
191
- throw err;
192
- }
193
- }
194
- }
195
-
196
- return undefined;
197
- };
@@ -1,38 +0,0 @@
1
- "use strict";
2
- module.exports = json_module;
3
-
4
- var util = require("../util");
5
-
6
- var protobuf = require("../..");
7
-
8
- json_module.description = "JSON representation as a module";
9
-
10
- function jsonSafeProp(json) {
11
- return json.replace(/^( +)"(\w+)":/mg, function($0, $1, $2) {
12
- return protobuf.util.safeProp($2).charAt(0) === "."
13
- ? $1 + $2 + ":"
14
- : $0;
15
- });
16
- }
17
-
18
- function json_module(root, options, callback) {
19
- try {
20
- var rootProp = protobuf.util.safeProp(options.root || "default");
21
- var output = [
22
- (options.es6 ? "const" : "var") + " $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
23
- ];
24
- if (root.options) {
25
- var optionsJson = jsonSafeProp(JSON.stringify(root.options, null, 2));
26
- output.push(".setOptions(" + optionsJson + ")\n");
27
- }
28
- var json = jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
29
- output.push(".addJSON(" + json + ");");
30
- output = util.wrap(output.join(""), protobuf.util.merge({ dependency: "protobufjs/light" }, options));
31
- process.nextTick(function() {
32
- callback(null, output);
33
- });
34
- } catch (e) {
35
- return callback(e);
36
- }
37
- return undefined;
38
- }
@@ -1,8 +0,0 @@
1
- "use strict";
2
- module.exports = json_target;
3
-
4
- json_target.description = "JSON representation";
5
-
6
- function json_target(root, options, callback) {
7
- callback(null, JSON.stringify(root, null, 2));
8
- }