-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use subpath imports to choose Node or vanilla JS environment (#88)
Import buffer from `node:buffer` instead of using `globalThis.Buffer` to prevent accidentally using janky polyfills and allow bundlers to cleanly supply valid versions. Fixes: #87 --------- Co-authored-by: achingbrain <alex@achingbrain.net>
- Loading branch information
1 parent
dc81e75
commit e43f1f4
Showing
27 changed files
with
192 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Buffer } from 'node:buffer' | ||
import { asUint8Array } from '#util/as-uint8array' | ||
|
||
/** | ||
* Returns a `Uint8Array` of the requested size. Referenced memory will | ||
* be initialized to 0. | ||
*/ | ||
export function alloc (size: number = 0): Uint8Array { | ||
return asUint8Array(Buffer.alloc(size)) | ||
} | ||
|
||
/** | ||
* Where possible returns a Uint8Array of the requested size that references | ||
* uninitialized memory. Only use if you are certain you will immediately | ||
* overwrite every value in the returned `Uint8Array`. | ||
*/ | ||
export function allocUnsafe (size: number = 0): Uint8Array { | ||
return asUint8Array(Buffer.allocUnsafe(size)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Buffer } from 'node:buffer' | ||
|
||
/** | ||
* Can be used with Array.sort to sort and array with Uint8Array entries | ||
*/ | ||
export function compare (a: Uint8Array, b: Uint8Array): number { | ||
return Buffer.compare(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Buffer } from 'node:buffer' | ||
import { asUint8Array } from '#util/as-uint8array' | ||
|
||
/** | ||
* Returns a new Uint8Array created by concatenating the passed Uint8Arrays | ||
*/ | ||
export function concat (arrays: Uint8Array[], length?: number): Uint8Array { | ||
return asUint8Array(Buffer.concat(arrays, length)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Buffer } from 'node:buffer' | ||
import bases, { type SupportedEncodings } from './util/bases.js' | ||
import { asUint8Array } from '#util/as-uint8array' | ||
|
||
export type { SupportedEncodings } | ||
|
||
/** | ||
* Create a `Uint8Array` from the passed string | ||
* | ||
* Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module. | ||
* | ||
* Also `ascii` which is similar to node's 'binary' encoding. | ||
*/ | ||
export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { | ||
const base = bases[encoding] | ||
|
||
if (base == null) { | ||
throw new Error(`Unsupported encoding "${encoding}"`) | ||
} | ||
|
||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return asUint8Array(Buffer.from(string, 'utf-8')) | ||
} | ||
|
||
// add multibase prefix | ||
return base.decoder.decode(`${base.prefix}${string}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Buffer } from 'node:buffer' | ||
import bases, { type SupportedEncodings } from './util/bases.js' | ||
|
||
export type { SupportedEncodings } | ||
|
||
/** | ||
* Turns a `Uint8Array` into a string. | ||
* | ||
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module. | ||
* | ||
* Also `ascii` which is similar to node's 'binary' encoding. | ||
*/ | ||
export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf8'): string { | ||
const base = bases[encoding] | ||
|
||
if (base == null) { | ||
throw new Error(`Unsupported encoding "${encoding}"`) | ||
} | ||
|
||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8') | ||
} | ||
|
||
// strip multibase prefix | ||
return base.encoder.encode(array).substring(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* To guarantee Uint8Array semantics, convert nodejs Buffers | ||
* into vanilla Uint8Arrays | ||
*/ | ||
export function asUint8Array (buf: Uint8Array): Uint8Array { | ||
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.