Skip to content

Commit

Permalink
feat: Add globalThisPolyfill, defaults false. (#931)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
stephenh authored Sep 30, 2023
1 parent 6f85637 commit 085fa21
Show file tree
Hide file tree
Showing 134 changed files with 644 additions and 3,077 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.10.0
v20.8.0
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
23 changes: 2 additions & 21 deletions integration/async-iterable-services-abort-signal/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const EchoMsg = {
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
): AsyncIterable<Uint8Array> {
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()];
}
Expand All @@ -65,7 +65,7 @@ export const EchoMsg = {
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
): AsyncIterable<EchoMsg> {
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)];
}
Expand Down Expand Up @@ -173,25 +173,6 @@ interface Rpc {
): AsyncIterable<Uint8Array>;
}

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> = T extends Builtin ? T
Expand Down
23 changes: 2 additions & 21 deletions integration/async-iterable-services/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const EchoMsg = {
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
): AsyncIterable<Uint8Array> {
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()];
}
Expand All @@ -65,7 +65,7 @@ export const EchoMsg = {
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
): AsyncIterable<EchoMsg> {
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)];
}
Expand Down Expand Up @@ -157,25 +157,6 @@ interface Rpc {
): AsyncIterable<Uint8Array>;
}

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> = T extends Builtin ? T
Expand Down
27 changes: 3 additions & 24 deletions integration/batching-with-context-esModuleInterop/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) : [],
};
},

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -757,25 +755,6 @@ export interface DataLoaders {
getDataLoader<T>(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> = T extends Builtin ? T
Expand Down
27 changes: 3 additions & 24 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) : [],
};
},

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -757,25 +755,6 @@ export interface DataLoaders {
getDataLoader<T>(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> = T extends Builtin ? T
Expand Down
27 changes: 3 additions & 24 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) : [],
};
},

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -714,25 +712,6 @@ interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
}

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> = T extends Builtin ? T
Expand Down
33 changes: 7 additions & 26 deletions integration/bytes-as-base64/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(""));
}
}

Expand Down
37 changes: 9 additions & 28 deletions integration/bytes-node/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(""));
}
}

Expand All @@ -663,8 +644,8 @@ export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: 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();
}
Expand Down
Loading

0 comments on commit 085fa21

Please sign in to comment.