Skip to content

Commit

Permalink
Change TextDecoder resolution to not rely on global (#40)
Browse files Browse the repository at this point in the history
`global` does not exist in the browser without Node polyfills.
  • Loading branch information
dimfeld authored Dec 1, 2021
1 parent 261d9cd commit 1eed6ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions borsh-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 1eed6ae

Please sign in to comment.