ts-proto 2.11.7 → 2.11.9

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/src/main.js CHANGED
@@ -1804,14 +1804,37 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1804
1804
  const chunks = [];
1805
1805
  // Handle Duration string format as a special case
1806
1806
  if (fullTypeName === "google.protobuf.Duration" && ctx.options.useDuration === options_1.DurationOption.STRING) {
1807
+ let parseSeconds;
1808
+ switch (ctx.options.forceLong) {
1809
+ case options_1.LongOption.BIGINT:
1810
+ parseSeconds = (0, ts_poet_1.code) `negative && secondsStr !== "0" ? -BigInt(secondsStr) : BigInt(secondsStr)`;
1811
+ break;
1812
+ case options_1.LongOption.LONG:
1813
+ // Long.fromString handles the sign internally, including the asymmetric i64 min case.
1814
+ parseSeconds = (0, ts_poet_1.code) `${utils.Long}.fromString(negative ? "-" + secondsStr : secondsStr)`;
1815
+ break;
1816
+ case options_1.LongOption.STRING:
1817
+ parseSeconds = (0, ts_poet_1.code) `negative && secondsStr !== "0" ? "-" + secondsStr : secondsStr`;
1818
+ break;
1819
+ case options_1.LongOption.NUMBER:
1820
+ default:
1821
+ parseSeconds = (0, ts_poet_1.code) `negative && secondsStr !== "0" ? -Number(secondsStr) : Number(secondsStr)`;
1822
+ break;
1823
+ }
1807
1824
  return (0, ts_poet_1.code) `
1808
1825
  fromJSON(object: string): ${fullName} {
1809
- const match = object.match(/^(-?[0-9.]+)s$/);
1810
- if (!match) throw new Error('Invalid duration string');
1811
- const seconds = Number(match[1]);
1812
- const wholeSeconds = Math.trunc(seconds);
1813
- const nanos = Math.round((seconds - wholeSeconds) * 1e9);
1814
- return { seconds: wholeSeconds, nanos };
1826
+ if (!object.endsWith("s")) throw new Error("Invalid duration string");
1827
+ const body = object.slice(0, -1);
1828
+ const negative = body.startsWith("-");
1829
+ const unsigned = negative ? body.slice(1) : body;
1830
+ const dot = unsigned.indexOf(".");
1831
+ const secondsStr = dot < 0 ? unsigned : unsigned.slice(0, dot);
1832
+ const fracStr = dot < 0 ? "" : unsigned.slice(dot + 1);
1833
+ if (secondsStr.length === 0) throw new Error("Invalid duration string");
1834
+ const seconds = ${parseSeconds};
1835
+ const nanosAbs = fracStr === "" ? 0 : Number(fracStr.padEnd(9, "0").slice(0, 9));
1836
+ const nanos = negative && nanosAbs !== 0 ? -nanosAbs : nanosAbs;
1837
+ return { seconds, nanos };
1815
1838
  }
1816
1839
  `;
1817
1840
  }
@@ -2128,7 +2151,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
2128
2151
  chunks.push((0, ts_poet_1.code) `}`);
2129
2152
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
2130
2153
  }
2131
- function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals, useNullAsOptional, useDuration }) {
2154
+ function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals, useNullAsOptional, useDuration, forceLong }) {
2132
2155
  if ((0, types_1.isFieldMaskTypeName)(fullProtobufTypeName)) {
2133
2156
  const returnType = useOptionals === "all" ? `string | ${(0, utils_1.nullOrUndefined)({ useNullAsOptional })}` : "string";
2134
2157
  const pathModifier = useOptionals === "all" ? "?" : "";
@@ -2139,11 +2162,38 @@ function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals,
2139
2162
  `;
2140
2163
  }
2141
2164
  if (fullProtobufTypeName === "google.protobuf.Duration" && useDuration === options_1.DurationOption.STRING) {
2165
+ let isNegSeconds;
2166
+ let absSeconds;
2167
+ switch (forceLong) {
2168
+ case options_1.LongOption.BIGINT:
2169
+ isNegSeconds = (0, ts_poet_1.code) `message.seconds < 0n`;
2170
+ absSeconds = (0, ts_poet_1.code) `message.seconds < 0n ? -message.seconds : message.seconds`;
2171
+ break;
2172
+ case options_1.LongOption.LONG:
2173
+ // Format via toString rather than negate(); Long.MIN_VALUE.negate() === Long.MIN_VALUE.
2174
+ isNegSeconds = (0, ts_poet_1.code) `message.seconds.isNegative()`;
2175
+ absSeconds = (0, ts_poet_1.code) `message.seconds.isNegative() ? message.seconds.toString().slice(1) : message.seconds.toString()`;
2176
+ break;
2177
+ case options_1.LongOption.STRING:
2178
+ isNegSeconds = (0, ts_poet_1.code) `message.seconds.startsWith("-")`;
2179
+ absSeconds = (0, ts_poet_1.code) `message.seconds.startsWith("-") ? message.seconds.slice(1) : message.seconds`;
2180
+ break;
2181
+ case options_1.LongOption.NUMBER:
2182
+ default:
2183
+ isNegSeconds = (0, ts_poet_1.code) `message.seconds < 0`;
2184
+ absSeconds = (0, ts_poet_1.code) `message.seconds < 0 ? -message.seconds : message.seconds`;
2185
+ break;
2186
+ }
2142
2187
  return (0, ts_poet_1.code) `
2143
2188
  toJSON(message: ${fullName}): string {
2144
- const secs = Number(message.seconds);
2145
- const nanos = message.nanos;
2146
- return \`\${secs + nanos/1e9}s\`;
2189
+ const negative = ${isNegSeconds} || message.nanos < 0;
2190
+ const sign = negative ? "-" : "";
2191
+ const absSeconds = ${absSeconds};
2192
+ const absNanos = message.nanos < 0 ? -message.nanos : message.nanos;
2193
+ if (absNanos === 0) return \`\${sign}\${absSeconds}s\`;
2194
+ const padded = absNanos.toString().padStart(9, "0");
2195
+ const len = absNanos % 1_000_000 === 0 ? 3 : absNanos % 1_000 === 0 ? 6 : 9;
2196
+ return \`\${sign}\${absSeconds}.\${padded.slice(0, len)}s\`;
2147
2197
  }
2148
2198
  `;
2149
2199
  }
@@ -178,7 +178,7 @@ function getExtensionValue(ctx, extension, data) {
178
178
  const reader = new wire_1.BinaryReader(data[0]);
179
179
  let value = reader[(0, types_1.toReaderCall)(extension)]();
180
180
  if (typeof value === "string") {
181
- value = (0, ts_poet_1.code) `"${value}"`;
181
+ value = JSON.stringify(value);
182
182
  }
183
183
  return (0, ts_poet_1.code) `'${extension.name}': ${value}`;
184
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.11.7",
3
+ "version": "2.11.9",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",