-
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.
- Loading branch information
Showing
23 changed files
with
153 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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(globalThis.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(globalThis.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,6 @@ | ||
/** | ||
* Can be used with Array.sort to sort and array with Uint8Array entries | ||
*/ | ||
export function compare (a: Uint8Array, b: Uint8Array): number { | ||
return globalThis.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,8 @@ | ||
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(globalThis.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,26 @@ | ||
import { asUint8Array } from '#util/as-uint8array' | ||
import bases, { type SupportedEncodings } from './util/bases.js' | ||
|
||
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(globalThis.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,25 @@ | ||
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 globalThis.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
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
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