ts-proto 1.142.1 → 1.144.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/build/main.js +103 -35
- package/build/types.js +6 -1
- package/package.json +2 -2
package/build/main.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.contextTypeVar = exports.makeUtils = exports.generateFile = void 0;
|
|
4
4
|
const ts_poet_1 = require("ts-poet");
|
|
5
|
+
const ts_proto_descriptors_1 = require("ts-proto-descriptors");
|
|
5
6
|
const types_1 = require("./types");
|
|
6
7
|
const sourceInfo_1 = require("./sourceInfo");
|
|
7
8
|
const utils_1 = require("./utils");
|
|
@@ -579,6 +580,9 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
579
580
|
const q = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options) ? "?" : "";
|
|
580
581
|
chunks.push((0, ts_poet_1.code) `${maybeReadonly(options)}${name}${q}: ${type}, `);
|
|
581
582
|
});
|
|
583
|
+
if (ctx.options.unknownFields) {
|
|
584
|
+
chunks.push((0, ts_poet_1.code) `_unknownFields?: {[key: number]: Uint8Array[]},`);
|
|
585
|
+
}
|
|
582
586
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
583
587
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
584
588
|
}
|
|
@@ -648,6 +652,9 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
|
|
|
648
652
|
if (ctx.options.outputTypeRegistry) {
|
|
649
653
|
fields.unshift((0, ts_poet_1.code) `$type: '${fullTypeName}'`);
|
|
650
654
|
}
|
|
655
|
+
if (ctx.options.unknownFields && ctx.options.initializeFieldsAsUndefined) {
|
|
656
|
+
fields.push((0, ts_poet_1.code) `_unknownFields: {}`);
|
|
657
|
+
}
|
|
651
658
|
return (0, ts_poet_1.code) `
|
|
652
659
|
function createBase${fullName}(): ${fullName} {
|
|
653
660
|
return { ${(0, ts_poet_1.joinCode)(fields, { on: "," })} };
|
|
@@ -673,9 +680,6 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
673
680
|
let end = length === undefined ? reader.len : reader.pos + length;
|
|
674
681
|
`);
|
|
675
682
|
chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
|
|
676
|
-
if (options.unknownFields) {
|
|
677
|
-
chunks.push((0, ts_poet_1.code) `(message as any)._unknownFields = {}`);
|
|
678
|
-
}
|
|
679
683
|
// start the tag loop
|
|
680
684
|
chunks.push((0, ts_poet_1.code) `
|
|
681
685
|
while (reader.pos < end) {
|
|
@@ -686,6 +690,11 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
686
690
|
messageDesc.field.forEach((field) => {
|
|
687
691
|
const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
|
|
688
692
|
chunks.push((0, ts_poet_1.code) `case ${field.number}:`);
|
|
693
|
+
const tag = ((field.number << 3) | (0, types_1.basicWireType)(field.type)) >>> 0;
|
|
694
|
+
const tagCheck = (0, ts_poet_1.code) `
|
|
695
|
+
if(tag != ${tag})
|
|
696
|
+
break;
|
|
697
|
+
`;
|
|
689
698
|
// get a generic 'reader.doSomething' bit that is specific to the basic type
|
|
690
699
|
let readSnippet;
|
|
691
700
|
if ((0, types_1.isPrimitive)(field)) {
|
|
@@ -740,12 +749,16 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
740
749
|
}
|
|
741
750
|
else if ((0, types_1.isMessage)(field)) {
|
|
742
751
|
const type = (0, types_1.basicTypeName)(ctx, field);
|
|
743
|
-
|
|
752
|
+
if (field.type == ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP)
|
|
753
|
+
readSnippet = (0, ts_poet_1.code) `${type}.decode(reader)`;
|
|
754
|
+
else
|
|
755
|
+
readSnippet = (0, ts_poet_1.code) `${type}.decode(reader, reader.uint32())`;
|
|
744
756
|
}
|
|
745
757
|
else {
|
|
746
758
|
throw new Error(`Unhandled field ${field}`);
|
|
747
759
|
}
|
|
748
760
|
// and then use the snippet to handle repeated fields if necessary
|
|
761
|
+
const initializerNecessary = !options.initializeFieldsAsUndefined && (0, types_1.isOptionalProperty)(field, messageDesc.options, options);
|
|
749
762
|
if ((0, types_1.isRepeated)(field)) {
|
|
750
763
|
const maybeNonNullAssertion = ctx.options.useOptionals === "all" ? "!" : "";
|
|
751
764
|
if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
@@ -754,56 +767,107 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
754
767
|
const valueSetterSnippet = ctx.options.useMapType
|
|
755
768
|
? `message.${fieldName}${maybeNonNullAssertion}.set(${varName}.key, ${varName}.value)`
|
|
756
769
|
: `message.${fieldName}${maybeNonNullAssertion}[${varName}.key] = ${varName}.value`;
|
|
770
|
+
const initializerSnippet = initializerNecessary
|
|
771
|
+
? `
|
|
772
|
+
if(message.${fieldName} === undefined)
|
|
773
|
+
message.${fieldName} = ${ctx.options.useMapType ? "new Map()" : "{}"};
|
|
774
|
+
`
|
|
775
|
+
: "";
|
|
757
776
|
chunks.push((0, ts_poet_1.code) `
|
|
777
|
+
${tagCheck}
|
|
758
778
|
const ${varName} = ${readSnippet};
|
|
759
779
|
if (${varName}.value !== undefined) {
|
|
780
|
+
${initializerSnippet}
|
|
760
781
|
${valueSetterSnippet};
|
|
761
782
|
}
|
|
762
783
|
`);
|
|
763
784
|
}
|
|
764
|
-
else if ((0, types_1.packedType)(field.type) === undefined) {
|
|
765
|
-
chunks.push((0, ts_poet_1.code) `message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});`);
|
|
766
|
-
}
|
|
767
785
|
else {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
786
|
+
const initializerSnippet = initializerNecessary
|
|
787
|
+
? `if(message.${fieldName} === undefined)
|
|
788
|
+
message.${fieldName} = [];`
|
|
789
|
+
: "";
|
|
790
|
+
if ((0, types_1.packedType)(field.type) === undefined) {
|
|
791
|
+
chunks.push((0, ts_poet_1.code) `
|
|
792
|
+
${tagCheck}
|
|
793
|
+
${initializerSnippet}
|
|
794
|
+
message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
|
|
795
|
+
`);
|
|
796
|
+
}
|
|
797
|
+
else {
|
|
798
|
+
const packedTag = ((field.number << 3) | 2) >>> 0;
|
|
799
|
+
chunks.push((0, ts_poet_1.code) `
|
|
800
|
+
if(tag == ${tag}){
|
|
801
|
+
${initializerSnippet}
|
|
772
802
|
message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
if(tag == ${packedTag}){
|
|
807
|
+
${initializerSnippet}
|
|
808
|
+
const end2 = reader.uint32() + reader.pos;
|
|
809
|
+
while (reader.pos < end2) {
|
|
810
|
+
message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
continue;
|
|
773
814
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
815
|
+
|
|
816
|
+
break;
|
|
817
|
+
`);
|
|
818
|
+
}
|
|
778
819
|
}
|
|
779
820
|
}
|
|
780
821
|
else if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
|
|
781
822
|
let oneofName = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options);
|
|
782
|
-
chunks.push((0, ts_poet_1.code) `
|
|
823
|
+
chunks.push((0, ts_poet_1.code) `
|
|
824
|
+
${tagCheck}
|
|
825
|
+
message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet} };
|
|
826
|
+
`);
|
|
783
827
|
}
|
|
784
828
|
else {
|
|
785
|
-
chunks.push((0, ts_poet_1.code) `
|
|
829
|
+
chunks.push((0, ts_poet_1.code) `
|
|
830
|
+
${tagCheck}
|
|
831
|
+
message.${fieldName} = ${readSnippet};
|
|
832
|
+
`);
|
|
786
833
|
}
|
|
787
|
-
|
|
834
|
+
if (!(0, types_1.isRepeated)(field) || (0, types_1.packedType)(field.type) === undefined)
|
|
835
|
+
chunks.push((0, ts_poet_1.code) `continue;`);
|
|
788
836
|
});
|
|
837
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
838
|
+
chunks.push((0, ts_poet_1.code) `
|
|
839
|
+
if((tag & 7) == 4 || tag == 0)
|
|
840
|
+
break;
|
|
841
|
+
`);
|
|
789
842
|
if (options.unknownFields) {
|
|
843
|
+
let unknownFieldsInitializerSnippet = "";
|
|
844
|
+
let maybeNonNullAssertion = options.initializeFieldsAsUndefined ? "!" : "";
|
|
845
|
+
if (!options.initializeFieldsAsUndefined) {
|
|
846
|
+
unknownFieldsInitializerSnippet = `
|
|
847
|
+
if(message._unknownFields === undefined)
|
|
848
|
+
message._unknownFields = {};
|
|
849
|
+
`;
|
|
850
|
+
}
|
|
790
851
|
chunks.push((0, ts_poet_1.code) `
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
852
|
+
const startPos = reader.pos;
|
|
853
|
+
reader.skipType(tag & 7);
|
|
854
|
+
const buf = reader.buf.slice(startPos, reader.pos);
|
|
855
|
+
|
|
856
|
+
${unknownFieldsInitializerSnippet}
|
|
857
|
+
const list = message._unknownFields${maybeNonNullAssertion}[tag];
|
|
858
|
+
|
|
859
|
+
if(list === undefined)
|
|
860
|
+
message._unknownFields${maybeNonNullAssertion}[tag] = [buf];
|
|
861
|
+
else
|
|
862
|
+
list.push(buf);
|
|
796
863
|
`);
|
|
797
864
|
}
|
|
798
865
|
else {
|
|
799
866
|
chunks.push((0, ts_poet_1.code) `
|
|
800
|
-
default:
|
|
801
867
|
reader.skipType(tag & 7);
|
|
802
|
-
break;
|
|
803
868
|
`);
|
|
804
869
|
}
|
|
805
|
-
// and then wrap up the
|
|
806
|
-
chunks.push((0, ts_poet_1.code) `}`);
|
|
870
|
+
// and then wrap up the while/return
|
|
807
871
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
808
872
|
chunks.push((0, ts_poet_1.code) `return message;`);
|
|
809
873
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
@@ -848,8 +912,12 @@ function getEncodeWriteSnippet(ctx, field) {
|
|
|
848
912
|
return (place) => (0, ts_poet_1.code) `${type}.encode(${wrappedValue(place)}, writer.uint32(${tag}).fork()).ldelim()`;
|
|
849
913
|
}
|
|
850
914
|
else if ((0, types_1.isMessage)(field)) {
|
|
851
|
-
const tag = ((field.number << 3) | 2) >>> 0;
|
|
852
915
|
const type = (0, types_1.basicTypeName)(ctx, field);
|
|
916
|
+
if (field.type == ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP) {
|
|
917
|
+
const startTag = ((field.number << 3) | 3) >>> 0, endTag = ((field.number << 3) | 4) >>> 0;
|
|
918
|
+
return (place) => (0, ts_poet_1.code) `${type}.encode(${place}, writer.uint32(${startTag})).uint32(${endTag})`;
|
|
919
|
+
}
|
|
920
|
+
const tag = ((field.number << 3) | 2) >>> 0;
|
|
853
921
|
return (place) => (0, ts_poet_1.code) `${type}.encode(${place}, writer.uint32(${tag}).fork()).ldelim()`;
|
|
854
922
|
}
|
|
855
923
|
else {
|
|
@@ -978,8 +1046,8 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
978
1046
|
for (const oneOfField of oneOfFieldsDict[field.oneofIndex]) {
|
|
979
1047
|
const writeSnippet = getEncodeWriteSnippet(ctx, oneOfField);
|
|
980
1048
|
const oneOfFieldName = (0, case_1.maybeSnakeToCamel)(oneOfField.name, ctx.options);
|
|
981
|
-
chunks.push((0, ts_poet_1.code) `case "${oneOfFieldName}":
|
|
982
|
-
${writeSnippet(`message.${oneofName}.${oneOfFieldName}`)};
|
|
1049
|
+
chunks.push((0, ts_poet_1.code) `case "${oneOfFieldName}":
|
|
1050
|
+
${writeSnippet(`message.${oneofName}.${oneOfFieldName}`)};
|
|
983
1051
|
break;`);
|
|
984
1052
|
}
|
|
985
1053
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
@@ -1012,12 +1080,12 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
1012
1080
|
}
|
|
1013
1081
|
});
|
|
1014
1082
|
if (options.unknownFields) {
|
|
1015
|
-
chunks.push((0, ts_poet_1.code) `if (
|
|
1016
|
-
const
|
|
1017
|
-
|
|
1018
|
-
const
|
|
1083
|
+
chunks.push((0, ts_poet_1.code) `if (message._unknownFields !== undefined) {
|
|
1084
|
+
for (const key in message._unknownFields) {
|
|
1085
|
+
const values = message._unknownFields[key];
|
|
1086
|
+
const tag = parseInt(key, 10);
|
|
1019
1087
|
for (const value of values) {
|
|
1020
|
-
writer.uint32(
|
|
1088
|
+
writer.uint32(tag);
|
|
1021
1089
|
(writer as any)['_push'](
|
|
1022
1090
|
(val: Uint8Array, buf: Buffer, pos: number) => buf.set(val, pos),
|
|
1023
1091
|
value.length,
|
package/build/types.js
CHANGED
|
@@ -34,7 +34,10 @@ function basicWireType(type) {
|
|
|
34
34
|
return 0;
|
|
35
35
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_STRING:
|
|
36
36
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BYTES:
|
|
37
|
+
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE:
|
|
37
38
|
return 2;
|
|
39
|
+
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP:
|
|
40
|
+
return 3;
|
|
38
41
|
default:
|
|
39
42
|
throw new Error("Invalid type " + type);
|
|
40
43
|
}
|
|
@@ -85,6 +88,7 @@ function basicTypeName(ctx, field, typeOptions = {}) {
|
|
|
85
88
|
return (0, ts_poet_1.code) `Uint8Array`;
|
|
86
89
|
}
|
|
87
90
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE:
|
|
91
|
+
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP:
|
|
88
92
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_ENUM:
|
|
89
93
|
return messageToTypeName(ctx, field.typeName, { ...typeOptions, repeated: isRepeated(field) });
|
|
90
94
|
default:
|
|
@@ -225,6 +229,7 @@ function defaultValue(ctx, field) {
|
|
|
225
229
|
return "new Uint8Array()";
|
|
226
230
|
}
|
|
227
231
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE:
|
|
232
|
+
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP:
|
|
228
233
|
default:
|
|
229
234
|
return "undefined";
|
|
230
235
|
}
|
|
@@ -350,7 +355,7 @@ function isBytes(field) {
|
|
|
350
355
|
}
|
|
351
356
|
exports.isBytes = isBytes;
|
|
352
357
|
function isMessage(field) {
|
|
353
|
-
return field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE;
|
|
358
|
+
return field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE || field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP;
|
|
354
359
|
}
|
|
355
360
|
exports.isMessage = isMessage;
|
|
356
361
|
function isEnum(field) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-proto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.144.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/plugin.js",
|
|
6
6
|
"repository": "github:stephenh/ts-proto",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"@improbable-eng/grpc-web": "^0.14.0",
|
|
34
34
|
"@improbable-eng/grpc-web-node-http-transport": "^0.14.0",
|
|
35
35
|
"@nestjs/common": "^8.2.2",
|
|
36
|
-
"@nestjs/core": "^
|
|
36
|
+
"@nestjs/core": "^9.0.5",
|
|
37
37
|
"@nestjs/microservices": "^8.2.2",
|
|
38
38
|
"@semantic-release/changelog": "^5.0.1",
|
|
39
39
|
"@semantic-release/commit-analyzer": "^8.0.1",
|