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
@@ -1,711 +0,0 @@
1
- "use strict";
2
- module.exports = static_target;
3
-
4
- var protobuf = require("../.."),
5
- UglifyJS = require("uglify-js"),
6
- espree = require("espree"),
7
- escodegen = require("escodegen"),
8
- estraverse = require("estraverse");
9
-
10
- var Type = protobuf.Type,
11
- Service = protobuf.Service,
12
- Enum = protobuf.Enum,
13
- Namespace = protobuf.Namespace,
14
- util = protobuf.util;
15
-
16
- var out = [];
17
- var indent = 0;
18
- var config = {};
19
-
20
- static_target.description = "Static code without reflection (non-functional on its own)";
21
-
22
- function static_target(root, options, callback) {
23
- config = options;
24
- try {
25
- var aliases = [];
26
- if (config.decode)
27
- aliases.push("Reader");
28
- if (config.encode)
29
- aliases.push("Writer");
30
- aliases.push("util");
31
- if (aliases.length) {
32
- if (config.comments)
33
- push("// Common aliases");
34
- push((config.es6 ? "const " : "var ") + aliases.map(function(name) { return "$" + name + " = $protobuf." + name; }).join(", ") + ";");
35
- push("");
36
- }
37
- if (config.comments) {
38
- if (root.comment) {
39
- pushComment("@fileoverview " + root.comment);
40
- push("");
41
- }
42
- push("// Exported root namespace");
43
- }
44
- var rootProp = util.safeProp(config.root || "default");
45
- push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
46
- buildNamespace(null, root);
47
- return callback(null, out.join("\n"));
48
- } catch (err) {
49
- return callback(err);
50
- } finally {
51
- out = [];
52
- indent = 0;
53
- config = {};
54
- }
55
- }
56
-
57
- function push(line) {
58
- if (line === "")
59
- return out.push("");
60
- var ind = "";
61
- for (var i = 0; i < indent; ++i)
62
- ind += " ";
63
- return out.push(ind + line);
64
- }
65
-
66
- function pushComment(lines) {
67
- if (!config.comments)
68
- return;
69
- var split = [];
70
- for (var i = 0; i < lines.length; ++i)
71
- if (lines[i] != null && lines[i].substring(0, 8) !== "@exclude")
72
- Array.prototype.push.apply(split, lines[i].split(/\r?\n/g));
73
- push("/**");
74
- split.forEach(function(line) {
75
- if (line === null)
76
- return;
77
- push(" * " + line.replace(/\*\//g, "* /"));
78
- });
79
- push(" */");
80
- }
81
-
82
- function exportName(object, asInterface) {
83
- if (asInterface) {
84
- if (object.__interfaceName)
85
- return object.__interfaceName;
86
- } else if (object.__exportName)
87
- return object.__exportName;
88
- var parts = object.fullName.substring(1).split("."),
89
- i = 0;
90
- while (i < parts.length)
91
- parts[i] = escapeName(parts[i++]);
92
- if (asInterface)
93
- parts[i - 1] = "I" + parts[i - 1];
94
- return object[asInterface ? "__interfaceName" : "__exportName"] = parts.join(".");
95
- }
96
-
97
- function escapeName(name) {
98
- if (!name)
99
- return "$root";
100
- return util.isReserved(name) ? name + "_" : name;
101
- }
102
-
103
- function aOrAn(name) {
104
- return ((/^[hH](?:ou|on|ei)/.test(name) || /^[aeiouAEIOU][a-z]/.test(name)) && !/^us/i.test(name)
105
- ? "an "
106
- : "a ") + name;
107
- }
108
-
109
- function buildNamespace(ref, ns) {
110
- if (!ns)
111
- return;
112
-
113
- if (ns instanceof Service && !config.service)
114
- return;
115
-
116
- if (ns.name !== "") {
117
- push("");
118
- if (!ref && config.es6)
119
- push("export const " + escapeName(ns.name) + " = " + escapeName(ref) + "." + escapeName(ns.name) + " = (() => {");
120
- else
121
- push(escapeName(ref) + "." + escapeName(ns.name) + " = (function() {");
122
- ++indent;
123
- }
124
-
125
- if (ns instanceof Type) {
126
- buildType(undefined, ns);
127
- } else if (ns instanceof Service)
128
- buildService(undefined, ns);
129
- else if (ns.name !== "") {
130
- push("");
131
- pushComment([
132
- ns.comment || "Namespace " + ns.name + ".",
133
- ns.parent instanceof protobuf.Root ? "@exports " + escapeName(ns.name) : "@memberof " + exportName(ns.parent),
134
- "@namespace"
135
- ]);
136
- push((config.es6 ? "const" : "var") + " " + escapeName(ns.name) + " = {};");
137
- }
138
-
139
- ns.nestedArray.forEach(function(nested) {
140
- if (nested instanceof Enum)
141
- buildEnum(ns.name, nested);
142
- else if (nested instanceof Namespace)
143
- buildNamespace(ns.name, nested);
144
- });
145
- if (ns.name !== "") {
146
- push("");
147
- push("return " + escapeName(ns.name) + ";");
148
- --indent;
149
- push("})();");
150
- }
151
- }
152
-
153
- var reduceableBlockStatements = {
154
- IfStatement: true,
155
- ForStatement: true,
156
- WhileStatement: true
157
- };
158
-
159
- var shortVars = {
160
- "r": "reader",
161
- "w": "writer",
162
- "m": "message",
163
- "t": "tag",
164
- "l": "length",
165
- "c": "end", "c2": "end2",
166
- "k": "key",
167
- "ks": "keys", "ks2": "keys2",
168
- "e": "error",
169
- "f": "impl",
170
- "o": "options",
171
- "d": "object",
172
- "n": "long",
173
- "p": "properties"
174
- };
175
-
176
- function beautifyCode(code) {
177
- // Add semicolons
178
- code = UglifyJS.minify(code, {
179
- compress: false,
180
- mangle: false,
181
- output: { beautify: true }
182
- }).code;
183
- // Properly beautify
184
- var ast = espree.parse(code);
185
- estraverse.replace(ast, {
186
- enter: function(node, parent) {
187
- // rename short vars
188
- if (node.type === "Identifier" && (parent.property !== node || parent.computed) && shortVars[node.name])
189
- return {
190
- "type": "Identifier",
191
- "name": shortVars[node.name]
192
- };
193
- // replace var with let if es6
194
- if (config.es6 && node.type === "VariableDeclaration" && node.kind === "var") {
195
- node.kind = "let";
196
- return undefined;
197
- }
198
- // remove braces around block statements with a single child
199
- if (node.type === "BlockStatement" && reduceableBlockStatements[parent.type] && node.body.length === 1)
200
- return node.body[0];
201
- return undefined;
202
- }
203
- });
204
- code = escodegen.generate(ast, {
205
- format: {
206
- newline: "\n",
207
- quotes: "double"
208
- }
209
- });
210
- // Add id, wireType comments
211
- if (config.comments)
212
- code = code.replace(/\.uint32\((\d+)\)/g, function($0, $1) {
213
- var id = $1 >>> 3,
214
- wireType = $1 & 7;
215
- return ".uint32(/* id " + id + ", wireType " + wireType + " =*/" + $1 + ")";
216
- });
217
- return code;
218
- }
219
-
220
- var renameVars = {
221
- "Writer": "$Writer",
222
- "Reader": "$Reader",
223
- "util": "$util"
224
- };
225
-
226
- function buildFunction(type, functionName, gen, scope) {
227
- var code = gen.toString(functionName)
228
- .replace(/((?!\.)types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
229
-
230
- var ast = espree.parse(code);
231
- /* eslint-disable no-extra-parens */
232
- estraverse.replace(ast, {
233
- enter: function(node, parent) {
234
- // rename vars
235
- if (
236
- node.type === "Identifier" && renameVars[node.name]
237
- && (
238
- (parent.type === "MemberExpression" && parent.object === node)
239
- || (parent.type === "BinaryExpression" && parent.right === node)
240
- )
241
- )
242
- return {
243
- "type": "Identifier",
244
- "name": renameVars[node.name]
245
- };
246
- // replace this.ctor with the actual ctor
247
- if (
248
- node.type === "MemberExpression"
249
- && node.object.type === "ThisExpression"
250
- && node.property.type === "Identifier" && node.property.name === "ctor"
251
- )
252
- return {
253
- "type": "Identifier",
254
- "name": "$root" + type.fullName
255
- };
256
- // replace types[N] with the field's actual type
257
- if (
258
- node.type === "MemberExpression"
259
- && node.object.type === "Identifier" && node.object.name === "types"
260
- && node.property.type === "Literal"
261
- )
262
- return {
263
- "type": "Identifier",
264
- "name": "$root" + type.fieldsArray[node.property.value].resolvedType.fullName
265
- };
266
- return undefined;
267
- }
268
- });
269
- /* eslint-enable no-extra-parens */
270
- code = escodegen.generate(ast, {
271
- format: {
272
- newline: "\n",
273
- quotes: "double"
274
- }
275
- });
276
-
277
- if (config.beautify)
278
- code = beautifyCode(code);
279
-
280
- code = code.replace(/ {4}/g, "\t");
281
-
282
- var hasScope = scope && Object.keys(scope).length,
283
- isCtor = functionName === type.name;
284
-
285
- if (hasScope) // remove unused scope vars
286
- Object.keys(scope).forEach(function(key) {
287
- if (!new RegExp("\\b(" + key + ")\\b", "g").test(code))
288
- delete scope[key];
289
- });
290
-
291
- var lines = code.split(/\n/g);
292
- if (isCtor) // constructor
293
- push(lines[0]);
294
- else if (hasScope) // enclose in an iife
295
- push(escapeName(type.name) + "." + escapeName(functionName) + " = (function(" + Object.keys(scope).map(escapeName).join(", ") + ") { return " + lines[0]);
296
- else
297
- push(escapeName(type.name) + "." + escapeName(functionName) + " = " + lines[0]);
298
- lines.slice(1, lines.length - 1).forEach(function(line) {
299
- var prev = indent;
300
- var i = 0;
301
- while (line.charAt(i++) === "\t")
302
- ++indent;
303
- push(line.trim());
304
- indent = prev;
305
- });
306
- if (isCtor)
307
- push("}");
308
- else if (hasScope)
309
- push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
310
- else
311
- push("};");
312
- }
313
-
314
- function toJsType(field) {
315
- var type;
316
-
317
- switch (field.type) {
318
- case "double":
319
- case "float":
320
- case "int32":
321
- case "uint32":
322
- case "sint32":
323
- case "fixed32":
324
- case "sfixed32":
325
- type = "number";
326
- break;
327
- case "int64":
328
- case "uint64":
329
- case "sint64":
330
- case "fixed64":
331
- case "sfixed64":
332
- type = config.forceLong ? "Long" : config.forceNumber ? "number" : "number|Long";
333
- break;
334
- case "bool":
335
- type = "boolean";
336
- break;
337
- case "string":
338
- type = "string";
339
- break;
340
- case "bytes":
341
- type = "Uint8Array";
342
- break;
343
- default:
344
- if (field.resolve().resolvedType)
345
- type = exportName(field.resolvedType, !(field.resolvedType instanceof protobuf.Enum || config.forceMessage));
346
- else
347
- type = "*"; // should not happen
348
- break;
349
- }
350
- if (field.map)
351
- return "Object.<string," + type + ">";
352
- if (field.repeated)
353
- return "Array.<" + type + ">";
354
- return type;
355
- }
356
-
357
- function buildType(ref, type) {
358
-
359
- if (config.comments) {
360
- var typeDef = [
361
- "Properties of " + aOrAn(type.name) + ".",
362
- type.parent instanceof protobuf.Root ? "@exports " + escapeName("I" + type.name) : "@memberof " + exportName(type.parent),
363
- "@interface " + escapeName("I" + type.name)
364
- ];
365
- type.fieldsArray.forEach(function(field) {
366
- var prop = util.safeProp(field.name); // either .name or ["name"]
367
- prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
368
- var jsType = toJsType(field);
369
- if (field.optional)
370
- jsType = jsType + "|null";
371
- typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name));
372
- });
373
- push("");
374
- pushComment(typeDef);
375
- }
376
-
377
- // constructor
378
- push("");
379
- pushComment([
380
- "Constructs a new " + type.name + ".",
381
- type.parent instanceof protobuf.Root ? "@exports " + escapeName(type.name) : "@memberof " + exportName(type.parent),
382
- "@classdesc " + (type.comment || "Represents " + aOrAn(type.name) + "."),
383
- config.comments ? "@implements " + escapeName("I" + type.name) : null,
384
- "@constructor",
385
- "@param {" + exportName(type, true) + "=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
386
- ]);
387
- buildFunction(type, type.name, Type.generateConstructor(type));
388
-
389
- // default values
390
- var firstField = true;
391
- type.fieldsArray.forEach(function(field) {
392
- field.resolve();
393
- var prop = util.safeProp(field.name);
394
- if (config.comments) {
395
- push("");
396
- var jsType = toJsType(field);
397
- if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type || field.partOf)
398
- jsType = jsType + "|null|undefined";
399
- pushComment([
400
- field.comment || type.name + " " + field.name + ".",
401
- "@member {" + jsType + "} " + field.name,
402
- "@memberof " + exportName(type),
403
- "@instance"
404
- ]);
405
- } else if (firstField) {
406
- push("");
407
- firstField = false;
408
- }
409
- if (field.repeated)
410
- push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyArray;"); // overwritten in constructor
411
- else if (field.map)
412
- push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyObject;"); // overwritten in constructor
413
- else if (field.partOf)
414
- push(escapeName(type.name) + ".prototype" + prop + " = null;"); // do not set default value for oneof members
415
- else if (field.long)
416
- push(escapeName(type.name) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
417
- + JSON.stringify(field.typeDefault.low) + ","
418
- + JSON.stringify(field.typeDefault.high) + ","
419
- + JSON.stringify(field.typeDefault.unsigned)
420
- + ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
421
- else if (field.bytes) {
422
- push(escapeName(type.name) + ".prototype" + prop + " = $util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
423
- } else
424
- push(escapeName(type.name) + ".prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
425
- });
426
-
427
- // virtual oneof fields
428
- var firstOneOf = true;
429
- type.oneofsArray.forEach(function(oneof) {
430
- if (firstOneOf) {
431
- firstOneOf = false;
432
- push("");
433
- if (config.comments)
434
- push("// OneOf field names bound to virtual getters and setters");
435
- push((config.es6 ? "let" : "var") + " $oneOfFields;");
436
- }
437
- oneof.resolve();
438
- push("");
439
- pushComment([
440
- oneof.comment || type.name + " " + oneof.name + ".",
441
- "@member {" + oneof.oneof.map(JSON.stringify).join("|") + "|undefined} " + escapeName(oneof.name),
442
- "@memberof " + exportName(type),
443
- "@instance"
444
- ]);
445
- push("Object.defineProperty(" + escapeName(type.name) + ".prototype, " + JSON.stringify(oneof.name) +", {");
446
- ++indent;
447
- push("get: $util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
448
- push("set: $util.oneOfSetter($oneOfFields)");
449
- --indent;
450
- push("});");
451
- });
452
-
453
- if (config.create) {
454
- push("");
455
- pushComment([
456
- "Creates a new " + type.name + " instance using the specified properties.",
457
- "@function create",
458
- "@memberof " + exportName(type),
459
- "@static",
460
- "@param {" + exportName(type, true) + "=} [properties] Properties to set",
461
- "@returns {" + exportName(type) + "} " + type.name + " instance"
462
- ]);
463
- push(escapeName(type.name) + ".create = function create(properties) {");
464
- ++indent;
465
- push("return new " + escapeName(type.name) + "(properties);");
466
- --indent;
467
- push("};");
468
- }
469
-
470
- if (config.encode) {
471
- push("");
472
- pushComment([
473
- "Encodes the specified " + type.name + " message. Does not implicitly {@link " + exportName(type) + ".verify|verify} messages.",
474
- "@function encode",
475
- "@memberof " + exportName(type),
476
- "@static",
477
- "@param {" + exportName(type, !config.forceMessage) + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
478
- "@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to",
479
- "@returns {$protobuf.Writer} Writer"
480
- ]);
481
- buildFunction(type, "encode", protobuf.encoder(type));
482
-
483
- if (config.delimited) {
484
- push("");
485
- pushComment([
486
- "Encodes the specified " + type.name + " message, length delimited. Does not implicitly {@link " + exportName(type) + ".verify|verify} messages.",
487
- "@function encodeDelimited",
488
- "@memberof " + exportName(type),
489
- "@static",
490
- "@param {" + exportName(type, !config.forceMessage) + "} message " + type.name + " message or plain object to encode",
491
- "@param {$protobuf.Writer} [writer] Writer to encode to",
492
- "@returns {$protobuf.Writer} Writer"
493
- ]);
494
- push(escapeName(type.name) + ".encodeDelimited = function encodeDelimited(message, writer) {");
495
- ++indent;
496
- push("return this.encode(message, writer).ldelim();");
497
- --indent;
498
- push("};");
499
- }
500
- }
501
-
502
- if (config.decode) {
503
- push("");
504
- pushComment([
505
- "Decodes " + aOrAn(type.name) + " message from the specified reader or buffer.",
506
- "@function decode",
507
- "@memberof " + exportName(type),
508
- "@static",
509
- "@param {$protobuf.Reader|Uint8Array} " + (config.beautify ? "reader" : "r") + " Reader or buffer to decode from",
510
- "@param {number} [" + (config.beautify ? "length" : "l") + "] Message length if known beforehand",
511
- "@returns {" + exportName(type) + "} " + type.name,
512
- "@throws {Error} If the payload is not a reader or valid buffer",
513
- "@throws {$protobuf.util.ProtocolError} If required fields are missing"
514
- ]);
515
- buildFunction(type, "decode", protobuf.decoder(type));
516
-
517
- if (config.delimited) {
518
- push("");
519
- pushComment([
520
- "Decodes " + aOrAn(type.name) + " message from the specified reader or buffer, length delimited.",
521
- "@function decodeDelimited",
522
- "@memberof " + exportName(type),
523
- "@static",
524
- "@param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from",
525
- "@returns {" + exportName(type) + "} " + type.name,
526
- "@throws {Error} If the payload is not a reader or valid buffer",
527
- "@throws {$protobuf.util.ProtocolError} If required fields are missing"
528
- ]);
529
- push(escapeName(type.name) + ".decodeDelimited = function decodeDelimited(reader) {");
530
- ++indent;
531
- push("if (!(reader instanceof $Reader))");
532
- ++indent;
533
- push("reader = new $Reader(reader);");
534
- --indent;
535
- push("return this.decode(reader, reader.uint32());");
536
- --indent;
537
- push("};");
538
- }
539
- }
540
-
541
- if (config.verify) {
542
- push("");
543
- pushComment([
544
- "Verifies " + aOrAn(type.name) + " message.",
545
- "@function verify",
546
- "@memberof " + exportName(type),
547
- "@static",
548
- "@param {Object.<string,*>} " + (config.beautify ? "message" : "m") + " Plain object to verify",
549
- "@returns {string|null} `null` if valid, otherwise the reason why it is not"
550
- ]);
551
- buildFunction(type, "verify", protobuf.verifier(type));
552
- }
553
-
554
- if (config.convert) {
555
- push("");
556
- pushComment([
557
- "Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
558
- "@function fromObject",
559
- "@memberof " + exportName(type),
560
- "@static",
561
- "@param {Object.<string,*>} " + (config.beautify ? "object" : "d") + " Plain object",
562
- "@returns {" + exportName(type) + "} " + type.name
563
- ]);
564
- buildFunction(type, "fromObject", protobuf.converter.fromObject(type));
565
-
566
- push("");
567
- pushComment([
568
- "Creates a plain object from " + aOrAn(type.name) + " message. Also converts values to other types if specified.",
569
- "@function toObject",
570
- "@memberof " + exportName(type),
571
- "@static",
572
- "@param {" + exportName(type) + "} " + (config.beautify ? "message" : "m") + " " + type.name,
573
- "@param {$protobuf.IConversionOptions} [" + (config.beautify ? "options" : "o") + "] Conversion options",
574
- "@returns {Object.<string,*>} Plain object"
575
- ]);
576
- buildFunction(type, "toObject", protobuf.converter.toObject(type));
577
-
578
- push("");
579
- pushComment([
580
- "Converts this " + type.name + " to JSON.",
581
- "@function toJSON",
582
- "@memberof " + exportName(type),
583
- "@instance",
584
- "@returns {Object.<string,*>} JSON object"
585
- ]);
586
- push(escapeName(type.name) + ".prototype.toJSON = function toJSON() {");
587
- ++indent;
588
- push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
589
- --indent;
590
- push("};");
591
- }
592
- }
593
-
594
- function buildService(ref, service) {
595
-
596
- push("");
597
- pushComment([
598
- "Constructs a new " + service.name + " service.",
599
- service.parent instanceof protobuf.Root ? "@exports " + escapeName(service.name) : "@memberof " + exportName(service.parent),
600
- "@classdesc " + (service.comment || "Represents " + aOrAn(service.name)),
601
- "@extends $protobuf.rpc.Service",
602
- "@constructor",
603
- "@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
604
- "@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
605
- "@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
606
- ]);
607
- push("function " + escapeName(service.name) + "(rpcImpl, requestDelimited, responseDelimited) {");
608
- ++indent;
609
- push("$protobuf.rpc.Service.call(this, rpcImpl, requestDelimited, responseDelimited);");
610
- --indent;
611
- push("}");
612
- push("");
613
- push("(" + escapeName(service.name) + ".prototype = Object.create($protobuf.rpc.Service.prototype)).constructor = " + escapeName(service.name) + ";");
614
-
615
- if (config.create) {
616
- push("");
617
- pushComment([
618
- "Creates new " + service.name + " service using the specified rpc implementation.",
619
- "@function create",
620
- "@memberof " + exportName(service),
621
- "@static",
622
- "@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
623
- "@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
624
- "@param {boolean} [responseDelimited=false] Whether responses are length-delimited",
625
- "@returns {" + escapeName(service.name) + "} RPC service. Useful where requests and/or responses are streamed."
626
- ]);
627
- push(escapeName(service.name) + ".create = function create(rpcImpl, requestDelimited, responseDelimited) {");
628
- ++indent;
629
- push("return new this(rpcImpl, requestDelimited, responseDelimited);");
630
- --indent;
631
- push("};");
632
- }
633
-
634
- service.methodsArray.forEach(function(method) {
635
- method.resolve();
636
- var lcName = protobuf.util.lcFirst(method.name),
637
- cbName = escapeName(method.name + "Callback");
638
- push("");
639
- pushComment([
640
- "Callback as used by {@link " + exportName(service) + "#" + escapeName(lcName) + "}.",
641
- // This is a more specialized version of protobuf.rpc.ServiceCallback
642
- "@memberof " + exportName(service),
643
- "@typedef " + cbName,
644
- "@type {function}",
645
- "@param {Error|null} error Error, if any",
646
- "@param {" + exportName(method.resolvedResponseType) + "} [response] " + method.resolvedResponseType.name
647
- ]);
648
- push("");
649
- pushComment([
650
- method.comment || "Calls " + method.name + ".",
651
- "@function " + lcName,
652
- "@memberof " + exportName(service),
653
- "@instance",
654
- "@param {" + exportName(method.resolvedRequestType, !config.forceMessage) + "} request " + method.resolvedRequestType.name + " message or plain object",
655
- "@param {" + exportName(service) + "." + cbName + "} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
656
- "@returns {undefined}",
657
- "@variation 1"
658
- ]);
659
- push("Object.defineProperty(" + escapeName(service.name) + ".prototype" + util.safeProp(lcName) + " = function " + escapeName(lcName) + "(request, callback) {");
660
- ++indent;
661
- push("return this.rpcCall(" + escapeName(lcName) + ", $root." + exportName(method.resolvedRequestType) + ", $root." + exportName(method.resolvedResponseType) + ", request, callback);");
662
- --indent;
663
- push("}, \"name\", { value: " + JSON.stringify(method.name) + " });");
664
- if (config.comments)
665
- push("");
666
- pushComment([
667
- method.comment || "Calls " + method.name + ".",
668
- "@function " + lcName,
669
- "@memberof " + exportName(service),
670
- "@instance",
671
- "@param {" + exportName(method.resolvedRequestType, !config.forceMessage) + "} request " + method.resolvedRequestType.name + " message or plain object",
672
- "@returns {Promise<" + exportName(method.resolvedResponseType) + ">} Promise",
673
- "@variation 2"
674
- ]);
675
- });
676
- }
677
-
678
- function buildEnum(ref, enm) {
679
-
680
- push("");
681
- var comment = [
682
- enm.comment || enm.name + " enum.",
683
- enm.parent instanceof protobuf.Root ? "@exports " + escapeName(enm.name) : "@name " + exportName(enm),
684
- config.forceEnumString ? "@enum {string}" : "@enum {number}",
685
- ];
686
- Object.keys(enm.values).forEach(function(key) {
687
- var val = config.forceEnumString ? key : enm.values[key];
688
- comment.push((config.forceEnumString ? "@property {string} " : "@property {number} ") + key + "=" + val + " " + (enm.comments[key] || key + " value"));
689
- });
690
- pushComment(comment);
691
- if (!ref && config.es6)
692
- push("export const " + escapeName(enm.name) + " = " + escapeName(ref) + "." + escapeName(enm.name) + " = (() => {");
693
- else
694
- push(escapeName(ref) + "." + escapeName(enm.name) + " = (function() {");
695
- ++indent;
696
- push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
697
- var aliased = [];
698
- Object.keys(enm.values).forEach(function(key) {
699
- var valueId = enm.values[key];
700
- var val = config.forceEnumString ? JSON.stringify(key) : valueId;
701
- if (aliased.indexOf(valueId) > -1)
702
- push("values[" + JSON.stringify(key) + "] = " + val + ";");
703
- else {
704
- push("values[valuesById[" + valueId + "] = " + JSON.stringify(key) + "] = " + val + ";");
705
- aliased.push(valueId);
706
- }
707
- });
708
- push("return values;");
709
- --indent;
710
- push("})();");
711
- }