Skip to content

Commit

Permalink
fix: swap global for globalThis
Browse files Browse the repository at this point in the history
`global` is not defined in browsers.  Use `globalThis` instead.

Fixes #7
  • Loading branch information
achingbrain committed Mar 26, 2021
1 parent 22e0a05 commit 4aac19f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/byte-utils.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 */
Expand All @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit 4aac19f

Please sign in to comment.