protobufjs 6.11.3 → 7.1.1

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 (65) 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 +12 -31
  16. package/scripts/postinstall.js +0 -3
  17. package/src/converter.js +12 -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/wrappers.js +2 -2
  27. package/tsconfig.json +2 -1
  28. package/CHANGELOG.md +0 -1027
  29. package/bin/pbjs +0 -6
  30. package/bin/pbts +0 -6
  31. package/cli/LICENSE +0 -33
  32. package/cli/README.md +0 -174
  33. package/cli/bin/pbjs +0 -6
  34. package/cli/bin/pbts +0 -6
  35. package/cli/index.d.ts +0 -3
  36. package/cli/index.js +0 -3
  37. package/cli/lib/tsd-jsdoc/LICENSE +0 -21
  38. package/cli/lib/tsd-jsdoc/README.md +0 -23
  39. package/cli/lib/tsd-jsdoc/plugin.js +0 -21
  40. package/cli/lib/tsd-jsdoc/publish.js +0 -705
  41. package/cli/lib/tsd-jsdoc.json +0 -18
  42. package/cli/package.json +0 -8
  43. package/cli/package.standalone.json +0 -32
  44. package/cli/pbjs.d.ts +0 -9
  45. package/cli/pbjs.js +0 -330
  46. package/cli/pbts.d.ts +0 -9
  47. package/cli/pbts.js +0 -197
  48. package/cli/targets/json-module.js +0 -38
  49. package/cli/targets/json.js +0 -8
  50. package/cli/targets/proto.js +0 -326
  51. package/cli/targets/proto2.js +0 -10
  52. package/cli/targets/proto3.js +0 -10
  53. package/cli/targets/static-module.js +0 -29
  54. package/cli/targets/static.js +0 -711
  55. package/cli/util.js +0 -183
  56. package/cli/wrappers/amd.js +0 -7
  57. package/cli/wrappers/closure.js +0 -7
  58. package/cli/wrappers/commonjs.js +0 -7
  59. package/cli/wrappers/default.js +0 -15
  60. package/cli/wrappers/es6.js +0 -5
  61. package/dist/README.md +0 -31
  62. package/dist/light/README.md +0 -31
  63. package/dist/minimal/README.md +0 -31
  64. package/package-lock.json +0 -7870
  65. package/scripts/changelog.js +0 -150
@@ -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
- }