From 1eed6aebc0d951dc41a95696f277afc7b42cc4d4 Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Wed, 1 Dec 2021 21:15:03 +0800 Subject: [PATCH] Change TextDecoder resolution to not rely on `global` (#40) `global` does not exist in the browser without Node polyfills. --- borsh-ts/index.ts | 8 ++++---- lib/index.js | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/borsh-ts/index.ts b/borsh-ts/index.ts index 67b02214..420b0202 100644 --- a/borsh-ts/index.ts +++ b/borsh-ts/index.ts @@ -3,11 +3,11 @@ import bs58 from "bs58"; // TODO: Make sure this polyfill not included when not required import * as encoding from "text-encoding-utf-8"; -const TextDecoder = - typeof (global as any).TextDecoder !== "function" +const ResolvedTextDecoder = + typeof TextDecoder !== "function" ? encoding.TextDecoder - : (global as any).TextDecoder; -const textDecoder = new TextDecoder("utf-8", { fatal: true }); + : TextDecoder; +const textDecoder = new ResolvedTextDecoder("utf-8", { fatal: true }); export function baseEncode(value: Uint8Array | string): string { if (typeof value === "string") { diff --git a/lib/index.js b/lib/index.js index 832c3e92..8fff761a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,10 +33,10 @@ const bn_js_1 = __importDefault(require("bn.js")); const bs58_1 = __importDefault(require("bs58")); // TODO: Make sure this polyfill not included when not required const encoding = __importStar(require("text-encoding-utf-8")); -const TextDecoder = typeof global.TextDecoder !== "function" +const ResolvedTextDecoder = typeof TextDecoder !== "function" ? encoding.TextDecoder - : global.TextDecoder; -const textDecoder = new TextDecoder("utf-8", { fatal: true }); + : TextDecoder; +const textDecoder = new ResolvedTextDecoder("utf-8", { fatal: true }); function baseEncode(value) { if (typeof value === "string") { value = Buffer.from(value, "utf8");