Skip to content

Commit

Permalink
fix: Avoid namespace conflict on globalThis. (#735)
Browse files Browse the repository at this point in the history
Fixes #732
  • Loading branch information
stephenh committed Dec 16, 2022
1 parent a2becd4 commit 71e919e
Show file tree
Hide file tree
Showing 52 changed files with 281 additions and 224 deletions.
14 changes: 7 additions & 7 deletions integration/bytes-as-base64/message.ts
Expand Up @@ -32,7 +32,7 @@ export const Message = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -49,10 +49,10 @@ var globalThis: any = (() => {
})();

function bytesFromBase64(b64: string): Uint8Array {
if (globalThis.Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
if (tsProtoGlobalThis.Buffer) {
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
} else {
const bin = globalThis.atob(b64);
const bin = tsProtoGlobalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
Expand All @@ -62,14 +62,14 @@ function bytesFromBase64(b64: string): Uint8Array {
}

function base64FromBytes(arr: Uint8Array): string {
if (globalThis.Buffer) {
return globalThis.Buffer.from(arr).toString("base64");
if (tsProtoGlobalThis.Buffer) {
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
return tsProtoGlobalThis.btoa(bin.join(""));
}
}

Expand Down
16 changes: 8 additions & 8 deletions integration/bytes-node/google/protobuf/wrappers.ts
Expand Up @@ -521,7 +521,7 @@ export const BytesValue = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -538,10 +538,10 @@ var globalThis: any = (() => {
})();

function bytesFromBase64(b64: string): Uint8Array {
if (globalThis.Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
if (tsProtoGlobalThis.Buffer) {
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
} else {
const bin = globalThis.atob(b64);
const bin = tsProtoGlobalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
Expand All @@ -551,14 +551,14 @@ function bytesFromBase64(b64: string): Uint8Array {
}

function base64FromBytes(arr: Uint8Array): string {
if (globalThis.Buffer) {
return globalThis.Buffer.from(arr).toString("base64");
if (tsProtoGlobalThis.Buffer) {
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
return tsProtoGlobalThis.btoa(bin.join(""));
}
}

Expand All @@ -575,7 +575,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
14 changes: 7 additions & 7 deletions integration/bytes-node/point.ts
Expand Up @@ -71,7 +71,7 @@ export const Point = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -88,10 +88,10 @@ var globalThis: any = (() => {
})();

function bytesFromBase64(b64: string): Uint8Array {
if (globalThis.Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
if (tsProtoGlobalThis.Buffer) {
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
} else {
const bin = globalThis.atob(b64);
const bin = tsProtoGlobalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
Expand All @@ -101,14 +101,14 @@ function bytesFromBase64(b64: string): Uint8Array {
}

function base64FromBytes(arr: Uint8Array): string {
if (globalThis.Buffer) {
return globalThis.Buffer.from(arr).toString("base64");
if (tsProtoGlobalThis.Buffer) {
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
return tsProtoGlobalThis.btoa(bin.join(""));
}
}

Expand Down
4 changes: 2 additions & 2 deletions integration/file-suffix/google/protobuf/timestamp.pb.ts
Expand Up @@ -172,7 +172,7 @@ export const Timestamp = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
4 changes: 2 additions & 2 deletions integration/grpc-js/google/protobuf/timestamp.ts
Expand Up @@ -172,7 +172,7 @@ export const Timestamp = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
16 changes: 8 additions & 8 deletions integration/grpc-js/google/protobuf/wrappers.ts
Expand Up @@ -521,7 +521,7 @@ export const BytesValue = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -538,10 +538,10 @@ var globalThis: any = (() => {
})();

function bytesFromBase64(b64: string): Uint8Array {
if (globalThis.Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
if (tsProtoGlobalThis.Buffer) {
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
} else {
const bin = globalThis.atob(b64);
const bin = tsProtoGlobalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
Expand All @@ -551,14 +551,14 @@ function bytesFromBase64(b64: string): Uint8Array {
}

function base64FromBytes(arr: Uint8Array): string {
if (globalThis.Buffer) {
return globalThis.Buffer.from(arr).toString("base64");
if (tsProtoGlobalThis.Buffer) {
return tsProtoGlobalThis.Buffer.from(arr).toString("base64");
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
return tsProtoGlobalThis.btoa(bin.join(""));
}
}

Expand All @@ -575,7 +575,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
21 changes: 20 additions & 1 deletion integration/grpc-web-no-streaming-observable/example.ts
Expand Up @@ -415,6 +415,25 @@ export class GrpcWebImpl {
}
}

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var 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 All @@ -430,7 +449,7 @@ function isSet(value: any): boolean {
return value !== null && value !== undefined;
}

export class GrpcWebError extends globalThis.Error {
export class GrpcWebError extends tsProtoGlobalThis.Error {
constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
super(message);
}
Expand Down
21 changes: 20 additions & 1 deletion integration/grpc-web-no-streaming/example.ts
Expand Up @@ -412,6 +412,25 @@ export class GrpcWebImpl {
}
}

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var 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 All @@ -427,7 +446,7 @@ function isSet(value: any): boolean {
return value !== null && value !== undefined;
}

export class GrpcWebError extends globalThis.Error {
export class GrpcWebError extends tsProtoGlobalThis.Error {
constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
super(message);
}
Expand Down
21 changes: 20 additions & 1 deletion integration/grpc-web/example.ts
Expand Up @@ -893,6 +893,25 @@ export class GrpcWebImpl {
}
}

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var 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 All @@ -908,7 +927,7 @@ function isSet(value: any): boolean {
return value !== null && value !== undefined;
}

export class GrpcWebError extends globalThis.Error {
export class GrpcWebError extends tsProtoGlobalThis.Error {
constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
super(message);
}
Expand Down
4 changes: 2 additions & 2 deletions integration/import-mapping/google/protobuf/timestamp.ts
Expand Up @@ -172,7 +172,7 @@ export const Timestamp = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
4 changes: 2 additions & 2 deletions integration/import-suffix/google/protobuf/timestamp.pb.ts
Expand Up @@ -172,7 +172,7 @@ export const Timestamp = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export type Exact<P, I extends P> = P extends Builtin ? P

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
4 changes: 2 additions & 2 deletions integration/meta-typings/google/protobuf/timestamp.ts
Expand Up @@ -272,7 +272,7 @@ export const protoMetadata: ProtoMetadata = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -290,7 +290,7 @@ var globalThis: any = (() => {

function longToNumber(long: Long): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
return long.toNumber();
}
Expand Down
4 changes: 2 additions & 2 deletions integration/meta-typings/google/protobuf/wrappers.ts
Expand Up @@ -775,7 +775,7 @@ export const protoMetadata: ProtoMetadata = {
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
var tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand All @@ -793,7 +793,7 @@ var globalThis: any = (() => {

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

0 comments on commit 71e919e

Please sign in to comment.