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

binaryparser

v0.2.0

Published

Library for parsing binary data

Downloads

10

Readme

BinaryParser

BinaryParser is a node.js module which facilitates parsing of binary data received from the network or from a local file. BinaryParser lets you define the data format using javascript object notation and allows for parsing of complex binary streams. Callbacks are called whenever a binary field has been received and the data format can be changed on-the-fly.

  1. Include the binary parser

    const bp = require ('binaryparser').BinaryParser;

  2. Create an instance of the parser:

    var parser = new bp.BinaryParser();

  3. Feed the buffers from the network to the binary parser

    socket.on("data", function(data){ parser.addBuffer(data); });

  4. Define a data format and process the parsed data:

    parser.setFormat({ fileSize: bp.dword(function(size){ util.puts("File size is " + size); }),

     fileData: bp.bits('fileSize', function(data){
     	util.puts("File data received: " + data.length + " bytes");
     })

    });

Documentation

Data types

The following data types are supported:

char8: 8 bits unsigned
ushort: 16 bits unsigned
dword: 32 bits unsigned
bits(size): a buffer of size bytes.

Data types are functions inside the BinaryParser namespace and take a callback as parameter:

myField: bp.dword([callback])

Where callback is the function called when the field has been parsed.

The bp.bits type is defined as:

myBits: bp.bits(size, callback)

The size parameter can be a number or a string which references a previous field in current format:

file = {
	dataSize: bp.dword(),
	data: bp.bits('dataSize', function(bits){});
}

parser.setFormat(file);

Processing order

Fields are parsed in the order they are defined in the format. If nested formats are present, parsing is resumed at the next field after the nested format has been consumed. onFinished callback is called for the current format.

Context

Every field callback has the context (this) set to the current format, so fields in current format can be accessed with this.fieldName.

parser.setFormat({
	chunkType: bp.dword(function(value){
		util.puts(value);
		//same as
		util.puts(this.chunkType);
	})
});

Nested formats

You can nest multiple formats and you can call setFormat inside a field callback. After the new format was processed, the next field from the parent format will be the active field.

parser.setFormat({

	header: bp.dword(),

	packet: {
		__repeat: 'forever',
		type: bp.char8(function(type){
			if (type === 0){
				parser.setFormat({
					checksum: bp.dword(function(checksum){
						util.puts('checksum is ' + this.checksum);
					})
				});
			}
		}),
		size: bp.ushort(),
		data: bp.bits('size'),
		onFinished: function(){
			//this refers to current format
			util.puts('packet type is ' + this.type);
			util.puts('packet size is ' + this.size);
			util.puts('packet data is ' + this.data.toString());
		}
	}
});

Special fields

Some field names are reserved and have a special meaning. These are:

formatName: string

Optional name for the format. Useful for debugging.

__repeat: 'forever' or number or reference. if __repeat is a number, the format will be parsed the specified number of times. if __repeat is 'forever', the parser will always repeat this format. if a different string is passed, it specifies a field name which is evaluated when the data is received.

Examples:

//read 4 bytes from the stream and print the values
parser.setFormat({
	__repeat: 4,
	value: char8(function(val){
		util.puts('The byte value is ' + val);
	});
});

//receive files clustered in groups: 
//group1: [nFiles][ file1, file2, file_nFiles], group2: [nFiles][file1, file2, file...], ..., groupn: [nFiles][....],....

parser.setFormat({
	__repeat: 'forever',
	nFiles: bp.char8(),
	files: {
		__repeat: 'nFiles',	//determined automatically 
		size: bp.dword(),
		data: bp.bits('size'),
		onFinished: function(){
			util.puts('File received. ' + this.size + ' bytes');
		}
	}
})

onFinished: callback This callback is called whenever the current format has been processed.

Methods

parser.setByteOrder(order)

order: "le" or "be" Meaning the data has 'little endian' or 'big endian' byte order. Default is 'le'.

parser.addBuffer(buffer)

Feed a data buffer to the parser. The parser will process the data, call the callbacks on the formats specified and stop when it runs out of data or the format is finished. Usually used in a data io callback: socket.on("data"), function(data){ parser.addBuffer(data); });

parser.endStream()

Tells the parser that no more buffers will arrive. Parser will process remaining bytes and when it runs out of data, processing is stopped and onFinished() callback is called on current format.

Debugging

const bp = require('binaryparser');
bp.setDebug(true)

Enables logging of the parser's state. Useful when debugging the data stream.

xxx. Acknowledgements

BinaryParser uses Bufferlist module Copyright 2010 James Halliday ([email protected]). Partly inspired by http://substack.net/posts/cb328d Uses OO - Class - Copyright TJ Holowaychuk [email protected] (MIT Licensed) whish is based on http://ejohn.org/blog/simple-javascript-inheritance/