From f14e8a5031e420a0b18ebcf9cb8f330bdd93a89c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 26 Mar 2021 12:27:55 +0000 Subject: [PATCH] fix: swap global for globalThis `global` is not defined in browsers. Use `globalThis` instead. Fixes #7 --- lib/byte-utils.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/byte-utils.js b/lib/byte-utils.js index 79ba98d..40a9546 100644 --- a/lib/byte-utils.js +++ b/lib/byte-utils.js @@ -1,16 +1,16 @@ // Use Uint8Array directly in the browser, use Buffer in Node.js but don't // speak its name directly to avoid bundlers pulling in the `Buffer` polyfill -export const useBuffer = global.process && - !global.process.browser && - global.Buffer && - typeof global.Buffer.isBuffer === 'function' +export const useBuffer = globalThis.process && + !globalThis.process.browser && + globalThis.Buffer && + typeof globalThis.Buffer.isBuffer === 'function' const textDecoder = new TextDecoder() const textEncoder = new TextEncoder() function isBuffer (buf) { - return useBuffer && global.Buffer.isBuffer(buf) + return useBuffer && globalThis.Buffer.isBuffer(buf) } export function asU8A (buf) { @@ -24,7 +24,7 @@ export function asU8A (buf) { export const toString = useBuffer ? (bytes, start, end) => { return end - start > 64 - ? global.Buffer.from(bytes.subarray(start, end)).toString('utf8') + ? globalThis.Buffer.from(bytes.subarray(start, end)).toString('utf8') : utf8Slice(bytes, start, end) } /* c8 ignore next 5 */ @@ -36,7 +36,7 @@ export const toString = useBuffer export const fromString = useBuffer ? (string) => { - return string.length > 64 ? global.Buffer.from(string) : utf8ToBytes(string) + return string.length > 64 ? globalThis.Buffer.from(string) : utf8ToBytes(string) } /* c8 ignore next 3 */ : (string) => { @@ -64,8 +64,8 @@ export const concat = useBuffer ? (chunks, length) => { // might get a stray plain Array here /* c8 ignore next 1 */ - chunks = chunks.map((c) => c instanceof Uint8Array ? c : global.Buffer.from(c)) - return asU8A(global.Buffer.concat(chunks, length)) + chunks = chunks.map((c) => c instanceof Uint8Array ? c : globalThis.Buffer.from(c)) + return asU8A(globalThis.Buffer.concat(chunks, length)) } /* c8 ignore next 13 */ : (chunks, length) => { @@ -85,7 +85,7 @@ export const concat = useBuffer export const alloc = useBuffer ? (size) => { // we always write over the contents we expose so this should be safe - return global.Buffer.allocUnsafe(size) + return globalThis.Buffer.allocUnsafe(size) } /* c8 ignore next 3 */ : (size) => { @@ -97,7 +97,7 @@ export const toHex = useBuffer if (typeof d === 'string') { return d } - return global.Buffer.from(toBytes(d)).toString('hex') + return globalThis.Buffer.from(toBytes(d)).toString('hex') } /* c8 ignore next 6 */ : (d) => { @@ -112,7 +112,7 @@ export const fromHex = useBuffer if (hex instanceof Uint8Array) { return hex } - return global.Buffer.from(hex, 'hex') + return globalThis.Buffer.from(hex, 'hex') } /* c8 ignore next 12 */ : (hex) => {