@bufbuild/protobuf 1.4.2 → 1.5.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.
- package/dist/cjs/create-descriptor-set.d.ts +19 -5
- package/dist/cjs/create-descriptor-set.js +157 -223
- package/dist/cjs/descriptor-set.d.ts +47 -5
- package/dist/cjs/google/protobuf/any_pb.js +1 -1
- package/dist/cjs/google/protobuf/descriptor_pb.js +1 -1
- package/dist/cjs/google/protobuf/struct_pb.js +1 -1
- package/dist/cjs/google/protobuf/type_pb.js +1 -1
- package/dist/cjs/private/feature-set.d.ts +21 -0
- package/dist/cjs/private/feature-set.js +107 -0
- package/dist/cjs/private/text-format.d.ts +4 -0
- package/dist/cjs/private/text-format.js +189 -0
- package/dist/esm/create-descriptor-set.d.ts +19 -5
- package/dist/esm/create-descriptor-set.js +157 -222
- package/dist/esm/descriptor-set.d.ts +47 -5
- package/dist/esm/google/protobuf/any_pb.js +1 -1
- package/dist/esm/google/protobuf/descriptor_pb.js +1 -1
- package/dist/esm/google/protobuf/struct_pb.js +1 -1
- package/dist/esm/google/protobuf/type_pb.js +1 -1
- package/dist/esm/private/feature-set.d.ts +21 -0
- package/dist/esm/private/feature-set.js +103 -0
- package/dist/esm/private/text-format.d.ts +4 -0
- package/dist/esm/private/text-format.js +184 -0
- package/package.json +5 -5
|
@@ -11,12 +11,13 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import { FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_JSType, FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, MethodOptions_IdempotencyLevel, ServiceDescriptorProto, SourceCodeInfo, } from "./google/protobuf/descriptor_pb.js";
|
|
14
|
+
import { Edition, FeatureSet_RepeatedFieldEncoding, FeatureSetDefaults, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_JSType, FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, MethodOptions_IdempotencyLevel, ServiceDescriptorProto, SourceCodeInfo, } from "./google/protobuf/descriptor_pb.js";
|
|
15
15
|
import { assert } from "./private/assert.js";
|
|
16
16
|
import { LongType, ScalarType } from "./field.js";
|
|
17
17
|
import { MethodIdempotency, MethodKind } from "./service-type.js";
|
|
18
18
|
import { fieldJsonName, findEnumSharedPrefix } from "./private/names.js";
|
|
19
|
-
import {
|
|
19
|
+
import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "./private/text-format.js";
|
|
20
|
+
import { createFeatureResolver, featureSetDefaults, } from "./private/feature-set.js";
|
|
20
21
|
/**
|
|
21
22
|
* Create a DescriptorSet, a convenient interface for working with a set of
|
|
22
23
|
* google.protobuf.FileDescriptorProto.
|
|
@@ -25,13 +26,15 @@ import { protoInt64 } from "./proto-int64.js";
|
|
|
25
26
|
* before any file that imports it. Protocol buffer compilers always produce
|
|
26
27
|
* files in topological order.
|
|
27
28
|
*/
|
|
28
|
-
export function createDescriptorSet(input) {
|
|
29
|
+
export function createDescriptorSet(input, options) {
|
|
30
|
+
var _a;
|
|
29
31
|
const cart = {
|
|
30
32
|
enums: new Map(),
|
|
31
33
|
messages: new Map(),
|
|
32
34
|
services: new Map(),
|
|
33
35
|
extensions: new Map(),
|
|
34
36
|
mapEntries: new Map(),
|
|
37
|
+
resolveFeatures: createFeatureResolver((_a = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _a !== void 0 ? _a : featureSetDefaults),
|
|
35
38
|
};
|
|
36
39
|
const fileDescriptors = input instanceof FileDescriptorSet
|
|
37
40
|
? input.file
|
|
@@ -45,20 +48,9 @@ export function createDescriptorSet(input) {
|
|
|
45
48
|
* Create a descriptor for a file.
|
|
46
49
|
*/
|
|
47
50
|
function newFile(proto, cart) {
|
|
48
|
-
var _a, _b
|
|
51
|
+
var _a, _b;
|
|
49
52
|
assert(proto.name, `invalid FileDescriptorProto: missing name`);
|
|
50
|
-
|
|
51
|
-
const file = {
|
|
52
|
-
kind: "file",
|
|
53
|
-
proto,
|
|
54
|
-
deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false,
|
|
55
|
-
syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
|
|
56
|
-
name: proto.name.replace(/\.proto/, ""),
|
|
57
|
-
enums: [],
|
|
58
|
-
messages: [],
|
|
59
|
-
extensions: [],
|
|
60
|
-
services: [],
|
|
61
|
-
toString() {
|
|
53
|
+
const file = Object.assign(Object.assign({ kind: "file", proto, deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false }, parseFileSyntax(proto.syntax, proto.edition)), { name: proto.name.replace(/\.proto/, ""), enums: [], messages: [], extensions: [], services: [], toString() {
|
|
62
54
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
|
|
63
55
|
return `file ${this.proto.name}`;
|
|
64
56
|
},
|
|
@@ -72,7 +64,10 @@ function newFile(proto, cart) {
|
|
|
72
64
|
FieldNumber.FileDescriptorProto_Package,
|
|
73
65
|
]);
|
|
74
66
|
},
|
|
75
|
-
|
|
67
|
+
getFeatures() {
|
|
68
|
+
var _a;
|
|
69
|
+
return cart.resolveFeatures(this.edition, (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
70
|
+
} });
|
|
76
71
|
cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
|
|
77
72
|
for (const enumProto of proto.enumType) {
|
|
78
73
|
addEnum(enumProto, file, undefined, cart);
|
|
@@ -125,7 +120,7 @@ function addExtensions(desc, cart) {
|
|
|
125
120
|
* Recurses into nested types.
|
|
126
121
|
*/
|
|
127
122
|
function addFields(message, cart) {
|
|
128
|
-
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message));
|
|
123
|
+
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, cart));
|
|
129
124
|
const oneofsSeen = new Set();
|
|
130
125
|
for (const proto of message.proto.field) {
|
|
131
126
|
const oneof = findOneof(proto, allOneofs);
|
|
@@ -182,6 +177,11 @@ function addEnum(proto, file, parent, cart) {
|
|
|
182
177
|
];
|
|
183
178
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
184
179
|
},
|
|
180
|
+
getFeatures() {
|
|
181
|
+
var _a, _b, _c;
|
|
182
|
+
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
|
|
183
|
+
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
|
|
184
|
+
},
|
|
185
185
|
};
|
|
186
186
|
cart.enums.set(desc.typeName, desc);
|
|
187
187
|
proto.value.forEach((proto) => {
|
|
@@ -214,6 +214,10 @@ function addEnum(proto, file, parent, cart) {
|
|
|
214
214
|
];
|
|
215
215
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
216
216
|
},
|
|
217
|
+
getFeatures() {
|
|
218
|
+
var _a;
|
|
219
|
+
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
220
|
+
},
|
|
217
221
|
});
|
|
218
222
|
});
|
|
219
223
|
((_c = parent === null || parent === void 0 ? void 0 : parent.nestedEnums) !== null && _c !== void 0 ? _c : file.enums).push(desc);
|
|
@@ -255,6 +259,11 @@ function addMessage(proto, file, parent, cart) {
|
|
|
255
259
|
];
|
|
256
260
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
257
261
|
},
|
|
262
|
+
getFeatures() {
|
|
263
|
+
var _a, _b, _c;
|
|
264
|
+
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
|
|
265
|
+
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
|
|
266
|
+
},
|
|
258
267
|
};
|
|
259
268
|
if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) {
|
|
260
269
|
cart.mapEntries.set(desc.typeName, desc);
|
|
@@ -295,6 +304,10 @@ function addService(proto, file, cart) {
|
|
|
295
304
|
];
|
|
296
305
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
297
306
|
},
|
|
307
|
+
getFeatures() {
|
|
308
|
+
var _a;
|
|
309
|
+
return cart.resolveFeatures(this.file.edition, this.file.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
310
|
+
},
|
|
298
311
|
};
|
|
299
312
|
file.services.push(desc);
|
|
300
313
|
cart.services.set(desc.typeName, desc);
|
|
@@ -362,12 +375,16 @@ function newMethod(proto, parent, cart) {
|
|
|
362
375
|
];
|
|
363
376
|
return findComments(parent.file.proto.sourceCodeInfo, path);
|
|
364
377
|
},
|
|
378
|
+
getFeatures() {
|
|
379
|
+
var _a;
|
|
380
|
+
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
381
|
+
},
|
|
365
382
|
};
|
|
366
383
|
}
|
|
367
384
|
/**
|
|
368
385
|
* Create a descriptor for a oneof group.
|
|
369
386
|
*/
|
|
370
|
-
function newOneof(proto, parent) {
|
|
387
|
+
function newOneof(proto, parent, cart) {
|
|
371
388
|
assert(proto.name, `invalid OneofDescriptorProto: missing name`);
|
|
372
389
|
return {
|
|
373
390
|
kind: "oneof",
|
|
@@ -387,17 +404,20 @@ function newOneof(proto, parent) {
|
|
|
387
404
|
];
|
|
388
405
|
return findComments(parent.file.proto.sourceCodeInfo, path);
|
|
389
406
|
},
|
|
407
|
+
getFeatures() {
|
|
408
|
+
var _a;
|
|
409
|
+
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
410
|
+
},
|
|
390
411
|
};
|
|
391
412
|
}
|
|
392
413
|
/**
|
|
393
414
|
* Create a descriptor for a field.
|
|
394
415
|
*/
|
|
395
416
|
function newField(proto, file, parent, oneof, cart) {
|
|
396
|
-
var _a, _b, _c
|
|
417
|
+
var _a, _b, _c;
|
|
397
418
|
assert(proto.name, `invalid FieldDescriptorProto: missing name`);
|
|
398
419
|
assert(proto.number, `invalid FieldDescriptorProto: missing number`);
|
|
399
420
|
assert(proto.type, `invalid FieldDescriptorProto: missing type`);
|
|
400
|
-
const packedByDefault = isPackedFieldByDefault(proto, file.syntax);
|
|
401
421
|
const common = {
|
|
402
422
|
proto,
|
|
403
423
|
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
|
|
@@ -406,8 +426,8 @@ function newField(proto, file, parent, oneof, cart) {
|
|
|
406
426
|
parent,
|
|
407
427
|
oneof,
|
|
408
428
|
optional: isOptionalField(proto, file.syntax),
|
|
409
|
-
|
|
410
|
-
|
|
429
|
+
packedByDefault: isPackedFieldByDefault(file, proto, cart.resolveFeatures),
|
|
430
|
+
packed: isPackedField(file, parent, proto, cart.resolveFeatures),
|
|
411
431
|
jsonName: proto.jsonName === fieldJsonName(proto.name) ? undefined : proto.jsonName,
|
|
412
432
|
scalar: undefined,
|
|
413
433
|
longType: undefined,
|
|
@@ -415,11 +435,11 @@ function newField(proto, file, parent, oneof, cart) {
|
|
|
415
435
|
enum: undefined,
|
|
416
436
|
mapKey: undefined,
|
|
417
437
|
mapValue: undefined,
|
|
438
|
+
declarationString,
|
|
439
|
+
// toString, getComments, getFeatures are overridden in newExtension
|
|
418
440
|
toString() {
|
|
419
|
-
// note that newExtension() calls us with parent = null
|
|
420
441
|
return `field ${this.parent.typeName}.${this.name}`;
|
|
421
442
|
},
|
|
422
|
-
declarationString,
|
|
423
443
|
getComments() {
|
|
424
444
|
const path = [
|
|
425
445
|
...this.parent.getComments().sourcePath,
|
|
@@ -428,6 +448,10 @@ function newField(proto, file, parent, oneof, cart) {
|
|
|
428
448
|
];
|
|
429
449
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
430
450
|
},
|
|
451
|
+
getFeatures() {
|
|
452
|
+
var _a;
|
|
453
|
+
return cart.resolveFeatures(file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
|
|
454
|
+
},
|
|
431
455
|
};
|
|
432
456
|
const repeated = proto.label === FieldDescriptorProto_Label.REPEATED;
|
|
433
457
|
switch (proto.type) {
|
|
@@ -456,7 +480,7 @@ function newField(proto, file, parent, oneof, cart) {
|
|
|
456
480
|
assert(scalar, `invalid FieldDescriptorProto: unknown type ${proto.type}`);
|
|
457
481
|
return Object.assign(Object.assign({}, common), { kind: "field", fieldKind: "scalar", getDefaultValue,
|
|
458
482
|
repeated,
|
|
459
|
-
scalar, longType: ((
|
|
483
|
+
scalar, longType: ((_c = proto.options) === null || _c === void 0 ? void 0 : _c.jstype) == FieldOptions_JSType.JS_STRING
|
|
460
484
|
? LongType.STRING
|
|
461
485
|
: LongType.BIGINT });
|
|
462
486
|
}
|
|
@@ -474,6 +498,8 @@ function newExtension(proto, file, parent, cart) {
|
|
|
474
498
|
return Object.assign(Object.assign({}, field), { kind: "extension", typeName: makeTypeName(proto, parent, file), parent,
|
|
475
499
|
file,
|
|
476
500
|
extendee,
|
|
501
|
+
// Must override toString, getComments, getFeatures from newField, because we
|
|
502
|
+
// call newField with parent undefined.
|
|
477
503
|
toString() {
|
|
478
504
|
return `extension ${this.typeName}`;
|
|
479
505
|
},
|
|
@@ -489,8 +515,56 @@ function newExtension(proto, file, parent, cart) {
|
|
|
489
515
|
this.file.proto.extension.indexOf(proto),
|
|
490
516
|
];
|
|
491
517
|
return findComments(file.proto.sourceCodeInfo, path);
|
|
518
|
+
},
|
|
519
|
+
getFeatures() {
|
|
520
|
+
var _a, _b;
|
|
521
|
+
return cart.resolveFeatures(this.file.edition, ((_a = this.parent) !== null && _a !== void 0 ? _a : this.file).getFeatures(), (_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.features);
|
|
492
522
|
} });
|
|
493
523
|
}
|
|
524
|
+
/**
|
|
525
|
+
* Parse the "syntax" and "edition" fields, stripping test editions.
|
|
526
|
+
*/
|
|
527
|
+
function parseFileSyntax(syntax, edition) {
|
|
528
|
+
let e;
|
|
529
|
+
let s;
|
|
530
|
+
switch (syntax) {
|
|
531
|
+
case undefined:
|
|
532
|
+
case "proto2":
|
|
533
|
+
s = "proto2";
|
|
534
|
+
e = Edition.EDITION_PROTO2;
|
|
535
|
+
break;
|
|
536
|
+
case "proto3":
|
|
537
|
+
s = "proto3";
|
|
538
|
+
e = Edition.EDITION_PROTO3;
|
|
539
|
+
break;
|
|
540
|
+
case "editions":
|
|
541
|
+
s = "editions";
|
|
542
|
+
switch (edition) {
|
|
543
|
+
case undefined:
|
|
544
|
+
case Edition.EDITION_1_TEST_ONLY:
|
|
545
|
+
case Edition.EDITION_2_TEST_ONLY:
|
|
546
|
+
case Edition.EDITION_99997_TEST_ONLY:
|
|
547
|
+
case Edition.EDITION_99998_TEST_ONLY:
|
|
548
|
+
case Edition.EDITION_99999_TEST_ONLY:
|
|
549
|
+
case Edition.EDITION_UNKNOWN:
|
|
550
|
+
e = Edition.EDITION_UNKNOWN;
|
|
551
|
+
break;
|
|
552
|
+
default:
|
|
553
|
+
e = edition;
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
break;
|
|
557
|
+
default:
|
|
558
|
+
throw new Error(`invalid FileDescriptorProto: unsupported syntax: ${syntax}`);
|
|
559
|
+
}
|
|
560
|
+
if (syntax === "editions" && edition === Edition.EDITION_UNKNOWN) {
|
|
561
|
+
throw new Error(`invalid FileDescriptorProto: syntax ${syntax} cannot have edition ${String(edition)}`);
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
syntax: s,
|
|
565
|
+
edition: e,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
494
568
|
/**
|
|
495
569
|
* Create a fully qualified name for a protobuf type or extension field.
|
|
496
570
|
*
|
|
@@ -588,40 +662,65 @@ function isOptionalField(proto, syntax) {
|
|
|
588
662
|
proto.label === FieldDescriptorProto_Label.OPTIONAL);
|
|
589
663
|
case "proto3":
|
|
590
664
|
return proto.proto3Optional === true;
|
|
665
|
+
case "editions":
|
|
666
|
+
return false;
|
|
591
667
|
}
|
|
592
668
|
}
|
|
593
669
|
/**
|
|
594
|
-
*
|
|
670
|
+
* Is this field packed by default? Only valid for repeated enum fields, and
|
|
671
|
+
* for repeated scalar fields except BYTES and STRING.
|
|
672
|
+
*
|
|
673
|
+
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields
|
|
674
|
+
* are unpacked by default. With editions, the default is whatever the edition
|
|
675
|
+
* specifies as a default. In edition 2023, fields are packed by default.
|
|
595
676
|
*/
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
if (
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
677
|
+
function isPackedFieldByDefault(file, proto, resolveFeatures) {
|
|
678
|
+
const { repeatedFieldEncoding } = resolveFeatures(file.edition);
|
|
679
|
+
if (repeatedFieldEncoding != FeatureSet_RepeatedFieldEncoding.PACKED) {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
// From the proto3 language guide:
|
|
683
|
+
// > In proto3, repeated fields of scalar numeric types are packed by default.
|
|
684
|
+
// This information is incomplete - according to the conformance tests, BOOL
|
|
685
|
+
// and ENUM are packed by default as well. This means only STRING and BYTES
|
|
686
|
+
// are not packed by default, which makes sense because they are length-delimited.
|
|
687
|
+
switch (proto.type) {
|
|
688
|
+
case FieldDescriptorProto_Type.STRING:
|
|
689
|
+
case FieldDescriptorProto_Type.BYTES:
|
|
690
|
+
case FieldDescriptorProto_Type.GROUP:
|
|
691
|
+
case FieldDescriptorProto_Type.MESSAGE:
|
|
692
|
+
return false;
|
|
693
|
+
default:
|
|
694
|
+
return true;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Pack this repeated field?
|
|
699
|
+
*
|
|
700
|
+
* Respects field type, proto2/proto3 defaults and the `packed` option, or
|
|
701
|
+
* edition defaults and the edition features.repeated_field_encoding options.
|
|
702
|
+
*/
|
|
703
|
+
function isPackedField(file, parent, proto, resolveFeatures) {
|
|
704
|
+
var _a, _b, _c, _d, _e, _f;
|
|
705
|
+
switch (proto.type) {
|
|
706
|
+
case FieldDescriptorProto_Type.STRING:
|
|
707
|
+
case FieldDescriptorProto_Type.BYTES:
|
|
708
|
+
case FieldDescriptorProto_Type.GROUP:
|
|
709
|
+
case FieldDescriptorProto_Type.MESSAGE:
|
|
710
|
+
// length-delimited types cannot be packed
|
|
711
|
+
return false;
|
|
712
|
+
default:
|
|
713
|
+
switch (file.edition) {
|
|
714
|
+
case Edition.EDITION_PROTO2:
|
|
715
|
+
return (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.packed) !== null && _b !== void 0 ? _b : false;
|
|
716
|
+
case Edition.EDITION_PROTO3:
|
|
717
|
+
return (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : true;
|
|
718
|
+
default: {
|
|
719
|
+
const { repeatedFieldEncoding } = resolveFeatures(file.edition, (_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features);
|
|
720
|
+
return (repeatedFieldEncoding == FeatureSet_RepeatedFieldEncoding.PACKED);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
623
723
|
}
|
|
624
|
-
return false;
|
|
625
724
|
}
|
|
626
725
|
/**
|
|
627
726
|
* Map from a compiler-generated field type to our ScalarType, which is a
|
|
@@ -779,175 +878,11 @@ function getDefaultValue() {
|
|
|
779
878
|
return undefined;
|
|
780
879
|
}
|
|
781
880
|
switch (this.fieldKind) {
|
|
782
|
-
case "enum":
|
|
783
|
-
|
|
784
|
-
assert(enumValue, `cannot parse ${this.toString()} default value: ${d}`);
|
|
785
|
-
return enumValue.number;
|
|
786
|
-
}
|
|
881
|
+
case "enum":
|
|
882
|
+
return parseTextFormatEnumValue(this.enum, d);
|
|
787
883
|
case "scalar":
|
|
788
|
-
|
|
789
|
-
case ScalarType.STRING:
|
|
790
|
-
return d;
|
|
791
|
-
case ScalarType.BYTES: {
|
|
792
|
-
const u = unescapeBytesDefaultValue(d);
|
|
793
|
-
if (u === false) {
|
|
794
|
-
throw new Error(`cannot parse ${this.toString()} default value: ${d}`);
|
|
795
|
-
}
|
|
796
|
-
return u;
|
|
797
|
-
}
|
|
798
|
-
case ScalarType.INT64:
|
|
799
|
-
case ScalarType.SFIXED64:
|
|
800
|
-
case ScalarType.SINT64:
|
|
801
|
-
return protoInt64.parse(d);
|
|
802
|
-
case ScalarType.UINT64:
|
|
803
|
-
case ScalarType.FIXED64:
|
|
804
|
-
return protoInt64.uParse(d);
|
|
805
|
-
case ScalarType.DOUBLE:
|
|
806
|
-
case ScalarType.FLOAT:
|
|
807
|
-
switch (d) {
|
|
808
|
-
case "inf":
|
|
809
|
-
return Number.POSITIVE_INFINITY;
|
|
810
|
-
case "-inf":
|
|
811
|
-
return Number.NEGATIVE_INFINITY;
|
|
812
|
-
case "nan":
|
|
813
|
-
return Number.NaN;
|
|
814
|
-
default:
|
|
815
|
-
return parseFloat(d);
|
|
816
|
-
}
|
|
817
|
-
case ScalarType.BOOL:
|
|
818
|
-
return d === "true";
|
|
819
|
-
case ScalarType.INT32:
|
|
820
|
-
case ScalarType.UINT32:
|
|
821
|
-
case ScalarType.SINT32:
|
|
822
|
-
case ScalarType.FIXED32:
|
|
823
|
-
case ScalarType.SFIXED32:
|
|
824
|
-
return parseInt(d, 10);
|
|
825
|
-
}
|
|
826
|
-
break;
|
|
884
|
+
return parseTextFormatScalarValue(this.scalar, d);
|
|
827
885
|
default:
|
|
828
886
|
return undefined;
|
|
829
887
|
}
|
|
830
888
|
}
|
|
831
|
-
/**
|
|
832
|
-
* Parses a text-encoded default value (proto2) of a BYTES field.
|
|
833
|
-
*/
|
|
834
|
-
function unescapeBytesDefaultValue(str) {
|
|
835
|
-
const b = [];
|
|
836
|
-
const input = {
|
|
837
|
-
tail: str,
|
|
838
|
-
c: "",
|
|
839
|
-
next() {
|
|
840
|
-
if (this.tail.length == 0) {
|
|
841
|
-
return false;
|
|
842
|
-
}
|
|
843
|
-
this.c = this.tail[0];
|
|
844
|
-
this.tail = this.tail.substring(1);
|
|
845
|
-
return true;
|
|
846
|
-
},
|
|
847
|
-
take(n) {
|
|
848
|
-
if (this.tail.length >= n) {
|
|
849
|
-
const r = this.tail.substring(0, n);
|
|
850
|
-
this.tail = this.tail.substring(n);
|
|
851
|
-
return r;
|
|
852
|
-
}
|
|
853
|
-
return false;
|
|
854
|
-
},
|
|
855
|
-
};
|
|
856
|
-
while (input.next()) {
|
|
857
|
-
switch (input.c) {
|
|
858
|
-
case "\\":
|
|
859
|
-
if (input.next()) {
|
|
860
|
-
switch (input.c) {
|
|
861
|
-
case "\\":
|
|
862
|
-
b.push(input.c.charCodeAt(0));
|
|
863
|
-
break;
|
|
864
|
-
case "b":
|
|
865
|
-
b.push(0x08);
|
|
866
|
-
break;
|
|
867
|
-
case "f":
|
|
868
|
-
b.push(0x0c);
|
|
869
|
-
break;
|
|
870
|
-
case "n":
|
|
871
|
-
b.push(0x0a);
|
|
872
|
-
break;
|
|
873
|
-
case "r":
|
|
874
|
-
b.push(0x0d);
|
|
875
|
-
break;
|
|
876
|
-
case "t":
|
|
877
|
-
b.push(0x09);
|
|
878
|
-
break;
|
|
879
|
-
case "v":
|
|
880
|
-
b.push(0x0b);
|
|
881
|
-
break;
|
|
882
|
-
case "0":
|
|
883
|
-
case "1":
|
|
884
|
-
case "2":
|
|
885
|
-
case "3":
|
|
886
|
-
case "4":
|
|
887
|
-
case "5":
|
|
888
|
-
case "6":
|
|
889
|
-
case "7": {
|
|
890
|
-
const s = input.c;
|
|
891
|
-
const t = input.take(2);
|
|
892
|
-
if (t === false) {
|
|
893
|
-
return false;
|
|
894
|
-
}
|
|
895
|
-
const n = parseInt(s + t, 8);
|
|
896
|
-
if (isNaN(n)) {
|
|
897
|
-
return false;
|
|
898
|
-
}
|
|
899
|
-
b.push(n);
|
|
900
|
-
break;
|
|
901
|
-
}
|
|
902
|
-
case "x": {
|
|
903
|
-
const s = input.c;
|
|
904
|
-
const t = input.take(2);
|
|
905
|
-
if (t === false) {
|
|
906
|
-
return false;
|
|
907
|
-
}
|
|
908
|
-
const n = parseInt(s + t, 16);
|
|
909
|
-
if (isNaN(n)) {
|
|
910
|
-
return false;
|
|
911
|
-
}
|
|
912
|
-
b.push(n);
|
|
913
|
-
break;
|
|
914
|
-
}
|
|
915
|
-
case "u": {
|
|
916
|
-
const s = input.c;
|
|
917
|
-
const t = input.take(4);
|
|
918
|
-
if (t === false) {
|
|
919
|
-
return false;
|
|
920
|
-
}
|
|
921
|
-
const n = parseInt(s + t, 16);
|
|
922
|
-
if (isNaN(n)) {
|
|
923
|
-
return false;
|
|
924
|
-
}
|
|
925
|
-
const chunk = new Uint8Array(4);
|
|
926
|
-
const view = new DataView(chunk.buffer);
|
|
927
|
-
view.setInt32(0, n, true);
|
|
928
|
-
b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
|
|
929
|
-
break;
|
|
930
|
-
}
|
|
931
|
-
case "U": {
|
|
932
|
-
const s = input.c;
|
|
933
|
-
const t = input.take(8);
|
|
934
|
-
if (t === false) {
|
|
935
|
-
return false;
|
|
936
|
-
}
|
|
937
|
-
const tc = protoInt64.uEnc(s + t);
|
|
938
|
-
const chunk = new Uint8Array(8);
|
|
939
|
-
const view = new DataView(chunk.buffer);
|
|
940
|
-
view.setInt32(0, tc.lo, true);
|
|
941
|
-
view.setInt32(4, tc.hi, true);
|
|
942
|
-
b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
|
|
943
|
-
break;
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
break;
|
|
948
|
-
default:
|
|
949
|
-
b.push(input.c.charCodeAt(0));
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
return new Uint8Array(b);
|
|
953
|
-
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js";
|
|
2
|
-
import type {
|
|
1
|
+
import type { DescriptorProto, Edition, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js";
|
|
2
|
+
import type { LongType, ScalarType } from "./field.js";
|
|
3
3
|
import type { MethodIdempotency, MethodKind } from "./service-type.js";
|
|
4
|
+
import type { MergedFeatureSet } from "./private/feature-set.js";
|
|
4
5
|
/**
|
|
5
6
|
* DescriptorSet provides a convenient interface for working with a set
|
|
6
7
|
* of google.protobuf.FileDescriptorProto.
|
|
@@ -50,7 +51,12 @@ export interface DescFile {
|
|
|
50
51
|
/**
|
|
51
52
|
* The syntax specified in the protobuf source.
|
|
52
53
|
*/
|
|
53
|
-
readonly syntax: "proto3" | "proto2";
|
|
54
|
+
readonly syntax: "proto3" | "proto2" | "editions";
|
|
55
|
+
/**
|
|
56
|
+
* The edition of the protobuf file. Will be EDITION_PROTO2 for syntax="proto2",
|
|
57
|
+
* EDITION_PROTO3 for syntax="proto3";
|
|
58
|
+
*/
|
|
59
|
+
readonly edition: Exclude<Edition, Edition.EDITION_1_TEST_ONLY | Edition.EDITION_2_TEST_ONLY | Edition.EDITION_99997_TEST_ONLY | Edition.EDITION_99998_TEST_ONLY | Edition.EDITION_99999_TEST_ONLY>;
|
|
54
60
|
/**
|
|
55
61
|
* The name of the file, excluding the .proto suffix.
|
|
56
62
|
* For a protobuf file `foo/bar.proto`, this is `foo/bar`.
|
|
@@ -91,6 +97,10 @@ export interface DescFile {
|
|
|
91
97
|
* Get comments on the package element in the protobuf source.
|
|
92
98
|
*/
|
|
93
99
|
getPackageComments(): DescComments;
|
|
100
|
+
/**
|
|
101
|
+
* Get the edition features for this protobuf element.
|
|
102
|
+
*/
|
|
103
|
+
getFeatures(): MergedFeatureSet;
|
|
94
104
|
toString(): string;
|
|
95
105
|
}
|
|
96
106
|
/**
|
|
@@ -135,6 +145,10 @@ export interface DescEnum {
|
|
|
135
145
|
* Get comments on the element in the protobuf source.
|
|
136
146
|
*/
|
|
137
147
|
getComments(): DescComments;
|
|
148
|
+
/**
|
|
149
|
+
* Get the edition features for this protobuf element.
|
|
150
|
+
*/
|
|
151
|
+
getFeatures(): MergedFeatureSet;
|
|
138
152
|
toString(): string;
|
|
139
153
|
}
|
|
140
154
|
/**
|
|
@@ -171,6 +185,10 @@ export interface DescEnumValue {
|
|
|
171
185
|
* Get comments on the element in the protobuf source.
|
|
172
186
|
*/
|
|
173
187
|
getComments(): DescComments;
|
|
188
|
+
/**
|
|
189
|
+
* Get the edition features for this protobuf element.
|
|
190
|
+
*/
|
|
191
|
+
getFeatures(): MergedFeatureSet;
|
|
174
192
|
toString(): string;
|
|
175
193
|
}
|
|
176
194
|
/**
|
|
@@ -234,6 +252,10 @@ export interface DescMessage {
|
|
|
234
252
|
* Get comments on the element in the protobuf source.
|
|
235
253
|
*/
|
|
236
254
|
getComments(): DescComments;
|
|
255
|
+
/**
|
|
256
|
+
* Get the edition features for this protobuf element.
|
|
257
|
+
*/
|
|
258
|
+
getFeatures(): MergedFeatureSet;
|
|
237
259
|
toString(): string;
|
|
238
260
|
}
|
|
239
261
|
/**
|
|
@@ -290,10 +312,14 @@ interface DescFieldCommon {
|
|
|
290
312
|
*/
|
|
291
313
|
readonly packed: boolean;
|
|
292
314
|
/**
|
|
293
|
-
* Is this field packed by default? Only valid for enum fields, and
|
|
294
|
-
* scalar fields except BYTES and STRING.
|
|
315
|
+
* Is this field packed by default? Only valid for repeated enum fields, and
|
|
316
|
+
* for repeated scalar fields except BYTES and STRING.
|
|
317
|
+
*
|
|
295
318
|
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields
|
|
296
319
|
* are unpacked by default.
|
|
320
|
+
*
|
|
321
|
+
* With editions, the default is whatever the edition specifies as a default.
|
|
322
|
+
* In edition 2023, fields are packed by default.
|
|
297
323
|
*/
|
|
298
324
|
readonly packedByDefault: boolean;
|
|
299
325
|
/**
|
|
@@ -318,6 +344,10 @@ interface DescFieldCommon {
|
|
|
318
344
|
* protobuf source.
|
|
319
345
|
*/
|
|
320
346
|
declarationString(): string;
|
|
347
|
+
/**
|
|
348
|
+
* Get the edition features for this protobuf element.
|
|
349
|
+
*/
|
|
350
|
+
getFeatures(): MergedFeatureSet;
|
|
321
351
|
toString(): string;
|
|
322
352
|
}
|
|
323
353
|
interface DescFieldScalar {
|
|
@@ -534,6 +564,10 @@ export interface DescOneof {
|
|
|
534
564
|
* Get comments on the element in the protobuf source.
|
|
535
565
|
*/
|
|
536
566
|
getComments(): DescComments;
|
|
567
|
+
/**
|
|
568
|
+
* Get the edition features for this protobuf element.
|
|
569
|
+
*/
|
|
570
|
+
getFeatures(): MergedFeatureSet;
|
|
537
571
|
toString(): string;
|
|
538
572
|
}
|
|
539
573
|
/**
|
|
@@ -569,6 +603,10 @@ export interface DescService {
|
|
|
569
603
|
* Get comments on the element in the protobuf source.
|
|
570
604
|
*/
|
|
571
605
|
getComments(): DescComments;
|
|
606
|
+
/**
|
|
607
|
+
* Get the edition features for this protobuf element.
|
|
608
|
+
*/
|
|
609
|
+
getFeatures(): MergedFeatureSet;
|
|
572
610
|
toString(): string;
|
|
573
611
|
}
|
|
574
612
|
/**
|
|
@@ -612,6 +650,10 @@ export interface DescMethod {
|
|
|
612
650
|
* Get comments on the element in the protobuf source.
|
|
613
651
|
*/
|
|
614
652
|
getComments(): DescComments;
|
|
653
|
+
/**
|
|
654
|
+
* Get the edition features for this protobuf element.
|
|
655
|
+
*/
|
|
656
|
+
getFeatures(): MergedFeatureSet;
|
|
615
657
|
toString(): string;
|
|
616
658
|
}
|
|
617
659
|
/**
|
|
@@ -237,7 +237,7 @@ export class Any extends Message {
|
|
|
237
237
|
throw new Error(`invalid type url: ${url}`);
|
|
238
238
|
}
|
|
239
239
|
const slash = url.lastIndexOf("/");
|
|
240
|
-
const name = slash
|
|
240
|
+
const name = slash >= 0 ? url.substring(slash + 1) : url;
|
|
241
241
|
if (!name.length) {
|
|
242
242
|
throw new Error(`invalid type url: ${url}`);
|
|
243
243
|
}
|