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

binary-message-parser

v0.0.1

Published

Puts together size-prefixed binary messages from received bytes. Works with pipes.

Downloads

5

Readme

Binary Message Parser

Puts together size-prefixed binary messages from received bytes. Works with pipes.

Example

const fs     = require('fs');
const Parser = require('binary-message-parser');

const rawData = Buffer.from([0, 0, 0, 8, 0, 0, 0, 5, 0, 0, 0, 8, 0, 1, 0, 0]);

// parser requires header extractor object which determines sizes of messages
// from their header bytes
const parser = new Parser(Parser.HeaderExtractorInt32BE);

// message is a Buffer containing all of the message bytes including header
parser.on('message', message => {
	console.log('message size:', message.readInt32BE(0),
	            'data:', message.readInt32BE(4));
});

parser.on('error', (err, args) => {
	console.log('error:', err, args);
});

// give data to the parser manually
parser.parseBytes(rawData);

// create file and pipe its contents to the parser
const filePath = './test.dat';
fs.writeFileSync(filePath, rawData);
fs.createReadStream(filePath)
	.pipe(parser)
	.on('finish', () => { fs.unlinkSync(filePath); });

Expected output:

message size: 8 data: 5
message size: 8 data: 65536
message size: 8 data: 5
message size: 8 data: 65536

Header Extractor

Header extractor is an object responsible for determining size of the message based on given header bytes.

Header extractors for some POD data types are available:

const Parser = require('binary-message-parser');

Parser.HeaderExtractorInt8
Parser.HeaderExtractorUInt8
Parser.HeaderExtractorInt16LE
Parser.HeaderExtractorUInt16LE
Parser.HeaderExtractorInt16BE
Parser.HeaderExtractorUInt16BE
Parser.HeaderExtractorInt32LE
Parser.HeaderExtractorUInt32LE
Parser.HeaderExtractorInt32BE
Parser.HeaderExtractorUInt32BE

Example of custom header extractor:

var customHeaderExtractor = {
	// number indicating number of bytes header consists of
	headerByteCount     : 6,

	/*
	 * this method returns number of bytes message consists of (including
	 * header bytes)
	 * @param {Buffer} buffer contains header bytes
	 */
	extractMessageSize  : buffer => {
		return buffer.readInt16BE(0) + buffer.readInt32BE(2);
	},

	/*
	 * this method is optional, by default always returns true
	 * returns true if message size is in valid range, false otherwise
	 * when false is returned 'error' event is emitted and parser object is
	 * shutdown (no longer usable).
	 * @param {Number} message size
	 */
	validateMessageSize : size => {
		return size >= headerByteCount;
	},
}

Tests

mocha