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: allow use in bundlers #10

Closed
Closed
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion lib/0uint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import { Token, Type } from './token.js'
import { decodeErrPrefix, assertEnoughData } from './common.js'

export const uintBoundaries = [24, 2 ** 8, 2 ** 16, 2 ** 32, BigInt(2) ** BigInt(64)]
export const uintBoundaries = [
24,
2 ** 8,
2 ** 16,
2 ** 32,
// BigInt(2) ** BigInt(64) gets transpiled to Math.pow(BigInt(2), BigInt(64)) so use the literal value
18446744073709551616n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use literals though, I started off with all literals but it breaks with some version of something, I can't recall which, but literals are a bit too new compared to the constructor, BigInt('18446744073709551616') might be acceptable though.

]

export function readUint8 (data, pos, options) {
assertEnoughData(data, pos, 1)
Expand Down
141 changes: 38 additions & 103 deletions lib/byte-utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// 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 = typeof process !== 'undefined' && !process.browser && global.Buffer && typeof global.Buffer.isBuffer === 'function'

const textDecoder = new TextDecoder()
const textEncoder = new TextEncoder()
import { Buffer } from 'buffer'

function isBuffer (buf) {
return useBuffer && global.Buffer.isBuffer(buf)
return Buffer.isBuffer(buf)
}

export function asU8A (buf) {
Expand All @@ -18,112 +12,53 @@ export function asU8A (buf) {
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf
}

export const toString = useBuffer
? (bytes, start, end) => {
return end - start > 64
? global.Buffer.from(bytes.subarray(start, end)).toString('utf8')
: utf8Slice(bytes, start, end)
}
/* c8 ignore next 5 */
: (bytes, start, end) => {
return end - start > 64
? textDecoder.decode(bytes.subarray(start, end))
: utf8Slice(bytes, start, end)
}
export const toString = (bytes, start, end) => {
return end - start > 64
? Buffer.from(bytes.subarray(start, end)).toString('utf8')
: utf8Slice(bytes, start, end)
}

export const fromString = useBuffer
? (string) => {
return string.length > 64 ? global.Buffer.from(string) : utf8ToBytes(string)
}
/* c8 ignore next 3 */
: (string) => {
return string.length > 64 ? textEncoder.encode(string) : utf8ToBytes(string)
}
export const fromString = (string) => {
return string.length > 64 ? Buffer.from(string) : utf8ToBytes(string)
}

// Buffer variant not fast enough for what we need
export const fromArray = (arr) => {
return Uint8Array.from(arr)
}

export const slice = useBuffer
? (bytes, start, end) => {
if (isBuffer(bytes)) {
return new Uint8Array(bytes.subarray(start, end))
}
return bytes.slice(start, end)
}
/* c8 ignore next 3 */
: (bytes, start, end) => {
return bytes.slice(start, end)
}
export const slice = (bytes, start, end) => {
if (isBuffer(bytes)) {
return new Uint8Array(bytes.subarray(start, end))
}
return bytes.slice(start, end)
}

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))
}
/* c8 ignore next 13 */
: (chunks, length) => {
const out = new Uint8Array(length)
let off = 0
for (let b of chunks) {
if (off + b.length > out.length) {
// final chunk that's bigger than we need
b = b.subarray(0, out.length - off)
}
out.set(b, off)
off += b.length
}
return out
}
export const concat = (chunks, length) => {
// might get a stray plain Array here
/* c8 ignore next 1 */
chunks = chunks.map((c) => c instanceof Uint8Array ? Buffer.from(c.buffer, c.byteOffset, c.byteLength) : Buffer.from(c))
return asU8A(Buffer.concat(chunks, length))
}

export const alloc = useBuffer
? (size) => {
// we always write over the contents we expose so this should be safe
return global.Buffer.allocUnsafe(size)
}
/* c8 ignore next 3 */
: (size) => {
return new Uint8Array(size)
}
export const alloc = (size) => {
// we always write over the contents we expose so this should be safe
return Buffer.allocUnsafe(size)
}

export const toHex = useBuffer
? (d) => {
if (typeof d === 'string') {
return d
}
return global.Buffer.from(toBytes(d)).toString('hex')
}
/* c8 ignore next 6 */
: (d) => {
if (typeof d === 'string') {
return d
}
return Array.prototype.reduce.call(toBytes(d), (p, c) => `${p}${c.toString(16).padStart(2, '0')}`, '')
}
export const toHex = (d) => {
if (typeof d === 'string') {
return d
}
return Buffer.from(toBytes(d)).toString('hex')
}

export const fromHex = useBuffer
? (hex) => {
if (hex instanceof Uint8Array) {
return hex
}
return global.Buffer.from(hex, 'hex')
}
/* c8 ignore next 12 */
: (hex) => {
if (hex instanceof Uint8Array) {
return hex
}
if (!hex.length) {
return new Uint8Array(0)
}
return new Uint8Array(hex.split('')
.map((c, i, d) => i % 2 === 0 ? `0x${c}${d[i + 1]}` : '')
.filter(Boolean)
.map((e) => parseInt(e, 16)))
}
export const fromHex = (hex) => {
if (hex instanceof Uint8Array) {
return hex
}
return Buffer.from(hex, 'hex')
}

function toBytes (obj) {
if (obj instanceof Uint8Array && obj.constructor.name === 'Uint8Array') {
Expand Down
Loading