diff --git a/.nvmrc b/.nvmrc index 3027af39c..a9b234d51 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -16.10.0 +v20.8.0 diff --git a/README.markdown b/README.markdown index eb96269fc..50e111430 100644 --- a/README.markdown +++ b/README.markdown @@ -289,6 +289,10 @@ Generated code will be placed in the Gradle build directory. ### Supported options +- With `--ts_proto_opt=globalThisPolyfill=true`, ts-proto will include a polyfill for globalThis. + + Defaults to `false`, i.e. we assume `globalThis` is available. + - With `--ts_proto_opt=context=true`, the services will have a Go-style `ctx` parameter, which is useful for tracing/logging/etc. if you're not using node's `async_hooks` api due to performance reasons. - With `--ts_proto_opt=forceLong=long`, all 64-bit numbers will be parsed as instances of `Long` (using the [long](https://www.npmjs.com/package/long) library). diff --git a/integration/async-iterable-services-abort-signal/simple.ts b/integration/async-iterable-services-abort-signal/simple.ts index d56502c48..2c25b9e6d 100644 --- a/integration/async-iterable-services-abort-signal/simple.ts +++ b/integration/async-iterable-services-abort-signal/simple.ts @@ -49,7 +49,7 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (tsProtoGlobalThis.Array.isArray(pkt)) { + if (globalThis.Array.isArray(pkt)) { for (const p of (pkt as any)) { yield* [EchoMsg.encode(p).finish()]; } @@ -65,7 +65,7 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (tsProtoGlobalThis.Array.isArray(pkt)) { + if (globalThis.Array.isArray(pkt)) { for (const p of (pkt as any)) { yield* [EchoMsg.decode(p)]; } @@ -173,25 +173,6 @@ 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 c952a6ca0..7321c4b6e 100644 --- a/integration/async-iterable-services/simple.ts +++ b/integration/async-iterable-services/simple.ts @@ -49,7 +49,7 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (tsProtoGlobalThis.Array.isArray(pkt)) { + if (globalThis.Array.isArray(pkt)) { for (const p of (pkt as any)) { yield* [EchoMsg.encode(p).finish()]; } @@ -65,7 +65,7 @@ export const EchoMsg = { source: AsyncIterable | Iterable, ): AsyncIterable { for await (const pkt of source) { - if (tsProtoGlobalThis.Array.isArray(pkt)) { + if (globalThis.Array.isArray(pkt)) { for (const p of (pkt as any)) { yield* [EchoMsg.decode(p)]; } @@ -157,25 +157,6 @@ 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 d490292ca..d98b83e4b 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: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -140,9 +140,7 @@ export const BatchQueryResponse = { fromJSON(object: any): BatchQueryResponse { return { - entities: tsProtoGlobalThis.Array.isArray(object?.entities) - ? object.entities.map((e: any) => Entity.fromJSON(e)) - : [], + entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [], }; }, @@ -200,7 +198,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -757,25 +755,6 @@ 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 0dfe190ff..7f598d5c0 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: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -140,9 +140,7 @@ export const BatchQueryResponse = { fromJSON(object: any): BatchQueryResponse { return { - entities: tsProtoGlobalThis.Array.isArray(object?.entities) - ? object.entities.map((e: any) => Entity.fromJSON(e)) - : [], + entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [], }; }, @@ -200,7 +198,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -757,25 +755,6 @@ 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 cede49b77..d8f317e24 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -80,7 +80,7 @@ export const BatchQueryRequest = { }, fromJSON(object: any): BatchQueryRequest { - return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchQueryRequest): unknown { @@ -138,9 +138,7 @@ export const BatchQueryResponse = { fromJSON(object: any): BatchQueryResponse { return { - entities: tsProtoGlobalThis.Array.isArray(object?.entities) - ? object.entities.map((e: any) => Entity.fromJSON(e)) - : [], + entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [], }; }, @@ -198,7 +196,7 @@ export const BatchMapQueryRequest = { }, fromJSON(object: any): BatchMapQueryRequest { - return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; + return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] }; }, toJSON(message: BatchMapQueryRequest): unknown { @@ -714,25 +712,6 @@ 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 e87f0842d..d2fde563c 100644 --- a/integration/bytes-as-base64/message.ts +++ b/integration/bytes-as-base64/message.ts @@ -33,30 +33,11 @@ export const Message = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -66,14 +47,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/bytes-node/google/protobuf/wrappers.ts b/integration/bytes-node/google/protobuf/wrappers.ts index 24641051e..3733bd145 100644 --- a/integration/bytes-node/google/protobuf/wrappers.ts +++ b/integration/bytes-node/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 5f72494a2..5ba70ad87 100644 --- a/integration/bytes-node/point.ts +++ b/integration/bytes-node/point.ts @@ -83,30 +83,11 @@ export const Point = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -116,14 +97,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.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 e06a6f836..03a7220b9 100644 --- a/integration/emit-default-values-json/google/protobuf/timestamp.ts +++ b/integration/emit-default-values-json/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 c6e1358ad..a5e6867ce 100644 --- a/integration/emit-default-values-json/test.ts +++ b/integration/emit-default-values-json/test.ts @@ -417,21 +417,15 @@ 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: 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) + repId: globalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], + repChild: globalThis.Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : [], + repState: globalThis.Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : [], + repLong: globalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], + repTruth: globalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], + repDescription: globalThis.Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : [], - repData: tsProtoGlobalThis.Array.isArray(object?.repData) - ? object.repData.map((e: any) => bytesFromBase64(e)) - : [], + repData: globalThis.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, @@ -689,30 +683,11 @@ export const Child = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -722,14 +697,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -753,22 +728,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 61f2c4039..14147ad36 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(tsProtoGlobalThis.Boolean) - : tsProtoGlobalThis.Array.isArray(object?.paths) - ? object.paths.map(tsProtoGlobalThis.String) + ? object.split(",").filter(globalThis.Boolean) + : globalThis.Array.isArray(object?.paths) + ? object.paths.map(globalThis.String) : [], }; }, @@ -277,25 +277,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/file-suffix/google/protobuf/timestamp.pb.ts +++ b/integration/file-suffix/google/protobuf/timestamp.pb.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 c00ede347..548440681 100644 --- a/integration/file-suffix/parent.pb.ts +++ b/integration/file-suffix/parent.pb.ts @@ -100,25 +100,6 @@ 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 @@ -139,14 +120,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 b01126a23..9cae047ca 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: tsProtoGlobalThis.Array.isArray(object?.repeatedMessage) + repeatedMessage: globalThis.Array.isArray(object?.repeatedMessage) ? object.repeatedMessage.map((e: any) => TPartialMessage.fromJSON(e)) : undefined, - repeatedString: tsProtoGlobalThis.Array.isArray(object?.repeatedString) + repeatedString: globalThis.Array.isArray(object?.repeatedString) ? object.repeatedString.map((e: any) => String(e)) : undefined, - repeatedNumber: tsProtoGlobalThis.Array.isArray(object?.repeatedNumber) + repeatedNumber: globalThis.Array.isArray(object?.repeatedNumber) ? object.repeatedNumber.map((e: any) => Number(e)) : undefined, }; @@ -361,25 +361,6 @@ 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/global-this/parameters.txt b/integration/global-this/parameters.txt new file mode 100644 index 000000000..de44407f6 --- /dev/null +++ b/integration/global-this/parameters.txt @@ -0,0 +1 @@ +globalThisPolyfill=true diff --git a/integration/groups/test.ts b/integration/groups/test.ts index 95f903210..76f11de6e 100644 --- a/integration/groups/test.ts +++ b/integration/groups/test.ts @@ -399,11 +399,11 @@ export const GroupsRepeatedTest = { fromJSON(object: any): GroupsRepeatedTest { return { - int1: tsProtoGlobalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, - group: tsProtoGlobalThis.Array.isArray(object?.group) + int1: globalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, + group: globalThis.Array.isArray(object?.group) ? object.group.map((e: any) => GroupsRepeatedTest_Group.fromJSON(e)) : undefined, - int3: tsProtoGlobalThis.Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, + int3: globalThis.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: 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, + key: globalThis.Array.isArray(object?.key) ? object.key.map((e: any) => String(e)) : undefined, + value: globalThis.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: tsProtoGlobalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, - group: tsProtoGlobalThis.Array.isArray(object?.group) + int1: globalThis.Array.isArray(object?.int1) ? object.int1.map((e: any) => Number(e)) : undefined, + group: globalThis.Array.isArray(object?.group) ? object.group.map((e: any) => GroupsNestedTest_Group.fromJSON(e)) : undefined, - int3: tsProtoGlobalThis.Array.isArray(object?.int3) ? object.int3.map((e: any) => Number(e)) : undefined, + int3: globalThis.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: tsProtoGlobalThis.Array.isArray(object?.nested) + nested: globalThis.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: tsProtoGlobalThis.Array.isArray(object?.nested2) + nested2: globalThis.Array.isArray(object?.nested2) ? object.nested2.map((e: any) => GroupsNestedTest_Group_Nested_Nested2.fromJSON(e)) : undefined, }; @@ -983,25 +983,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-false/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 e97c2d20d..11ad8eeec 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,25 +138,6 @@ 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 @@ -177,14 +158,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Timestamp { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return toTimestamp(o); } else if (typeof o === "string") { - return toTimestamp(new tsProtoGlobalThis.Date(o)); + return toTimestamp(new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-string/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 206813327..abd5e3ccd 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) ? tsProtoGlobalThis.String(object.timestamp) : undefined }; + return { timestamp: isSet(object.timestamp) ? globalThis.String(object.timestamp) : undefined }; }, toJSON(message: TimestampMessage): unknown { @@ -136,25 +136,6 @@ 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 @@ -167,7 +148,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 tsProtoGlobalThis.Date(dateStr); + const date = new globalThis.Date(dateStr); const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; return { seconds, nanos }; @@ -176,7 +157,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 tsProtoGlobalThis.Date(millis).toISOString(); + return new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts +++ b/integration/grpc-js-use-date-true/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 cb18d6163..e22366a39 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,25 +136,6 @@ 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 @@ -175,14 +156,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 35e974c3a..1fbd240e3 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/grpc-js/google/protobuf/timestamp.ts +++ b/integration/grpc-js/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/grpc-js/google/protobuf/wrappers.ts +++ b/integration/grpc-js/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 60ce23081..9d4d4946b 100644 --- a/integration/grpc-js/simple.ts +++ b/integration/grpc-js/simple.ts @@ -633,25 +633,6 @@ 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 @@ -672,14 +653,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 69d9068ef..2d5fc9fd8 100644 --- a/integration/grpc-web-abort-signal/example.ts +++ b/integration/grpc-web-abort-signal/example.ts @@ -228,9 +228,7 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) - ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) - : [], + flashes: globalThis.Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], }; }, @@ -1101,25 +1099,6 @@ export class GrpcWebImpl { } } -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 @@ -1135,7 +1114,7 @@ function isSet(value: any): boolean { return value !== null && value !== undefined; } -export class GrpcWebError extends tsProtoGlobalThis.Error { +export class GrpcWebError extends globalThis.Error { constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) { super(message); } diff --git a/integration/grpc-web-go-server/example.ts b/integration/grpc-web-go-server/example.ts index 24ccc46b2..c4617b40b 100644 --- a/integration/grpc-web-go-server/example.ts +++ b/integration/grpc-web-go-server/example.ts @@ -226,9 +226,7 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) - ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) - : [], + flashes: globalThis.Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], }; }, @@ -809,25 +807,6 @@ 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 7fccc79c6..09727110f 100644 --- a/integration/grpc-web-no-streaming-observable/example.ts +++ b/integration/grpc-web-no-streaming-observable/example.ts @@ -204,9 +204,7 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) - ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) - : [], + flashes: globalThis.Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], }; }, @@ -470,25 +468,6 @@ export class GrpcWebImpl { } } -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 @@ -504,7 +483,7 @@ function isSet(value: any): boolean { return value !== null && value !== undefined; } -export class GrpcWebError extends tsProtoGlobalThis.Error { +export class GrpcWebError extends globalThis.Error { constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) { super(message); } diff --git a/integration/grpc-web-no-streaming/example.ts b/integration/grpc-web-no-streaming/example.ts index 9b4c6b265..4d69775dd 100644 --- a/integration/grpc-web-no-streaming/example.ts +++ b/integration/grpc-web-no-streaming/example.ts @@ -202,9 +202,7 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) - ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) - : [], + flashes: globalThis.Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], }; }, @@ -467,25 +465,6 @@ export class GrpcWebImpl { } } -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 @@ -501,7 +480,7 @@ function isSet(value: any): boolean { return value !== null && value !== undefined; } -export class GrpcWebError extends tsProtoGlobalThis.Error { +export class GrpcWebError extends globalThis.Error { constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) { super(message); } diff --git a/integration/grpc-web/example.ts b/integration/grpc-web/example.ts index b76869ff1..2b57770fd 100644 --- a/integration/grpc-web/example.ts +++ b/integration/grpc-web/example.ts @@ -228,9 +228,7 @@ export const DashUserSettingsState = { return { email: isSet(object.email) ? String(object.email) : "", urls: isSet(object.urls) ? DashUserSettingsState_URLs.fromJSON(object.urls) : undefined, - flashes: tsProtoGlobalThis.Array.isArray(object?.flashes) - ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) - : [], + flashes: globalThis.Array.isArray(object?.flashes) ? object.flashes.map((e: any) => DashFlash.fromJSON(e)) : [], }; }, @@ -1039,25 +1037,6 @@ export class GrpcWebImpl { } } -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 @@ -1073,7 +1052,7 @@ function isSet(value: any): boolean { return value !== null && value !== undefined; } -export class GrpcWebError extends tsProtoGlobalThis.Error { +export class GrpcWebError extends globalThis.Error { constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) { super(message); } diff --git a/integration/import-mapping/google/protobuf/timestamp.ts b/integration/import-mapping/google/protobuf/timestamp.ts index 8a8bf6005..7b5fb6231 100644 --- a/integration/import-mapping/google/protobuf/timestamp.ts +++ b/integration/import-mapping/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 5adcf16c4..69be657a7 100644 --- a/integration/import-mapping/mapping.ts +++ b/integration/import-mapping/mapping.ts @@ -322,25 +322,6 @@ 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 @@ -361,14 +342,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 e1ec4a551..cd239f79d 100644 --- a/integration/import-suffix/google/protobuf/timestamp.pb.ts +++ b/integration/import-suffix/google/protobuf/timestamp.pb.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 76be24db9..bc5f653cb 100644 --- a/integration/import-suffix/parent.pb.ts +++ b/integration/import-suffix/parent.pb.ts @@ -100,25 +100,6 @@ 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 @@ -139,14 +120,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 315ed9dbe..e9558cf70 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: tsProtoGlobalThis.Array.isArray(object?.num) ? object.num.map((e: any) => Number(e)) : [] }; + return { num: globalThis.Array.isArray(object?.num) ? object.num.map((e: any) => Number(e)) : [] }; }, toJSON(message: Numbers): unknown { @@ -275,25 +275,6 @@ 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 3ee999800..e37f85cd5 100644 --- a/integration/map-long-optional/test.ts +++ b/integration/map-long-optional/test.ts @@ -224,25 +224,6 @@ export const MapBigInt_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 @@ -256,8 +237,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 367a260e2..cf6373a8f 100644 --- a/integration/meta-typings/google/protobuf/timestamp.ts +++ b/integration/meta-typings/google/protobuf/timestamp.ts @@ -278,28 +278,9 @@ export const protoMetadata: ProtoMetadata = { dependencies: [], }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 348848f31..158ef3eeb 100644 --- a/integration/meta-typings/google/protobuf/wrappers.ts +++ b/integration/meta-typings/google/protobuf/wrappers.ts @@ -817,28 +817,9 @@ export const protoMetadata: ProtoMetadata = { dependencies: [], }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8f2cf816d..6e0f7a227 100644 --- a/integration/meta-typings/import_dir/thing.ts +++ b/integration/meta-typings/import_dir/thing.ts @@ -108,25 +108,6 @@ 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; @@ -136,5 +117,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } diff --git a/integration/meta-typings/simple.ts b/integration/meta-typings/simple.ts index c566e1fc5..8c39b5b49 100644 --- a/integration/meta-typings/simple.ts +++ b/integration/meta-typings/simple.ts @@ -2950,25 +2950,6 @@ export const protoMetadata: ProtoMetadata = { dependencies: [protoMetadata1, protoMetadata2, protoMetadata3, protoMetadata4], }; -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; @@ -2978,12 +2959,12 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 1d9cf5acf..957649d66 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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (gt.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && gt.Array.isArray(message.values)) { return message.values.map(Value.unwrap); } else { return message as any; @@ -174,7 +174,7 @@ wrappers[".google.protobuf.Struct"] = { fromObject: Struct.wrap, toObject: Struc declare const self: any | undefined; declare const window: any | undefined; declare const global: any | undefined; -const tsProtoGlobalThis: any = (() => { +const gt: any = (() => { if (typeof globalThis !== "undefined") { return globalThis; } diff --git a/integration/nestjs-simple/parameters.txt b/integration/nestjs-simple/parameters.txt index db0c6840e..ced922b52 100644 --- a/integration/nestjs-simple/parameters.txt +++ b/integration/nestjs-simple/parameters.txt @@ -1 +1 @@ -nestJs=true +nestJs=true,globalThisPolyfill=true diff --git a/integration/nice-grpc/google/protobuf/struct.ts b/integration/nice-grpc/google/protobuf/struct.ts index a2effb032..3a66cccd4 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 4161887db..500536f18 100644 --- a/integration/nice-grpc/google/protobuf/timestamp.ts +++ b/integration/nice-grpc/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -212,8 +193,8 @@ export type DeepPartial = T extends Builtin ? T : Partial; function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 b79dd9e99..3e74ece15 100644 --- a/integration/nice-grpc/google/protobuf/wrappers.ts +++ b/integration/nice-grpc/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -659,8 +640,8 @@ export type DeepPartial = T extends Builtin ? T : Partial; function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 c72dafdaa..b33cfb4bb 100644 --- a/integration/nice-grpc/simple.ts +++ b/integration/nice-grpc/simple.ts @@ -371,25 +371,6 @@ 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 @@ -406,14 +387,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/oneof-properties/oneof.ts b/integration/oneof-properties/oneof.ts index d5e92c6a6..2aa5a5315 100644 --- a/integration/oneof-properties/oneof.ts +++ b/integration/oneof-properties/oneof.ts @@ -356,30 +356,11 @@ export const PleaseChoose_Submessage = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -389,14 +370,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/oneof-unions-snake/google/protobuf/struct.ts b/integration/oneof-unions-snake/google/protobuf/struct.ts index 6856c8ed2..a71a5b6d5 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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -545,25 +545,6 @@ 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 a5b26d5e6..58eee70d7 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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -536,25 +536,6 @@ 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 825231382..459afef80 100644 --- a/integration/oneof-unions/oneof.ts +++ b/integration/oneof-unions/oneof.ts @@ -487,30 +487,11 @@ export const SimpleButOptional = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -520,14 +501,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/options/google/protobuf/descriptor.ts b/integration/options/google/protobuf/descriptor.ts index 6696b7f21..31205f591 100644 --- a/integration/options/google/protobuf/descriptor.ts +++ b/integration/options/google/protobuf/descriptor.ts @@ -5782,28 +5782,9 @@ export const protoMetadata: ProtoMetadata = { dependencies: [], }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 589c277ef..35767f587 100644 --- a/integration/output-decode-only/google/protobuf/wrappers.ts +++ b/integration/output-decode-only/google/protobuf/wrappers.ts @@ -355,28 +355,9 @@ export const BytesValue = { }, }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); } diff --git a/integration/output-fromJSON-only/google/protobuf/wrappers.ts b/integration/output-fromJSON-only/google/protobuf/wrappers.ts index 36b083d6a..0469421f6 100644 --- a/integration/output-fromJSON-only/google/protobuf/wrappers.ts +++ b/integration/output-fromJSON-only/google/protobuf/wrappers.ts @@ -146,30 +146,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); diff --git a/integration/output-toJSON-only/google/protobuf/wrappers.ts b/integration/output-toJSON-only/google/protobuf/wrappers.ts index cbc07235d..3627ad626 100644 --- a/integration/output-toJSON-only/google/protobuf/wrappers.ts +++ b/integration/output-toJSON-only/google/protobuf/wrappers.ts @@ -182,33 +182,14 @@ export const BytesValue = { }, }; -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 base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts index 5351246ea..d192683dc 100644 --- a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts +++ b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts @@ -21,7 +21,7 @@ export function fooFromJSON(object: any): Foo { case "FOO_BAZ": return Foo.BAZ; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Foo"); } } @@ -34,7 +34,7 @@ export function fooToJSON(object: Foo): string { case Foo.BAZ: return "FOO_BAZ"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Foo"); } } @@ -47,7 +47,7 @@ export function fooToNumber(object: Foo): number { case Foo.BAZ: return 2; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Foo"); } } @@ -69,7 +69,7 @@ export function barFromJSON(object: any): Bar { case "QUX": return Bar.QUX; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Bar"); } } @@ -82,7 +82,7 @@ export function barToJSON(object: Bar): string { case Bar.QUX: return "QUX"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Bar"); } } @@ -95,7 +95,7 @@ export function barToNumber(object: Bar): number { case Bar.QUX: return 2; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Bar"); } } @@ -124,7 +124,7 @@ export function withNestedEnum_BazFromJSON(object: any): WithNestedEnum_Baz { case "BAZ_TWO": return WithNestedEnum_Baz.TWO; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); } } @@ -137,7 +137,7 @@ export function withNestedEnum_BazToJSON(object: WithNestedEnum_Baz): string { case WithNestedEnum_Baz.TWO: return "BAZ_TWO"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); } } @@ -150,7 +150,7 @@ export function withNestedEnum_BazToNumber(object: WithNestedEnum_Baz): number { case WithNestedEnum_Baz.TWO: return 2; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); } } @@ -172,7 +172,7 @@ export function withNestedEnum_QuxFromJSON(object: any): WithNestedEnum_Qux { case "TWO": return WithNestedEnum_Qux.TWO; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); } } @@ -185,7 +185,7 @@ export function withNestedEnum_QuxToJSON(object: WithNestedEnum_Qux): string { case WithNestedEnum_Qux.TWO: return "TWO"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); } } @@ -198,7 +198,7 @@ export function withNestedEnum_QuxToNumber(object: WithNestedEnum_Qux): number { case WithNestedEnum_Qux.TWO: return 2; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); } } @@ -311,25 +311,6 @@ export const WithNestedEnum = { }, }; -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/remove-enum-prefix/remove-enum-prefix.ts b/integration/remove-enum-prefix/remove-enum-prefix.ts index 48b7b73c8..fa297be92 100644 --- a/integration/remove-enum-prefix/remove-enum-prefix.ts +++ b/integration/remove-enum-prefix/remove-enum-prefix.ts @@ -21,7 +21,7 @@ export function fooFromJSON(object: any): Foo { case "FOO_BAZ": return Foo.BAZ; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Foo"); } } @@ -34,7 +34,7 @@ export function fooToJSON(object: Foo): string { case Foo.BAZ: return "FOO_BAZ"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Foo"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Foo"); } } @@ -56,7 +56,7 @@ export function barFromJSON(object: any): Bar { case "QUX": return Bar.QUX; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Bar"); } } @@ -69,7 +69,7 @@ export function barToJSON(object: Bar): string { case Bar.QUX: return "QUX"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Bar"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Bar"); } } @@ -98,7 +98,7 @@ export function withNestedEnum_BazFromJSON(object: any): WithNestedEnum_Baz { case "BAZ_TWO": return WithNestedEnum_Baz.TWO; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); } } @@ -111,7 +111,7 @@ export function withNestedEnum_BazToJSON(object: WithNestedEnum_Baz): string { case WithNestedEnum_Baz.TWO: return "BAZ_TWO"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz"); } } @@ -133,7 +133,7 @@ export function withNestedEnum_QuxFromJSON(object: any): WithNestedEnum_Qux { case "TWO": return WithNestedEnum_Qux.TWO; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); } } @@ -146,7 +146,7 @@ export function withNestedEnum_QuxToJSON(object: WithNestedEnum_Qux): string { case WithNestedEnum_Qux.TWO: return "TWO"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux"); } } @@ -254,25 +254,6 @@ export const WithNestedEnum = { }, }; -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-esmodule-interop/simple.ts b/integration/simple-esmodule-interop/simple.ts index 3f784f98a..612f0b920 100644 --- a/integration/simple-esmodule-interop/simple.ts +++ b/integration/simple-esmodule-interop/simple.ts @@ -332,25 +332,6 @@ 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 @@ -363,8 +344,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/simple-json-name/google/protobuf/timestamp.ts +++ b/integration/simple-json-name/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 7a0b9b322..2f7f14f0c 100644 --- a/integration/simple-json-name/simple.ts +++ b/integration/simple-json-name/simple.ts @@ -138,7 +138,7 @@ 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: tsProtoGlobalThis.Array.isArray(object?.["hyphen-list"]) + hyphenList: globalThis.Array.isArray(object?.["hyphen-list"]) ? object["hyphen-list"].map((e: any) => String(e)) : [], }; @@ -190,25 +190,6 @@ 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 @@ -229,14 +210,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 38b71dc87..ddfc5c942 100644 --- a/integration/simple-long-bigint/google/protobuf/wrappers.ts +++ b/integration/simple-long-bigint/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/simple-long-bigint/simple.ts b/integration/simple-long-bigint/simple.ts index 6e9bc1e70..5a3b26970 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: tsProtoGlobalThis.Array.isArray(object?.uint64s) ? object.uint64s.map((e: any) => BigInt(e)) : [], + uint64s: globalThis.Array.isArray(object?.uint64s) ? object.uint64s.map((e: any) => BigInt(e)) : [], }; }, @@ -321,25 +321,6 @@ 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 @@ -358,16 +339,16 @@ function toTimestamp(date: Date): Timestamp { } function fromTimestamp(t: Timestamp): Date { - let millis = (tsProtoGlobalThis.Number(t.seconds.toString()) || 0) * 1_000; + let millis = (globalThis.Number(t.seconds.toString()) || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 375f8dbe5..64cf9eda8 100644 --- a/integration/simple-long-string/google/protobuf/wrappers.ts +++ b/integration/simple-long-string/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/simple-long-string/simple.ts b/integration/simple-long-string/simple.ts index 47e7d3d6a..a6917b35e 100644 --- a/integration/simple-long-string/simple.ts +++ b/integration/simple-long-string/simple.ts @@ -292,25 +292,6 @@ 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 @@ -329,16 +310,16 @@ function toTimestamp(date: Date): Timestamp { } function fromTimestamp(t: Timestamp): Date { - let millis = (tsProtoGlobalThis.Number(t.seconds) || 0) * 1_000; + let millis = (globalThis.Number(t.seconds) || 0) * 1_000; millis += (t.nanos || 0) / 1_000_000; - return new tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 4fc7b3028..1ed04d466 100644 --- a/integration/simple-long/google/protobuf/wrappers.ts +++ b/integration/simple-long/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } diff --git a/integration/simple-long/simple.ts b/integration/simple-long/simple.ts index 5163cf4c6..263c5bc5f 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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.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[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.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[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -755,7 +755,7 @@ 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: tsProtoGlobalThis.Array.isArray(object?.manyUint64) + manyUint64: globalThis.Array.isArray(object?.manyUint64) ? object.manyUint64.map((e: any) => Long.fromValue(e)) : [], }; @@ -835,25 +835,6 @@ 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 @@ -867,8 +848,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/simple-optionals/google/protobuf/timestamp.ts +++ b/integration/simple-optionals/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/simple-optionals/google/protobuf/wrappers.ts +++ b/integration/simple-optionals/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 44595e74d..6e534663c 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index b22c6c89b..a41a17140 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -405,12 +405,12 @@ 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: tsProtoGlobalThis.Array.isArray(object?.grandChildren) + grandChildren: globalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(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) + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: globalThis.Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, @@ -927,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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1091,7 +1091,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1103,7 +1103,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1150,7 +1150,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1168,7 +1168,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -1442,7 +1442,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1471,7 +1471,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1936,25 +1936,6 @@ 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 @@ -1975,22 +1956,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 90a802d78..38c56ba8b 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 b6d253b05..562616fca 100644 --- a/integration/simple-prototype-defaults/google/protobuf/timestamp.ts +++ b/integration/simple-prototype-defaults/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 84ca98602..2a043867a 100644 --- a/integration/simple-prototype-defaults/google/protobuf/wrappers.ts +++ b/integration/simple-prototype-defaults/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 3860c4c01..7c6a9e205 100644 --- a/integration/simple-prototype-defaults/import_dir/thing.ts +++ b/integration/simple-prototype-defaults/import_dir/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 e7c41b45b..564321f67 100644 --- a/integration/simple-prototype-defaults/simple.ts +++ b/integration/simple-prototype-defaults/simple.ts @@ -500,16 +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: tsProtoGlobalThis.Array.isArray(object?.grandChildren) + grandChildren: globalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(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) + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: globalThis.Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, - blobs: tsProtoGlobalThis.Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], + blobs: globalThis.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), }; @@ -1049,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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], id: isSet(object.id) ? new Uint8Array(object.id) : undefined, }; }, @@ -1280,7 +1280,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1292,7 +1292,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1319,7 +1319,7 @@ export const SimpleWithMap = { : {}, longLookup: isObject(object.longLookup) ? Object.entries(object.longLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1402,7 +1402,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1420,7 +1420,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -1455,7 +1455,7 @@ export const SimpleWithMap = { message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -2038,7 +2038,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -2067,7 +2067,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -2203,7 +2203,7 @@ export const SimpleWithMapOfEnums = { return { enumsById: isObject(object.enumsById) ? Object.entries(object.enumsById).reduce<{ [key: number]: StateEnum }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = stateEnumFromJSON(value); + acc[globalThis.Number(key)] = stateEnumFromJSON(value); return acc; }, {}) : {}, @@ -2232,7 +2232,7 @@ export const SimpleWithMapOfEnums = { message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = value as StateEnum; + acc[globalThis.Number(key)] = value as StateEnum; } return acc; }, @@ -2903,30 +2903,11 @@ 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"; -})(); - function bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -2936,14 +2917,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -2967,22 +2948,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0750b9923..b619c1a28 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: tsProtoGlobalThis.Array.isArray(object.list_value) ? [...object.list_value] : undefined, + list_value: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/simple-snake/google/protobuf/timestamp.ts +++ b/integration/simple-snake/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/simple-snake/google/protobuf/wrappers.ts +++ b/integration/simple-snake/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 e7fc308f7..c782ec4d8 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index c6a28afae..5fccb0ec1 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -410,12 +410,12 @@ 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: tsProtoGlobalThis.Array.isArray(object?.grand_children) + grand_children: globalThis.Array.isArray(object?.grand_children) ? object.grand_children.map((e: any) => Child.fromJSON(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) + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + old_states: globalThis.Array.isArray(object?.old_states) ? object.old_states.map((e: any) => stateEnumFromJSON(e)) : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, @@ -932,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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1096,7 +1096,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1108,7 +1108,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1155,7 +1155,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1173,7 +1173,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -1447,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[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1476,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[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1998,25 +1998,6 @@ 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 @@ -2037,22 +2018,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 f91d21bfa..d86becab5 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -533,25 +533,6 @@ 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 2ee9ae079..bacb46e0f 100644 --- a/integration/simple-string-enums/simple.ts +++ b/integration/simple-string-enums/simple.ts @@ -164,9 +164,7 @@ export const Simple = { return { name: isSet(object.name) ? String(object.name) : "", state: isSet(object.state) ? stateEnumFromJSON(object.state) : StateEnum.UNKNOWN, - states: tsProtoGlobalThis.Array.isArray(object?.states) - ? object.states.map((e: any) => stateEnumFromJSON(e)) - : [], + states: globalThis.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]) => { @@ -299,25 +297,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 e10fddf1f..c14794156 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 89c3b5351..a76524d34 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -30,7 +30,7 @@ export function stateEnumFromJSON(object: any): StateEnum { case "OFF": return StateEnum.OFF; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum StateEnum"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum StateEnum"); } } @@ -43,7 +43,7 @@ export function stateEnumToJSON(object: StateEnum): string { case StateEnum.OFF: return "OFF"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum StateEnum"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum StateEnum"); } } @@ -88,7 +88,7 @@ export function child_TypeFromJSON(object: any): Child_Type { case "BAD": return Child_Type.BAD; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Child_Type"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Child_Type"); } } @@ -101,7 +101,7 @@ export function child_TypeToJSON(object: Child_Type): string { case Child_Type.BAD: return "BAD"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Child_Type"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Child_Type"); } } @@ -129,7 +129,7 @@ export function nested_InnerEnumFromJSON(object: any): Nested_InnerEnum { case "BAD": return Nested_InnerEnum.BAD; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Nested_InnerEnum"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Nested_InnerEnum"); } } @@ -142,7 +142,7 @@ export function nested_InnerEnumToJSON(object: Nested_InnerEnum): string { case Nested_InnerEnum.BAD: return "BAD"; default: - throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum Nested_InnerEnum"); + throw new globalThis.Error("Unrecognized enum value " + object + " for enum Nested_InnerEnum"); } } @@ -393,12 +393,12 @@ 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: tsProtoGlobalThis.Array.isArray(object?.grandChildren) + grandChildren: globalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(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) + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: globalThis.Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, @@ -915,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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], }; }, @@ -1079,7 +1079,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1091,7 +1091,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1138,7 +1138,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1156,7 +1156,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -1430,7 +1430,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1459,7 +1459,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1924,25 +1924,6 @@ 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 @@ -1963,22 +1944,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/simple/google/protobuf/timestamp.ts +++ b/integration/simple/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/simple/google/protobuf/wrappers.ts +++ b/integration/simple/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 e10fddf1f..c14794156 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -65,25 +65,6 @@ 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 @@ -104,14 +85,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 4596e06a2..f7dd4bb05 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -512,16 +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: tsProtoGlobalThis.Array.isArray(object?.grandChildren) + grandChildren: globalThis.Array.isArray(object?.grandChildren) ? object.grandChildren.map((e: any) => Child.fromJSON(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) + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], + oldStates: globalThis.Array.isArray(object?.oldStates) ? object.oldStates.map((e: any) => stateEnumFromJSON(e)) : [], thing: isSet(object.thing) ? ImportedThing.fromJSON(object.thing) : undefined, - blobs: tsProtoGlobalThis.Array.isArray(object?.blobs) ? object.blobs.map((e: any) => bytesFromBase64(e)) : [], + blobs: globalThis.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, @@ -1066,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: 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)) : [], + coins: globalThis.Array.isArray(object?.coins) ? object.coins.map((e: any) => Number(e)) : [], + snacks: globalThis.Array.isArray(object?.snacks) ? object.snacks.map((e: any) => String(e)) : [], id: isSet(object.id) ? new Uint8Array(object.id) : undefined, }; }, @@ -1297,7 +1297,7 @@ export const SimpleWithMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -1309,7 +1309,7 @@ export const SimpleWithMap = { : {}, intLookup: isObject(object.intLookup) ? Object.entries(object.intLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1336,7 +1336,7 @@ export const SimpleWithMap = { : {}, longLookup: isObject(object.longLookup) ? Object.entries(object.longLookup).reduce<{ [key: number]: number }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); return acc; }, {}) : {}, @@ -1419,7 +1419,7 @@ export const SimpleWithMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -1437,7 +1437,7 @@ export const SimpleWithMap = { message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -1472,7 +1472,7 @@ export const SimpleWithMap = { message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Number(value); + acc[globalThis.Number(key)] = Number(value); } return acc; }, @@ -2051,7 +2051,7 @@ export const SimpleWithSnakeCaseMap = { return { entitiesById: isObject(object.entitiesById) ? Object.entries(object.entitiesById).reduce<{ [key: number]: Entity }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromJSON(value); + acc[globalThis.Number(key)] = Entity.fromJSON(value); return acc; }, {}) : {}, @@ -2080,7 +2080,7 @@ export const SimpleWithSnakeCaseMap = { message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = Entity.fromPartial(value); + acc[globalThis.Number(key)] = Entity.fromPartial(value); } return acc; }, @@ -2212,7 +2212,7 @@ export const SimpleWithMapOfEnums = { return { enumsById: isObject(object.enumsById) ? Object.entries(object.enumsById).reduce<{ [key: number]: StateEnum }>((acc, [key, value]) => { - acc[tsProtoGlobalThis.Number(key)] = stateEnumFromJSON(value); + acc[globalThis.Number(key)] = stateEnumFromJSON(value); return acc; }, {}) : {}, @@ -2241,7 +2241,7 @@ export const SimpleWithMapOfEnums = { message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( (acc, [key, value]) => { if (value !== undefined) { - acc[tsProtoGlobalThis.Number(key)] = value as StateEnum; + acc[globalThis.Number(key)] = value as StateEnum; } return acc; }, @@ -2908,30 +2908,11 @@ 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"; -})(); - function bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -2941,14 +2922,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -2972,22 +2953,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 222c3f0f5..4ef8ab393 100644 --- a/integration/static-only-type-registry/foo.ts +++ b/integration/static-only-type-registry/foo.ts @@ -201,25 +201,6 @@ 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 @@ -240,14 +221,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 475b4d083..291c5d8c7 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -540,25 +540,6 @@ 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 5df884670..6d43d66cb 100644 --- a/integration/static-only-type-registry/google/protobuf/timestamp.ts +++ b/integration/static-only-type-registry/google/protobuf/timestamp.ts @@ -190,25 +190,6 @@ export const Timestamp = { messageTypeRegistry.set(Timestamp.$type, Timestamp); -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,8 +202,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 7de11d28c..00bda351d 100644 --- a/integration/static-only/foo.ts +++ b/integration/static-only/foo.ts @@ -194,25 +194,6 @@ 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 @@ -233,14 +214,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 403605339..5e430399a 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -531,25 +531,6 @@ 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 d5c111f8c..e2c971479 100644 --- a/integration/static-only/google/protobuf/timestamp.ts +++ b/integration/static-only/google/protobuf/timestamp.ts @@ -187,25 +187,6 @@ export const Timestamp = { }, }; -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 @@ -218,8 +199,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 35e974c3a..1fbd240e3 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 55b5fafdd..cd610b9d7 100644 --- a/integration/type-annotations/foo.ts +++ b/integration/type-annotations/foo.ts @@ -197,25 +197,6 @@ 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 @@ -236,14 +217,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 c923111ef..3a20c5d74 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -510,10 +510,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { - $type: ListValue.$type, - values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [], - }; + return { $type: ListValue.$type, values: globalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -540,7 +537,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -548,25 +545,6 @@ 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 686732f27..73cdf09d5 100644 --- a/integration/type-annotations/google/protobuf/timestamp.ts +++ b/integration/type-annotations/google/protobuf/timestamp.ts @@ -189,25 +189,6 @@ export const Timestamp = { }, }; -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 @@ -220,8 +201,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 f203c9d8a..b13abaa86 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -204,25 +204,6 @@ 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 @@ -243,14 +224,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 77deb1b18..9f168d4e0 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.Array.isArray(value)) { result.listValue = value; } else if (typeof value === "object") { result.structValue = value; @@ -517,10 +517,7 @@ export const ListValue = { }, fromJSON(object: any): ListValue { - return { - $type: ListValue.$type, - values: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [], - }; + return { $type: ListValue.$type, values: globalThis.Array.isArray(object?.values) ? [...object.values] : [] }; }, toJSON(message: ListValue): unknown { @@ -547,7 +544,7 @@ export const ListValue = { }, unwrap(message: ListValue): Array { - if (message?.hasOwnProperty("values") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -557,25 +554,6 @@ 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 225e4068c..5176dbb6b 100644 --- a/integration/type-registry/google/protobuf/timestamp.ts +++ b/integration/type-registry/google/protobuf/timestamp.ts @@ -192,25 +192,6 @@ export const Timestamp = { messageTypeRegistry.set(Timestamp.$type, Timestamp); -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 @@ -223,8 +204,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 85445aa5a..a98e909a5 100644 --- a/integration/unknown-fields/google/protobuf/compiler/plugin.ts +++ b/integration/unknown-fields/google/protobuf/compiler/plugin.ts @@ -492,28 +492,9 @@ export const CodeGeneratorResponse_File = { }, }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 5639c65e0..4eba9cbd4 100644 --- a/integration/unknown-fields/google/protobuf/descriptor.ts +++ b/integration/unknown-fields/google/protobuf/descriptor.ts @@ -3622,28 +3622,9 @@ export const GeneratedCodeInfo_Annotation = { }, }; -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 longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/use-date-false/google/protobuf/timestamp.ts +++ b/integration/use-date-false/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 86daf9ef9..ef34a58df 100644 --- a/integration/use-date-false/metadata.ts +++ b/integration/use-date-false/metadata.ts @@ -67,25 +67,6 @@ 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 @@ -106,14 +87,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Timestamp { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return toTimestamp(o); } else if (typeof o === "string") { - return toTimestamp(new tsProtoGlobalThis.Date(o)); + return toTimestamp(new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/use-date-string/google/protobuf/timestamp.ts +++ b/integration/use-date-string/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 6aa47527b..63bea1fb0 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -98,16 +98,14 @@ export const Todo = { fromJSON(object: any): Todo { return { id: isSet(object.id) ? String(object.id) : "", - timestamp: isSet(object.timestamp) ? tsProtoGlobalThis.String(object.timestamp) : undefined, - repeatedTimestamp: tsProtoGlobalThis.Array.isArray(object?.repeatedTimestamp) - ? object.repeatedTimestamp.map((e: any) => tsProtoGlobalThis.String(e)) + timestamp: isSet(object.timestamp) ? globalThis.String(object.timestamp) : undefined, + repeatedTimestamp: globalThis.Array.isArray(object?.repeatedTimestamp) + ? object.repeatedTimestamp.map((e: any) => globalThis.String(e)) : [], - optionalTimestamp: isSet(object.optionalTimestamp) - ? tsProtoGlobalThis.String(object.optionalTimestamp) - : undefined, + optionalTimestamp: isSet(object.optionalTimestamp) ? globalThis.String(object.optionalTimestamp) : undefined, mapOfTimestamps: isObject(object.mapOfTimestamps) ? Object.entries(object.mapOfTimestamps).reduce<{ [key: string]: string }>((acc, [key, value]) => { - acc[key] = tsProtoGlobalThis.String(value); + acc[key] = globalThis.String(value); return acc; }, {}) : {}, @@ -210,7 +208,7 @@ export const Todo_MapOfTimestampsEntry = { fromJSON(object: any): Todo_MapOfTimestampsEntry { return { key: isSet(object.key) ? String(object.key) : "", - value: isSet(object.value) ? tsProtoGlobalThis.String(object.value) : undefined, + value: isSet(object.value) ? globalThis.String(object.value) : undefined, }; }, @@ -236,25 +234,6 @@ 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 @@ -267,7 +246,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 tsProtoGlobalThis.Date(dateStr); + const date = new globalThis.Date(dateStr); const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; return { seconds, nanos }; @@ -276,7 +255,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 tsProtoGlobalThis.Date(millis).toISOString(); + return new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/use-date-true/google/protobuf/timestamp.ts +++ b/integration/use-date-true/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 6bc466750..e1de58c47 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: tsProtoGlobalThis.Array.isArray(object?.repeatedTimestamp) + repeatedTimestamp: globalThis.Array.isArray(object?.repeatedTimestamp) ? object.repeatedTimestamp.map((e: any) => fromJsonTimestamp(e)) : [], optionalTimestamp: isSet(object.optionalTimestamp) ? fromJsonTimestamp(object.optionalTimestamp) : undefined, @@ -275,25 +275,6 @@ 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 @@ -314,14 +295,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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.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 290e65668..19f8928c2 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -518,25 +518,6 @@ 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 8a8bf6005..7b5fb6231 100644 --- a/integration/use-map-type/google/protobuf/timestamp.ts +++ b/integration/use-map-type/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 cb7cb87ab..20e3d8b52 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(tsProtoGlobalThis.Number(key), Number(value)); + acc.set(globalThis.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(tsProtoGlobalThis.Number(key), Number(value)); + acc.set(globalThis.Number(key), Number(value)); return acc; }, new Map()) : new Map(), @@ -703,30 +703,11 @@ export const Maps_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"; -})(); - function bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -736,14 +717,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -767,22 +748,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 9f9a70277..80e632542 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 bbc7fbd44..b4780779e 100644 --- a/integration/use-numeric-enum-json/simple.ts +++ b/integration/use-numeric-enum-json/simple.ts @@ -150,9 +150,7 @@ export const Simple = { return { name: isSet(object.name) ? String(object.name) : "", state: isSet(object.state) ? stateEnumFromJSON(object.state) : 0, - states: tsProtoGlobalThis.Array.isArray(object?.states) - ? object.states.map((e: any) => stateEnumFromJSON(e)) - : [], + states: globalThis.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]) => { @@ -285,25 +283,6 @@ 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 d2011f0b4..2a7e9d170 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,7 @@ export const Todo = { return { id: isSet(object.id) ? String(object.id) : "", oid: isSet(object.oid) ? fromJsonObjectId(object.oid) : undefined, - repeatedOid: tsProtoGlobalThis.Array.isArray(object?.repeatedOid) + repeatedOid: globalThis.Array.isArray(object?.repeatedOid) ? object.repeatedOid.map((e: any) => fromJsonObjectId(e)) : [], optionalOid: isSet(object.optionalOid) ? fromJsonObjectId(object.optionalOid) : undefined, @@ -239,25 +239,6 @@ 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 40731e1b4..a38f36433 100644 --- a/integration/use-optionals-all/google/protobuf/timestamp.ts +++ b/integration/use-optionals-all/google/protobuf/timestamp.ts @@ -187,25 +187,6 @@ export const Timestamp = { }, }; -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 @@ -218,8 +199,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 daedb0c3a..d9cef1d43 100644 --- a/integration/use-optionals-all/test.ts +++ b/integration/use-optionals-all/test.ts @@ -431,21 +431,15 @@ 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: 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) + repId: globalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [], + repChild: globalThis.Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : [], + repState: globalThis.Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : [], + repLong: globalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [], + repTruth: globalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [], + repDescription: globalThis.Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : [], - repData: tsProtoGlobalThis.Array.isArray(object?.repData) - ? object.repData.map((e: any) => bytesFromBase64(e)) - : [], + repData: globalThis.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, @@ -701,30 +695,11 @@ export const Child = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -734,14 +709,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -765,22 +740,22 @@ 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 tsProtoGlobalThis.Date(millis); + return new globalThis.Date(millis); } function fromJsonTimestamp(o: any): Date { - if (o instanceof tsProtoGlobalThis.Date) { + if (o instanceof globalThis.Date) { return o; } else if (typeof o === "string") { - return new tsProtoGlobalThis.Date(o); + return new globalThis.Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function longToNumber(long: Long): number { - if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 b5c799b9d..3a41552d1 100644 --- a/integration/use-optionals-no-undefined/test.ts +++ b/integration/use-optionals-no-undefined/test.ts @@ -431,21 +431,19 @@ 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: tsProtoGlobalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : undefined, - repChild: tsProtoGlobalThis.Array.isArray(object?.repChild) + repId: globalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : undefined, + repChild: globalThis.Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : undefined, - repState: tsProtoGlobalThis.Array.isArray(object?.repState) + repState: globalThis.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) + repLong: globalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : undefined, + repTruth: globalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : undefined, + repDescription: globalThis.Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : undefined, - repData: tsProtoGlobalThis.Array.isArray(object?.repData) + repData: globalThis.Array.isArray(object?.repData) ? object.repData.map((e: any) => bytesFromBase64(e)) : undefined, optId: isSet(object.optId) ? Number(object.optId) : undefined, @@ -697,30 +695,11 @@ export const Child = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -730,14 +709,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -753,8 +732,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 89d0d954d..878325a12 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(tsProtoGlobalThis.Boolean) - : tsProtoGlobalThis.Array.isArray(object?.paths) - ? object.paths.map(tsProtoGlobalThis.String) + ? object.split(",").filter(globalThis.Boolean) + : globalThis.Array.isArray(object?.paths) + ? object.paths.map(globalThis.String) : [], }; }, @@ -277,25 +277,6 @@ 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 71aea3fd3..50017f93e 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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -536,25 +536,6 @@ 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 fbf2eadeb..5a9b60a45 100644 --- a/integration/use-readonly-types/use-readonly-types.ts +++ b/integration/use-readonly-types/use-readonly-types.ts @@ -201,17 +201,15 @@ export const Entity = { return { intVal: isSet(object.intVal) ? Number(object.intVal) : 0, stringVal: isSet(object.stringVal) ? String(object.stringVal) : "", - 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)) - : [], + intArray: globalThis.Array.isArray(object?.intArray) ? object.intArray.map((e: any) => Number(e)) : [], + stringArray: globalThis.Array.isArray(object?.stringArray) ? object.stringArray.map((e: any) => String(e)) : [], subEntity: isSet(object.subEntity) ? SubEntity.fromJSON(object.subEntity) : undefined, - subEntityArray: tsProtoGlobalThis.Array.isArray(object?.subEntityArray) + subEntityArray: globalThis.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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, structValue: isObject(object.structValue) ? object.structValue : undefined, oneOfValue: isSet(object.theStringValue) ? { $case: "theStringValue", theStringValue: String(object.theStringValue) } @@ -354,25 +352,6 @@ 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 35e974c3a..1fbd240e3 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: tsProtoGlobalThis.Array.isArray(object.listValue) ? [...object.listValue] : undefined, + listValue: globalThis.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 (tsProtoGlobalThis.Array.isArray(value)) { + } else if (globalThis.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: tsProtoGlobalThis.Array.isArray(object?.values) ? [...object.values] : [] }; + return { values: globalThis.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") && tsProtoGlobalThis.Array.isArray(message.values)) { + if (message?.hasOwnProperty("values") && globalThis.Array.isArray(message.values)) { return message.values; } else { return message as any; @@ -523,25 +523,6 @@ 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 0a7c60274..98a564806 100644 --- a/integration/value/google/protobuf/wrappers.ts +++ b/integration/value/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 c86dc6522..511631d83 100644 --- a/integration/value/value.ts +++ b/integration/value/value.ts @@ -91,9 +91,9 @@ export const ValueMessage = { fromJSON(object: any): ValueMessage { return { value: isSet(object?.value) ? object.value : undefined, - anyList: tsProtoGlobalThis.Array.isArray(object.anyList) ? [...object.anyList] : undefined, - repeatedAny: tsProtoGlobalThis.Array.isArray(object?.repeatedAny) ? [...object.repeatedAny] : [], - repeatedStrings: tsProtoGlobalThis.Array.isArray(object?.repeatedStrings) + anyList: globalThis.Array.isArray(object.anyList) ? [...object.anyList] : undefined, + repeatedAny: globalThis.Array.isArray(object?.repeatedAny) ? [...object.repeatedAny] : [], + repeatedStrings: globalThis.Array.isArray(object?.repeatedStrings) ? object.repeatedStrings.map((e: any) => String(e)) : [], structValue: isObject(object.structValue) ? object.structValue : undefined, @@ -134,25 +134,6 @@ 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 b55250ece..5bf5d6b11 100644 --- a/integration/vector-tile/vector_tile.ts +++ b/integration/vector-tile/vector_tile.ts @@ -116,9 +116,7 @@ export const Tile = { fromJSON(object: any): Tile { return { - layers: tsProtoGlobalThis.Array.isArray(object?.layers) - ? object.layers.map((e: any) => Tile_Layer.fromJSON(e)) - : [], + layers: globalThis.Array.isArray(object?.layers) ? object.layers.map((e: any) => Tile_Layer.fromJSON(e)) : [], }; }, @@ -381,9 +379,9 @@ export const Tile_Feature = { fromJSON(object: any): Tile_Feature { return { id: isSet(object.id) ? Number(object.id) : 0, - tags: tsProtoGlobalThis.Array.isArray(object?.tags) ? object.tags.map((e: any) => Number(e)) : [], + tags: globalThis.Array.isArray(object?.tags) ? object.tags.map((e: any) => Number(e)) : [], type: isSet(object.type) ? tile_GeomTypeFromJSON(object.type) : 0, - geometry: tsProtoGlobalThis.Array.isArray(object?.geometry) ? object.geometry.map((e: any) => Number(e)) : [], + geometry: globalThis.Array.isArray(object?.geometry) ? object.geometry.map((e: any) => Number(e)) : [], }; }, @@ -506,13 +504,11 @@ export const Tile_Layer = { return { version: isSet(object.version) ? Number(object.version) : 0, name: isSet(object.name) ? String(object.name) : "", - features: tsProtoGlobalThis.Array.isArray(object?.features) + features: globalThis.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)) - : [], + keys: globalThis.Array.isArray(object?.keys) ? object.keys.map((e: any) => String(e)) : [], + values: globalThis.Array.isArray(object?.values) ? object.values.map((e: any) => Tile_Value.fromJSON(e)) : [], extent: isSet(object.extent) ? Number(object.extent) : 0, }; }, @@ -555,25 +551,6 @@ export const Tile_Layer = { }, }; -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 @@ -586,8 +563,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 8a8bf6005..7b5fb6231 100644 --- a/integration/wrappers-regression/google/protobuf/timestamp.ts +++ b/integration/wrappers-regression/google/protobuf/timestamp.ts @@ -185,25 +185,6 @@ export const Timestamp = { }, }; -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 @@ -216,8 +197,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.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 0a7c60274..98a564806 100644 --- a/integration/wrappers-regression/google/protobuf/wrappers.ts +++ b/integration/wrappers-regression/google/protobuf/wrappers.ts @@ -607,30 +607,11 @@ export const BytesValue = { }, }; -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 bytesFromBase64(b64: string): Uint8Array { - if (tsProtoGlobalThis.Buffer) { - return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); + if (globalThis.Buffer) { + return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); } else { - const bin = tsProtoGlobalThis.atob(b64); + const bin = globalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); @@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array { } function base64FromBytes(arr: Uint8Array): string { - if (tsProtoGlobalThis.Buffer) { - return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); + if (globalThis.Buffer) { + return globalThis.Buffer.from(arr).toString("base64"); } else { const bin: string[] = []; arr.forEach((byte) => { - bin.push(tsProtoGlobalThis.String.fromCharCode(byte)); + bin.push(globalThis.String.fromCharCode(byte)); }); - return tsProtoGlobalThis.btoa(bin.join("")); + return globalThis.btoa(bin.join("")); } } @@ -663,8 +644,8 @@ 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(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) { - throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); + if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { + throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); } return long.toNumber(); } diff --git a/src/main.ts b/src/main.ts index df7afc22e..375c7e68b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -412,7 +412,7 @@ export type Utils = ReturnType & /** These are runtime utility methods used by the generated code. */ export function makeUtils(options: Options): Utils { - const bytes = makeByteUtils(); + const bytes = makeByteUtils(options); const longs = makeLongUtils(options, bytes); return { ...bytes, @@ -517,14 +517,14 @@ function makeLongUtils(options: Options, bytes: ReturnType return { numberToLong, longToNumber, longToString, longToBigint, Long }; } -function makeByteUtils() { - const globalThis = conditionalOutput( - "tsProtoGlobalThis", +function makeByteUtils(options: Options) { + const globalThisPolyfill = conditionalOutput( + "gt", code` declare const self: any | undefined; declare const window: any | undefined; declare const global: any | undefined; - const tsProtoGlobalThis: any = (() => { + const gt: any = (() => { if (typeof globalThis !== "undefined") return globalThis; if (typeof self !== "undefined") return self; if (typeof window !== "undefined") return window; @@ -533,6 +533,7 @@ function makeByteUtils() { })(); `, ); + const globalThis = options.globalThisPolyfill ? globalThisPolyfill : conditionalOutput("globalThis", code``); const bytesFromBase64 = conditionalOutput( "bytesFromBase64", diff --git a/src/options.ts b/src/options.ts index 04c959f65..8fdda8e1c 100644 --- a/src/options.ts +++ b/src/options.ts @@ -38,6 +38,7 @@ export type Options = { context: boolean; snakeToCamel: Array<"json" | "keys">; forceLong: LongOption; + globalThisPolyfill: boolean; useOptionals: boolean | "none" | "messages" | "all"; // boolean is deprecated emitDefaultValues: Array<"json-methods">; useDate: DateOption; @@ -90,6 +91,7 @@ export function defaultOptions(): Options { context: false, snakeToCamel: ["json", "keys"], emitDefaultValues: [], + globalThisPolyfill: false, forceLong: LongOption.NUMBER, useOptionals: "none", useDate: DateOption.DATE, diff --git a/tests/options-test.ts b/tests/options-test.ts index 4d2ce183d..9a45bbef9 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -17,6 +17,7 @@ describe("options", () => { "exportCommonSymbols": true, "fileSuffix": "", "forceLong": "number", + "globalThisPolyfill": false, "importSuffix": "", "initializeFieldsAsUndefined": false, "lowerCaseServiceMethods": true,