ts-proto 1.141.1 → 1.142.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.
@@ -53,11 +53,11 @@ function generateMethodDefinition(ctx, methodDesc) {
53
53
  requestStream: ${methodDesc.clientStreaming},
54
54
  responseType: ${outputType},
55
55
  responseStream: ${methodDesc.serverStreaming},
56
- options: ${generateMethodOptions(methodDesc.options)}
56
+ options: ${generateMethodOptions(ctx, methodDesc.options)}
57
57
  }
58
58
  `;
59
59
  }
60
- function generateMethodOptions(options) {
60
+ function generateMethodOptions(ctx, options) {
61
61
  const chunks = [];
62
62
  chunks.push((0, ts_poet_1.code) `{`);
63
63
  if (options != null) {
@@ -67,6 +67,21 @@ function generateMethodOptions(options) {
67
67
  else if (options.idempotencyLevel === ts_proto_descriptors_1.MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS) {
68
68
  chunks.push((0, ts_poet_1.code) `idempotencyLevel: 'NO_SIDE_EFFECTS',`);
69
69
  }
70
+ if ("_unknownFields" in options) {
71
+ const msgUnknownFields = options["_unknownFields"];
72
+ const unknownFieldsChunks = [];
73
+ unknownFieldsChunks.push((0, ts_poet_1.code) `{`);
74
+ for (const key of Object.keys(msgUnknownFields)) {
75
+ const values = msgUnknownFields[key];
76
+ const valuesChunks = [];
77
+ for (const value of values) {
78
+ valuesChunks.push((0, ts_poet_1.code) `${ctx.options.env == "node" ? "Buffer.from" : "new Uint8Array"}([${value.join(", ")}])`);
79
+ }
80
+ unknownFieldsChunks.push((0, ts_poet_1.code) `${key}: [\n${(0, ts_poet_1.joinCode)(valuesChunks, { on: "," }).toCodeString([])}\n],`);
81
+ }
82
+ unknownFieldsChunks.push((0, ts_poet_1.code) `}`);
83
+ chunks.push((0, ts_poet_1.code) `_unknownFields: ${(0, ts_poet_1.joinCode)(unknownFieldsChunks, { on: "\n" }).toCodeString([])}`);
84
+ }
70
85
  }
71
86
  chunks.push((0, ts_poet_1.code) `}`);
72
87
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
@@ -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
@@ -392,7 +392,7 @@ function makeDeepPartial(options, longs) {
392
392
  const maybeExport = options.exportCommonSymbols ? "export" : "";
393
393
  // Allow passing longs as numbers or strings, nad we'll convert them
394
394
  const maybeLong = options.forceLong === options_1.LongOption.LONG ? (0, ts_poet_1.code) ` : T extends ${longs.Long} ? string | number | Long ` : "";
395
- const Builtin = (0, ts_poet_1.conditionalOutput)("Builtin", (0, ts_poet_1.code) `type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;`);
395
+ const Builtin = (0, ts_poet_1.conditionalOutput)("Builtin", (0, ts_poet_1.code) `type Builtin = Date | Function | Uint8Array | string | number | boolean |${options.forceLong === options_1.LongOption.BIGINT ? " bigint |" : ""} undefined;`);
396
396
  // Based on https://github.com/sindresorhus/type-fest/pull/259
397
397
  const maybeExcludeType = options.outputTypeRegistry ? `| '$type'` : "";
398
398
  const Exact = (0, ts_poet_1.conditionalOutput)("Exact", (0, ts_poet_1.code) `
