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

parse-binary-stream

v1.0.1

Published

Parse n-byte chunks from a binary stream

Downloads

13

Readme

parse-binary-stream

build status

Parse n-byte chunks from a binary stream.

Call read(n, callback) and your callback will be call when n bytes of data are available from the stream. Reads can be nested to read a series of values in order.

Example

This example parses a binary message where the first two bytes represent the length of the data in the following bytes.

For example, 04 16 a1 means that there are 4 bytes of data and the data is 16 a1.

var parse = require("parse-binary-stream");

var stream = parse(function(read) {
  // Parse the length value
  read(1, function(data) {
    var length = data.readUInt8(0);
    // Parse the data of size length
    read(length, function(data) {
      console.log(data);
    });
  });
});

stream.end(new Buffer([0, 4, 1, 6, 10, 1]));
// -> logs <Buffer 16 a1>

Looping Example

Sometimes you want to continuously parse messages from an indefinite stream of data. This example parses the same protocol as above, but will parse out as many messages possible until the stream ends.

var through = require("through2");
var parse = require("parse-binary-stream");

var stream = parse(function(read) {
  (function next() {
    parseMessage(function(data) {
      console.log(data);
      next();
    });
  })();

  function parseMessage(callback) {
    // Parse the length value
    read(1, function(data) {
      var length = data.readUInt8(0);
      // Parse the data of size length
      read(length, function(data) {
        callback(data);
      });
    });
  }
});

stream.write(new Buffer([0, 2]));
stream.write(new Buffer([5]));
// logs <Buffer 05>
stream.end(new Buffer([0, 4, 1, 6, 10, 1]));
// logs <Buffer 16 a1>

API

var parse = require("parse-binary-stream");

parse(callback)

Creates a stream that will parse n-length chunks from the binary data written to it.

The callback is called with a read function that is used to parse chunks.

read(n, callback)

Calls the callback with n bytes of data when the data is available. The callback may never be called if enough data never becomes available.

Installation

npm install parse-binary-stream