npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

length-prefixed-buffers

v1.1.1

Published

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

Downloads

17

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

example

use nodejs buffers:

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:

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:

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 Buffers then encoded would be a Buffer.

api

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 Buffers or Uint8Arrays 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