ts-proto 2.13.1 → 2.13.3

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.
@@ -16,6 +16,7 @@ declare function makeByteUtils(options: Options): {
16
16
  globalThis: ConditionalOutput;
17
17
  bytesFromBase64: ConditionalOutput;
18
18
  base64FromBytes: ConditionalOutput;
19
+ floatToJson: ConditionalOutput;
19
20
  };
20
21
  declare function makeDeepPartial(options: Options, longs: ReturnType<typeof makeLongUtils>): {
21
22
  Builtin: ConditionalOutput;
package/build/src/main.js CHANGED
@@ -473,7 +473,14 @@ function makeByteUtils(options) {
473
473
  ${getBase64FromBytesSnippet()}
474
474
  }
475
475
  `);
476
- return { globalThis, bytesFromBase64, base64FromBytes };
476
+ // JSON numbers can't represent NaN or +/-Infinity, so the proto3 JSON mapping
477
+ // writes them as the strings "NaN", "Infinity" and "-Infinity".
478
+ const floatToJson = (0, ts_poet_1.conditionalOutput)("floatToJson", (0, ts_poet_1.code) `
479
+ function floatToJson(value: number | null | undefined): number | string | null | undefined {
480
+ return typeof value === "number" && !${globalThis}.Number.isFinite(value) ? ${globalThis}.String(value) : value;
481
+ }
482
+ `);
483
+ return { globalThis, bytesFromBase64, base64FromBytes, floatToJson };
477
484
  }
478
485
  function makeDeepPartial(options, longs) {
479
486
  let oneofCase = "";
@@ -2307,6 +2314,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2307
2314
  else if ((0, types_1.isWholeNumber)(valueType) && !((0, types_1.isLong)(valueType) && options.forceLong === options_1.LongOption.STRING)) {
2308
2315
  return (0, ts_poet_1.code) `Math.round(${from})`;
2309
2316
  }
2317
+ else if ((0, types_1.isFloatingPoint)(valueType)) {
2318
+ return (0, ts_poet_1.code) `${utils.floatToJson}(${from})`;
2319
+ }
2310
2320
  else if ((0, types_1.isScalar)(valueType) || (0, types_1.isValueType)(ctx, valueType)) {
2311
2321
  return (0, ts_poet_1.code) `${from}`;
2312
2322
  }
@@ -2349,6 +2359,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2349
2359
  else if ((0, types_1.isWholeNumber)(field) && !((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.STRING)) {
2350
2360
  return (0, ts_poet_1.code) `Math.round(${from})`;
2351
2361
  }
2362
+ else if ((0, types_1.isFloatingPoint)(field)) {
2363
+ return (0, ts_poet_1.code) `${utils.floatToJson}(${from})`;
2364
+ }
2352
2365
  else {
2353
2366
  return (0, ts_poet_1.code) `${from}`;
2354
2367
  }
@@ -2669,10 +2682,9 @@ function convertToObjectKey(ctx, messageDesc, field, variableName) {
2669
2682
  return (0, ts_poet_1.code) `${variableName}`;
2670
2683
  }
2671
2684
  else if ((0, types_1.isLong)(keyField) && (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
2672
- if (ctx.options.forceLong === options_1.LongOption.LONG) {
2673
- return (0, ts_poet_1.code) `${ctx.utils.longToNumber}(${variableName})`;
2674
- }
2675
- else if (ctx.options.forceLong === options_1.LongOption.BIGINT) {
2685
+ // JSON object keys are strings, and converting a 64-bit key to a JS number
2686
+ // would lose precision (or throw, for Long) above Number.MAX_SAFE_INTEGER.
2687
+ if (ctx.options.forceLong === options_1.LongOption.LONG || ctx.options.forceLong === options_1.LongOption.BIGINT) {
2676
2688
  return (0, ts_poet_1.code) `${variableName}.toString()`;
2677
2689
  }
2678
2690
  else {
@@ -34,6 +34,8 @@ export declare function isWithinOneOfThatShouldBeUnion(options: Options, field:
34
34
  export declare function isRepeated(field: FieldDescriptorProto): boolean;
35
35
  export declare function isLong(field: FieldDescriptorProto): boolean;
36
36
  export declare function isWholeNumber(field: FieldDescriptorProto): boolean;
37
+ /** Whether `field` holds a JS number that may be non-finite: a double/float or its wrapper type. */
38
+ export declare function isFloatingPoint(field: FieldDescriptorProto): boolean;
37
39
  export declare function isMapType(ctx: Context, messageDesc: DescriptorProto, field: FieldDescriptorProto): boolean;
38
40
  export declare function isObjectId(field: FieldDescriptorProto): boolean;
39
41
  export declare function isTimestamp(field: FieldDescriptorProto): boolean;
@@ -21,6 +21,7 @@ exports.isWithinOneOfThatShouldBeUnion = isWithinOneOfThatShouldBeUnion;
21
21
  exports.isRepeated = isRepeated;
22
22
  exports.isLong = isLong;
23
23
  exports.isWholeNumber = isWholeNumber;
24
+ exports.isFloatingPoint = isFloatingPoint;
24
25
  exports.isMapType = isMapType;
25
26
  exports.isObjectId = isObjectId;
26
27
  exports.isTimestamp = isTimestamp;
@@ -467,6 +468,13 @@ function isWholeNumber(field) {
467
468
  field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED32 ||
468
469
  field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED64);
469
470
  }
471
+ /** Whether `field` holds a JS number that may be non-finite: a double/float or its wrapper type. */
472
+ function isFloatingPoint(field) {
473
+ return (field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_DOUBLE ||
474
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FLOAT ||
475
+ field.typeName === ".google.protobuf.DoubleValue" ||
476
+ field.typeName === ".google.protobuf.FloatValue");
477
+ }
470
478
  function isMapType(ctx, messageDesc, field) {
471
479
  return detectMapType(ctx, messageDesc, field) !== undefined;
472
480
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.13.1",
3
+ "version": "2.13.3",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",