2.4.8 • Published 4 months ago

uint8arraylist v2.4.8

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
4 months ago

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.

@lodestar/beacon-node@infinitebrahmanuniverse/nolb-uinlibp2p@everything-registry/sub-chunk-3007ltnfue-w3ipfs-sdkjs-wakuit-splitit-tarit-pb-streamit-protobuf-streamit-readeripnslibp2p-mplexlongbitsit-batched-bytesit-blockit-byte-streamit-concatit-length-prefixedit-length-prefixed-streamit-handshakeipfs-bitswapipfs-unixfsipfs-unixfs-importermtcrawleeprotons-runtimeuint8-varintuint8arraylist-accessorstarpczkitter-js@web3-storage/multistream-select@canvas-js/store@tracelabs/kad-dht@waku/core@waku/noiseaioz-w3ipfs-sdk0o6pfj2-ipfs-sdk0ba9f-ipfs-sdk@savid/libp2p-pest@dcdn/graphsync@futureverse/sylo-protocol-sdkcarstreamdenetwork-gossipsub@chainsafe/libp2p-gossipsub@chainsafe/libp2p-identify@chainsafe/libp2p-noise@chainsafe/libp2p-yamux@helia/rpc-protocol@helia/utils@helia/bitswap@hashprotocol/libp2p-noisebyte-access@canvas-js/gossiplog@canvas-js/libp2p-okra-service@canvas-js/pubsub-service-discovery@canvas-js/core@dao-xyz/libp2p-noise@dao-xyz/libp2p-webrtc@individe/libp2p-snappy-muxer@libp2p/peer-id-factory@libp2p/peer-record@libp2p/peer-store@libp2p/plaintext@libp2p/pnet@libp2p/prometheus-metrics@libp2p/pubsub@libp2p/pubsub-peer-discovery@libp2p/record@libp2p/rsa@libp2p/multistream-select@libp2p/noise@libp2p/kad-dht@libp2p/floodsub@libp2p/identify@libp2p/interface@libp2p/interface-compliance-tests@libp2p/interface-connection@libp2p/interface-internal@libp2p/interface-mocks@libp2p/mplex@libp2p/interface-pubsub@libp2p/interface-record@libp2p/interface-stream-muxer@libp2p/interface-stream-muxer-compliance-tests@libp2p/interop@libp2p/crypto@libp2p/daemon-protocol@libp2p/dcutr@libp2p/fetch@libp2p/autonat@libp2p/circuit-relay-v2@libp2p/utils@libp2p/webrtc@libp2p/webtransport@libp2p/tls@lodestar/reqresp@marcopolo_/libp2p-fetch
2.4.8

4 months ago

2.4.5

5 months ago

2.4.4

5 months ago

2.4.7

5 months ago

2.4.6

5 months ago

2.4.1

1 year ago

2.4.0

1 year ago

2.4.3

1 year ago

2.4.2

1 year ago

2.3.3

2 years ago

1.5.2

2 years ago

1.6.0

2 years ago

2.3.0

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago