ts-proto 2.13.0 → 2.13.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.
@@ -39,6 +39,7 @@ declare function makeObjectIdMethods(): {
39
39
  declare function makeTimestampMethods(options: Options, longs: ReturnType<typeof makeLongUtils>, bytes: ReturnType<typeof makeByteUtils>): {
40
40
  toTimestamp: ConditionalOutput;
41
41
  fromTimestamp: ConditionalOutput;
42
+ timestampFractionRegex: ConditionalOutput;
42
43
  fromJsonTimestamp: ConditionalOutput;
43
44
  };
44
45
  declare function makeComparisonUtils(): {
package/build/src/main.js CHANGED
@@ -773,6 +773,8 @@ function makeTimestampMethods(options, longs, bytes) {
773
773
  return new ${bytes.globalThis}.Date(millis);
774
774
  }
775
775
  `);
776
+ // Declared once per file, so the regex isn't compiled on each fromJsonTimestamp call
777
+ const timestampFractionRegex = (0, ts_poet_1.conditionalOutput)("timestampFractionRegex", (0, ts_poet_1.code) `const timestampFractionRegex = /:\\d{2}\\.\\d+/;`);
776
778
  const fromJsonTimestamp = (0, ts_poet_1.conditionalOutput)("fromJsonTimestamp", options.useDate === options_1.DateOption.DATE
777
779
  ? (0, ts_poet_1.code) `
778
780
  function fromJsonTimestamp(o: any): Date {
@@ -802,13 +804,24 @@ function makeTimestampMethods(options, longs, bytes) {
802
804
  if (o instanceof ${bytes.globalThis}.Date) {
803
805
  return ${toTimestamp}(o);
804
806
  } else if (typeof o === "string") {
805
- return ${toTimestamp}(new ${bytes.globalThis}.Date(o));
807
+ // Date only keeps milliseconds, and parses longer fractions in an engine-specific way,
808
+ // so parse the fractional seconds ourselves and give Date only the whole seconds
809
+ const fraction = ${timestampFractionRegex}.exec(o);
810
+ if (fraction === null) {
811
+ return ${toTimestamp}(new ${bytes.globalThis}.Date(o));
812
+ }
813
+ const fractionNanos = Number(fraction[0].slice(4, 13).padEnd(9, "0"));
814
+ const wholeSeconds = new ${bytes.globalThis}.Date(o.replace(fraction[0], fraction[0].slice(0, 3)));
815
+ const timestamp = ${toTimestamp}(
816
+ new ${bytes.globalThis}.Date(wholeSeconds.getTime() + Math.trunc(fractionNanos / 1_000_000)),
817
+ );
818
+ return { ...timestamp, nanos: (timestamp.nanos ?? 0) + (fractionNanos % 1_000_000) };
806
819
  } else {
807
820
  return ${options.typePrefix}Timestamp${options.typeSuffix}.fromJSON(o);
808
821
  }
809
822
  }
810
823
  `);
811
- return { toTimestamp, fromTimestamp, fromJsonTimestamp };
824
+ return { toTimestamp, fromTimestamp, timestampFractionRegex, fromJsonTimestamp };
812
825
  }
813
826
  function makeComparisonUtils() {
814
827
  const isObject = (0, ts_poet_1.conditionalOutput)("isObject", (0, ts_poet_1.code) `
@@ -938,9 +951,7 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
938
951
  const { oneofIndex } = field;
939
952
  if (!processedOneofs.has(oneofIndex)) {
940
953
  processedOneofs.add(oneofIndex);
941
- const name = options.useJsonName
942
- ? (0, utils_1.getFieldName)(field, options)
943
- : (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[oneofIndex].name, ctx.options);
954
+ const name = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[oneofIndex].name, ctx.options);
944
955
  fields.push((0, ts_poet_1.code) `${(0, utils_1.safeAccessor)(name)}: ${(0, utils_1.nullOrUndefined)(options)}`);
945
956
  }
946
957
  continue;
@@ -1205,9 +1216,7 @@ function generateDecode(ctx, fullName, messageDesc) {
1205
1216
  }
1206
1217
  }
1207
1218
  else if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
1208
- const oneofNameWithMessage = options.useJsonName
1209
- ? messageProperty
1210
- : (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
1219
+ const oneofNameWithMessage = (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
1211
1220
  const valueName = (0, utils_1.oneofValueName)(fieldName, options);
1212
1221
  chunks.push((0, ts_poet_1.code) `
1213
1222
  ${tagCheck}
@@ -1509,9 +1518,7 @@ function generateEncode(ctx, fullName, messageDesc) {
1509
1518
  else if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
1510
1519
  if (!processedOneofs.has(field.oneofIndex)) {
1511
1520
  processedOneofs.add(field.oneofIndex);
1512
- const oneofNameWithMessage = options.useJsonName
1513
- ? messageProperty
1514
- : (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
1521
+ const oneofNameWithMessage = (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
1515
1522
  chunks.push((0, ts_poet_1.code) `switch (${oneofNameWithMessage}?.$case) {`);
1516
1523
  for (const oneOfField of oneOfFieldsDict[field.oneofIndex]) {
1517
1524
  const writeSnippet = getEncodeWriteSnippet(ctx, oneOfField);
@@ -2386,9 +2393,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2386
2393
  }
2387
2394
  else if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
2388
2395
  // oneofs in a union are only output as `oneof name = ...`
2389
- const oneofNameWithMessage = options.useJsonName
2390
- ? messageProperty
2391
- : (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
2396
+ const oneofNameWithMessage = (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
2392
2397
  const valueName = (0, utils_1.oneofValueName)(fieldName, options);
2393
2398
  chunks.push((0, ts_poet_1.code) `
2394
2399
  ${currentIfTarget === oneofNameWithMessage ? "else " : ""}if (${oneofNameWithMessage}?.$case === '${fieldName}') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.13.0",
3
+ "version": "2.13.1",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",