# length-prefixed-buffers

> encode and decode an array of buffers as a single varint length-prefixed binary blob

Latest version **1.1.1** (published 2022-06-18) · bsd license · 0 weekly downloads

## Install

```sh
npm install length-prefixed-buffers
pnpm add length-prefixed-buffers
yarn add length-prefixed-buffers
bun add length-prefixed-buffers
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-06-18 |
| First published | 2021-03-24 |
| Weekly downloads | 0 |
| License | bsd |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | substack |
| Keywords | buffer, uint8array, array, varint, length-prefixed |

## Links

- npm: https://www.npmjs.com/package/length-prefixed-buffers
- npm.io page: https://npm.io/package/length-prefixed-buffers

## Dependencies (1)

- [varint](https://npm.io/package/varint.md) ^6.0.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-06-18
- 1.1.0 — 2021-11-10
- 1.0.0 — 2021-03-24

## README

# length-prefixed-buffers

encode and decode an array of buffers as a single [varint][] length-prefixed binary blob

* for encoding, turns an array of buffers into a single buffer
* for decoding, turns a single buffer into an array of buffers

Works in nodejs and the browser and does not pull in a `Buffer` implementation when compiled for the
browser:

```
$ browserify example/u8.js | wc -c
4815
$ browserify -p tinyify example/u8.js | gzip | wc -c
756
```

[varint]: https://github.com/chrisdickinson/varint

# example

use nodejs buffers:

``` js
var lpb = require('length-prefixed-buffers')

var buffers = [
  Buffer.from('abc'),
  Buffer.from('defgh'),
  Buffer.from('ijk')
]
var encoded = Buffer.alloc(lpb.length(buffers))
lpb.encode(encoded, buffers)

console.log('encoded:', encoded)
console.log('decoded:', lpb.decode(encoded))
```

or Uint8arrays:

``` js
var lpb = require('length-prefixed-buffers')

var buffers = [
  Uint8Array.from([97,98,99]),
  Uint8Array.from([100,101,102,103,104]),
  Uint8Array.from([105,106,107])
]
var encoded = new Uint8Array(lpb.length(buffers))
lpb.encode(encoded, buffers)

console.log('encoded:', encoded)
console.log('decoded:', lpb.decode(encoded))
```

or use `from()` which takes an array and constructs a collection of the appropriate type based on
the first buffer in the array:

``` js
var lpb = require('length-prefixed-buffers')

var encoded = lpb.from([
  Uint8Array.from([97,98,99]),
  Uint8Array.from([100,101,102,103,104]),
  Uint8Array.from([105,106,107])
])
console.log('encoded:', encoded) // encoded is a Uint8Array
console.log('decoded:', lpb.decode(encoded))
```

In this example `encoded` is a `Uint8Array` but if `from()` were passed an array of `Buffer`s then
`encoded` would be a `Buffer`.

# api

``` js
var { encode, decode, length, from } = require('length-prefixed-buffers')
var encode = require('length-prefixed-buffers/encode')
var decode = require('length-prefixed-buffers/decode')
var length = require('length-prefixed-buffers/length')
var from = require('length-prefixed-buffers/from')
```

Each of these except `from()` has a variant where the initial count of buffers is not included:

```
var { encode, decode, length, from } = require('length-prefixed-buffers/without-count')
var encode = require('length-prefixed-buffers/without-count/encode')
var decode = require('length-prefixed-buffers/without-count/decode')
var length = require('length-prefixed-buffers/without-count/length')
```

## encode(out, buffers, offset=0)

Write the data from `buffers`, an array of `Buffer`s or `Uint8Array`s into `out` starting at
the index `offset`. Returns `out`.

The number of bytes written is stored in `encode.bytes` (similar to the [varint][] api).

## var buffers = decode(src, offset=0)

Reconstruct an array of `buffers` from `src`, a `Buffer` or `Uint8Array` starting at `offset`.

The `buffers` are constructed using `subarray()` so if you mutate them you will mutate `src`.

The types of `buffers` are the same as the type of `src`.

The number of bytes read is stored in `decode.bytes` (similar to the [varint][] api).

## var nbytes = length(buffers)

Return the number of bytes `nbytes` that are required to store the array `buffers`.

## var encoded = from(buffers)

Allocate a buffer `encoded` for an array of `buffers`.

This is a convenience method that uses `length` and `encode()` to construct an encoded buffer.

# install

```
npm install length-prefixed-buffers
```

# license

bsd

---
_Source: https://npm.io/package/length-prefixed-buffers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
