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()
list.consume(3)
list.subarray()
for (const buf of list) {
}
list.subarray(0, 1)
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)
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)
list.subarray(2, 5)
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)
list.sublist(2, 5)
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.