Skip to content package
uint8array-extras v1.5.0 Useful utilities for working with Uint8Array (and Buffer)
node >=18 0 deps esm types open source
overview readme
license MIT
deps 0
unpacked 28.9 kB
files 5
node >=18 $ npm install uint8array-extras@1.5.0n npmREADME GitHub-flavored package documentation.
downloads Across all versions.
Download trend unavailable.
compatibility Runtime metadata.
keywords Tagged topics.
uint8array uint8 typedarray buffer typedarray arraybuffer is assert concat equals compare base64
versions Latest published Sep 26, 2026
maintainers Registry package owners.
install size Estimated unpacked size across 1 packages.
29.8 kB
Includes dependencies.
contents
Useful utilities for working with Uint8Array (and Buffer )
It's time to transition from Buffer to Uint8Array , and this package helps fill in the gaps.
Note that Buffer is a Uint8Array subclass, so you can use this package with Buffer too.
This package is tree-shakeable and browser-compatible.
This package also includes methods to convert a string to Base64 and back.
Note: In the browser, do not use / because they . This package does.
Install npm install uint8array-extras copy
Usage import {concatUint8Arrays} from 'uint8array-extras' ;
const a = new Uint8Array ([ 1 , 2 , 3 ]);
const b = new Uint8Array ([ 4 , 5 , 6 ]);
console. log ( concatUint8Arrays ([a, b]));
//=> Uint8Array [1, 2, 3, 4, 5, 6] copy
API
isUint8Array(value: unknown): booleanCheck if the given value is an instance of Uint8Array.
import {isUint8Array} from 'uint8array-extras' ;
console. log ( isUint8Array ( new Uint8Array ()));
//=> true
console. log ( isUint8Array (Buffer. from ( 'x' )));
//=> true
console. log ( isUint8Array ( new ArrayBuffer ( 10 )));
//=> false copy
assertUint8Array(value: unknown)Throw a TypeError if the given value is not an instance of Uint8Array.
import {assertUint8Array} from 'uint8array-extras' ;
try {
assertUint8Array ( new ArrayBuffer ( 10 )); // Throws a TypeError
} catch (error) {
console. error (error.message);
} copy
assertUint8ArrayOrArrayBuffer(value: unknown)Throw a TypeError if the given value is not an instance of Uint8Array or ArrayBuffer.
Useful as a guard for functions that accept either a Uint8Array or ArrayBuffer.
import {assertUint8ArrayOrArrayBuffer} from 'uint8array-extras' ;
assertUint8ArrayOrArrayBuffer ( new Uint8Array ());
assertUint8ArrayOrArrayBuffer ( new ArrayBuffer ( 8 )); copy
toUint8Array(value: TypedArray | ArrayBuffer | DataView): Uint8ArrayConvert a value to a Uint8Array without copying its data.
This can be useful for converting a Buffer to a pure Uint8Array. Buffer is already a Uint8Array subclass, but Buffer alters some behavior , so it can be useful to cast it to a pure Uint8Array before returning it.
Tip: If you want a copy, just call .slice() on the return value.
concatUint8Arrays(arrays: Array<Uint8Array | ArrayBuffer>, totalLength?: number): Uint8ArrayConcatenate the given arrays into a new array.
If arrays is empty, it will return a zero-sized Uint8Array.
If totalLength is not specified, it is calculated from summing the lengths of the given arrays.
import {concatUint8Arrays} from 'uint8array-extras' ;
const a = new Uint8Array ([ 1 , 2 , 3 ]);
const b = new Uint8Array ([ 4 , 5 , 6 ]);
console. log ( concatUint8Arrays ([a, b]));
//=> Uint8Array [1, 2, 3, 4, 5, 6] copy
areUint8ArraysEqual(a: Uint8Array | ArrayBuffer, b: Uint8Array | ArrayBuffer): booleanCheck if two arrays are identical by verifying that they contain the same bytes in the same sequence.
import {areUint8ArraysEqual} from 'uint8array-extras' ;
const a = new Uint8Array ([ 1 , 2 , 3 ]);
const b = new Uint8Array ([ 1 , 2 , 3 ]);
const c = new Uint8Array ([ 4 , 5 , 6 ]);
console. log ( areUint8ArraysEqual (a, b));
//=> true
console. log ( areUint8ArraysEqual (a, c));
//=> false copy
compareUint8Arrays(a: Uint8Array | ArrayBuffer, b: Uint8Array | ArrayBuffer): 0 | 1 | -1Compare two arrays and indicate their relative order or equality. Useful for sorting.
import {compareUint8Arrays} from 'uint8array-extras' ;
const array1 = new Uint8Array ([ 1 , 2 , 3 ]);
const array2 = new Uint8Array ([ 4 , 5 , 6 ]);
const array3 = new Uint8Array ([ 7 , 8 , 9 ]);
[array3, array1, array2]. sort (compareUint8Arrays);
//=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] copy
uint8ArrayToString(array: Uint8Array | ArrayBuffer, encoding?: string = 'utf8'): stringConvert a Uint8Array to a string.
Parameter: encoding - The encoding to convert from.
Replacement for Buffer#toString() . For the encoding parameter, latin1 should be used instead of binary and utf-16le instead of utf16le.
import {uint8ArrayToString} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 72 , 101 , 108 , 108 , 111 ]);
console. log ( uint8ArrayToString (byteArray));
//=> 'Hello'
const zh = new Uint8Array ([ 167 , 65 , 166 , 110 ]);
console. log ( uint8ArrayToString (zh, 'big5' ));
//=> '你好'
const ja = new Uint8Array ([ 130 , 177 , 130 , 241 , 130 , 201 , 130 , 191 , 130 , 205 ]);
console. log ( uint8ArrayToString (ja, 'shift-jis' ));
//=> 'こんにちは' copy
stringToUint8Array(string: string): Uint8ArrayConvert a string to a Uint8Array (using UTF-8 encoding).
import {stringToUint8Array} from 'uint8array-extras' ;
console. log ( stringToUint8Array ( 'Hello' ));
//=> Uint8Array [72, 101, 108, 108, 111] copy
uint8ArrayToBase64(array: Uint8Array | ArrayBuffer, options?: {urlSafe: boolean}): stringConvert a Uint8Array to a Base64-encoded string.
Specify {urlSafe: true} to get a Base64URL -encoded string.
import {uint8ArrayToBase64} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 72 , 101 , 108 , 108 , 111 ]);
console. log ( uint8ArrayToBase64 (byteArray));
//=> 'SGVsbG8=' copy
base64ToUint8Array(string: string): Uint8ArrayConvert a Base64-encoded or Base64URL -encoded string to a Uint8Array.
Accepts Base64URL with or without padding.
import {base64ToUint8Array} from 'uint8array-extras' ;
console. log ( base64ToUint8Array ( 'SGVsbG8=' ));
//=> Uint8Array [72, 101, 108, 108, 111] copy
stringToBase64(string: string, options?: {urlSafe: boolean}): stringEncode a string to a Base64-encoded string.
Specify {urlSafe: true} to get a Base64URL -encoded string.
Replacement for Buffer.from('Hello').toString('base64') and btoa() .
import {stringToBase64} from 'uint8array-extras' ;
console. log ( stringToBase64 ( 'Hello' ));
//=> 'SGVsbG8=' copy
base64ToString(base64String: string): stringDecode a Base64-encoded or Base64URL -encoded string to a string.
Accepts Base64URL with or without padding.
Replacement for Buffer.from('SGVsbG8=', 'base64').toString() and atob() .
import {base64ToString} from 'uint8array-extras' ;
console. log ( base64ToString ( 'SGVsbG8=' ));
//=> 'Hello' copy
uint8ArrayToHex(array: Uint8Array | ArrayBuffer): stringConvert a Uint8Array to a Hex string.
import {uint8ArrayToHex} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 72 , 101 , 108 , 108 , 111 ]);
console. log ( uint8ArrayToHex (byteArray));
//=> '48656c6c6f' copy
hexToUint8Array(hexString: string): Uint8ArrayConvert a Hex string to a Uint8Array.
import {hexToUint8Array} from 'uint8array-extras' ;
console. log ( hexToUint8Array ( '48656c6c6f' ));
//=> Uint8Array [72, 101, 108, 108, 111] copy
getUintBE(view: DataView): numberRead DataView#byteLength number of bytes from the given view, up to 48-bit.
import {getUintBE} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xab ]);
console. log ( getUintBE ( new DataView (byteArray.buffer)));
//=> 20015998341291 copy
indexOf(array: Uint8Array | ArrayBuffer, value: Uint8Array | ArrayBuffer): numberFind the index of the first occurrence of the given sequence of bytes (value) within the given Uint8Array (array).
Replacement for Buffer#indexOf . Uint8Array#indexOf only takes a number which is different from Buffer's indexOf implementation.
import {indexOf} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xab , 0xcd , 0xef ]);
console. log ( indexOf (byteArray, new Uint8Array ([ 0x78 , 0x90 ])));
//=> 3 copy
includes(array: Uint8Array | ArrayBuffer, value: Uint8Array | ArrayBuffer): booleanChecks if the given sequence of bytes (value) is within the given Uint8Array (array).
Returns true if the value is included, otherwise false.
Replacement for Buffer#includes . Uint8Array#includes only takes a number which is different from Buffer's includes implementation.
import {includes} from 'uint8array-extras' ;
const byteArray = new Uint8Array ([ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xab , 0xcd , 0xef ]);
console. log ( includes (byteArray, new Uint8Array ([ 0x78 , 0x90 ])));
//=> true copy