Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: swap global for globalThis #12

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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