protobufjs 8.5.0 → 8.6.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.
- package/README.md +46 -57
- package/dist/light/protobuf.js +159 -121
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +26 -69
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +174 -122
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +38 -49
- package/ext/descriptor.d.ts +8 -7
- package/ext/descriptor.generated.d.ts +16 -8
- package/ext/descriptor.js +170 -104
- package/ext/protojson.LICENSE +201 -0
- package/ext/protojson.d.ts +20 -0
- package/ext/protojson.generated.d.ts +49 -0
- package/ext/protojson.js +925 -0
- package/ext/textformat.d.ts +2 -2
- package/ext/textformat.generated.d.ts +22 -0
- package/ext/textformat.js +40 -19
- package/index.d.ts +35 -17
- package/package.json +2 -6
- package/src/converter.js +3 -3
- package/src/decoder.js +2 -2
- package/src/encoder.js +1 -1
- package/src/field.js +49 -7
- package/src/mapfield.js +16 -7
- package/src/parse.js +15 -1
- package/src/service.js +5 -7
- package/src/type.js +12 -3
- package/src/util/minimal.js +1 -4
- package/src/util.js +23 -0
- package/src/verifier.js +2 -2
- package/scripts/postinstall.js +0 -32
- package/src/util/inquire.d.ts +0 -10
- package/src/util/inquire.js +0 -38
package/ext/descriptor.js
CHANGED
|
@@ -88,14 +88,24 @@ var numberRe = patterns.numberRe,
|
|
|
88
88
|
* @property {number} LITE_RUNTIME=3
|
|
89
89
|
*/
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Descriptor context.
|
|
93
|
+
* @interface IDescriptorContext
|
|
94
|
+
* @property {string} [edition="proto2"] Syntax or edition to use for direct object descriptor imports
|
|
95
|
+
* @property {IFeatureSet} [features] File-level features to apply for direct object descriptor imports
|
|
96
|
+
* @property {boolean} [keepCase=false] Uses proto field names as reflected field names
|
|
97
|
+
*/
|
|
98
|
+
|
|
91
99
|
/**
|
|
92
100
|
* Creates a root from a descriptor set.
|
|
93
101
|
* @param {IFileDescriptorSet|Reader|Uint8Array} descriptor Descriptor
|
|
102
|
+
* @param {{keepCase?: boolean}} [options] Conversion options
|
|
94
103
|
* @returns {Root} Root instance
|
|
95
104
|
*/
|
|
96
|
-
Root.fromDescriptor = function fromDescriptor(descriptor) {
|
|
105
|
+
Root.fromDescriptor = function fromDescriptor(descriptor, options) {
|
|
97
106
|
|
|
98
107
|
descriptor = decodeDescriptor(descriptor, exports.FileDescriptorSet);
|
|
108
|
+
options = options || {};
|
|
99
109
|
|
|
100
110
|
var root = new Root();
|
|
101
111
|
|
|
@@ -106,26 +116,35 @@ Root.fromDescriptor = function fromDescriptor(descriptor) {
|
|
|
106
116
|
filePackage = root;
|
|
107
117
|
if ((fileDescriptor = descriptor.file[j])["package"] && fileDescriptor["package"].length)
|
|
108
118
|
filePackage = root.define(fileDescriptor["package"]);
|
|
109
|
-
var
|
|
119
|
+
var fileOptions = fromDescriptorOptions(fileDescriptor.options, exports.FileOptions),
|
|
120
|
+
ctx = descriptorContext({
|
|
121
|
+
edition: editionFromDescriptor(fileDescriptor),
|
|
122
|
+
features: fileOptions && fileOptions.features,
|
|
123
|
+
keepCase: options && options.keepCase
|
|
124
|
+
});
|
|
110
125
|
if (fileDescriptor.name && fileDescriptor.name.length)
|
|
111
126
|
root.files.push(filePackage.filename = fileDescriptor.name);
|
|
127
|
+
var groupTypes = groupTypeNames(fileDescriptor.extension);
|
|
112
128
|
if (fileDescriptor.messageType)
|
|
113
|
-
for (i = 0; i < fileDescriptor.messageType.length; ++i)
|
|
114
|
-
|
|
129
|
+
for (i = 0; i < fileDescriptor.messageType.length; ++i) {
|
|
130
|
+
var type = Type_fromDescriptor(fileDescriptor.messageType[i], ctx, false, 0);
|
|
131
|
+
if (groupTypes[type.name])
|
|
132
|
+
type.group = true;
|
|
133
|
+
filePackage.add(type);
|
|
134
|
+
}
|
|
115
135
|
if (fileDescriptor.enumType)
|
|
116
136
|
for (i = 0; i < fileDescriptor.enumType.length; ++i)
|
|
117
|
-
filePackage.add(
|
|
137
|
+
filePackage.add(Enum_fromDescriptor(fileDescriptor.enumType[i], ctx, false));
|
|
118
138
|
if (fileDescriptor.extension)
|
|
119
139
|
for (i = 0; i < fileDescriptor.extension.length; ++i)
|
|
120
|
-
filePackage.add(
|
|
140
|
+
filePackage.add(Field_fromDescriptor(fileDescriptor.extension[i], ctx, false));
|
|
121
141
|
if (fileDescriptor.service)
|
|
122
142
|
for (i = 0; i < fileDescriptor.service.length; ++i)
|
|
123
|
-
filePackage.add(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
var ks = Object.keys(opts);
|
|
143
|
+
filePackage.add(Service_fromDescriptor(fileDescriptor.service[i], ctx, false));
|
|
144
|
+
if (fileOptions) {
|
|
145
|
+
var ks = Object.keys(fileOptions);
|
|
127
146
|
for (i = 0; i < ks.length; ++i)
|
|
128
|
-
filePackage.setOption(ks[i],
|
|
147
|
+
filePackage.setOption(ks[i], fileOptions[ks[i]]);
|
|
129
148
|
}
|
|
130
149
|
}
|
|
131
150
|
}
|
|
@@ -215,16 +234,15 @@ var unnamedMessageIndex = 0;
|
|
|
215
234
|
|
|
216
235
|
/**
|
|
217
236
|
* Creates a type from a descriptor.
|
|
218
|
-
*
|
|
219
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
220
|
-
*
|
|
221
237
|
* @param {IDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
222
|
-
* @param {string} [
|
|
223
|
-
* @param {boolean} [nested=false] Whether or not this is a nested object
|
|
224
|
-
* @param {number} [depth] Current nesting depth, defaults to `0`
|
|
238
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
225
239
|
* @returns {Type} Type instance
|
|
226
240
|
*/
|
|
227
|
-
Type.fromDescriptor = function fromDescriptor(descriptor,
|
|
241
|
+
Type.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
242
|
+
return Type_fromDescriptor(descriptor, descriptorContext(editionOrContext), false, 0);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
function Type_fromDescriptor(descriptor, ctx, nested, depth) {
|
|
228
246
|
if (depth === undefined)
|
|
229
247
|
depth = 0;
|
|
230
248
|
if (depth > $protobuf.util.nestingLimit)
|
|
@@ -235,36 +253,43 @@ Type.fromDescriptor = function fromDescriptor(descriptor, edition, nested, depth
|
|
|
235
253
|
i,
|
|
236
254
|
mapEntries = {};
|
|
237
255
|
|
|
238
|
-
if (!nested)
|
|
239
|
-
type._edition = edition;
|
|
256
|
+
if (!nested) {
|
|
257
|
+
type._edition = ctx.edition;
|
|
258
|
+
applyContextFeatures(type, ctx);
|
|
259
|
+
}
|
|
240
260
|
|
|
241
261
|
if (descriptor.nestedType)
|
|
242
262
|
for (i = 0; i < descriptor.nestedType.length; ++i)
|
|
243
263
|
if (descriptor.nestedType[i].options && descriptor.nestedType[i].options.mapEntry)
|
|
244
264
|
mapEntries[descriptor.nestedType[i].name] = descriptor.nestedType[i];
|
|
245
265
|
|
|
266
|
+
var groupTypes = groupTypeNames(descriptor.field, descriptor.extension);
|
|
267
|
+
|
|
246
268
|
/* Oneofs */ if (descriptor.oneofDecl)
|
|
247
269
|
for (i = 0; i < descriptor.oneofDecl.length; ++i)
|
|
248
|
-
type.add(
|
|
270
|
+
type.add(OneOf_fromDescriptor(descriptor.oneofDecl[i], ctx, true));
|
|
249
271
|
/* Fields */ if (descriptor.field)
|
|
250
272
|
for (i = 0; i < descriptor.field.length; ++i) {
|
|
251
|
-
var field = FieldBase_fromDescriptor(descriptor.field[i],
|
|
273
|
+
var field = FieldBase_fromDescriptor(descriptor.field[i], ctx, true, mapEntries);
|
|
252
274
|
type.add(field);
|
|
253
275
|
if (descriptor.field[i].hasOwnProperty("oneofIndex")) // eslint-disable-line no-prototype-builtins
|
|
254
276
|
type.oneofsArray[descriptor.field[i].oneofIndex].add(field);
|
|
255
277
|
}
|
|
256
278
|
/* Extension fields */ if (descriptor.extension)
|
|
257
279
|
for (i = 0; i < descriptor.extension.length; ++i)
|
|
258
|
-
type.add(
|
|
280
|
+
type.add(Field_fromDescriptor(descriptor.extension[i], ctx, true));
|
|
259
281
|
/* Nested types */ if (descriptor.nestedType)
|
|
260
282
|
for (i = 0; i < descriptor.nestedType.length; ++i) {
|
|
261
283
|
if (descriptor.nestedType[i].options && descriptor.nestedType[i].options.mapEntry)
|
|
262
284
|
continue;
|
|
263
|
-
|
|
285
|
+
var nestedType = Type_fromDescriptor(descriptor.nestedType[i], ctx, true, depth + 1);
|
|
286
|
+
if (groupTypes[nestedType.name])
|
|
287
|
+
nestedType.group = true;
|
|
288
|
+
type.add(nestedType);
|
|
264
289
|
}
|
|
265
290
|
/* Nested enums */ if (descriptor.enumType)
|
|
266
291
|
for (i = 0; i < descriptor.enumType.length; ++i)
|
|
267
|
-
type.add(
|
|
292
|
+
type.add(Enum_fromDescriptor(descriptor.enumType[i], ctx, true));
|
|
268
293
|
/* Extension ranges */ if (descriptor.extensionRange && descriptor.extensionRange.length) {
|
|
269
294
|
type.extensions = [];
|
|
270
295
|
for (i = 0; i < descriptor.extensionRange.length; ++i)
|
|
@@ -281,7 +306,7 @@ Type.fromDescriptor = function fromDescriptor(descriptor, edition, nested, depth
|
|
|
281
306
|
}
|
|
282
307
|
|
|
283
308
|
return type;
|
|
284
|
-
}
|
|
309
|
+
}
|
|
285
310
|
|
|
286
311
|
/**
|
|
287
312
|
* Converts a type to a descriptor.
|
|
@@ -339,15 +364,31 @@ Type.prototype.toDescriptor = function toDescriptor(edition) {
|
|
|
339
364
|
|
|
340
365
|
// --- FieldBase ---
|
|
341
366
|
|
|
342
|
-
function FieldBase_fromDescriptor(descriptor,
|
|
367
|
+
function FieldBase_fromDescriptor(descriptor, ctx, nested, mapEntries) {
|
|
343
368
|
var entryName = descriptor.typeName && descriptor.typeName.substring(descriptor.typeName.lastIndexOf(".") + 1),
|
|
344
369
|
mapEntry = descriptor.label === 3 && descriptor.type === 11 && entryName
|
|
345
370
|
? mapEntries[entryName]
|
|
346
371
|
: null;
|
|
347
372
|
|
|
348
373
|
return mapEntry
|
|
349
|
-
? MapField_fromDescriptor(descriptor, mapEntry)
|
|
350
|
-
:
|
|
374
|
+
? MapField_fromDescriptor(descriptor, mapEntry, ctx)
|
|
375
|
+
: Field_fromDescriptor(descriptor, ctx, nested);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function fieldNameFromDescriptor(descriptor, ctx) {
|
|
379
|
+
return ctx.keepCase && descriptor.name.length
|
|
380
|
+
? descriptor.name
|
|
381
|
+
: descriptor.jsonName && descriptor.jsonName.length
|
|
382
|
+
? descriptor.jsonName
|
|
383
|
+
: descriptor.name.length ? descriptor.name : "field" + descriptor.number;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function applyFieldNamesFromDescriptor(field, descriptor) {
|
|
387
|
+
if (descriptor.name.length && descriptor.name !== field.name)
|
|
388
|
+
field.protoName = descriptor.name;
|
|
389
|
+
if (descriptor.jsonName && descriptor.jsonName.length)
|
|
390
|
+
field.jsonName = descriptor.jsonName;
|
|
391
|
+
return field;
|
|
351
392
|
}
|
|
352
393
|
|
|
353
394
|
// --- Field ---
|
|
@@ -363,7 +404,7 @@ function FieldBase_fromDescriptor(descriptor, edition, nested, mapEntries) {
|
|
|
363
404
|
* @property {string} [extendee] Extended type name
|
|
364
405
|
* @property {string} [defaultValue] Literal default value
|
|
365
406
|
* @property {number} [oneofIndex] Oneof index if part of a oneof
|
|
366
|
-
* @property {
|
|
407
|
+
* @property {string} [jsonName] JSON name (lowerCamelCase)
|
|
367
408
|
* @property {IFieldOptions} [options] Field options
|
|
368
409
|
* @property {boolean} [proto3Optional] Whether this is a proto3 optional field
|
|
369
410
|
*/
|
|
@@ -419,16 +460,15 @@ function FieldBase_fromDescriptor(descriptor, edition, nested, mapEntries) {
|
|
|
419
460
|
|
|
420
461
|
/**
|
|
421
462
|
* Creates a field from a descriptor.
|
|
422
|
-
*
|
|
423
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
424
|
-
*
|
|
425
463
|
* @param {IFieldDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
426
|
-
* @param {string} [
|
|
427
|
-
* @param {boolean} [nested=false] Whether or not this is a top-level object
|
|
464
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
428
465
|
* @returns {Field} Field instance
|
|
429
466
|
*/
|
|
430
|
-
Field.fromDescriptor = function fromDescriptor(descriptor,
|
|
467
|
+
Field.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
468
|
+
return Field_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
|
|
469
|
+
};
|
|
431
470
|
|
|
471
|
+
function Field_fromDescriptor(descriptor, ctx, nested) {
|
|
432
472
|
descriptor = decodeDescriptor(descriptor, exports.FieldDescriptorProto);
|
|
433
473
|
|
|
434
474
|
if (typeof descriptor.number !== "number")
|
|
@@ -460,18 +500,20 @@ Field.fromDescriptor = function fromDescriptor(descriptor, edition, nested) {
|
|
|
460
500
|
throw Error("illegal type name: " + extendee);
|
|
461
501
|
} else
|
|
462
502
|
extendee = undefined;
|
|
463
|
-
var field = new Field(
|
|
464
|
-
descriptor
|
|
503
|
+
var field = applyFieldNamesFromDescriptor(new Field(
|
|
504
|
+
fieldNameFromDescriptor(descriptor, ctx),
|
|
465
505
|
descriptor.number,
|
|
466
506
|
fieldType,
|
|
467
507
|
fieldRule,
|
|
468
508
|
extendee
|
|
469
|
-
);
|
|
509
|
+
), descriptor);
|
|
470
510
|
|
|
471
511
|
if (!nested)
|
|
472
|
-
field._edition = edition;
|
|
512
|
+
field._edition = ctx.edition;
|
|
473
513
|
|
|
474
514
|
field.options = fromDescriptorOptions(descriptor.options, exports.FieldOptions);
|
|
515
|
+
if (!nested)
|
|
516
|
+
applyContextFeatures(field, ctx);
|
|
475
517
|
if (descriptor.proto3Optional || descriptor.proto3_optional)
|
|
476
518
|
(field.options || (field.options = {})).proto3_optional = true;
|
|
477
519
|
|
|
@@ -493,20 +535,12 @@ Field.fromDescriptor = function fromDescriptor(descriptor, edition, nested) {
|
|
|
493
535
|
field.setOption("default", defaultValue);
|
|
494
536
|
}
|
|
495
537
|
|
|
496
|
-
if (packableDescriptorType(descriptor.type)) {
|
|
497
|
-
if (edition === "proto3") { // defaults to packed=true (internal preset is packed=true)
|
|
498
|
-
if (descriptor.options && !descriptor.options.packed)
|
|
499
|
-
field.setOption("packed", false);
|
|
500
|
-
} else if ((!edition || edition === "proto2") && descriptor.options && descriptor.options.packed) // defaults to packed=false
|
|
501
|
-
field.setOption("packed", true);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
538
|
return field;
|
|
505
|
-
}
|
|
539
|
+
}
|
|
506
540
|
|
|
507
541
|
// --- MapField ---
|
|
508
542
|
|
|
509
|
-
function MapField_fromDescriptor(descriptor, entryDescriptor) {
|
|
543
|
+
function MapField_fromDescriptor(descriptor, entryDescriptor, ctx) {
|
|
510
544
|
function entryField(number) {
|
|
511
545
|
if (entryDescriptor.field)
|
|
512
546
|
for (var i = 0; i < entryDescriptor.field.length; ++i)
|
|
@@ -521,13 +555,13 @@ function MapField_fromDescriptor(descriptor, entryDescriptor) {
|
|
|
521
555
|
? valueDescriptor.typeName
|
|
522
556
|
: fromDescriptorType(valueDescriptor.type);
|
|
523
557
|
|
|
524
|
-
return new MapField(
|
|
525
|
-
descriptor
|
|
558
|
+
return applyFieldNamesFromDescriptor(new MapField(
|
|
559
|
+
fieldNameFromDescriptor(descriptor, ctx),
|
|
526
560
|
descriptor.number,
|
|
527
561
|
fromDescriptorType(keyDescriptor.type),
|
|
528
562
|
valueType,
|
|
529
563
|
fromDescriptorOptions(descriptor.options, exports.FieldOptions)
|
|
530
|
-
);
|
|
564
|
+
), descriptor);
|
|
531
565
|
}
|
|
532
566
|
|
|
533
567
|
/**
|
|
@@ -536,7 +570,13 @@ function MapField_fromDescriptor(descriptor, entryDescriptor) {
|
|
|
536
570
|
* @param {string} [edition="proto2"] The syntax or edition to use
|
|
537
571
|
*/
|
|
538
572
|
Field.prototype.toDescriptor = function toDescriptor(edition) {
|
|
539
|
-
|
|
573
|
+
// Emit descriptor names in FieldDescriptorProto form, including json_name only
|
|
574
|
+
// when it differs, and derive missing names so unresolved fields serialize consistently.
|
|
575
|
+
var protoName = this.protoName || this.name;
|
|
576
|
+
var jsonName = this.jsonName || $protobuf.util.jsonName(protoName);
|
|
577
|
+
var descriptor = exports.FieldDescriptorProto.create({ name: protoName, number: this.id });
|
|
578
|
+
if (jsonName !== protoName)
|
|
579
|
+
descriptor.jsonName = jsonName;
|
|
540
580
|
|
|
541
581
|
if (this.map) {
|
|
542
582
|
|
|
@@ -643,16 +683,15 @@ var unnamedEnumIndex = 0;
|
|
|
643
683
|
|
|
644
684
|
/**
|
|
645
685
|
* Creates an enum from a descriptor.
|
|
646
|
-
*
|
|
647
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
648
|
-
*
|
|
649
686
|
* @param {IEnumDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
650
|
-
* @param {string} [
|
|
651
|
-
* @param {boolean} [nested=false] Whether or not this is a top-level object
|
|
687
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
652
688
|
* @returns {Enum} Enum instance
|
|
653
689
|
*/
|
|
654
|
-
Enum.fromDescriptor = function fromDescriptor(descriptor,
|
|
690
|
+
Enum.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
691
|
+
return Enum_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
|
|
692
|
+
};
|
|
655
693
|
|
|
694
|
+
function Enum_fromDescriptor(descriptor, ctx, nested) {
|
|
656
695
|
descriptor = decodeDescriptor(descriptor, exports.EnumDescriptorProto);
|
|
657
696
|
|
|
658
697
|
// Construct values object
|
|
@@ -679,8 +718,10 @@ Enum.fromDescriptor = function fromDescriptor(descriptor, edition, nested) {
|
|
|
679
718
|
valuesOptions
|
|
680
719
|
);
|
|
681
720
|
|
|
682
|
-
if (!nested)
|
|
683
|
-
enm._edition = edition;
|
|
721
|
+
if (!nested) {
|
|
722
|
+
enm._edition = ctx.edition;
|
|
723
|
+
applyContextFeatures(enm, ctx);
|
|
724
|
+
}
|
|
684
725
|
|
|
685
726
|
/* Reserved... */ if (descriptor.reservedRange && descriptor.reservedRange.length || descriptor.reservedName && descriptor.reservedName.length) {
|
|
686
727
|
enm.reserved = [];
|
|
@@ -693,7 +734,7 @@ Enum.fromDescriptor = function fromDescriptor(descriptor, edition, nested) {
|
|
|
693
734
|
}
|
|
694
735
|
|
|
695
736
|
return enm;
|
|
696
|
-
}
|
|
737
|
+
}
|
|
697
738
|
|
|
698
739
|
/**
|
|
699
740
|
* Converts an enum to a descriptor.
|
|
@@ -748,22 +789,28 @@ var unnamedOneofIndex = 0;
|
|
|
748
789
|
|
|
749
790
|
/**
|
|
750
791
|
* Creates a oneof from a descriptor.
|
|
751
|
-
*
|
|
752
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
753
|
-
*
|
|
754
792
|
* @param {IOneofDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
793
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
755
794
|
* @returns {OneOf} OneOf instance
|
|
756
795
|
*/
|
|
757
|
-
OneOf.fromDescriptor = function fromDescriptor(descriptor) {
|
|
796
|
+
OneOf.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
797
|
+
return OneOf_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
|
|
798
|
+
};
|
|
758
799
|
|
|
800
|
+
function OneOf_fromDescriptor(descriptor, ctx, nested) {
|
|
759
801
|
descriptor = decodeDescriptor(descriptor, exports.OneofDescriptorProto);
|
|
760
802
|
|
|
761
|
-
|
|
803
|
+
var oneof = new OneOf(
|
|
762
804
|
// unnamedOneOfIndex is global, not per type, because we have no ref to a type here
|
|
763
805
|
descriptor.name && descriptor.name.length ? descriptor.name : "oneof" + unnamedOneofIndex++,
|
|
764
806
|
fromDescriptorOptions(descriptor.options, exports.OneofOptions)
|
|
765
807
|
);
|
|
766
|
-
|
|
808
|
+
if (!nested) {
|
|
809
|
+
oneof._edition = ctx.edition;
|
|
810
|
+
applyContextFeatures(oneof, ctx);
|
|
811
|
+
}
|
|
812
|
+
return oneof;
|
|
813
|
+
}
|
|
767
814
|
|
|
768
815
|
/**
|
|
769
816
|
* Converts a oneof to a descriptor.
|
|
@@ -796,27 +843,28 @@ var unnamedServiceIndex = 0;
|
|
|
796
843
|
|
|
797
844
|
/**
|
|
798
845
|
* Creates a service from a descriptor.
|
|
799
|
-
*
|
|
800
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
801
|
-
*
|
|
802
846
|
* @param {IServiceDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
803
|
-
* @param {string} [
|
|
804
|
-
* @param {boolean} [nested=false] Whether or not this is a top-level object
|
|
847
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
805
848
|
* @returns {Service} Service instance
|
|
806
849
|
*/
|
|
807
|
-
Service.fromDescriptor = function fromDescriptor(descriptor,
|
|
850
|
+
Service.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
851
|
+
return Service_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
|
|
852
|
+
};
|
|
808
853
|
|
|
854
|
+
function Service_fromDescriptor(descriptor, ctx, nested) {
|
|
809
855
|
descriptor = decodeDescriptor(descriptor, exports.ServiceDescriptorProto);
|
|
810
856
|
|
|
811
857
|
var service = new Service(descriptor.name && descriptor.name.length ? descriptor.name : "Service" + unnamedServiceIndex++, fromDescriptorOptions(descriptor.options, exports.ServiceOptions));
|
|
812
|
-
if (!nested)
|
|
813
|
-
service._edition = edition;
|
|
858
|
+
if (!nested) {
|
|
859
|
+
service._edition = ctx.edition;
|
|
860
|
+
applyContextFeatures(service, ctx);
|
|
861
|
+
}
|
|
814
862
|
if (descriptor.method)
|
|
815
863
|
for (var i = 0; i < descriptor.method.length; ++i)
|
|
816
|
-
service.add(
|
|
864
|
+
service.add(Method_fromDescriptor(descriptor.method[i], ctx, true));
|
|
817
865
|
|
|
818
866
|
return service;
|
|
819
|
-
}
|
|
867
|
+
}
|
|
820
868
|
|
|
821
869
|
/**
|
|
822
870
|
* Converts a service to a descriptor.
|
|
@@ -851,9 +899,6 @@ Service.prototype.toDescriptor = function toDescriptor() {
|
|
|
851
899
|
|
|
852
900
|
/**
|
|
853
901
|
* Properties of a MethodOptions message.
|
|
854
|
-
*
|
|
855
|
-
* Warning: this is not safe to use with editions protos, since it discards relevant file context.
|
|
856
|
-
*
|
|
857
902
|
* @interface IMethodOptions
|
|
858
903
|
* @property {boolean} [deprecated]
|
|
859
904
|
*/
|
|
@@ -863,10 +908,14 @@ var unnamedMethodIndex = 0;
|
|
|
863
908
|
/**
|
|
864
909
|
* Creates a method from a descriptor.
|
|
865
910
|
* @param {IMethodDescriptorProto|Reader|Uint8Array} descriptor Descriptor
|
|
911
|
+
* @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
|
|
866
912
|
* @returns {Method} Reflected method instance
|
|
867
913
|
*/
|
|
868
|
-
Method.fromDescriptor = function fromDescriptor(descriptor) {
|
|
914
|
+
Method.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
|
|
915
|
+
return Method_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
|
|
916
|
+
};
|
|
869
917
|
|
|
918
|
+
function Method_fromDescriptor(descriptor, ctx, nested) {
|
|
870
919
|
descriptor = decodeDescriptor(descriptor, exports.MethodDescriptorProto);
|
|
871
920
|
|
|
872
921
|
var inputType = descriptor.inputType,
|
|
@@ -881,7 +930,7 @@ Method.fromDescriptor = function fromDescriptor(descriptor) {
|
|
|
881
930
|
throw Error("illegal type name: " + outputType);
|
|
882
931
|
}
|
|
883
932
|
|
|
884
|
-
|
|
933
|
+
var method = new Method(
|
|
885
934
|
// unnamedMethodIndex is global, not per service, because we have no ref to a service here
|
|
886
935
|
descriptor.name && descriptor.name.length ? descriptor.name : "Method" + unnamedMethodIndex++,
|
|
887
936
|
"rpc",
|
|
@@ -891,7 +940,12 @@ Method.fromDescriptor = function fromDescriptor(descriptor) {
|
|
|
891
940
|
Boolean(descriptor.serverStreaming),
|
|
892
941
|
fromDescriptorOptions(descriptor.options, exports.MethodOptions)
|
|
893
942
|
);
|
|
894
|
-
|
|
943
|
+
if (!nested) {
|
|
944
|
+
method._edition = ctx.edition;
|
|
945
|
+
applyContextFeatures(method, ctx);
|
|
946
|
+
}
|
|
947
|
+
return method;
|
|
948
|
+
}
|
|
895
949
|
|
|
896
950
|
/**
|
|
897
951
|
* Converts a method to a descriptor.
|
|
@@ -910,6 +964,24 @@ Method.prototype.toDescriptor = function toDescriptor() {
|
|
|
910
964
|
|
|
911
965
|
// --- utility ---
|
|
912
966
|
|
|
967
|
+
function descriptorContext(editionOrContext) {
|
|
968
|
+
if (editionOrContext && typeof editionOrContext === "object") {
|
|
969
|
+
var ctx = $protobuf.util.merge({}, editionOrContext);
|
|
970
|
+
if (!ctx.edition)
|
|
971
|
+
ctx.edition = "proto2";
|
|
972
|
+
return ctx;
|
|
973
|
+
}
|
|
974
|
+
return { edition: editionOrContext || "proto2" };
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function applyContextFeatures(object, ctx) {
|
|
978
|
+
if (!ctx.features)
|
|
979
|
+
return object;
|
|
980
|
+
var options = object.options || (object.options = {});
|
|
981
|
+
options.features = $protobuf.util.merge({}, ctx.features, options.features);
|
|
982
|
+
return object;
|
|
983
|
+
}
|
|
984
|
+
|
|
913
985
|
// Converts a descriptor type to a protobuf.js basic type
|
|
914
986
|
function fromDescriptorType(type) {
|
|
915
987
|
switch (type) {
|
|
@@ -933,26 +1005,20 @@ function fromDescriptorType(type) {
|
|
|
933
1005
|
throw Error("illegal type: " + type);
|
|
934
1006
|
}
|
|
935
1007
|
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
case 14: // enum (!)
|
|
949
|
-
case 15: // sfixed32
|
|
950
|
-
case 16: // sfixed64
|
|
951
|
-
case 17: // sint32
|
|
952
|
-
case 18: // sint64
|
|
953
|
-
return true;
|
|
1008
|
+
function groupTypeNames() {
|
|
1009
|
+
var names = {};
|
|
1010
|
+
for (var a = 0; a < arguments.length; ++a) {
|
|
1011
|
+
var fields = arguments[a];
|
|
1012
|
+
if (!fields)
|
|
1013
|
+
continue;
|
|
1014
|
+
for (var i = 0; i < fields.length; ++i)
|
|
1015
|
+
if (fields[i].type === 10 && fields[i].typeName) {
|
|
1016
|
+
var name = fields[i].typeName.split(".").pop();
|
|
1017
|
+
if (name)
|
|
1018
|
+
names[name] = true;
|
|
1019
|
+
}
|
|
954
1020
|
}
|
|
955
|
-
return
|
|
1021
|
+
return names;
|
|
956
1022
|
}
|
|
957
1023
|
|
|
958
1024
|
// Converts a protobuf.js basic type to a descriptor type
|