From 9a252c3d4cf988496f6de17cc378dbb09a1baf92 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Sat, 30 Sep 2023 10:19:02 -0500 Subject: [PATCH] fix: Use globalThis for Array/String/Boolean (#930) * fix: Use globalThis for Array/String/Boolean. * Re-codegen. * Fix missing code tag, some dates. * Re-codegen. * Add as any since tsProtoGlobalThis is untyped. * Format. --- .../simple.ts | 31 ++++++++-- integration/async-iterable-services/simple.ts | 31 ++++++++-- .../batching.ts | 29 ++++++++- integration/batching-with-context/batching.ts | 29 ++++++++- integration/batching/batching.ts | 29 ++++++++- integration/bytes-as-base64/message.ts | 2 +- .../bytes-node/google/protobuf/wrappers.ts | 4 +- integration/bytes-node/point.ts | 2 +- .../google/protobuf/timestamp.ts | 2 +- integration/emit-default-values-json/test.ts | 32 ++++++---- .../fieldmask/google/protobuf/field_mask.ts | 25 +++++++- .../google/protobuf/timestamp.pb.ts | 2 +- integration/file-suffix/parent.pb.ts | 25 +++++++- .../from-partial-no-initialize/test.ts | 25 +++++++- integration/groups/test.ts | 39 ++++++++---- .../google/protobuf/timestamp.ts | 2 +- .../grpc-js-use-date-false.ts | 25 +++++++- .../google/protobuf/timestamp.ts | 2 +- .../grpc-js-use-date-string.ts | 25 +++++++- .../google/protobuf/timestamp.ts | 2 +- .../grpc-js-use-date-true.ts | 25 +++++++- integration/grpc-js/google/protobuf/struct.ts | 27 +++++++-- .../grpc-js/google/protobuf/timestamp.ts | 2 +- .../grpc-js/google/protobuf/wrappers.ts | 4 +- integration/grpc-js/simple.ts | 25 +++++++- integration/grpc-web-abort-signal/example.ts | 4 +- integration/grpc-web-go-server/example.ts | 23 ++++++- .../example.ts | 4 +- integration/grpc-web-no-streaming/example.ts | 4 +- integration/grpc-web/example.ts | 4 +- .../google/protobuf/timestamp.ts | 2 +- integration/import-mapping/mapping.ts | 25 +++++++- .../google/protobuf/timestamp.pb.ts | 2 +- integration/import-suffix/parent.pb.ts | 25 +++++++- integration/lower-case-svc-methods/math.ts | 21 ++++++- integration/map-long-optional/test.ts | 2 +- .../meta-typings/google/protobuf/timestamp.ts | 2 +- .../meta-typings/google/protobuf/wrappers.ts | 2 +- integration/meta-typings/import_dir/thing.ts | 21 ++++++- integration/meta-typings/simple.ts | 4 +- .../nestjs-simple/google/protobuf/struct.ts | 23 ++++++- .../nice-grpc/google/protobuf/struct.ts | 27 +++++++-- .../nice-grpc/google/protobuf/timestamp.ts | 2 +- .../nice-grpc/google/protobuf/wrappers.ts | 4 +- integration/nice-grpc/simple.ts | 25 +++++++- integration/oneof-properties/oneof.ts | 2 +- .../google/protobuf/struct.ts | 25 +++++++- .../oneof-unions/google/protobuf/struct.ts | 25 +++++++- integration/oneof-unions/oneof.ts | 2 +- .../options/google/protobuf/descriptor.ts | 2 +- .../google/protobuf/wrappers.ts | 2 +- .../google/protobuf/wrappers.ts | 2 +- integration/simple-esmodule-interop/simple.ts | 2 +- .../google/protobuf/timestamp.ts | 2 +- integration/simple-json-name/simple.ts | 29 +++++++-- .../google/protobuf/wrappers.ts | 2 +- integration/simple-long-bigint/simple.ts | 29 +++++++-- .../google/protobuf/wrappers.ts | 2 +- integration/simple-long-string/simple.ts | 27 +++++++-- .../simple-long/google/protobuf/wrappers.ts | 2 +- integration/simple-long/simple.ts | 14 +++-- .../google/protobuf/timestamp.ts | 2 +- .../google/protobuf/wrappers.ts | 4 +- .../simple-optionals/import_dir/thing.ts | 25 +++++++- integration/simple-optionals/simple.ts | 34 ++++++----- integration/simple-optionals/thing.ts | 25 +++++++- .../google/protobuf/timestamp.ts | 2 +- .../google/protobuf/wrappers.ts | 4 +- .../import_dir/thing.ts | 25 +++++++- .../simple-prototype-defaults/simple.ts | 46 +++++++------- .../simple-snake/google/protobuf/struct.ts | 27 +++++++-- .../simple-snake/google/protobuf/timestamp.ts | 2 +- .../simple-snake/google/protobuf/wrappers.ts | 4 +- integration/simple-snake/import_dir/thing.ts | 25 +++++++- integration/simple-snake/simple.ts | 34 ++++++----- .../google/protobuf/struct.ts | 27 +++++++-- integration/simple-string-enums/simple.ts | 23 ++++++- .../google/protobuf/timestamp.ts | 2 +- .../google/protobuf/wrappers.ts | 4 +- .../import_dir/thing.ts | 25 +++++++- .../simple-unrecognized-enum/simple.ts | 34 ++++++----- .../simple/google/protobuf/timestamp.ts | 2 +- .../simple/google/protobuf/wrappers.ts | 4 +- integration/simple/import_dir/thing.ts | 25 +++++++- integration/simple/simple.ts | 46 +++++++------- integration/static-only-type-registry/foo.ts | 25 +++++++- .../google/protobuf/struct.ts | 27 +++++++-- .../google/protobuf/timestamp.ts | 2 +- integration/static-only/foo.ts | 25 +++++++- .../static-only/google/protobuf/struct.ts | 27 +++++++-- .../static-only/google/protobuf/timestamp.ts | 2 +- integration/struct/google/protobuf/struct.ts | 27 +++++++-- integration/type-annotations/foo.ts | 25 +++++++- .../google/protobuf/struct.ts | 30 ++++++++-- .../google/protobuf/timestamp.ts | 2 +- integration/type-registry/foo.ts | 25 +++++++- .../type-registry/google/protobuf/struct.ts | 30 ++++++++-- .../google/protobuf/timestamp.ts | 2 +- .../google/protobuf/compiler/plugin.ts | 2 +- .../google/protobuf/descriptor.ts | 2 +- .../google/protobuf/timestamp.ts | 2 +- integration/use-date-false/metadata.ts | 25 +++++++- .../google/protobuf/timestamp.ts | 2 +- .../use-date-string/use-date-string.ts | 37 +++++++++--- .../google/protobuf/timestamp.ts | 2 +- integration/use-date-true/use-date-true.ts | 27 +++++++-- .../use-map-type/google/protobuf/struct.ts | 27 +++++++-- .../use-map-type/google/protobuf/timestamp.ts | 2 +- integration/use-map-type/use-map-type.ts | 14 ++--- .../google/protobuf/struct.ts | 27 +++++++-- integration/use-numeric-enum-json/simple.ts | 23 ++++++- .../use-objectid-true.ts | 23 ++++++- .../google/protobuf/timestamp.ts | 2 +- integration/use-optionals-all/test.ts | 32 ++++++---- .../use-optionals-no-undefined/test.ts | 26 +++++--- .../google/protobuf/field_mask.ts | 25 +++++++- .../google/protobuf/struct.ts | 25 +++++++- .../use-readonly-types/use-readonly-types.ts | 29 +++++++-- integration/value/google/protobuf/struct.ts | 27 +++++++-- integration/value/google/protobuf/wrappers.ts | 4 +- integration/value/value.ts | 27 ++++++++- integration/vector-tile/vector_tile.ts | 22 ++++--- .../google/protobuf/timestamp.ts | 2 +- .../google/protobuf/wrappers.ts | 4 +- src/generate-async-iterable.ts | 17 +++--- src/generate-struct-wrappers.ts | 10 ++-- src/main.ts | 60 ++++++++++--------- 127 files changed, 1622 insertions(+), 446 deletions(-) diff --git a/integration/async-iterable-services-abort-signal/simple.ts b/integration/async-iterable-services-abort-signal/simple.ts index 2fef10081..d56502c48 100644 --- a/integration/async-iterable-services-abort-signal/simple.ts +++ b/integration/async-iterable-services-abort-signal/simple.ts @@ -49,12 +49,12 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (tsProtoGlobalThis.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [EchoMsg.encode(p).finish()]; } } else { - yield* [EchoMsg.encode(pkt).finish()]; + yield* [EchoMsg.encode(pkt as any).finish()]; } } }, @@ -65,12 +65,12 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (tsProtoGlobalThis.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [EchoMsg.decode(p)]; } } else { - yield* [EchoMsg.decode(pkt)]; + yield* [EchoMsg.decode(pkt as any)]; } } }, @@ -173,6 +173,25 @@ interface Rpc { ): AsyncIterable; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/async-iterable-services/simple.ts b/integration/async-iterable-services/simple.ts index 4b6c45a32..c952a6ca0 100644 --- a/integration/async-iterable-services/simple.ts +++ b/integration/async-iterable-services/simple.ts @@ -49,12 +49,12 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (tsProtoGlobalThis.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [EchoMsg.encode(p).finish()]; } } else { - yield* [EchoMsg.encode(pkt).finish()]; + yield* [EchoMsg.encode(pkt as any).finish()]; } } }, @@ -65,12 +65,12 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (tsProtoGlobalThis.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [EchoMsg.decode(p)]; } } else { - yield* [EchoMsg.decode(pkt)]; + yield* [EchoMsg.decode(pkt as any)]; } } }, @@ -157,6 +157,25 @@ interface Rpc { ): AsyncIterable; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/batching-with-context-esModuleInterop/batching.ts b/integration/batching-with-context-esModuleInterop/batching.ts index ed1f9bc41..d490292ca 100644 --- a/integration/batching-with-context-esModuleInterop/batching.ts +++ b/integration/batching-with-context-esModuleInterop/batching.ts @@ -82,7 +82,7 @@ export const BatchQueryRequest = { }, fromJSON(object: any): BatchQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -139,7 +139,11 @@ export const BatchQueryResponse = { }, fromJSON(object: any): BatchQueryResponse { - return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] }; + return { + entities: tsProtoGlobalThis.Array.isArray(object?.entities) + ? object.entities.map((e: any) => Entity.fromJSON(e)) + : [], + }; }, toJSON(message: BatchQueryResponse): unknown { @@ -196,7 +200,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -753,6 +757,25 @@ export interface DataLoaders { getDataLoader(identifier: string, constructorFn: () => T): T; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index 3f5426019..0dfe190ff 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -82,7 +82,7 @@ export const BatchQueryRequest = { }, fromJSON(object: any): BatchQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -139,7 +139,11 @@ export const BatchQueryResponse = { }, fromJSON(object: any): BatchQueryResponse { - return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] }; + return { + entities: tsProtoGlobalThis.Array.isArray(object?.entities) + ? object.entities.map((e: any) => Entity.fromJSON(e)) + : [], + }; }, toJSON(message: BatchQueryResponse): unknown { @@ -196,7 +200,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -753,6 +757,25 @@ export interface DataLoaders { getDataLoader(identifier: string, constructorFn: () => T): T; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/batching/batching.ts b/integration/batching/batching.ts index 836fc00d5..cede49b77 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -80,7 +80,7 @@ export const BatchQueryRequest = { }, fromJSON(object: any): BatchQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -137,7 +137,11 @@ export const BatchQueryResponse = { }, fromJSON(object: any): BatchQueryResponse { - return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] }; + return { + entities: tsProtoGlobalThis.Array.isArray(object?.entities) + ? object.entities.map((e: any) => Entity.fromJSON(e)) + : [], + }; }, toJSON(message: BatchQueryResponse): unknown { @@ -194,7 +198,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -710,6 +714,25 @@ interface Rpc { request(service: string, method: string, data: Uint8Array): Promise; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/bytes-as-base64/message.ts b/integration/bytes-as-base64/message.ts index 0c78fea60..e87f0842d 100644 --- a/integration/bytes-as-base64/message.ts +++ b/integration/bytes-as-base64/message.ts @@ -71,7 +71,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/bytes-node/google/protobuf/wrappers.ts b/integration/bytes-node/google/protobuf/wrappers.ts index 360a266bf..24641051e 100644 --- a/integration/bytes-node/google/protobuf/wrappers.ts +++ b/integration/bytes-node/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/bytes-node/point.ts b/integration/bytes-node/point.ts index 78fd909b0..5f72494a2 100644 --- a/integration/bytes-node/point.ts +++ b/integration/bytes-node/point.ts @@ -121,7 +121,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/emit-default-values-json/google/protobuf/timestamp.ts b/integration/emit-default-values-json/google/protobuf/timestamp.ts index ad9c1e7d1..e06a6f836 100644 --- a/integration/emit-default-values-json/google/protobuf/timestamp.ts +++ b/integration/emit-default-values-json/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/emit-default-values-json/test.ts b/integration/emit-default-values-json/test.ts index a4d193e0c..c6e1358ad 100644 --- a/integration/emit-default-values-json/test.ts +++ b/integration/emit-default-values-json/test.ts @@ -417,13 +417,21 @@ export const DefaultValuesTest = { truth: isSet(object.truth) ? Boolean(object.truth) : false, description: isSet(object.description) ? String(object.description) : "", data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(0), - repId: Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], - repChild: Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : [], - repState: Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : [], - repLong: Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], - repTruth: Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], - repDescription: Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : [], - repData: Array.isArray(object?.repData) ? object.repData.map((e: any) => bytesFromBase64(e)) : [], + repId: tsProtoGlobalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], + repChild: tsProtoGlobalThis.Array.isArray(object?.repChild) + ? object.repChild.map((e: any) => Child.fromJSON(e)) + : [], + repState: tsProtoGlobalThis.Array.isArray(object?.repState) + ? object.repState.map((e: any) => stateEnumFromJSON(e)) + : [], + repLong: tsProtoGlobalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], + repTruth: tsProtoGlobalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], + repDescription: tsProtoGlobalThis.Array.isArray(object?.repDescription) + ? object.repDescription.map((e: any) => String(e)) + : [], + repData: tsProtoGlobalThis.Array.isArray(object?.repData) + ? object.repData.map((e: any) => bytesFromBase64(e)) + : [], optId: isSet(object.optId) ? Number(object.optId) : undefined, optChild: isSet(object.optChild) ? Child.fromJSON(object.optChild) : undefined, optState: isSet(object.optState) ? stateEnumFromJSON(object.optState) : undefined, @@ -719,7 +727,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -745,21 +753,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/fieldmask/google/protobuf/field_mask.ts b/integration/fieldmask/google/protobuf/field_mask.ts index 98ce1bb8b..61f2c4039 100644 --- a/integration/fieldmask/google/protobuf/field_mask.ts +++ b/integration/fieldmask/google/protobuf/field_mask.ts @@ -246,9 +246,9 @@ export const FieldMask = { fromJSON(object: any): FieldMask { return { paths: typeof (object) === "string" - ? object.split(",").filter(Boolean) - : Array.isArray(object?.paths) - ? object.paths.map(String) + ? object.split(",").filter(tsProtoGlobalThis.Boolean) + : tsProtoGlobalThis.Array.isArray(object?.paths) + ? object.paths.map(tsProtoGlobalThis.String) : [], }; }, @@ -277,6 +277,25 @@ export const FieldMask = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/file-suffix/google/protobuf/timestamp.pb.ts b/integration/file-suffix/google/protobuf/timestamp.pb.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/file-suffix/google/protobuf/timestamp.pb.ts +++ b/integration/file-suffix/google/protobuf/timestamp.pb.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/file-suffix/parent.pb.ts b/integration/file-suffix/parent.pb.ts index f26b84bcf..c00ede347 100644 --- a/integration/file-suffix/parent.pb.ts +++ b/integration/file-suffix/parent.pb.ts @@ -100,6 +100,25 @@ export const Parent = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -120,14 +139,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/from-partial-no-initialize/test.ts b/integration/from-partial-no-initialize/test.ts index 38648a4dc..b01126a23 100644 --- a/integration/from-partial-no-initialize/test.ts +++ b/integration/from-partial-no-initialize/test.ts @@ -221,13 +221,13 @@ export const TPartial = { }, {}) : undefined, message: isSet(object.message) ? TPartialMessage.fromJSON(object.message) : undefined, - repeatedMessage: Array.isArray(object?.repeatedMessage) + repeatedMessage: tsProtoGlobalThis.Array.isArray(object?.repeatedMessage) ? object.repeatedMessage.map((e: any) => TPartialMessage.fromJSON(e)) : undefined, - repeatedString: Array.isArray(object?.repeatedString) + repeatedString: tsProtoGlobalThis.Array.isArray(object?.repeatedString) ? object.repeatedString.map((e: any) => String(e)) : undefined, - repeatedNumber: Array.isArray(object?.repeatedNumber) + repeatedNumber: tsProtoGlobalThis.Array.isArray(object?.repeatedNumber) ? object.repeatedNumber.map((e: any) => Number(e)) : undefined, }; @@ -361,6 +361,25 @@ export const TPartial_MapEntry = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/groups/test.ts b/integration/groups/test.ts index 8b8cf45bc..95f903210 100644 --- a/integration/groups/test.ts +++ b/integration/groups/test.ts @@ -399,11 +399,11 @@ export const GroupsRepeatedTest = { fromJSON(object: any): GroupsRepeatedTest { return { - int1: Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, - group: Array.isArray(object?.group) + int1: tsProtoGlobalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, + group: tsProtoGlobalThis.Array.isArray(object?.group) ? object.group.map((e: any) => GroupsRepeatedTest_Group.fromJSON(e)) : undefined, - int3: Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, + int3: tsProtoGlobalThis.Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, }; }, @@ -517,8 +517,8 @@ export const GroupsRepeatedTest_Group = { fromJSON(object: any): GroupsRepeatedTest_Group { return { - key: Array.isArray(object?.key) ? object.key.map((e: any) => String(e)) : undefined, - value: Array.isArray(object?.value) ? object.value.map((e: any) => String(e)) : undefined, + key: tsProtoGlobalThis.Array.isArray(object?.key) ? object.key.map((e: any) => String(e)) : undefined, + value: tsProtoGlobalThis.Array.isArray(object?.value) ? object.value.map((e: any) => String(e)) : undefined, }; }, @@ -673,11 +673,11 @@ export const GroupsNestedTest = { fromJSON(object: any): GroupsNestedTest { return { - int1: Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, - group: Array.isArray(object?.group) + int1: tsProtoGlobalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, + group: tsProtoGlobalThis.Array.isArray(object?.group) ? object.group.map((e: any) => GroupsNestedTest_Group.fromJSON(e)) : undefined, - int3: Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, + int3: tsProtoGlobalThis.Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, }; }, @@ -776,7 +776,7 @@ export const GroupsNestedTest_Group = { fromJSON(object: any): GroupsNestedTest_Group { return { - nested: Array.isArray(object?.nested) + nested: tsProtoGlobalThis.Array.isArray(object?.nested) ? object.nested.map((e: any) => GroupsNestedTest_Group_Nested.fromJSON(e)) : undefined, }; @@ -869,7 +869,7 @@ export const GroupsNestedTest_Group_Nested = { fromJSON(object: any): GroupsNestedTest_Group_Nested { return { - nested2: Array.isArray(object?.nested2) + nested2: tsProtoGlobalThis.Array.isArray(object?.nested2) ? object.nested2.map((e: any) => GroupsNestedTest_Group_Nested_Nested2.fromJSON(e)) : undefined, }; @@ -983,6 +983,25 @@ export const GroupsNestedTest_Group_Nested_Nested2 = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts b/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); 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 f389bd7e0..e97c2d20d 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 @@ -138,6 +138,25 @@ export const TestClient = makeGenericClientConstructor(TestService, "simple.Test service: typeof TestService; }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -158,14 +177,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Timestamp { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return toTimestamp(o); } else if (typeof o === "string") { - return toTimestamp(new Date(o)); + return toTimestamp(new tsProtoGlobalThis.Date(o)); } else { return Timestamp.fromJSON(o); } diff --git a/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts b/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/grpc-js-use-date-string/grpc-js-use-date-string.ts b/integration/grpc-js-use-date-string/grpc-js-use-date-string.ts index 5a82625e6..206813327 100644 --- a/integration/grpc-js-use-date-string/grpc-js-use-date-string.ts +++ b/integration/grpc-js-use-date-string/grpc-js-use-date-string.ts @@ -53,7 +53,7 @@ export const TimestampMessage = { }, fromJSON(object: any): TimestampMessage { - return { timestamp: isSet(object.timestamp) ? String(object.timestamp) : undefined }; + return { timestamp: isSet(object.timestamp) ? tsProtoGlobalThis.String(object.timestamp) : undefined }; }, toJSON(message: TimestampMessage): unknown { @@ -136,6 +136,25 @@ export const TestClient = makeGenericClientConstructor(TestService, "simple.Test service: typeof TestService; }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -148,7 +167,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function toTimestamp(dateStr: string): Timestamp { - const date = new Date(dateStr); + const date = new tsProtoGlobalThis.Date(dateStr); const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; return { seconds, nanos }; @@ -157,7 +176,7 @@ function toTimestamp(dateStr: string): Timestamp { function fromTimestamp(t: Timestamp): string { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis).toISOString(); + return new tsProtoGlobalThis.Date(millis).toISOString(); } function isSet(value: any): boolean { diff --git a/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts b/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/grpc-js-use-date-true/grpc-js-use-date-true.ts b/integration/grpc-js-use-date-true/grpc-js-use-date-true.ts index fc4124084..cb18d6163 100644 --- a/integration/grpc-js-use-date-true/grpc-js-use-date-true.ts +++ b/integration/grpc-js-use-date-true/grpc-js-use-date-true.ts @@ -136,6 +136,25 @@ export const TestClient = makeGenericClientConstructor(TestService, "simple.Test service: typeof TestService; }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -156,14 +175,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/grpc-js/google/protobuf/struct.ts b/integration/grpc-js/google/protobuf/struct.ts index 4d9869bbe..35e974c3a 100644 --- a/integration/grpc-js/google/protobuf/struct.ts +++ b/integration/grpc-js/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/grpc-js/google/protobuf/timestamp.ts b/integration/grpc-js/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/grpc-js/google/protobuf/timestamp.ts +++ b/integration/grpc-js/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/grpc-js/google/protobuf/wrappers.ts b/integration/grpc-js/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/grpc-js/google/protobuf/wrappers.ts +++ b/integration/grpc-js/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/grpc-js/simple.ts b/integration/grpc-js/simple.ts index 35841e8c6..60ce23081 100644 --- a/integration/grpc-js/simple.ts +++ b/integration/grpc-js/simple.ts @@ -633,6 +633,25 @@ export const TestClient = makeGenericClientConstructor(TestService, "simple.Test service: typeof TestService; }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -653,14 +672,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/grpc-web-abort-signal/example.ts b/integration/grpc-web-abort-signal/example.ts index 78503684e..69d9068ef 100644 --- a/integration/grpc-web-abort-signal/example.ts +++ b/integration/grpc-web-abort-signal/example.ts @@ -228,7 +228,9 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], + flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) + ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) + : [], }; }, diff --git a/integration/grpc-web-go-server/example.ts b/integration/grpc-web-go-server/example.ts index ce5dfebd7..24ccc46b2 100644 --- a/integration/grpc-web-go-server/example.ts +++ b/integration/grpc-web-go-server/example.ts @@ -226,7 +226,9 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], + flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) + ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) + : [], }; }, @@ -807,6 +809,25 @@ interface Rpc { bidirectionalStreamingRequest(service: string, method: string, data: Observable): Observable; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/grpc-web-no-streaming-observable/example.ts b/integration/grpc-web-no-streaming-observable/example.ts index a59e9953c..7fccc79c6 100644 --- a/integration/grpc-web-no-streaming-observable/example.ts +++ b/integration/grpc-web-no-streaming-observable/example.ts @@ -204,7 +204,9 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], + flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) + ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) + : [], }; }, diff --git a/integration/grpc-web-no-streaming/example.ts b/integration/grpc-web-no-streaming/example.ts index 61c5a4722..9b4c6b265 100644 --- a/integration/grpc-web-no-streaming/example.ts +++ b/integration/grpc-web-no-streaming/example.ts @@ -202,7 +202,9 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], + flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) + ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) + : [], }; }, diff --git a/integration/grpc-web/example.ts b/integration/grpc-web/example.ts index c12a43c3d..b76869ff1 100644 --- a/integration/grpc-web/example.ts +++ b/integration/grpc-web/example.ts @@ -228,7 +228,9 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], + flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) + ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) + : [], }; }, diff --git a/integration/import-mapping/google/protobuf/timestamp.ts b/integration/import-mapping/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/import-mapping/google/protobuf/timestamp.ts +++ b/integration/import-mapping/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/import-mapping/mapping.ts b/integration/import-mapping/mapping.ts index 84775f10b..5adcf16c4 100644 --- a/integration/import-mapping/mapping.ts +++ b/integration/import-mapping/mapping.ts @@ -322,6 +322,25 @@ export const WithAll = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -342,14 +361,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/import-suffix/google/protobuf/timestamp.pb.ts b/integration/import-suffix/google/protobuf/timestamp.pb.ts index 7d2f9170d..e1ec4a551 100644 --- a/integration/import-suffix/google/protobuf/timestamp.pb.ts +++ b/integration/import-suffix/google/protobuf/timestamp.pb.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/import-suffix/parent.pb.ts b/integration/import-suffix/parent.pb.ts index 9fef51576..76be24db9 100644 --- a/integration/import-suffix/parent.pb.ts +++ b/integration/import-suffix/parent.pb.ts @@ -100,6 +100,25 @@ export const Parent = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -120,14 +139,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/lower-case-svc-methods/math.ts b/integration/lower-case-svc-methods/math.ts index 6d7ea8a01..315ed9dbe 100644 --- a/integration/lower-case-svc-methods/math.ts +++ b/integration/lower-case-svc-methods/math.ts @@ -194,7 +194,7 @@ export const Numbers = { }, fromJSON(object: any): Numbers { - return { num: Array.isArray(object?.num) ? object.num.map((e: any) => Number(e)) : [] }; + return { num: tsProtoGlobalThis.Array.isArray(object?.num) ? object.num.map((e: any) => Number(e)) : [] }; }, toJSON(message: Numbers): unknown { @@ -275,6 +275,25 @@ export interface DataLoaders { getDataLoader(identifier: string, constructorFn: () => T): T; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/map-long-optional/test.ts b/integration/map-long-optional/test.ts index 5f8f46124..3ee999800 100644 --- a/integration/map-long-optional/test.ts +++ b/integration/map-long-optional/test.ts @@ -256,7 +256,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/meta-typings/google/protobuf/timestamp.ts b/integration/meta-typings/google/protobuf/timestamp.ts index b8fa3c667..367a260e2 100644 --- a/integration/meta-typings/google/protobuf/timestamp.ts +++ b/integration/meta-typings/google/protobuf/timestamp.ts @@ -298,7 +298,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/meta-typings/google/protobuf/wrappers.ts b/integration/meta-typings/google/protobuf/wrappers.ts index 2101658f6..348848f31 100644 --- a/integration/meta-typings/google/protobuf/wrappers.ts +++ b/integration/meta-typings/google/protobuf/wrappers.ts @@ -837,7 +837,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/meta-typings/import_dir/thing.ts b/integration/meta-typings/import_dir/thing.ts index f38c56dae..8f2cf816d 100644 --- a/integration/meta-typings/import_dir/thing.ts +++ b/integration/meta-typings/import_dir/thing.ts @@ -108,6 +108,25 @@ export const protoMetadata: ProtoMetadata = { dependencies: [protoMetadata1], }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; @@ -117,5 +136,5 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } diff --git a/integration/meta-typings/simple.ts b/integration/meta-typings/simple.ts index 04f69cd5e..c566e1fc5 100644 --- a/integration/meta-typings/simple.ts +++ b/integration/meta-typings/simple.ts @@ -2978,11 +2978,11 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/nestjs-simple/google/protobuf/struct.ts b/integration/nestjs-simple/google/protobuf/struct.ts index d9ae0e11d..1d9cf5acf 100644 --- a/integration/nestjs-simple/google/protobuf/struct.ts +++ b/integration/nestjs-simple/google/protobuf/struct.ts @@ -121,7 +121,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = ListValue.wrap(value); } else if (typeof value === "object") { result.structValue = Struct.wrap(value); @@ -161,7 +161,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values.map(Value.unwrap); } else { return message as any; @@ -170,3 +170,22 @@ export const ListValue = { }; wrappers[".google.protobuf.Struct"] = { fromObject: Struct.wrap, toObject: Struct.unwrap } as any; + +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); diff --git a/integration/nice-grpc/google/protobuf/struct.ts b/integration/nice-grpc/google/protobuf/struct.ts index ec6e0fc6c..a2effb032 100644 --- a/integration/nice-grpc/google/protobuf/struct.ts +++ b/integration/nice-grpc/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/nice-grpc/google/protobuf/timestamp.ts b/integration/nice-grpc/google/protobuf/timestamp.ts index bb9e014d2..4161887db 100644 --- a/integration/nice-grpc/google/protobuf/timestamp.ts +++ b/integration/nice-grpc/google/protobuf/timestamp.ts @@ -212,7 +212,7 @@ export type DeepPartial = T extends Builtin ? T : Partial; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/nice-grpc/google/protobuf/wrappers.ts b/integration/nice-grpc/google/protobuf/wrappers.ts index 4cffaaa4e..b79dd9e99 100644 --- a/integration/nice-grpc/google/protobuf/wrappers.ts +++ b/integration/nice-grpc/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -659,7 +659,7 @@ export type DeepPartial = T extends Builtin ? T : Partial; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/nice-grpc/simple.ts b/integration/nice-grpc/simple.ts index ae1ec03c1..c72dafdaa 100644 --- a/integration/nice-grpc/simple.ts +++ b/integration/nice-grpc/simple.ts @@ -371,6 +371,25 @@ export interface TestClient { ): AsyncIterable; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -387,14 +406,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/oneof-properties/oneof.ts b/integration/oneof-properties/oneof.ts index 4764d3fa4..d5e92c6a6 100644 --- a/integration/oneof-properties/oneof.ts +++ b/integration/oneof-properties/oneof.ts @@ -394,7 +394,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/oneof-unions-snake/google/protobuf/struct.ts b/integration/oneof-unions-snake/google/protobuf/struct.ts index a7b40be14..6856c8ed2 100644 --- a/integration/oneof-unions-snake/google/protobuf/struct.ts +++ b/integration/oneof-unions-snake/google/protobuf/struct.ts @@ -445,7 +445,7 @@ export const Value = { result.kind = { $case: "number_value", number_value: value }; } else if (typeof value === "string") { result.kind = { $case: "string_value", string_value: value }; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.kind = { $case: "list_value", list_value: value }; } else if (typeof value === "object") { result.kind = { $case: "struct_value", struct_value: value }; @@ -510,7 +510,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -537,7 +537,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -545,6 +545,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/oneof-unions/google/protobuf/struct.ts b/integration/oneof-unions/google/protobuf/struct.ts index b1f613072..a5b26d5e6 100644 --- a/integration/oneof-unions/google/protobuf/struct.ts +++ b/integration/oneof-unions/google/protobuf/struct.ts @@ -436,7 +436,7 @@ export const Value = { result.kind = { $case: "numberValue", numberValue: value }; } else if (typeof value === "string") { result.kind = { $case: "stringValue", stringValue: value }; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.kind = { $case: "listValue", listValue: value }; } else if (typeof value === "object") { result.kind = { $case: "structValue", structValue: value }; @@ -501,7 +501,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -528,7 +528,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -536,6 +536,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/oneof-unions/oneof.ts b/integration/oneof-unions/oneof.ts index dc6f0da10..825231382 100644 --- a/integration/oneof-unions/oneof.ts +++ b/integration/oneof-unions/oneof.ts @@ -525,7 +525,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/options/google/protobuf/descriptor.ts b/integration/options/google/protobuf/descriptor.ts index bc0a27d29..6696b7f21 100644 --- a/integration/options/google/protobuf/descriptor.ts +++ b/integration/options/google/protobuf/descriptor.ts @@ -5802,7 +5802,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/output-decode-only/google/protobuf/wrappers.ts b/integration/output-decode-only/google/protobuf/wrappers.ts index 0674c218b..589c277ef 100644 --- a/integration/output-decode-only/google/protobuf/wrappers.ts +++ b/integration/output-decode-only/google/protobuf/wrappers.ts @@ -375,7 +375,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/output-toJSON-only/google/protobuf/wrappers.ts b/integration/output-toJSON-only/google/protobuf/wrappers.ts index 3ffad0310..cbc07235d 100644 --- a/integration/output-toJSON-only/google/protobuf/wrappers.ts +++ b/integration/output-toJSON-only/google/protobuf/wrappers.ts @@ -207,7 +207,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/simple-esmodule-interop/simple.ts b/integration/simple-esmodule-interop/simple.ts index e555cb95e..3f784f98a 100644 --- a/integration/simple-esmodule-interop/simple.ts +++ b/integration/simple-esmodule-interop/simple.ts @@ -363,7 +363,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-json-name/google/protobuf/timestamp.ts b/integration/simple-json-name/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/simple-json-name/google/protobuf/timestamp.ts +++ b/integration/simple-json-name/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-json-name/simple.ts b/integration/simple-json-name/simple.ts index 7eb951423..7a0b9b322 100644 --- a/integration/simple-json-name/simple.ts +++ b/integration/simple-json-name/simple.ts @@ -138,7 +138,9 @@ export const Simple = { spaces: isSet(object["name with spaces"]) ? String(object["name with spaces"]) : "", dollarStart: isSet(object.$dollar) ? String(object.$dollar) : "", dollarEnd: isSet(object.dollar$) ? String(object.dollar$) : "", - hyphenList: Array.isArray(object?.["hyphen-list"]) ? object["hyphen-list"].map((e: any) => String(e)) : [], + hyphenList: tsProtoGlobalThis.Array.isArray(object?.["hyphen-list"]) + ? object["hyphen-list"].map((e: any) => String(e)) + : [], }; }, @@ -188,6 +190,25 @@ export const Simple = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -208,14 +229,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-long-bigint/google/protobuf/wrappers.ts b/integration/simple-long-bigint/google/protobuf/wrappers.ts index b0dbbcf8c..38b71dc87 100644 --- a/integration/simple-long-bigint/google/protobuf/wrappers.ts +++ b/integration/simple-long-bigint/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/simple-long-bigint/simple.ts b/integration/simple-long-bigint/simple.ts index 2a75ff903..6e9bc1e70 100644 --- a/integration/simple-long-bigint/simple.ts +++ b/integration/simple-long-bigint/simple.ts @@ -243,7 +243,7 @@ export const Numbers = { sfixed64: isSet(object.sfixed64) ? BigInt(object.sfixed64) : BigInt("0"), guint64: isSet(object.guint64) ? BigInt(object.guint64) : undefined, timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined, - uint64s: Array.isArray(object?.uint64s) ? object.uint64s.map((e: any) => BigInt(e)) : [], + uint64s: tsProtoGlobalThis.Array.isArray(object?.uint64s) ? object.uint64s.map((e: any) => BigInt(e)) : [], }; }, @@ -321,6 +321,25 @@ export const Numbers = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined; export type DeepPartial = T extends Builtin ? T @@ -339,16 +358,16 @@ function toTimestamp(date: Date): Timestamp { } function fromTimestamp(t: Timestamp): Date { - let millis = (Number(t.seconds.toString()) || 0) * 1_000; + let millis = (tsProtoGlobalThis.Number(t.seconds.toString()) || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-long-string/google/protobuf/wrappers.ts b/integration/simple-long-string/google/protobuf/wrappers.ts index eb21b8a94..375f8dbe5 100644 --- a/integration/simple-long-string/google/protobuf/wrappers.ts +++ b/integration/simple-long-string/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/simple-long-string/simple.ts b/integration/simple-long-string/simple.ts index 40b15d0f1..47e7d3d6a 100644 --- a/integration/simple-long-string/simple.ts +++ b/integration/simple-long-string/simple.ts @@ -292,6 +292,25 @@ export const Numbers = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -310,16 +329,16 @@ function toTimestamp(date: Date): Timestamp { } function fromTimestamp(t: Timestamp): Date { - let millis = (Number(t.seconds) || 0) * 1_000; + let millis = (tsProtoGlobalThis.Number(t.seconds) || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-long/google/protobuf/wrappers.ts b/integration/simple-long/google/protobuf/wrappers.ts index 875de68bd..4fc7b3028 100644 --- a/integration/simple-long/google/protobuf/wrappers.ts +++ b/integration/simple-long/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } diff --git a/integration/simple-long/simple.ts b/integration/simple-long/simple.ts index e58ccda17..5163cf4c6 100644 --- a/integration/simple-long/simple.ts +++ b/integration/simple-long/simple.ts @@ -143,8 +143,8 @@ export const SimpleWithWrappers = { age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, bananas: isSet(object.bananas) ? Long.fromValue(object.bananas) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -262,7 +262,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -321,7 +321,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -755,7 +755,9 @@ export const Numbers = { fixed64: isSet(object.fixed64) ? Long.fromValue(object.fixed64) : Long.UZERO, sfixed32: isSet(object.sfixed32) ? Number(object.sfixed32) : 0, sfixed64: isSet(object.sfixed64) ? Long.fromValue(object.sfixed64) : Long.ZERO, - manyUint64: Array.isArray(object?.manyUint64) ? object.manyUint64.map((e: any) => Long.fromValue(e)) : [], + manyUint64: tsProtoGlobalThis.Array.isArray(object?.manyUint64) + ? object.manyUint64.map((e: any) => Long.fromValue(e)) + : [], }; }, @@ -865,7 +867,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-optionals/google/protobuf/timestamp.ts b/integration/simple-optionals/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/simple-optionals/google/protobuf/timestamp.ts +++ b/integration/simple-optionals/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-optionals/google/protobuf/wrappers.ts b/integration/simple-optionals/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/simple-optionals/google/protobuf/wrappers.ts +++ b/integration/simple-optionals/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-optionals/import_dir/thing.ts b/integration/simple-optionals/import_dir/thing.ts index 645df09b8..44595e74d 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index b30261b05..b22c6c89b 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -405,12 +405,14 @@ export const Simple = { createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - grandChildren: Array.isArray(object?.grandChildren) + grandChildren: tsProtoGlobalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(e)) : [], - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], - oldStates: Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: tsProtoGlobalThis.Array.isArray(object?.oldStates) + ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) + : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, }; }, @@ -925,8 +927,8 @@ export const SimpleWithWrappers = { name: isSet(object.name) ? String(object.name) : undefined, age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1089,7 +1091,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1101,7 +1103,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1148,7 +1150,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1166,7 +1168,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -1440,7 +1442,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1469,7 +1471,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1973,21 +1975,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-optionals/thing.ts b/integration/simple-optionals/thing.ts index 25dd77fdd..90a802d78 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-prototype-defaults/google/protobuf/timestamp.ts b/integration/simple-prototype-defaults/google/protobuf/timestamp.ts index 5aaf79e75..b6d253b05 100644 --- a/integration/simple-prototype-defaults/google/protobuf/timestamp.ts +++ b/integration/simple-prototype-defaults/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-prototype-defaults/google/protobuf/wrappers.ts b/integration/simple-prototype-defaults/google/protobuf/wrappers.ts index 03f92a4fe..84ca98602 100644 --- a/integration/simple-prototype-defaults/google/protobuf/wrappers.ts +++ b/integration/simple-prototype-defaults/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-prototype-defaults/import_dir/thing.ts b/integration/simple-prototype-defaults/import_dir/thing.ts index 7b91073dc..3860c4c01 100644 --- a/integration/simple-prototype-defaults/import_dir/thing.ts +++ b/integration/simple-prototype-defaults/import_dir/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-prototype-defaults/simple.ts b/integration/simple-prototype-defaults/simple.ts index c834bc216..e7c41b45b 100644 --- a/integration/simple-prototype-defaults/simple.ts +++ b/integration/simple-prototype-defaults/simple.ts @@ -500,14 +500,16 @@ export const Simple = { createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - grandChildren: Array.isArray(object?.grandChildren) + grandChildren: tsProtoGlobalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(e)) : [], - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], - oldStates: Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: tsProtoGlobalThis.Array.isArray(object?.oldStates) + ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) + : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, - blobs: Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], + blobs: tsProtoGlobalThis.Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], birthday: isSet(object.birthday) ? DateMessage.fromJSON(object.birthday) : undefined, blob: isSet(object.blob) ? bytesFromBase64(object.blob) : new Uint8Array(0), }; @@ -1047,8 +1049,8 @@ export const SimpleWithWrappers = { name: isSet(object.name) ? String(object.name) : undefined, age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], id: isSet(object.id) ? new Uint8Array(object.id) : undefined, }; }, @@ -1278,7 +1280,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1290,7 +1292,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1317,7 +1319,7 @@ export const SimpleWithMap = { : {}, longLookup: isObject(object.longLookup) ? Object.entries(object.longLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1400,7 +1402,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1418,7 +1420,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -1453,7 +1455,7 @@ export const SimpleWithMap = { message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -2036,7 +2038,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -2065,7 +2067,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -2201,7 +2203,7 @@ export const SimpleWithMapOfEnums = { return { enumsById: isObject(object.enumsById) ? Object.entries(object.enumsById).reduce<{ [key: number]: StateEnum }>((acc, [key, value]) => { - acc[Number(key)] = stateEnumFromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = stateEnumFromJSON(value); return acc; }, {}) : {}, @@ -2230,7 +2232,7 @@ export const SimpleWithMapOfEnums = { message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = value as StateEnum; + acc[tsProtoGlobalThis.Number(key)] = value as StateEnum; } return acc; }, @@ -2939,7 +2941,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -2965,21 +2967,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-snake/google/protobuf/struct.ts b/integration/simple-snake/google/protobuf/struct.ts index aac735d0d..0750b9923 100644 --- a/integration/simple-snake/google/protobuf/struct.ts +++ b/integration/simple-snake/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { string_value: isSet(object.string_value) ? String(object.string_value) : undefined, bool_value: isSet(object.bool_value) ? Boolean(object.bool_value) : undefined, struct_value: isObject(object.struct_value) ? object.struct_value : undefined, - list_value: Array.isArray(object.list_value) ? [...object.list_value] : undefined, + list_value: tsProtoGlobalThis.Array.isArray(object.list_value) ? [...object.list_value] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.number_value = value; } else if (typeof value === "string") { result.string_value = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.list_value = value; } else if (typeof value === "object") { result.struct_value = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/simple-snake/google/protobuf/timestamp.ts b/integration/simple-snake/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/simple-snake/google/protobuf/timestamp.ts +++ b/integration/simple-snake/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-snake/google/protobuf/wrappers.ts b/integration/simple-snake/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/simple-snake/google/protobuf/wrappers.ts +++ b/integration/simple-snake/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-snake/import_dir/thing.ts b/integration/simple-snake/import_dir/thing.ts index bc7abd064..e7fc308f7 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index d0b2088b0..c6a28afae 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -410,12 +410,14 @@ export const Simple = { created_at: isSet(object.created_at) ? fromJsonTimestamp(object.created_at) : undefined, child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - grand_children: Array.isArray(object?.grand_children) + grand_children: tsProtoGlobalThis.Array.isArray(object?.grand_children) ? object.grand_children.map((e: any) => Child.fromJSON(e)) : [], - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], - old_states: Array.isArray(object?.old_states) ? object.old_states.map((e: any) => stateEnumFromJSON(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + old_states: tsProtoGlobalThis.Array.isArray(object?.old_states) + ? object.old_states.map((e: any) => stateEnumFromJSON(e)) + : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, }; }, @@ -930,8 +932,8 @@ export const SimpleWithWrappers = { name: isSet(object.name) ? String(object.name) : undefined, age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1094,7 +1096,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1106,7 +1108,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1153,7 +1155,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1171,7 +1173,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -1445,7 +1447,7 @@ export const SimpleWithSnakeCaseMap = { return { entities_by_id: isObject(object.entities_by_id) ? Object.entries(object.entities_by_id).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1474,7 +1476,7 @@ export const SimpleWithSnakeCaseMap = { message.entities_by_id = Object.entries(object.entities_by_id ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -2035,21 +2037,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-string-enums/google/protobuf/struct.ts b/integration/simple-string-enums/google/protobuf/struct.ts index ff6efe0ff..f91d21bfa 100644 --- a/integration/simple-string-enums/google/protobuf/struct.ts +++ b/integration/simple-string-enums/google/protobuf/struct.ts @@ -383,7 +383,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -434,7 +434,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -498,7 +498,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -525,7 +525,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -533,6 +533,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/simple-string-enums/simple.ts b/integration/simple-string-enums/simple.ts index 3221a6281..2ee9ae079 100644 --- a/integration/simple-string-enums/simple.ts +++ b/integration/simple-string-enums/simple.ts @@ -164,7 +164,9 @@ export const Simple = { return { name: isSet(object.name) ? String(object.name) : "", state: isSet(object.state) ? stateEnumFromJSON(object.state) : StateEnum.UNKNOWN, - states: Array.isArray(object?.states) ? object.states.map((e: any) => stateEnumFromJSON(e)) : [], + states: tsProtoGlobalThis.Array.isArray(object?.states) + ? object.states.map((e: any) => stateEnumFromJSON(e)) + : [], nullValue: isSet(object.nullValue) ? nullValueFromJSON(object.nullValue) : NullValue.NULL_VALUE, stateMap: isObject(object.stateMap) ? Object.entries(object.stateMap).reduce<{ [key: string]: StateEnum }>((acc, [key, value]) => { @@ -297,6 +299,25 @@ export const Simple_StateMapEntry = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple-unrecognized-enum/import_dir/thing.ts b/integration/simple-unrecognized-enum/import_dir/thing.ts index e57f3e817..e10fddf1f 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 0019a47af..89c3b5351 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -393,12 +393,14 @@ export const Simple = { createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - grandChildren: Array.isArray(object?.grandChildren) + grandChildren: tsProtoGlobalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(e)) : [], - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], - oldStates: Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: tsProtoGlobalThis.Array.isArray(object?.oldStates) + ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) + : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, }; }, @@ -913,8 +915,8 @@ export const SimpleWithWrappers = { name: isSet(object.name) ? String(object.name) : undefined, age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1077,7 +1079,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1089,7 +1091,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1136,7 +1138,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1154,7 +1156,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -1428,7 +1430,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1457,7 +1459,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1961,21 +1963,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple/google/protobuf/timestamp.ts b/integration/simple/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/simple/google/protobuf/timestamp.ts +++ b/integration/simple/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple/google/protobuf/wrappers.ts b/integration/simple/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/simple/google/protobuf/wrappers.ts +++ b/integration/simple/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/simple/import_dir/thing.ts b/integration/simple/import_dir/thing.ts index e57f3e817..e10fddf1f 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -65,6 +65,25 @@ export const ImportedThing = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -85,14 +104,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index c8a55c59f..4596e06a2 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -512,14 +512,16 @@ export const Simple = { createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined, child: isSet(object.child) ? Child.fromJSON(object.child) : undefined, state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - grandChildren: Array.isArray(object?.grandChildren) + grandChildren: tsProtoGlobalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(e)) : [], - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], - oldStates: Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: tsProtoGlobalThis.Array.isArray(object?.oldStates) + ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) + : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, - blobs: Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], + blobs: tsProtoGlobalThis.Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], birthday: isSet(object.birthday) ? DateMessage.fromJSON(object.birthday) : undefined, blob: isSet(object.blob) ? bytesFromBase64(object.blob) : new Uint8Array(0), enabled: isSet(object.enabled) ? Boolean(object.enabled) : false, @@ -1064,8 +1066,8 @@ export const SimpleWithWrappers = { name: isSet(object.name) ? String(object.name) : undefined, age: isSet(object.age) ? Number(object.age) : undefined, enabled: isSet(object.enabled) ? Boolean(object.enabled) : undefined, - coins: Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], - snacks: Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + coins: tsProtoGlobalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: tsProtoGlobalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], id: isSet(object.id) ? new Uint8Array(object.id) : undefined, }; }, @@ -1295,7 +1297,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1307,7 +1309,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1334,7 +1336,7 @@ export const SimpleWithMap = { : {}, longLookup: isObject(object.longLookup) ? Object.entries(object.longLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1417,7 +1419,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1435,7 +1437,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -1470,7 +1472,7 @@ export const SimpleWithMap = { message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Number(value); + acc[tsProtoGlobalThis.Number(key)] = Number(value); } return acc; }, @@ -2049,7 +2051,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[Number(key)] = Entity.fromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -2078,7 +2080,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = Entity.fromPartial(value); + acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -2210,7 +2212,7 @@ export const SimpleWithMapOfEnums = { return { enumsById: isObject(object.enumsById) ? Object.entries(object.enumsById).reduce<{ [key: number]: StateEnum }>((acc, [key, value]) => { - acc[Number(key)] = stateEnumFromJSON(value); + acc[tsProtoGlobalThis.Number(key)] = stateEnumFromJSON(value); return acc; }, {}) : {}, @@ -2239,7 +2241,7 @@ export const SimpleWithMapOfEnums = { message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( (acc, [key, value]) => { if (value !== undefined) { - acc[Number(key)] = value as StateEnum; + acc[tsProtoGlobalThis.Number(key)] = value as StateEnum; } return acc; }, @@ -2944,7 +2946,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -2970,21 +2972,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/static-only-type-registry/foo.ts b/integration/static-only-type-registry/foo.ts index 60f5856c0..222c3f0f5 100644 --- a/integration/static-only-type-registry/foo.ts +++ b/integration/static-only-type-registry/foo.ts @@ -201,6 +201,25 @@ export const WithStruct = { messageTypeRegistry.set(WithStruct.$type, WithStruct); +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -221,14 +240,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/static-only-type-registry/google/protobuf/struct.ts b/integration/static-only-type-registry/google/protobuf/struct.ts index ae9f4bbc9..475b4d083 100644 --- a/integration/static-only-type-registry/google/protobuf/struct.ts +++ b/integration/static-only-type-registry/google/protobuf/struct.ts @@ -384,7 +384,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -435,7 +435,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -503,7 +503,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -530,7 +530,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -540,6 +540,25 @@ export const ListValue = { messageTypeRegistry.set(ListValue.$type, ListValue); +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/static-only-type-registry/google/protobuf/timestamp.ts b/integration/static-only-type-registry/google/protobuf/timestamp.ts index b6699d2e6..5df884670 100644 --- a/integration/static-only-type-registry/google/protobuf/timestamp.ts +++ b/integration/static-only-type-registry/google/protobuf/timestamp.ts @@ -221,7 +221,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/static-only/foo.ts b/integration/static-only/foo.ts index 79faf9709..7de11d28c 100644 --- a/integration/static-only/foo.ts +++ b/integration/static-only/foo.ts @@ -194,6 +194,25 @@ export const WithStruct = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -214,14 +233,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/static-only/google/protobuf/struct.ts b/integration/static-only/google/protobuf/struct.ts index 8fd19c518..403605339 100644 --- a/integration/static-only/google/protobuf/struct.ts +++ b/integration/static-only/google/protobuf/struct.ts @@ -379,7 +379,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -430,7 +430,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -496,7 +496,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -523,7 +523,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -531,6 +531,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/static-only/google/protobuf/timestamp.ts b/integration/static-only/google/protobuf/timestamp.ts index 6ccab68ee..d5c111f8c 100644 --- a/integration/static-only/google/protobuf/timestamp.ts +++ b/integration/static-only/google/protobuf/timestamp.ts @@ -218,7 +218,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/struct/google/protobuf/struct.ts b/integration/struct/google/protobuf/struct.ts index 4d9869bbe..35e974c3a 100644 --- a/integration/struct/google/protobuf/struct.ts +++ b/integration/struct/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/type-annotations/foo.ts b/integration/type-annotations/foo.ts index a70df82dd..55b5fafdd 100644 --- a/integration/type-annotations/foo.ts +++ b/integration/type-annotations/foo.ts @@ -197,6 +197,25 @@ export const WithStruct = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -217,14 +236,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/type-annotations/google/protobuf/struct.ts b/integration/type-annotations/google/protobuf/struct.ts index 44688c3bf..c923111ef 100644 --- a/integration/type-annotations/google/protobuf/struct.ts +++ b/integration/type-annotations/google/protobuf/struct.ts @@ -393,7 +393,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -444,7 +444,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -510,7 +510,10 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { $type: ListValue.$type, values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { + $type: ListValue.$type, + values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [], + }; }, toJSON(message: ListValue): unknown { @@ -537,7 +540,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -545,6 +548,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/type-annotations/google/protobuf/timestamp.ts b/integration/type-annotations/google/protobuf/timestamp.ts index 2e4805245..686732f27 100644 --- a/integration/type-annotations/google/protobuf/timestamp.ts +++ b/integration/type-annotations/google/protobuf/timestamp.ts @@ -220,7 +220,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude | "$type">]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/type-registry/foo.ts b/integration/type-registry/foo.ts index 66e28dce9..f203c9d8a 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -204,6 +204,25 @@ export const WithStruct = { messageTypeRegistry.set(WithStruct.$type, WithStruct); +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -224,14 +243,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/type-registry/google/protobuf/struct.ts b/integration/type-registry/google/protobuf/struct.ts index aa4e48cb5..77deb1b18 100644 --- a/integration/type-registry/google/protobuf/struct.ts +++ b/integration/type-registry/google/protobuf/struct.ts @@ -398,7 +398,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -449,7 +449,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -517,7 +517,10 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { $type: ListValue.$type, values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { + $type: ListValue.$type, + values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [], + }; }, toJSON(message: ListValue): unknown { @@ -544,7 +547,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -554,6 +557,25 @@ export const ListValue = { messageTypeRegistry.set(ListValue.$type, ListValue); +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/type-registry/google/protobuf/timestamp.ts b/integration/type-registry/google/protobuf/timestamp.ts index 02b9e8321..225e4068c 100644 --- a/integration/type-registry/google/protobuf/timestamp.ts +++ b/integration/type-registry/google/protobuf/timestamp.ts @@ -223,7 +223,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude | "$type">]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/unknown-fields/google/protobuf/compiler/plugin.ts b/integration/unknown-fields/google/protobuf/compiler/plugin.ts index 0a8e6d1c7..85445aa5a 100644 --- a/integration/unknown-fields/google/protobuf/compiler/plugin.ts +++ b/integration/unknown-fields/google/protobuf/compiler/plugin.ts @@ -512,7 +512,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/unknown-fields/google/protobuf/descriptor.ts b/integration/unknown-fields/google/protobuf/descriptor.ts index 0dc72596a..5639c65e0 100644 --- a/integration/unknown-fields/google/protobuf/descriptor.ts +++ b/integration/unknown-fields/google/protobuf/descriptor.ts @@ -3642,7 +3642,7 @@ const tsProtoGlobalThis: any = (() => { })(); function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-date-false/google/protobuf/timestamp.ts b/integration/use-date-false/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/use-date-false/google/protobuf/timestamp.ts +++ b/integration/use-date-false/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-date-false/metadata.ts b/integration/use-date-false/metadata.ts index 5ea696d73..86daf9ef9 100644 --- a/integration/use-date-false/metadata.ts +++ b/integration/use-date-false/metadata.ts @@ -67,6 +67,25 @@ export const Metadata = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -87,14 +106,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Timestamp { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return toTimestamp(o); } else if (typeof o === "string") { - return toTimestamp(new Date(o)); + return toTimestamp(new tsProtoGlobalThis.Date(o)); } else { return Timestamp.fromJSON(o); } diff --git a/integration/use-date-string/google/protobuf/timestamp.ts b/integration/use-date-string/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/use-date-string/google/protobuf/timestamp.ts +++ b/integration/use-date-string/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts index b71bfb533..6aa47527b 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -98,14 +98,16 @@ export const Todo = { fromJSON(object: any): Todo { return { id: isSet(object.id) ? String(object.id) : "", - timestamp: isSet(object.timestamp) ? String(object.timestamp) : undefined, - repeatedTimestamp: Array.isArray(object?.repeatedTimestamp) - ? object.repeatedTimestamp.map((e: any) => String(e)) + timestamp: isSet(object.timestamp) ? tsProtoGlobalThis.String(object.timestamp) : undefined, + repeatedTimestamp: tsProtoGlobalThis.Array.isArray(object?.repeatedTimestamp) + ? object.repeatedTimestamp.map((e: any) => tsProtoGlobalThis.String(e)) : [], - optionalTimestamp: isSet(object.optionalTimestamp) ? String(object.optionalTimestamp) : undefined, + optionalTimestamp: isSet(object.optionalTimestamp) + ? tsProtoGlobalThis.String(object.optionalTimestamp) + : undefined, mapOfTimestamps: isObject(object.mapOfTimestamps) ? Object.entries(object.mapOfTimestamps).reduce<{ [key: string]: string }>((acc, [key, value]) => { - acc[key] = String(value); + acc[key] = tsProtoGlobalThis.String(value); return acc; }, {}) : {}, @@ -208,7 +210,7 @@ export const Todo_MapOfTimestampsEntry = { fromJSON(object: any): Todo_MapOfTimestampsEntry { return { key: isSet(object.key) ? String(object.key) : "", - value: isSet(object.value) ? String(object.value) : undefined, + value: isSet(object.value) ? tsProtoGlobalThis.String(object.value) : undefined, }; }, @@ -234,6 +236,25 @@ export const Todo_MapOfTimestampsEntry = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -246,7 +267,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function toTimestamp(dateStr: string): Timestamp { - const date = new Date(dateStr); + const date = new tsProtoGlobalThis.Date(dateStr); const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; return { seconds, nanos }; @@ -255,7 +276,7 @@ function toTimestamp(dateStr: string): Timestamp { function fromTimestamp(t: Timestamp): string { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis).toISOString(); + return new tsProtoGlobalThis.Date(millis).toISOString(); } function isObject(value: any): boolean { diff --git a/integration/use-date-true/google/protobuf/timestamp.ts b/integration/use-date-true/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/use-date-true/google/protobuf/timestamp.ts +++ b/integration/use-date-true/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts index e5dae4c12..6bc466750 100644 --- a/integration/use-date-true/use-date-true.ts +++ b/integration/use-date-true/use-date-true.ts @@ -100,7 +100,7 @@ export const Todo = { return { id: isSet(object.id) ? String(object.id) : "", timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined, - repeatedTimestamp: Array.isArray(object?.repeatedTimestamp) + repeatedTimestamp: tsProtoGlobalThis.Array.isArray(object?.repeatedTimestamp) ? object.repeatedTimestamp.map((e: any) => fromJsonTimestamp(e)) : [], optionalTimestamp: isSet(object.optionalTimestamp) ? fromJsonTimestamp(object.optionalTimestamp) : undefined, @@ -275,6 +275,25 @@ interface Rpc { request(service: string, method: string, data: Uint8Array): Promise; } +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T @@ -295,14 +314,14 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/use-map-type/google/protobuf/struct.ts b/integration/use-map-type/google/protobuf/struct.ts index da5112d66..290e65668 100644 --- a/integration/use-map-type/google/protobuf/struct.ts +++ b/integration/use-map-type/google/protobuf/struct.ts @@ -368,7 +368,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -419,7 +419,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -483,7 +483,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -510,7 +510,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -518,6 +518,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-map-type/google/protobuf/timestamp.ts b/integration/use-map-type/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/use-map-type/google/protobuf/timestamp.ts +++ b/integration/use-map-type/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-map-type/use-map-type.ts b/integration/use-map-type/use-map-type.ts index 70a6183ce..cb7cb87ab 100644 --- a/integration/use-map-type/use-map-type.ts +++ b/integration/use-map-type/use-map-type.ts @@ -218,7 +218,7 @@ export const Maps = { : new Map(), int32ToInt32: isObject(object.int32ToInt32) ? Object.entries(object.int32ToInt32).reduce>((acc, [key, value]) => { - acc.set(Number(key), Number(value)); + acc.set(tsProtoGlobalThis.Number(key), Number(value)); return acc; }, new Map()) : new Map(), @@ -230,7 +230,7 @@ export const Maps = { : new Map(), int64ToInt64: isObject(object.int64ToInt64) ? Object.entries(object.int64ToInt64).reduce>((acc, [key, value]) => { - acc.set(Number(key), Number(value)); + acc.set(tsProtoGlobalThis.Number(key), Number(value)); return acc; }, new Map()) : new Map(), @@ -741,7 +741,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -767,21 +767,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-numeric-enum-json/google/protobuf/struct.ts b/integration/use-numeric-enum-json/google/protobuf/struct.ts index 4fac6f104..9f9a70277 100644 --- a/integration/use-numeric-enum-json/google/protobuf/struct.ts +++ b/integration/use-numeric-enum-json/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-numeric-enum-json/simple.ts b/integration/use-numeric-enum-json/simple.ts index 5be507613..bbc7fbd44 100644 --- a/integration/use-numeric-enum-json/simple.ts +++ b/integration/use-numeric-enum-json/simple.ts @@ -150,7 +150,9 @@ export const Simple = { return { name: isSet(object.name) ? String(object.name) : "", state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - states: Array.isArray(object?.states) ? object.states.map((e: any) => stateEnumFromJSON(e)) : [], + states: tsProtoGlobalThis.Array.isArray(object?.states) + ? object.states.map((e: any) => stateEnumFromJSON(e)) + : [], nullValue: isSet(object.nullValue) ? nullValueFromJSON(object.nullValue) : 0, stateMap: isObject(object.stateMap) ? Object.entries(object.stateMap).reduce<{ [key: string]: StateEnum }>((acc, [key, value]) => { @@ -283,6 +285,25 @@ export const Simple_StateMapEntry = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-objectid-true-external-import/use-objectid-true.ts b/integration/use-objectid-true-external-import/use-objectid-true.ts index 796561952..d2011f0b4 100644 --- a/integration/use-objectid-true-external-import/use-objectid-true.ts +++ b/integration/use-objectid-true-external-import/use-objectid-true.ts @@ -100,7 +100,9 @@ export const Todo = { return { id: isSet(object.id) ? String(object.id) : "", oid: isSet(object.oid) ? fromJsonObjectId(object.oid) : undefined, - repeatedOid: Array.isArray(object?.repeatedOid) ? object.repeatedOid.map((e: any) => fromJsonObjectId(e)) : [], + repeatedOid: tsProtoGlobalThis.Array.isArray(object?.repeatedOid) + ? object.repeatedOid.map((e: any) => fromJsonObjectId(e)) + : [], optionalOid: isSet(object.optionalOid) ? fromJsonObjectId(object.optionalOid) : undefined, mapOfOids: isObject(object.mapOfOids) ? Object.entries(object.mapOfOids).reduce<{ [key: string]: mongodb.ObjectId }>((acc, [key, value]) => { @@ -237,6 +239,25 @@ export const Todo_MapOfOidsEntry = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-optionals-all/google/protobuf/timestamp.ts b/integration/use-optionals-all/google/protobuf/timestamp.ts index 81148d009..40731e1b4 100644 --- a/integration/use-optionals-all/google/protobuf/timestamp.ts +++ b/integration/use-optionals-all/google/protobuf/timestamp.ts @@ -218,7 +218,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-optionals-all/test.ts b/integration/use-optionals-all/test.ts index 848e2fabd..daedb0c3a 100644 --- a/integration/use-optionals-all/test.ts +++ b/integration/use-optionals-all/test.ts @@ -431,13 +431,21 @@ export const OptionalsTest = { truth: isSet(object.truth) ? Boolean(object.truth) : false, description: isSet(object.description) ? String(object.description) : "", data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(0), - repId: Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], - repChild: Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : [], - repState: Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : [], - repLong: Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], - repTruth: Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], - repDescription: Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : [], - repData: Array.isArray(object?.repData) ? object.repData.map((e: any) => bytesFromBase64(e)) : [], + repId: tsProtoGlobalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], + repChild: tsProtoGlobalThis.Array.isArray(object?.repChild) + ? object.repChild.map((e: any) => Child.fromJSON(e)) + : [], + repState: tsProtoGlobalThis.Array.isArray(object?.repState) + ? object.repState.map((e: any) => stateEnumFromJSON(e)) + : [], + repLong: tsProtoGlobalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], + repTruth: tsProtoGlobalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], + repDescription: tsProtoGlobalThis.Array.isArray(object?.repDescription) + ? object.repDescription.map((e: any) => String(e)) + : [], + repData: tsProtoGlobalThis.Array.isArray(object?.repData) + ? object.repData.map((e: any) => bytesFromBase64(e)) + : [], optId: isSet(object.optId) ? Number(object.optId) : undefined, optChild: isSet(object.optChild) ? Child.fromJSON(object.optChild) : undefined, optState: isSet(object.optState) ? stateEnumFromJSON(object.optState) : undefined, @@ -731,7 +739,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -757,21 +765,21 @@ function toTimestamp(date: Date): Timestamp { function fromTimestamp(t: Timestamp): Date { let millis = (t.seconds || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new Date(millis); + return new tsProtoGlobalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof Date) { + if (o instanceof tsProtoGlobalThis.Date) { return o; } else if (typeof o === "string") { - return new Date(o); + return new tsProtoGlobalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-optionals-no-undefined/test.ts b/integration/use-optionals-no-undefined/test.ts index 2cd3a6e07..b5c799b9d 100644 --- a/integration/use-optionals-no-undefined/test.ts +++ b/integration/use-optionals-no-undefined/test.ts @@ -431,15 +431,23 @@ export const OptionalsTest = { truth: isSet(object.truth) ? Boolean(object.truth) : undefined, description: isSet(object.description) ? String(object.description) : undefined, data: isSet(object.data) ? bytesFromBase64(object.data) : undefined, - repId: Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : undefined, - repChild: Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : undefined, - repState: Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : undefined, - repLong: Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : undefined, - repTruth: Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : undefined, - repDescription: Array.isArray(object?.repDescription) + repId: tsProtoGlobalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : undefined, + repChild: tsProtoGlobalThis.Array.isArray(object?.repChild) + ? object.repChild.map((e: any) => Child.fromJSON(e)) + : undefined, + repState: tsProtoGlobalThis.Array.isArray(object?.repState) + ? object.repState.map((e: any) => stateEnumFromJSON(e)) + : undefined, + repLong: tsProtoGlobalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : undefined, + repTruth: tsProtoGlobalThis.Array.isArray(object?.repTruth) + ? object.repTruth.map((e: any) => Boolean(e)) + : undefined, + repDescription: tsProtoGlobalThis.Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : undefined, - repData: Array.isArray(object?.repData) ? object.repData.map((e: any) => bytesFromBase64(e)) : undefined, + repData: tsProtoGlobalThis.Array.isArray(object?.repData) + ? object.repData.map((e: any) => bytesFromBase64(e)) + : undefined, optId: isSet(object.optId) ? Number(object.optId) : undefined, optChild: isSet(object.optChild) ? Child.fromJSON(object.optChild) : undefined, optState: isSet(object.optState) ? stateEnumFromJSON(object.optState) : undefined, @@ -727,7 +735,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -745,7 +753,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/use-readonly-types/google/protobuf/field_mask.ts b/integration/use-readonly-types/google/protobuf/field_mask.ts index c2c42efc6..89d0d954d 100644 --- a/integration/use-readonly-types/google/protobuf/field_mask.ts +++ b/integration/use-readonly-types/google/protobuf/field_mask.ts @@ -246,9 +246,9 @@ export const FieldMask = { fromJSON(object: any): FieldMask { return { paths: typeof (object) === "string" - ? object.split(",").filter(Boolean) - : Array.isArray(object?.paths) - ? object.paths.map(String) + ? object.split(",").filter(tsProtoGlobalThis.Boolean) + : tsProtoGlobalThis.Array.isArray(object?.paths) + ? object.paths.map(tsProtoGlobalThis.String) : [], }; }, @@ -277,6 +277,25 @@ export const FieldMask = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-readonly-types/google/protobuf/struct.ts b/integration/use-readonly-types/google/protobuf/struct.ts index fa8a370e1..71aea3fd3 100644 --- a/integration/use-readonly-types/google/protobuf/struct.ts +++ b/integration/use-readonly-types/google/protobuf/struct.ts @@ -436,7 +436,7 @@ export const Value = { result.kind = { $case: "numberValue", numberValue: value }; } else if (typeof value === "string") { result.kind = { $case: "stringValue", stringValue: value }; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.kind = { $case: "listValue", listValue: value }; } else if (typeof value === "object") { result.kind = { $case: "structValue", structValue: value }; @@ -501,7 +501,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -528,7 +528,7 @@ export const ListValue = { }, unwrap(message: any): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -536,6 +536,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/use-readonly-types/use-readonly-types.ts b/integration/use-readonly-types/use-readonly-types.ts index 33e6ad30c..fbf2eadeb 100644 --- a/integration/use-readonly-types/use-readonly-types.ts +++ b/integration/use-readonly-types/use-readonly-types.ts @@ -201,15 +201,17 @@ export const Entity = { return { intVal: isSet(object.intVal) ? Number(object.intVal) : 0, stringVal: isSet(object.stringVal) ? String(object.stringVal) : "", - intArray: Array.isArray(object?.intArray) ? object.intArray.map((e: any) => Number(e)) : [], - stringArray: Array.isArray(object?.stringArray) ? object.stringArray.map((e: any) => String(e)) : [], + intArray: tsProtoGlobalThis.Array.isArray(object?.intArray) ? object.intArray.map((e: any) => Number(e)) : [], + stringArray: tsProtoGlobalThis.Array.isArray(object?.stringArray) + ? object.stringArray.map((e: any) => String(e)) + : [], subEntity: isSet(object.subEntity) ? SubEntity.fromJSON(object.subEntity) : undefined, - subEntityArray: Array.isArray(object?.subEntityArray) + subEntityArray: tsProtoGlobalThis.Array.isArray(object?.subEntityArray) ? object.subEntityArray.map((e: any) => SubEntity.fromJSON(e)) : [], optionalIntVal: isSet(object.optionalIntVal) ? Number(object.optionalIntVal) : undefined, fieldMask: isSet(object.fieldMask) ? FieldMask.unwrap(FieldMask.fromJSON(object.fieldMask)) : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, oneOfValue: isSet(object.theStringValue) ? { $case: "theStringValue", theStringValue: String(object.theStringValue) } @@ -352,6 +354,25 @@ export const SubEntity = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/value/google/protobuf/struct.ts b/integration/value/google/protobuf/struct.ts index 4d9869bbe..35e974c3a 100644 --- a/integration/value/google/protobuf/struct.ts +++ b/integration/value/google/protobuf/struct.ts @@ -373,7 +373,7 @@ export const Value = { stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined, boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, - listValue: Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, }; }, @@ -424,7 +424,7 @@ export const Value = { result.numberValue = value; } else if (typeof value === "string") { result.stringValue = value; - } else if (Array.isArray(value)) { + } else if (tsProtoGlobalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -488,7 +488,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { values: Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -515,7 +515,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,6 +523,25 @@ export const ListValue = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/value/google/protobuf/wrappers.ts b/integration/value/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/value/google/protobuf/wrappers.ts +++ b/integration/value/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/value/value.ts b/integration/value/value.ts index de18a9501..c86dc6522 100644 --- a/integration/value/value.ts +++ b/integration/value/value.ts @@ -91,9 +91,11 @@ export const ValueMessage = { fromJSON(object: any): ValueMessage { return { value: isSet(object?.value) ? object.value : undefined, - anyList: Array.isArray(object.anyList) ? [...object.anyList] : undefined, - repeatedAny: Array.isArray(object?.repeatedAny) ? [...object.repeatedAny] : [], - repeatedStrings: Array.isArray(object?.repeatedStrings) ? object.repeatedStrings.map((e: any) => String(e)) : [], + anyList: tsProtoGlobalThis.Array.isArray(object.anyList) ? [...object.anyList] : undefined, + repeatedAny: tsProtoGlobalThis.Array.isArray(object?.repeatedAny) ? [...object.repeatedAny] : [], + repeatedStrings: tsProtoGlobalThis.Array.isArray(object?.repeatedStrings) + ? object.repeatedStrings.map((e: any) => String(e)) + : [], structValue: isObject(object.structValue) ? object.structValue : undefined, }; }, @@ -132,6 +134,25 @@ export const ValueMessage = { }, }; +declare const self: any | undefined; +declare const window: any | undefined; +declare const global: any | undefined; +const tsProtoGlobalThis: any = (() => { + if (typeof globalThis !== "undefined") { + return globalThis; + } + if (typeof self !== "undefined") { + return self; + } + if (typeof window !== "undefined") { + return window; + } + if (typeof global !== "undefined") { + return global; + } + throw "Unable to locate global object"; +})(); + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/integration/vector-tile/vector_tile.ts b/integration/vector-tile/vector_tile.ts index 0b8b98571..b55250ece 100644 --- a/integration/vector-tile/vector_tile.ts +++ b/integration/vector-tile/vector_tile.ts @@ -115,7 +115,11 @@ export const Tile = { }, fromJSON(object: any): Tile { - return { layers: Array.isArray(object?.layers) ? object.layers.map((e: any) => Tile_Layer.fromJSON(e)) : [] }; + return { + layers: tsProtoGlobalThis.Array.isArray(object?.layers) + ? object.layers.map((e: any) => Tile_Layer.fromJSON(e)) + : [], + }; }, toJSON(message: Tile): unknown { @@ -377,9 +381,9 @@ export const Tile_Feature = { fromJSON(object: any): Tile_Feature { return { id: isSet(object.id) ? Number(object.id) : 0, - tags: Array.isArray(object?.tags) ? object.tags.map((e: any) => Number(e)) : [], + tags: tsProtoGlobalThis.Array.isArray(object?.tags) ? object.tags.map((e: any) => Number(e)) : [], type: isSet(object.type) ? tile_GeomTypeFromJSON(object.type) : 0, - geometry: Array.isArray(object?.geometry) ? object.geometry.map((e: any) => Number(e)) : [], + geometry: tsProtoGlobalThis.Array.isArray(object?.geometry) ? object.geometry.map((e: any) => Number(e)) : [], }; }, @@ -502,9 +506,13 @@ export const Tile_Layer = { return { version: isSet(object.version) ? Number(object.version) : 0, name: isSet(object.name) ? String(object.name) : "", - features: Array.isArray(object?.features) ? object.features.map((e: any) => Tile_Feature.fromJSON(e)) : [], - keys: Array.isArray(object?.keys) ? object.keys.map((e: any) => String(e)) : [], - values: Array.isArray(object?.values) ? object.values.map((e: any) => Tile_Value.fromJSON(e)) : [], + features: tsProtoGlobalThis.Array.isArray(object?.features) + ? object.features.map((e: any) => Tile_Feature.fromJSON(e)) + : [], + keys: tsProtoGlobalThis.Array.isArray(object?.keys) ? object.keys.map((e: any) => String(e)) : [], + values: tsProtoGlobalThis.Array.isArray(object?.values) + ? object.values.map((e: any) => Tile_Value.fromJSON(e)) + : [], extent: isSet(object.extent) ? Number(object.extent) : 0, }; }, @@ -578,7 +586,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/wrappers-regression/google/protobuf/timestamp.ts b/integration/wrappers-regression/google/protobuf/timestamp.ts index 585a2e7e9..8a8bf6005 100644 --- a/integration/wrappers-regression/google/protobuf/timestamp.ts +++ b/integration/wrappers-regression/google/protobuf/timestamp.ts @@ -216,7 +216,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/integration/wrappers-regression/google/protobuf/wrappers.ts b/integration/wrappers-regression/google/protobuf/wrappers.ts index 47d1af1f6..0a7c60274 100644 --- a/integration/wrappers-regression/google/protobuf/wrappers.ts +++ b/integration/wrappers-regression/google/protobuf/wrappers.ts @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } @@ -663,7 +663,7 @@ export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function longToNumber(long: Long): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); diff --git a/src/generate-async-iterable.ts b/src/generate-async-iterable.ts index fe445183b..da5301318 100644 --- a/src/generate-async-iterable.ts +++ b/src/generate-async-iterable.ts @@ -1,7 +1,8 @@ import { code, Code } from "ts-poet"; +import { Utils } from "./main"; /** Creates a function to transform a message Source to a Uint8Array Source. */ -export function generateEncodeTransform(fullName: string): Code { +export function generateEncodeTransform(utils: Utils, fullName: string): Code { return code` // encodeTransform encodes a source of message objects. // Transform<${fullName}, Uint8Array> @@ -9,12 +10,12 @@ export function generateEncodeTransform(fullName: string): Code { source: AsyncIterable<${fullName} | ${fullName}[]> | Iterable<${fullName} | ${fullName}[]> ): AsyncIterable { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (${utils.globalThis}.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [${fullName}.encode(p).finish()] } } else { - yield* [${fullName}.encode(pkt).finish()] + yield* [${fullName}.encode(pkt as any).finish()] } } } @@ -22,7 +23,7 @@ export function generateEncodeTransform(fullName: string): Code { } /** Creates a function to transform a Uint8Array Source to a message Source. */ -export function generateDecodeTransform(fullName: string): Code { +export function generateDecodeTransform(utils: Utils, fullName: string): Code { return code` // decodeTransform decodes a source of encoded messages. // Transform @@ -30,12 +31,12 @@ export function generateDecodeTransform(fullName: string): Code { source: AsyncIterable | Iterable ): AsyncIterable<${fullName}> { for await (const pkt of source) { - if (Array.isArray(pkt)) { - for (const p of pkt) { + if (${utils.globalThis}.Array.isArray(pkt)) { + for (const p of (pkt as any)) { yield* [${fullName}.decode(p)] } } else { - yield* [${fullName}.decode(pkt)] + yield* [${fullName}.decode(pkt as any)] } } } diff --git a/src/generate-struct-wrappers.ts b/src/generate-struct-wrappers.ts index 1a6dbbb6e..3ed8caeb2 100644 --- a/src/generate-struct-wrappers.ts +++ b/src/generate-struct-wrappers.ts @@ -57,7 +57,7 @@ export function generateWrapDeep(ctx: Context, fullProtoTypeName: string, fieldN result.${fieldNames.numberValue} = value; } else if (typeof value === 'string') { result.${fieldNames.stringValue} = value; - } else if (Array.isArray(value)) { + } else if (${ctx.utils.globalThis}.Array.isArray(value)) { result.${fieldNames.listValue} = ListValue.wrap(value); } else if (typeof value === 'object') { result.${fieldNames.structValue} = Struct.wrap(value); @@ -141,7 +141,7 @@ export function generateUnwrapDeep(ctx: Context, fullProtoTypeName: string, fiel if (isListValueTypeName(fullProtoTypeName)) { chunks.push(code`unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "ListValue"}): Array { - if (message?.hasOwnProperty('values') && Array.isArray(message.values)) { + if (message?.hasOwnProperty('values') && ${ctx.utils.globalThis}.Array.isArray(message.values)) { return message.values.map(Value.unwrap); } else { return message as any; @@ -193,7 +193,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie result.kind = {$case: '${fieldNames.numberValue}', ${fieldNames.numberValue}: value}; } else if (typeof value === 'string') { result.kind = {$case: '${fieldNames.stringValue}', ${fieldNames.stringValue}: value}; - } else if (Array.isArray(value)) { + } else if (${ctx.utils.globalThis}.Array.isArray(value)) { result.kind = {$case: '${fieldNames.listValue}', ${fieldNames.listValue}: value}; } else if (typeof value === 'object') { result.kind = {$case: '${fieldNames.structValue}', ${fieldNames.structValue}: value}; @@ -213,7 +213,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie result.${fieldNames.numberValue} = value; } else if (typeof value === 'string') { result.${fieldNames.stringValue} = value; - } else if (Array.isArray(value)) { + } else if (${ctx.utils.globalThis}.Array.isArray(value)) { result.${fieldNames.listValue} = value; } else if (typeof value === 'object') { result.${fieldNames.structValue} = value; @@ -315,7 +315,7 @@ export function generateUnwrapShallow(ctx: Context, fullProtoTypeName: string, f if (isListValueTypeName(fullProtoTypeName)) { chunks.push(code`unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "ListValue"}): Array { - if (message?.hasOwnProperty('values') && Array.isArray(message.values)) { + if (message?.hasOwnProperty('values') && ${ctx.utils.globalThis}.Array.isArray(message.values)) { return message.values; } else { return message as any; diff --git a/src/main.ts b/src/main.ts index 2a1cc6b0d..df7afc22e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -233,8 +233,8 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri } } if (options.useAsyncIterable) { - staticMembers.push(generateEncodeTransform(fullName)); - staticMembers.push(generateDecodeTransform(fullName)); + staticMembers.push(generateEncodeTransform(ctx.utils, fullName)); + staticMembers.push(generateDecodeTransform(ctx.utils, fullName)); } if (options.outputJsonMethods) { if (options.outputJsonMethods === true || options.outputJsonMethods === "from-only") { @@ -418,7 +418,7 @@ export function makeUtils(options: Options): Utils { ...bytes, ...makeDeepPartial(options, longs), ...makeObjectIdMethods(), - ...makeTimestampMethods(options, longs), + ...makeTimestampMethods(options, longs, bytes), ...longs, ...makeComparisonUtils(), ...makeNiceGrpcServerStreamingMethodResult(), @@ -506,7 +506,7 @@ function makeLongUtils(options: Options, bytes: ReturnType "longToNumber", code` function longToNumber(long: ${Long}): number { - if (long.gt(Number.MAX_SAFE_INTEGER)) { + if (long.gt(${bytes.globalThis}.Number.MAX_SAFE_INTEGER)) { throw new ${bytes.globalThis}.Error("Value is larger than Number.MAX_SAFE_INTEGER") } return long.toNumber(); @@ -560,7 +560,7 @@ function makeByteUtils() { } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(String.fromCharCode(byte)); + bin.push(${globalThis}.String.fromCharCode(byte)); }); return ${globalThis}.btoa(bin.join('')); } @@ -665,19 +665,23 @@ function makeObjectIdMethods() { return { fromJsonObjectId, fromProtoObjectId, toProtoObjectId }; } -function makeTimestampMethods(options: Options, longs: ReturnType) { +function makeTimestampMethods( + options: Options, + longs: ReturnType, + bytes: ReturnType, +) { const Timestamp = impProto(options, "google/protobuf/timestamp", "Timestamp"); let seconds: string | Code = "date.getTime() / 1_000"; - let toNumberCode = "t.seconds"; + let toNumberCode: string | Code = "t.seconds"; if (options.forceLong === LongOption.LONG) { toNumberCode = "t.seconds.toNumber()"; seconds = code`${longs.numberToLong}(date.getTime() / 1_000)`; } else if (options.forceLong === LongOption.BIGINT) { - toNumberCode = "Number(t.seconds.toString())"; + toNumberCode = code`${bytes.globalThis}.Number(t.seconds.toString())`; seconds = code`BigInt(Math.trunc(date.getTime() / 1_000))`; } else if (options.forceLong === LongOption.STRING) { - toNumberCode = "Number(t.seconds)"; + toNumberCode = code`${bytes.globalThis}.Number(t.seconds)`; // Must discard the fractional piece here // Otherwise the fraction ends up on the seconds when parsed as a Long // (note this only occurs when the string is > 8 characters) @@ -691,7 +695,7 @@ function makeTimestampMethods(options: Options, longs: ReturnType Code } } = { ["google.protobuf.FieldMask"]: { paths: (from: string) => code`typeof(${from}) === 'string' - ? ${from}.split(",").filter(Boolean) - : Array.isArray(${from}?.paths) - ? ${from}.paths.map(String) + ? ${from}.split(",").filter(${ctx.utils.globalThis}.Boolean) + : ${ctx.utils.globalThis}.Array.isArray(${from}?.paths) + ? ${from}.paths.map(${ctx.utils.globalThis}.String) : []`, }, }; @@ -1759,7 +1763,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, } else if (isObjectId(field) && options.useMongoObjectId) { return code`${utils.fromJsonObjectId}(${from})`; } else if (isTimestamp(field) && options.useDate === DateOption.STRING) { - return code`String(${from})`; + return code`${utils.globalThis}.String(${from})`; } else if ( isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP) @@ -1808,7 +1812,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, } else if (isObjectId(valueField) && options.useMongoObjectId) { return code`${utils.fromJsonObjectId}(${from})`; } else if (isTimestamp(valueField) && options.useDate === DateOption.STRING) { - return code`String(${from})`; + return code`${utils.globalThis}.String(${from})`; } else if ( isTimestamp(valueField) && (options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP) @@ -1869,11 +1873,13 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, const readValueSnippet = readSnippet("e"); if (readValueSnippet.toString() === code`e`.toString()) { - chunks.push(code`${fieldName}: Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`); + chunks.push( + code`${fieldName}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`, + ); } else { // Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here. chunks.push(code` - ${fieldName}: Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readValueSnippet}): ${fallback}, + ${fieldName}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readValueSnippet}): ${fallback}, `); } } @@ -1907,7 +1913,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, ); } else if (isListValueType(field)) { chunks.push(code` - ${fieldName}: Array.isArray(${jsonProperty}) + ${fieldName}: ${ctx.utils.globalThis}.Array.isArray(${jsonProperty}) ? ${readSnippet(`${jsonProperty}`)} : undefined, `); @@ -2276,12 +2282,12 @@ function convertFromObjectKey( } else if (ctx.options.forceLong === LongOption.BIGINT) { return code`BigInt(${variableName})`; } else if (ctx.options.forceLong === LongOption.STRING) { - return code`String(${variableName})`; + return code`${ctx.utils.globalThis}.String(${variableName})`; } else { - return code`Number(${variableName})`; + return code`${ctx.utils.globalThis}.Number(${variableName})`; } } else { - return code`Number(${variableName})`; + return code`${ctx.utils.globalThis}.Number(${variableName})`; } }