segment-binary
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

segment-binary

Split and Join binary data into their individual segments.

Installation

npm install segment-binary buffer --save

or using yarn

yarn add segment-binary buffer

Usage

const { segmentBinary, desegmentBinary } = require("segment-binary");

// segment blocks of data

const segmentedBuf = Buffer.concat([
  segmentBinary(Buffer.from("first data block", "utf8")),
  segmentBinary(Buffer.from("second data block", "utf8")),
  segmentBinary(Buffer.from("third data block", "utf8")),
  Buffer.from("unsegmented data block", "utf8"),
]);

// desegment buffer to blocks of data

const { blocks, remainder } = desegmentBinary(segmentedBuf);

/**
 * output will be:
 * [
 *  'first data block',
 *  'second data block',
 *  'third data block'
 * ]
 */
console.log("blocks:", blocks.map(v => v.toString('utf8')));

// output will be: "unsegmented data block"
console.log("remainder:", remainder.toString('utf8'));

Algorithms

in order to stream the data back in the same order they were written, each written data are transformed into three boundaries which size_bytes, data_bytes and data.

  • size_bytes: this consumes one byte and it contains the bytes size of the data_bytes as an unsigned big-endian number.
  • data_bytes: this dynamically consumes around 1 - 8 bytes that stores the bytes size of written data as an unsigned big-endian number.
  • data: the data that was written

License

MIT


Readme

Keywords

Package Sidebar

Install

npm i segment-binary

Weekly Downloads

7

Version

1.0.3

License

MIT

Unpacked Size

7.04 kB

Total Files

6

Last publish

Collaborators

  • deflexable