Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

uint8arraylist

achingbrain331.1kApache-2.0 OR MIT2.4.8TypeScript support: included

Append and consume bytes using only no-copy operations

readme

codecov CI

Append and consume bytes using only no-copy operations

About

A class that lets you do operations over a list of Uint8Arrays without copying them.

import { Uint8ArrayList } from 'uint8arraylist'

const list = new Uint8ArrayList()
list.append(Uint8Array.from([0, 1, 2]))
list.append(Uint8Array.from([3, 4, 5]))

list.subarray()
// -> Uint8Array([0, 1, 2, 3, 4, 5])

list.consume(3)
list.subarray()
// -> Uint8Array([3, 4, 5])

// you can also iterate over the list
for (const buf of list) {
  // ..do something with `buf`
}

list.subarray(0, 1)
// -> Uint8Array([0])

Converting Uint8ArrayLists to Uint8Arrays

There are two ways to turn a Uint8ArrayList into a Uint8Array - .slice and .subarray and one way to turn a Uint8ArrayList into a Uint8ArrayList with different contents - .sublist.

slice

Slice follows the same semantics as Uint8Array.slice in that it creates a new Uint8Array and copies bytes into it using an optional offset & length.

const list = new Uint8ArrayList()
list.append(Uint8Array.from([0, 1, 2]))
list.append(Uint8Array.from([3, 4, 5]))

list.slice(0, 1)
// -> Uint8Array([0])

subarray

Subarray attempts to follow the same semantics as Uint8Array.subarray with one important different - this is a no-copy operation, unless the requested bytes span two internal buffers in which case it is a copy operation.

const list = new Uint8ArrayList()
list.append(Uint8Array.from([0, 1, 2]))
list.append(Uint8Array.from([3, 4, 5]))

list.subarray(0, 1)
// -> Uint8Array([0]) - no-copy

list.subarray(2, 5)
// -> Uint8Array([2, 3, 4]) - copy

sublist

Sublist creates and returns a new Uint8ArrayList that shares the underlying buffers with the original so is always a no-copy operation.

const list = new Uint8ArrayList()
list.append(Uint8Array.from([0, 1, 2]))
list.append(Uint8Array.from([3, 4, 5]))

list.sublist(0, 1)
// -> Uint8ArrayList([0]) - no-copy

list.sublist(2, 5)
// -> Uint8ArrayList([2], [3, 4]) - no-copy

Inspiration

Borrows liberally from bl but only uses native JS types.

Install

$ npm i uint8arraylist

Browser <script> tag

Loading this module through a script tag will make it's exports available as Uint8arraylist in the global namespace.

<script src="https://unpkg.com/uint8arraylist/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

changelog

2.4.8 (2023-12-30)

Trivial Changes

Dependencies

  • bump uint8arrays from 4.0.10 to 5.0.1 (#67) (a18ad9a)

2.4.7 (2023-11-24)

Bug Fixes

  • skip array initialisation in sublist method (#65) (23836ac)

2.4.6 (2023-11-24)

Dependencies

2.4.5 (2023-11-24)

2.4.4 (2023-11-24)

Documentation

2.4.3 (2022-12-16)

Documentation

2.4.2 (2022-11-24)

Bug Fixes

2.4.1 (2022-11-24)

Dependencies

  • dev: bump bl from 5.1.0 to 6.0.0 (#46) (61b168b)

Trivial Changes

2.4.0 (2022-11-24)

Features

  • add .indexOf method to search for sub-arrays (f3ebef1)

Dependencies

  • dev: bump it-all from 1.0.6 to 2.0.0 (#45) (dee7c47)

2.3.3 (2022-10-12)

Dependencies

  • bump uint8arrays from 3.x.x to 4.x.x (#43) (c47b4de)

Trivial Changes

2.3.2 (2022-08-06)

Bug Fixes

  • return copy of internal buffer list (#41) (1a3cb65)

2.3.1 (2022-08-05)

Documentation

  • remove reference to toUint8Array (#40) (3bed0bb)

2.3.0 (2022-08-05)

Features

  • add fromUint8Arrays factory method (#39) (b970bdc)

2.2.0 (2022-08-04)

Features

2.1.3 (2022-08-04)

Bug Fixes

2.1.2 (2022-08-04)

Trivial Changes

2.1.1 (2022-08-03)

Trivial Changes

2.1.0 (2022-08-03)

Features

2.0.0 (2022-07-28)

⚠ BREAKING CHANGES

  • subarray now returns a Uint8Array - use sublist if you need a Uint8ArrayList

Features

  • add sublist method and change subarray return type (#30) (07e044d)

1.6.0 (2022-07-28)

Features

  • support negative indices in slice/subarray (#29) (7d08eef)

Dependencies

1.5.2 (2022-07-26)

Trivial Changes

1.5.1 (2022-03-29)

Bug Fixes

  • update return type - use bigint instead of BigInt (#7) (0281a17)

1.5.0 (2022-03-21)

Features

  • detect uint8arraylists when appending or writing (#6) (db9a8ef)

1.4.0 (2022-03-08)

Features

1.3.0 (2022-03-08)

Features

1.2.0 (2022-02-05)

Features

  • add appendAll and subarray methods and byteLength property (#2) (1db968b)

1.1.0 (2022-02-04)

Features

  • append multiple and make list iterable (#1) (70d7542)

1.0.0 (2022-02-03)

Trivial Changes