diff --git a/integration/global-this/global-this.bin b/integration/global-this/global-this.bin index 9e29a07b9..4d78dcf07 100644 Binary files a/integration/global-this/global-this.bin and b/integration/global-this/global-this.bin differ diff --git a/integration/grpc-js-use-date-false/grpc-js-use-date-false.ts b/integration/grpc-js-use-date-false/grpc-js-use-date-false.ts index 6191ec2fd..441ecbc3b 100644 --- a/integration/grpc-js-use-date-false/grpc-js-use-date-false.ts +++ b/integration/grpc-js-use-date-false/grpc-js-use-date-false.ts @@ -59,7 +59,7 @@ export const TimestampMessage = { toJSON(message: TimestampMessage): unknown { const obj: any = {}; if (message.timestamp !== undefined) { - obj.timestamp = fromTimestamp(message.timestamp).toISOString(); + obj.timestamp = message.timestamp; } return obj; }, @@ -156,12 +156,6 @@ function toTimestamp(date: Date): Timestamp { return { seconds, nanos }; } -function fromTimestamp(t: Timestamp): Date { - let millis = (t.seconds || 0) * 1_000; - millis += (t.nanos || 0) / 1_000_000; - return new globalThis.Date(millis); -} - function fromJsonTimestamp(o: any): Timestamp { if (o instanceof globalThis.Date) { return toTimestamp(o); diff --git a/integration/use-date-false/metadata.ts b/integration/use-date-false/metadata.ts index fd39acde6..614e33d5b 100644 --- a/integration/use-date-false/metadata.ts +++ b/integration/use-date-false/metadata.ts @@ -50,7 +50,7 @@ export const Metadata = { toJSON(message: Metadata): unknown { const obj: any = {}; if (message.lastEdited !== undefined) { - obj.lastEdited = fromTimestamp(message.lastEdited).toISOString(); + obj.lastEdited = message.lastEdited; } return obj; }, @@ -85,12 +85,6 @@ function toTimestamp(date: Date): Timestamp { return { seconds, nanos }; } -function fromTimestamp(t: Timestamp): Date { - let millis = (t.seconds || 0) * 1_000; - millis += (t.nanos || 0) / 1_000_000; - return new globalThis.Date(millis); -} - function fromJsonTimestamp(o: any): Timestamp { if (o instanceof globalThis.Date) { return toTimestamp(o); diff --git a/integration/use-date-false/use-date-test.ts b/integration/use-date-false/use-date-test.ts index e81c22ceb..c350b16e8 100644 --- a/integration/use-date-false/use-date-test.ts +++ b/integration/use-date-false/use-date-test.ts @@ -19,14 +19,23 @@ describe("useDate=false", () => { const json = Metadata.toJSON({ lastEdited: nov29 }); expect(json).toMatchInlineSnapshot(` { - "lastEdited": "1973-11-29T21:33:09.234Z", + "lastEdited": { + "nanos": 234567890, + "seconds": 123456789, + }, } `); expect(Metadata.fromJSON(json).lastEdited).toMatchInlineSnapshot(` { - "nanos": 234000000, - "seconds": 123456789.234, + "nanos": 234567890, + "seconds": 123456789, } `); }); + + it("doesn't lose precision in encoding/decoding", () => { + const d = Metadata.fromJSON(Metadata.toJSON({ lastEdited: nov29 })); + expect(d.lastEdited?.seconds).toStrictEqual(nov29.seconds); + expect(d.lastEdited?.nanos).toStrictEqual(nov29.nanos); + }); }); diff --git a/protoc.Dockerfile b/protoc.Dockerfile index 08812dd47..286b16f24 100644 --- a/protoc.Dockerfile +++ b/protoc.Dockerfile @@ -1,10 +1,9 @@ # Docker image for protoc -FROM node:17-alpine3.14 +FROM node:current-slim ARG PROTOC_VERSION="3.19.1" ARG ARCH="x86_64" -RUN apk add bash -RUN apk add gcompat +RUN apt-get update --yes && apt-get install --yes unzip ADD "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/protoc-$PROTOC_VERSION-linux-$ARCH.zip" protoc.zip RUN mkdir /usr/local/lib/protoc && unzip protoc.zip -d /usr/local/lib/protoc && rm protoc.zip RUN ln -s /usr/local/lib/protoc/bin/protoc /usr/local/bin/protoc diff --git a/src/main.ts b/src/main.ts index 9033b2806..f4e19183b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1979,7 +1979,7 @@ function generateToJson( } else if (isTimestamp(field) && options.useDate === DateOption.STRING) { return code`${from}`; } else if (isTimestamp(field) && options.useDate === DateOption.TIMESTAMP) { - return code`${utils.fromTimestamp}(${from}).toISOString()`; + return code`${from}`; } else if (isMapType(ctx, messageDesc, field)) { // For map types, drill-in and then admittedly re-hard-code our per-value-type logic const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1];