ts-proto 1.142.0 → 1.143.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.
@@ -90,9 +90,9 @@ function generateRegularRpcMethod(ctx, methodDesc) {
90
90
  const maybeCtx = options.context ? "ctx," : "";
91
91
  const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";
92
92
  let encode = (0, ts_poet_1.code) `${rawInputType}.encode(request).finish()`;
93
- let decode = (0, ts_poet_1.code) `data => ${rawOutputType}.decode(new ${Reader}(data))`;
93
+ let decode = (0, ts_poet_1.code) `data => ${rawOutputType}.decode(${Reader}.create(data))`;
94
94
  // if (options.useDate && rawOutputType.toString().includes("Timestamp")) {
95
- // decode = code`data => ${utils.fromTimestamp}(${rawOutputType}.decode(new ${Reader}(data)))`;
95
+ // decode = code`data => ${utils.fromTimestamp}(${rawOutputType}.decode(${Reader}.create(data)))`;
96
96
  // }
97
97
  if (methodDesc.clientStreaming) {
98
98
  if (options.useAsyncIterable) {
@@ -239,7 +239,7 @@ function generateCachingRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
239
239
  const responses = requests.map(async request => {
240
240
  const data = ${inputType}.encode(request).finish()
241
241
  const response = await this.rpc.request(ctx, "${(0, utils_1.maybePrefixPackage)(fileDesc, serviceDesc.name)}", "${methodDesc.name}", data);
242
- return ${outputType}.decode(new ${Reader}(response));
242
+ return ${outputType}.decode(${Reader}.create(response));
243
243
  });
244
244
  return Promise.all(responses);
245
245
  }
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");
@@ -669,7 +670,7 @@ function generateDecode(ctx, fullName, messageDesc) {
669
670
  input: ${Reader} | Uint8Array,
670
671
  length?: number,
671
672
  ): ${fullName} {
672
- const reader = input instanceof ${Reader} ? input : new ${Reader}(input);
673
+ const reader = input instanceof ${Reader} ? input : ${Reader}.create(input);
673
674
  let end = length === undefined ? reader.len : reader.pos + length;
674
675
  `);
675
676
  chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
@@ -686,6 +687,11 @@ function generateDecode(ctx, fullName, messageDesc) {
686
687
  messageDesc.field.forEach((field) => {
687
688
  const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
688
689
  chunks.push((0, ts_poet_1.code) `case ${field.number}:`);
690
+ const tag = ((field.number << 3) | (0, types_1.basicWireType)(field.type)) >>> 0;
691
+ const tagCheck = (0, ts_poet_1.code) `
692
+ if(tag != ${tag})
693
+ break;
694
+ `;
689
695
  // get a generic 'reader.doSomething' bit that is specific to the basic type
690
696
  let readSnippet;
691
697
  if ((0, types_1.isPrimitive)(field)) {
@@ -740,12 +746,16 @@ function generateDecode(ctx, fullName, messageDesc) {
740
746
  }
741
747
  else if ((0, types_1.isMessage)(field)) {
742
748
  const type = (0, types_1.basicTypeName)(ctx, field);
743
- readSnippet = (0, ts_poet_1.code) `${type}.decode(reader, reader.uint32())`;
749
+ if (field.type == ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP)
750
+ readSnippet = (0, ts_poet_1.code) `${type}.decode(reader)`;
751
+ else
752
+ readSnippet = (0, ts_poet_1.code) `${type}.decode(reader, reader.uint32())`;
744
753
  }
745
754
  else {
746
755
  throw new Error(`Unhandled field ${field}`);
747
756
  }
748
757
  // and then use the snippet to handle repeated fields if necessary
758
+ const initializerNecessary = !options.initializeFieldsAsUndefined && (0, types_1.isOptionalProperty)(field, messageDesc.options, options);
749
759
  if ((0, types_1.isRepeated)(field)) {
750
760
  const maybeNonNullAssertion = ctx.options.useOptionals === "all" ? "!" : "";
751
761
  if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
@@ -754,56 +764,97 @@ function generateDecode(ctx, fullName, messageDesc) {
754
764
  const valueSetterSnippet = ctx.options.useMapType
755
765
  ? `message.${fieldName}${maybeNonNullAssertion}.set(${varName}.key, ${varName}.value)`
756
766
  : `message.${fieldName}${maybeNonNullAssertion}[${varName}.key] = ${varName}.value`;
767
+ const initializerSnippet = initializerNecessary
768
+ ? `
769
+ if(message.${fieldName} === undefined)
770
+ message.${fieldName} = ${ctx.options.useMapType ? "new Map()" : "{}"};
771
+ `
772
+ : "";
757
773
  chunks.push((0, ts_poet_1.code) `
774
+ ${tagCheck}
758
775
  const ${varName} = ${readSnippet};
759
776
  if (${varName}.value !== undefined) {
777
+ ${initializerSnippet}
760
778
  ${valueSetterSnippet};
761
779
  }
762
780
  `);
763
781
  }
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
782
  else {
768
- chunks.push((0, ts_poet_1.code) `
769
- if ((tag & 7) === 2) {
770
- const end2 = reader.uint32() + reader.pos;
771
- while (reader.pos < end2) {
783
+ const initializerSnippet = initializerNecessary
784
+ ? `if(message.${fieldName} === undefined)
785
+ message.${fieldName} = [];`
786
+ : "";
787
+ if ((0, types_1.packedType)(field.type) === undefined) {
788
+ chunks.push((0, ts_poet_1.code) `
789
+ ${tagCheck}
790
+ ${initializerSnippet}
791
+ message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
792
+ `);
793
+ }
794
+ else {
795
+ const packedTag = ((field.number << 3) | 2) >>> 0;
796
+ chunks.push((0, ts_poet_1.code) `
797
+ if(tag == ${tag}){
798
+ ${initializerSnippet}
772
799
  message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
800
+ continue;
801
+ }
802
+
803
+ if(tag == ${packedTag}){
804
+ ${initializerSnippet}
805
+ const end2 = reader.uint32() + reader.pos;
806
+ while (reader.pos < end2) {
807
+ message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
808
+ }
809
+
810
+ continue;
773
811
  }
774
- } else {
775
- message.${fieldName}${maybeNonNullAssertion}.push(${readSnippet});
776
- }
777
- `);
812
+
813
+ break;
814
+ `);
815
+ }
778
816
  }
779
817
  }
780
818
  else if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
781
819
  let oneofName = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options);
782
- chunks.push((0, ts_poet_1.code) `message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet} };`);
820
+ chunks.push((0, ts_poet_1.code) `
821
+ ${tagCheck}
822
+ message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet} };
823
+ `);
783
824
  }
784
825
  else {
785
- chunks.push((0, ts_poet_1.code) `message.${fieldName} = ${readSnippet};`);
826
+ chunks.push((0, ts_poet_1.code) `
827
+ ${tagCheck}
828
+ message.${fieldName} = ${readSnippet};
829
+ `);
786
830
  }
787
- chunks.push((0, ts_poet_1.code) `break;`);
831
+ if (!(0, types_1.isRepeated)(field) || (0, types_1.packedType)(field.type) === undefined)
832
+ chunks.push((0, ts_poet_1.code) `continue;`);
788
833
  });
834
+ chunks.push((0, ts_poet_1.code) `}`);
835
+ chunks.push((0, ts_poet_1.code) `
836
+ if((tag & 7) == 4 || tag == 0)
837
+ break;
838
+ `);
789
839
  if (options.unknownFields) {
790
840
  chunks.push((0, ts_poet_1.code) `
791
- default:
792
841
  const startPos = reader.pos;
793
842
  reader.skipType(tag & 7);
794
- (message as any)._unknownFields[tag] = [...((message as any)._unknownFields[tag] || []), reader.buf.slice(startPos, reader.pos)];
795
- break;
843
+ const buf = reader.buf.slice(startPos, reader.pos);
844
+ const list = (message as any)._unknownFields[tag];
845
+
846
+ if(list === undefined)
847
+ (message as any)._unknownFields[tag] = [buf];
848
+ else
849
+ list.push(buf);
796
850
  `);
797
851
  }
798
852
  else {
799
853
  chunks.push((0, ts_poet_1.code) `
800
- default:
801
854
  reader.skipType(tag & 7);
802
- break;
803
855
  `);
804
856
  }
805
- // and then wrap up the switch/while/return
806
- chunks.push((0, ts_poet_1.code) `}`);
857
+ // and then wrap up the while/return
807
858
  chunks.push((0, ts_poet_1.code) `}`);
808
859
  chunks.push((0, ts_poet_1.code) `return message;`);
809
860
  chunks.push((0, ts_poet_1.code) `}`);
@@ -848,8 +899,12 @@ function getEncodeWriteSnippet(ctx, field) {
848
899
  return (place) => (0, ts_poet_1.code) `${type}.encode(${wrappedValue(place)}, writer.uint32(${tag}).fork()).ldelim()`;
849
900
  }
850
901
  else if ((0, types_1.isMessage)(field)) {
851
- const tag = ((field.number << 3) | 2) >>> 0;
852
902
  const type = (0, types_1.basicTypeName)(ctx, field);
903
+ if (field.type == ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_GROUP) {
904
+ const startTag = ((field.number << 3) | 3) >>> 0, endTag = ((field.number << 3) | 4) >>> 0;
905
+ return (place) => (0, ts_poet_1.code) `${type}.encode(${place}, writer.uint32(${startTag})).uint32(${endTag})`;
906
+ }
907
+ const tag = ((field.number << 3) | 2) >>> 0;
853
908
  return (place) => (0, ts_poet_1.code) `${type}.encode(${place}, writer.uint32(${tag}).fork()).ldelim()`;
854
909
  }
855
910
  else {
@@ -978,8 +1033,8 @@ function generateEncode(ctx, fullName, messageDesc) {
978
1033
  for (const oneOfField of oneOfFieldsDict[field.oneofIndex]) {
979
1034
  const writeSnippet = getEncodeWriteSnippet(ctx, oneOfField);
980
1035
  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}`)};
1036
+ chunks.push((0, ts_poet_1.code) `case "${oneOfFieldName}":
1037
+ ${writeSnippet(`message.${oneofName}.${oneOfFieldName}`)};
983
1038
  break;`);
984
1039
  }
985
1040
  chunks.push((0, ts_poet_1.code) `}`);
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.142.0",
3
+ "version": "1.143.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": "^8.2.2",
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",