@@ -669,7 +669,7 @@ function generateDecode(ctx, fullName, messageDesc) {
669
669
  input: ${Reader} | Uint8Array,
670
670
  length?: number,
671
671
  ): ${fullName} {
672
- const reader = input instanceof ${Reader} ? input : new ${Reader}(input);
672
+ const reader = input instanceof ${Reader} ? input : ${Reader}.create(input);
673
673
  let end = length === undefined ? reader.len : reader.pos + length;
674
674
  `);
675
675
  chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
@@ -1082,14 +1082,14 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1082
1082
  }
1083
1083
  }
1084
1084
  else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.LONG) {
1085
- const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString());
1085
+ const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString([]));
1086
1086
  return (0, ts_poet_1.code) `${cstr}.fromValue(${from})`;
1087
1087
  }
1088
1088
  else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT) {
1089
1089
  return (0, ts_poet_1.code) `BigInt(${from})`;
1090
1090
  }
1091
1091
  else {
1092
- const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString());
1092
+ const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString([]));
1093
1093
  return (0, ts_poet_1.code) `${cstr}(${from})`;
1094
1094
  }
1095
1095
  }
@@ -1116,16 +1116,16 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1116
1116
  else if ((0, types_1.isValueType)(ctx, field)) {
1117
1117
  const valueType = (0, types_1.valueTypeName)(ctx, field.typeName);
1118
1118
  if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.LONG) {
1119
- return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString())}.fromValue(${from})`;
1119
+ return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}.fromValue(${from})`;
1120
1120
  }
1121
1121
  else if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.BIGINT) {
1122
1122
  return (0, ts_poet_1.code) `BigInt(${from})`;
1123
1123
  }
1124
1124
  else if ((0, types_1.isBytesValueType)(field)) {
1125
- return (0, ts_poet_1.code) `new ${(0, case_1.capitalize)(valueType.toCodeString())}(${from})`;
1125
+ return (0, ts_poet_1.code) `new ${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
1126
1126
  }
1127
1127
  else {
1128
- return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString())}(${from})`;
1128
+ return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
1129
1129
  }
1130
1130
  }
1131
1131
  else if ((0, types_1.isMessage)(field)) {
@@ -1149,7 +1149,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1149
1149
  return (0, ts_poet_1.code) `${fromJson}(${from})`;
1150
1150
  }
1151
1151
  else {
1152
- const cstr = (0, case_1.capitalize)(valueType.toCodeString());
1152
+ const cstr = (0, case_1.capitalize)(valueType.toCodeString([]));
1153
1153
  return (0, ts_poet_1.code) `${cstr}(${from})`;
1154
1154
  }
1155
1155
  }
@@ -1505,7 +1505,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1505
1505
  return (0, ts_poet_1.code) `Long.fromValue(${from})`;
1506
1506
  }
1507
1507
  else {
1508
- const cstr = (0, case_1.capitalize)(valueType.toCodeString());
1508
+ const cstr = (0, case_1.capitalize)(valueType.toCodeString([]));
1509
1509
  return (0, ts_poet_1.code) `${cstr}(${from})`;
1510
1510
  }
1511
1511
  }
@@ -1587,7 +1587,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1587
1587
  }
1588
1588
  `);
1589
1589
  }
1590
- else if (readSnippet(`x`).toCodeString() == "x") {
1590
+ else if (readSnippet(`x`).toCodeString([]) == "x") {
1591
1591
  // An optimized case of the else below that works when `readSnippet` returns the plain input
1592
1592
  const fallback = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
1593
1593
  chunks.push((0, ts_poet_1.code) `message.${fieldName} = object.${fieldName} ?? ${fallback};`);
@@ -1609,7 +1609,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1609
1609
  exports.contextTypeVar = "Context extends DataLoaders";
1610
1610
  function maybeCastToNumber(ctx, messageDesc, field, variableName) {
1611
1611
  const { keyType } = (0, types_1.detectMapType)(ctx, messageDesc, field);
1612
- if (keyType.toCodeString() === "string") {
1612
+ if (keyType.toCodeString([]) === "string") {
1613
1613
  return variableName;
1614
1614
  }
1615
1615
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.141.1",
3
+ "version": "1.142.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -64,7 +64,7 @@
64
64
  "dataloader": "^1.4.0",
65
65
  "object-hash": "^1.3.1",
66
66
  "protobufjs": "^6.11.3",
67
- "ts-poet": "^6.2.0",
67
+ "ts-poet": "^6.4.1",
68
68
  "ts-proto-descriptors": "1.7.1"
69
69
  },
70
70
  "packageManager": "yarn@3.3.0"