Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

buffer-utils

lucastan39deprecated1.1.0

this package has been deprecated

Stream and buffer utils.

buffer, data reader, data writer, buffer streams

readme

NPM version Build Status Coveralls Status Known Vulnerabilities Downloads

Buffer utils that allow you to read and write bytes, strings, ints and floats sequentially.

BufferWriter

var BufferWriter = require('buffer-utils').BufferWriter;

var bw = new BufferWriter();

// Writes sequentially.
bw.writeInt8(0x12)
  .writeInt16LE(0x1234); // chaining allowed.

// Writes a buffer
bw.writeBytes(someBuffer);

// Writes a string
bw.writeString("hello world", "utf8");

// Gets the number of bytes written.
var bytesWritten = bw.size();

// Gets the contents and resets to empty.
var contents = bw.getContents();

// now bw.size() === 0

BufferReader

var BufferReader = require('buffer-utils').BufferReader;

// Wrapper over the buffer that keeps track of the current offset.
var br = new BufferReader(contents);

// Read integers sequentially.
// Supports all readInt*, readUInt*, readFloat*, readDouble* from Buffer.
var i8 = br.readInt8();     // 0x12
var i16 = br.readInt16LE(); // 0x1234

// Reads a chunk of bytes.
// Modifying `buf` will modify `contents` since it is a `slice` of
// `contents`.
var buf = br.readBytes();

// Reads a string
var str = br.readString('utf8'); // hello world

// Gets the number of bytes left.
var bytesLeft = br.bytesLeft(); // 0

RetainedBuffer

This is useful for reading a stream of data that is non-seekable and you do not want to store the entire stream in memory (e.g., when the data is huge). This util always retains the last X number of bytes read as you feed data into it, useful for reading variable-sized data followed by a fix-sized footer structure.

var retainedBuf = new RetainedBuffer(16); // retain 16 bytes

file.on('data', function(incomingChunk){
    // `chunks` is an array of Buffer's. Could be empty if all bytes read are
    // retained.
    var chunks = retainedBuf.read(incomingChunk);

    chunks.forEach(function(chunk){
        // do something with `chunk`...
        // the individual chunks will ultimately piece up to the
        // data read, minus the retained last 16 bytes.
    });
})
.on('end', function(){
    // `retained` is a buffer of 16 bytes if we have read at least 16 bytes
    // so far.
    var retained = retainedBuf.getRetained();
});