Skip to content

Commit 085fa21

Browse files
authored
feat: Add globalThisPolyfill, defaults false. (#931)
* feat: Add globalThisPolyfill, defaults false. We assume most users have globalThis-capable runtimes, but provide our previous fallback/polify for those that don't. * Fix test.
1 parent 6f85637 commit 085fa21

File tree

134 files changed

+644
-3077
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+644
-3077
lines changed

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16.10.0
1+
v20.8.0

README.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ Generated code will be placed in the Gradle build directory.
289289

290290
### Supported options
291291

292+
- With `--ts_proto_opt=globalThisPolyfill=true`, ts-proto will include a polyfill for globalThis.
293+
294+
Defaults to `false`, i.e. we assume `globalThis` is available.
295+
292296
- 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.
293297

294298
- 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).

integration/async-iterable-services-abort-signal/simple.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const EchoMsg = {
4949
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
5050
): AsyncIterable<Uint8Array> {
5151
for await (const pkt of source) {
52-
if (tsProtoGlobalThis.Array.isArray(pkt)) {
52+
if (globalThis.Array.isArray(pkt)) {
5353
for (const p of (pkt as any)) {
5454
yield* [EchoMsg.encode(p).finish()];
5555
}
@@ -65,7 +65,7 @@ export const EchoMsg = {
6565
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
6666
): AsyncIterable<EchoMsg> {
6767
for await (const pkt of source) {
68-
if (tsProtoGlobalThis.Array.isArray(pkt)) {
68+
if (globalThis.Array.isArray(pkt)) {
6969
for (const p of (pkt as any)) {
7070
yield* [EchoMsg.decode(p)];
7171
}
@@ -173,25 +173,6 @@ interface Rpc {
173173
): AsyncIterable<Uint8Array>;
174174
}
175175

176-
declare const self: any | undefined;
177-
declare const window: any | undefined;
178-
declare const global: any | undefined;
179-
const tsProtoGlobalThis: any = (() => {
180-
if (typeof globalThis !== "undefined") {
181-
return globalThis;
182-
}
183-
if (typeof self !== "undefined") {
184-
return self;
185-
}
186-
if (typeof window !== "undefined") {
187-
return window;
188-
}
189-
if (typeof global !== "undefined") {
190-
return global;
191-
}
192-
throw "Unable to locate global object";
193-
})();
194-
195176
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
196177

197178
export type DeepPartial<T> = T extends Builtin ? T

integration/async-iterable-services/simple.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const EchoMsg = {
4949
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
5050
): AsyncIterable<Uint8Array> {
5151
for await (const pkt of source) {
52-
if (tsProtoGlobalThis.Array.isArray(pkt)) {
52+
if (globalThis.Array.isArray(pkt)) {
5353
for (const p of (pkt as any)) {
5454
yield* [EchoMsg.encode(p).finish()];
5555
}
@@ -65,7 +65,7 @@ export const EchoMsg = {
6565
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
6666
): AsyncIterable<EchoMsg> {
6767
for await (const pkt of source) {
68-
if (tsProtoGlobalThis.Array.isArray(pkt)) {
68+
if (globalThis.Array.isArray(pkt)) {
6969
for (const p of (pkt as any)) {
7070
yield* [EchoMsg.decode(p)];
7171
}
@@ -157,25 +157,6 @@ interface Rpc {
157157
): AsyncIterable<Uint8Array>;
158158
}
159159

160-
declare const self: any | undefined;
161-
declare const window: any | undefined;
162-
declare const global: any | undefined;
163-
const tsProtoGlobalThis: any = (() => {
164-
if (typeof globalThis !== "undefined") {
165-
return globalThis;
166-
}
167-
if (typeof self !== "undefined") {
168-
return self;
169-
}
170-
if (typeof window !== "undefined") {
171-
return window;
172-
}
173-
if (typeof global !== "undefined") {
174-
return global;
175-
}
176-
throw "Unable to locate global object";
177-
})();
178-
179160
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
180161

181162
export type DeepPartial<T> = T extends Builtin ? T

integration/batching-with-context-esModuleInterop/batching.ts

+3-24
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const BatchQueryRequest = {
8282
},
8383

8484
fromJSON(object: any): BatchQueryRequest {
85-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
85+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
8686
},
8787

8888
toJSON(message: BatchQueryRequest): unknown {
@@ -140,9 +140,7 @@ export const BatchQueryResponse = {
140140

141141
fromJSON(object: any): BatchQueryResponse {
142142
return {
143-
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
144-
? object.entities.map((e: any) => Entity.fromJSON(e))
145-
: [],
143+
entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [],
146144
};
147145
},
148146

@@ -200,7 +198,7 @@ export const BatchMapQueryRequest = {
200198
},
201199

202200
fromJSON(object: any): BatchMapQueryRequest {
203-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
201+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
204202
},
205203

206204
toJSON(message: BatchMapQueryRequest): unknown {
@@ -757,25 +755,6 @@ export interface DataLoaders {
757755
getDataLoader<T>(identifier: string, constructorFn: () => T): T;
758756
}
759757

760-
declare const self: any | undefined;
761-
declare const window: any | undefined;
762-
declare const global: any | undefined;
763-
const tsProtoGlobalThis: any = (() => {
764-
if (typeof globalThis !== "undefined") {
765-
return globalThis;
766-
}
767-
if (typeof self !== "undefined") {
768-
return self;
769-
}
770-
if (typeof window !== "undefined") {
771-
return window;
772-
}
773-
if (typeof global !== "undefined") {
774-
return global;
775-
}
776-
throw "Unable to locate global object";
777-
})();
778-
779758
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
780759

781760
export type DeepPartial<T> = T extends Builtin ? T

integration/batching-with-context/batching.ts

+3-24
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const BatchQueryRequest = {
8282
},
8383

8484
fromJSON(object: any): BatchQueryRequest {
85-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
85+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
8686
},
8787

8888
toJSON(message: BatchQueryRequest): unknown {
@@ -140,9 +140,7 @@ export const BatchQueryResponse = {
140140

141141
fromJSON(object: any): BatchQueryResponse {
142142
return {
143-
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
144-
? object.entities.map((e: any) => Entity.fromJSON(e))
145-
: [],
143+
entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [],
146144
};
147145
},
148146

@@ -200,7 +198,7 @@ export const BatchMapQueryRequest = {
200198
},
201199

202200
fromJSON(object: any): BatchMapQueryRequest {
203-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
201+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
204202
},
205203

206204
toJSON(message: BatchMapQueryRequest): unknown {
@@ -757,25 +755,6 @@ export interface DataLoaders {
757755
getDataLoader<T>(identifier: string, constructorFn: () => T): T;
758756
}
759757

760-
declare const self: any | undefined;
761-
declare const window: any | undefined;
762-
declare const global: any | undefined;
763-
const tsProtoGlobalThis: any = (() => {
764-
if (typeof globalThis !== "undefined") {
765-
return globalThis;
766-
}
767-
if (typeof self !== "undefined") {
768-
return self;
769-
}
770-
if (typeof window !== "undefined") {
771-
return window;
772-
}
773-
if (typeof global !== "undefined") {
774-
return global;
775-
}
776-
throw "Unable to locate global object";
777-
})();
778-
779758
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
780759

781760
export type DeepPartial<T> = T extends Builtin ? T

integration/batching/batching.ts

+3-24
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const BatchQueryRequest = {
8080
},
8181

8282
fromJSON(object: any): BatchQueryRequest {
83-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
83+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
8484
},
8585

8686
toJSON(message: BatchQueryRequest): unknown {
@@ -138,9 +138,7 @@ export const BatchQueryResponse = {
138138

139139
fromJSON(object: any): BatchQueryResponse {
140140
return {
141-
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
142-
? object.entities.map((e: any) => Entity.fromJSON(e))
143-
: [],
141+
entities: globalThis.Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [],
144142
};
145143
},
146144

@@ -198,7 +196,7 @@ export const BatchMapQueryRequest = {
198196
},
199197

200198
fromJSON(object: any): BatchMapQueryRequest {
201-
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
199+
return { ids: globalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
202200
},
203201

204202
toJSON(message: BatchMapQueryRequest): unknown {
@@ -714,25 +712,6 @@ interface Rpc {
714712
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
715713
}
716714

717-
declare const self: any | undefined;
718-
declare const window: any | undefined;
719-
declare const global: any | undefined;
720-
const tsProtoGlobalThis: any = (() => {
721-
if (typeof globalThis !== "undefined") {
722-
return globalThis;
723-
}
724-
if (typeof self !== "undefined") {
725-
return self;
726-
}
727-
if (typeof window !== "undefined") {
728-
return window;
729-
}
730-
if (typeof global !== "undefined") {
731-
return global;
732-
}
733-
throw "Unable to locate global object";
734-
})();
735-
736715
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
737716

738717
export type DeepPartial<T> = T extends Builtin ? T

integration/bytes-as-base64/message.ts

+7-26
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,11 @@ export const Message = {
3333
},
3434
};
3535

36-
declare const self: any | undefined;
37-
declare const window: any | undefined;
38-
declare const global: any | undefined;
39-
const tsProtoGlobalThis: any = (() => {
40-
if (typeof globalThis !== "undefined") {
41-
return globalThis;
42-
}
43-
if (typeof self !== "undefined") {
44-
return self;
45-
}
46-
if (typeof window !== "undefined") {
47-
return window;
48-
}
49-
if (typeof global !== "undefined") {
50-
return global;
51-
}
52-
throw "Unable to locate global object";
53-
})();
54-
5536
function bytesFromBase64(b64: string): Uint8Array {
56-
if (tsProtoGlobalThis.Buffer) {
57-
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
37+
if (globalThis.Buffer) {
38+
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
5839
} else {
59-
const bin = tsProtoGlobalThis.atob(b64);
40+
const bin = globalThis.atob(b64);
6041
const arr = new Uint8Array(bin.length);
6142
for (let i = 0; i < bin.length; ++i) {
6243
arr[i] = bin.charCodeAt(i);
@@ -66,14 +47,14 @@ function bytesFromBase64(b64: string): Uint8Array {
6647
}
6748

6849
function base64FromBytes(arr: Uint8Array): string {
69-
if (tsProtoGlobalThis.Buffer) {
70-
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
50+
if (globalThis.Buffer) {
51+
return globalThis.Buffer.from(arr).toString("base64");
7152
} else {
7253
const bin: string[] = [];
7354
arr.forEach((byte) => {
74-
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
55+
bin.push(globalThis.String.fromCharCode(byte));
7556
});
76-
return tsProtoGlobalThis.btoa(bin.join(""));
57+
return globalThis.btoa(bin.join(""));
7758
}
7859
}
7960

integration/bytes-node/google/protobuf/wrappers.ts

+9-28
Original file line numberDiff line numberDiff line change
@@ -607,30 +607,11 @@ export const BytesValue = {
607607
},
608608
};
609609

610-
declare const self: any | undefined;
611-
declare const window: any | undefined;
612-
declare const global: any | undefined;
613-
const tsProtoGlobalThis: any = (() => {
614-
if (typeof globalThis !== "undefined") {
615-
return globalThis;
616-
}
617-
if (typeof self !== "undefined") {
618-
return self;
619-
}
620-
if (typeof window !== "undefined") {
621-
return window;
622-
}
623-
if (typeof global !== "undefined") {
624-
return global;
625-
}
626-
throw "Unable to locate global object";
627-
})();
628-
629610
function bytesFromBase64(b64: string): Uint8Array {
630-
if (tsProtoGlobalThis.Buffer) {
631-
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
611+
if (globalThis.Buffer) {
612+
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
632613
} else {
633-
const bin = tsProtoGlobalThis.atob(b64);
614+
const bin = globalThis.atob(b64);
634615
const arr = new Uint8Array(bin.length);
635616
for (let i = 0; i < bin.length; ++i) {
636617
arr[i] = bin.charCodeAt(i);
@@ -640,14 +621,14 @@ function bytesFromBase64(b64: string): Uint8Array {
640621
}
641622

642623
function base64FromBytes(arr: Uint8Array): string {
643-
if (tsProtoGlobalThis.Buffer) {
644-
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
624+
if (globalThis.Buffer) {
625+
return globalThis.Buffer.from(arr).toString("base64");
645626
} else {
646627
const bin: string[] = [];
647628
arr.forEach((byte) => {
648-
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
629+
bin.push(globalThis.String.fromCharCode(byte));
649630
});
650-
return tsProtoGlobalThis.btoa(bin.join(""));
631+
return globalThis.btoa(bin.join(""));
651632
}
652633
}
653634

@@ -663,8 +644,8 @@ export type Exact<P, I extends P> = P extends Builtin ? P
663644
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
664645

665646
function longToNumber(long: Long): number {
666-
if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) {
667-
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
647+
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
648+
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
668649
}
669650
return long.toNumber();
670651
}

0 commit comments

Comments
 (0)