Skip to content

Commit

Permalink
fix: Use globalThis for Array/String/Boolean (#930)
Browse files Browse the repository at this point in the history
* fix: Use globalThis for Array/String/Boolean.

* Re-codegen.

* Fix missing code tag, some dates.

* Re-codegen.

* Add as any since tsProtoGlobalThis is untyped.

* Format.
  • Loading branch information
stephenh authored Sep 30, 2023
1 parent 76c6bbf commit 9a252c3
Show file tree
Hide file tree
Showing 127 changed files with 1,622 additions and 446 deletions.
31 changes: 25 additions & 6 deletions integration/async-iterable-services-abort-signal/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export const EchoMsg = {
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
): AsyncIterable<Uint8Array> {
for await (const pkt of source) {
if (Array.isArray(pkt)) {
for (const p of pkt) {
if (tsProtoGlobalThis.Array.isArray(pkt)) {
for (const p of (pkt as any)) {
yield* [EchoMsg.encode(p).finish()];
}
} else {
yield* [EchoMsg.encode(pkt).finish()];
yield* [EchoMsg.encode(pkt as any).finish()];
}
}
},
Expand All @@ -65,12 +65,12 @@ export const EchoMsg = {
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
): AsyncIterable<EchoMsg> {
for await (const pkt of source) {
if (Array.isArray(pkt)) {
for (const p of pkt) {
if (tsProtoGlobalThis.Array.isArray(pkt)) {
for (const p of (pkt as any)) {
yield* [EchoMsg.decode(p)];
}
} else {
yield* [EchoMsg.decode(pkt)];
yield* [EchoMsg.decode(pkt as any)];
}
}
},
Expand Down Expand Up @@ -173,6 +173,25 @@ 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
31 changes: 25 additions & 6 deletions integration/async-iterable-services/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export const EchoMsg = {
source: AsyncIterable<EchoMsg | EchoMsg[]> | Iterable<EchoMsg | EchoMsg[]>,
): AsyncIterable<Uint8Array> {
for await (const pkt of source) {
if (Array.isArray(pkt)) {
for (const p of pkt) {
if (tsProtoGlobalThis.Array.isArray(pkt)) {
for (const p of (pkt as any)) {
yield* [EchoMsg.encode(p).finish()];
}
} else {
yield* [EchoMsg.encode(pkt).finish()];
yield* [EchoMsg.encode(pkt as any).finish()];
}
}
},
Expand All @@ -65,12 +65,12 @@ export const EchoMsg = {
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>,
): AsyncIterable<EchoMsg> {
for await (const pkt of source) {
if (Array.isArray(pkt)) {
for (const p of pkt) {
if (tsProtoGlobalThis.Array.isArray(pkt)) {
for (const p of (pkt as any)) {
yield* [EchoMsg.decode(p)];
}
} else {
yield* [EchoMsg.decode(pkt)];
yield* [EchoMsg.decode(pkt as any)];
}
}
},
Expand Down Expand Up @@ -157,6 +157,25 @@ 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
29 changes: 26 additions & 3 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: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchQueryRequest): unknown {
Expand Down Expand Up @@ -139,7 +139,11 @@ export const BatchQueryResponse = {
},

fromJSON(object: any): BatchQueryResponse {
return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] };
return {
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
? object.entities.map((e: any) => Entity.fromJSON(e))
: [],
};
},

toJSON(message: BatchQueryResponse): unknown {
Expand Down Expand Up @@ -196,7 +200,7 @@ export const BatchMapQueryRequest = {
},

fromJSON(object: any): BatchMapQueryRequest {
return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchMapQueryRequest): unknown {
Expand Down Expand Up @@ -753,6 +757,25 @@ 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
29 changes: 26 additions & 3 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: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchQueryRequest): unknown {
Expand Down Expand Up @@ -139,7 +139,11 @@ export const BatchQueryResponse = {
},

fromJSON(object: any): BatchQueryResponse {
return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] };
return {
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
? object.entities.map((e: any) => Entity.fromJSON(e))
: [],
};
},

toJSON(message: BatchQueryResponse): unknown {
Expand Down Expand Up @@ -196,7 +200,7 @@ export const BatchMapQueryRequest = {
},

fromJSON(object: any): BatchMapQueryRequest {
return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchMapQueryRequest): unknown {
Expand Down Expand Up @@ -753,6 +757,25 @@ 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
29 changes: 26 additions & 3 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: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchQueryRequest): unknown {
Expand Down Expand Up @@ -137,7 +137,11 @@ export const BatchQueryResponse = {
},

fromJSON(object: any): BatchQueryResponse {
return { entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [] };
return {
entities: tsProtoGlobalThis.Array.isArray(object?.entities)
? object.entities.map((e: any) => Entity.fromJSON(e))
: [],
};
},

toJSON(message: BatchQueryResponse): unknown {
Expand Down Expand Up @@ -194,7 +198,7 @@ export const BatchMapQueryRequest = {
},

fromJSON(object: any): BatchMapQueryRequest {
return { ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
return { ids: tsProtoGlobalThis.Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [] };
},

toJSON(message: BatchMapQueryRequest): unknown {
Expand Down Expand Up @@ -710,6 +714,25 @@ 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
2 changes: 1 addition & 1 deletion integration/bytes-as-base64/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function base64FromBytes(arr: Uint8Array): string {
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
});
return tsProtoGlobalThis.btoa(bin.join(""));
}
Expand Down
4 changes: 2 additions & 2 deletions integration/bytes-node/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ function base64FromBytes(arr: Uint8Array): string {
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
});
return tsProtoGlobalThis.btoa(bin.join(""));
}
Expand All @@ -663,7 +663,7 @@ 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(Number.MAX_SAFE_INTEGER)) {
if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) {
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
Expand Down
2 changes: 1 addition & 1 deletion integration/bytes-node/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function base64FromBytes(arr: Uint8Array): string {
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
});
return tsProtoGlobalThis.btoa(bin.join(""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ 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(Number.MAX_SAFE_INTEGER)) {
if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) {
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
Expand Down
32 changes: 20 additions & 12 deletions integration/emit-default-values-json/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,21 @@ export const DefaultValuesTest = {
truth: isSet(object.truth) ? Boolean(object.truth) : false,
description: isSet(object.description) ? String(object.description) : "",
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(0),
repId: Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [],
repChild: Array.isArray(object?.repChild) ? object.repChild.map((e: any) => Child.fromJSON(e)) : [],
repState: Array.isArray(object?.repState) ? object.repState.map((e: any) => stateEnumFromJSON(e)) : [],
repLong: Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [],
repTruth: Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [],
repDescription: Array.isArray(object?.repDescription) ? object.repDescription.map((e: any) => String(e)) : [],
repData: Array.isArray(object?.repData) ? object.repData.map((e: any) => bytesFromBase64(e)) : [],
repId: tsProtoGlobalThis.Array.isArray(object?.repId) ? object.repId.map((e: any) => Number(e)) : [],
repChild: tsProtoGlobalThis.Array.isArray(object?.repChild)
? object.repChild.map((e: any) => Child.fromJSON(e))
: [],
repState: tsProtoGlobalThis.Array.isArray(object?.repState)
? object.repState.map((e: any) => stateEnumFromJSON(e))
: [],
repLong: tsProtoGlobalThis.Array.isArray(object?.repLong) ? object.repLong.map((e: any) => Number(e)) : [],
repTruth: tsProtoGlobalThis.Array.isArray(object?.repTruth) ? object.repTruth.map((e: any) => Boolean(e)) : [],
repDescription: tsProtoGlobalThis.Array.isArray(object?.repDescription)
? object.repDescription.map((e: any) => String(e))
: [],
repData: tsProtoGlobalThis.Array.isArray(object?.repData)
? object.repData.map((e: any) => bytesFromBase64(e))
: [],
optId: isSet(object.optId) ? Number(object.optId) : undefined,
optChild: isSet(object.optChild) ? Child.fromJSON(object.optChild) : undefined,
optState: isSet(object.optState) ? stateEnumFromJSON(object.optState) : undefined,
Expand Down Expand Up @@ -719,7 +727,7 @@ function base64FromBytes(arr: Uint8Array): string {
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
bin.push(tsProtoGlobalThis.String.fromCharCode(byte));
});
return tsProtoGlobalThis.btoa(bin.join(""));
}
Expand All @@ -745,21 +753,21 @@ function toTimestamp(date: Date): Timestamp {
function fromTimestamp(t: Timestamp): Date {
let millis = (t.seconds || 0) * 1_000;
millis += (t.nanos || 0) / 1_000_000;
return new Date(millis);
return new tsProtoGlobalThis.Date(millis);
}

function fromJsonTimestamp(o: any): Date {
if (o instanceof Date) {
if (o instanceof tsProtoGlobalThis.Date) {
return o;
} else if (typeof o === "string") {
return new Date(o);
return new tsProtoGlobalThis.Date(o);
} else {
return fromTimestamp(Timestamp.fromJSON(o));
}
}

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
if (long.gt(tsProtoGlobalThis.Number.MAX_SAFE_INTEGER)) {
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
Expand Down
Loading

0 comments on commit 9a252c3

Please sign in to comment.