2.4.8 • Published 1 year ago

uint8arraylist v2.4.8

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
1 year 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.

@infinitebrahmanuniverse/nolb-uin@everything-registry/sub-chunk-3007ltnfue-w3ipfs-sdkstarpc@canvas-js/core@canvas-js/gossiplog@canvas-js/libp2p-okra-service@canvas-js/libp2p-rendezvousjs-ds-crdtjs-wakuipnsipfs-bitswapipfs-unixfsipfs-unixfs-importerit-handshakeit-batched-bytesit-blockit-byte-streamit-concatit-readerit-rpcit-splitit-tarit-ndjsonit-pb-streamit-protobuf-streamit-length-prefixedit-length-prefixed-streamdenetwork-gossipsub0ba9f-ipfs-sdk@bpx-chain/synapse-coreuint8arraylist-accessoruint8-varint@canvas-js/pubsub-service-discovery@canvas-js/store@chainsafe/libp2p-identify@chainsafe/libp2p-noise@chainsafe/libp2p-yamux@chainsafe/libp2p-gossipsub0o6pfj2-ipfs-sdk@dcdn/graphsync@dao-xyz/libp2p-noise@dao-xyz/libp2p-webrtczkitter-js@helia/rpc-protocol@helia/bitswap@hashprotocol/libp2p-noise@futureverse/sylo-protocol-sdk@individe/libp2p-snappy-muxer@ladans-io/libp2p-interface@lodestar/beacon-node@lodestar/reqresp@libp2p/crypto@libp2p/daemon-protocol@libp2p/dcutr@libp2p/fetch@libp2p/identify@libp2p/interface@libp2p/interface-compliance-tests@libp2p/autonat@libp2p/interface-connection@libp2p/interface-internal@libp2p/interface-mocks@libp2p/interface-pubsub@libp2p/interface-record@libp2p/floodsub@libp2p/http-fetch@libp2p/circuit-relay-v2@libp2p/utils@libp2p/webrtc@libp2p/webtransport@libp2p/peer-id-factory@libp2p/peer-record@libp2p/peer-store@libp2p/multistream-select@libp2p/noise@libp2p/plaintext@libp2p/pnet@libp2p/prometheus-metrics@libp2p/protocol-adventure@libp2p/tls@libp2p/interface-stream-muxer@libp2p/interface-stream-muxer-compliance-tests@libp2p/interop@libp2p/kad-dht@libp2p/pubsub@libp2p/pubsub-peer-discovery@libp2p/record@libp2p/rsa@libp2p/mplex@marcopolo_/libp2p-fetch@savid/libp2p-pestmultipart-byte-rangemtcrawleeprotons-runtimelibp2p-mplexlongbits@waku/noise@waku/core@web3-storage/multistream-select
2.4.8

1 year ago

2.4.5

2 years ago

2.4.4

2 years ago

2.4.7

2 years ago

2.4.6

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.4.3

2 years ago

2.4.2

3 years ago

2.3.3

3 years ago

1.5.2

3 years ago

1.6.0

3 years ago

2.3.0

3 years ago

2.1.2

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.0.0

3 years ago