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