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

binser

v0.9.8

Published

A binary serializer and deserializer with tools to read and write objects to buffers.

Downloads

16

Readme

Binser - Binary Serializer

Build Status

Binser uses its own binary protocol to serialize nested object structures to binary stream. Some features:

  • Primitives such as Int8, UInt8 ... Int32, UInt32;
  • CompactNumber minimizes size of numbers;
  • String type that takes care of string transmission by pre-pending string size;
  • Custom object type for your own objects;
  • Arrays;
  • Unlimited mixing and nesting of all available type combinations;
  • Documented protocol: look at /doc.

Stable usage is documented bellow.

Serialize/deserialize simple object using ObjectType

Require this:

// Require "types" object that contains built-in object serializers.
var types = require('binser').types;
// Require "ObjectType" that can be used to make a template for your object.
var ObjectType = require('binser').ObjectType;

Create your type serializer:

// Create your type that is composed from other types.
var MyType = new ObjectType(
    {
        name: types.String,
        age: types.UInt8
    }
);

Serialize your hash to buffer using your type serializer:

var buffer = MyType.serialize({ name: "John", age: 12 });

Deserialize it back:

var hash = MyType.deserialize(buffer);

Serialize/deserialize built-in object

Any built-in type serializer can be used the same way as ObjectType. For example, serializing and deserializing a string:

var buffer = types.String.serialize("Hello World!");
var str = types.String.deserialize(buffer);

Serialize/deserialize an array of objects using ArrayType

ArrayType can be used for serializing arrays of any type of objects, here is an example of string array:

var ArrayType = require('binser').ArrayType;

var stringArrayType = new ArrayType(types.String);

var buffer = stringArrayType.serialize([ "Hello", " ", "World!" ]);
var stringArray = stringArrayType.deserialize(buffer);

ObjectType can also be used:

var ArrayType = require('binser').ArrayType;
var ObjectType = require('binser').ObjectType;

var pointType = new ObjectType({
    x: types.CompactNumber,
    y: types.CompactNumber,
});
var pointsArrayType = new ArrayType(pointType);

var buffer = pointsArrayType.serialize([ { x: 15, y: -10 }, { x: 0, y: 62111 } ]);
var pointsArray = pointsArrayType.deserialize(buffer);

And any other nested combination.

The list of built-in serializers so far:

Value | Size ------------------- | --------- types.Int8 | 8-bit signed integer. types.UInt8 | 8-bit unsigned integer. types.Int16 | 16-bit signed integer. types.UInt16 | 16-bit unsigned integer. types.Int32 | 32-bit signed integer. types.UInt32 | 32-bit unsigned integer. types.CompactNumber | Integer takes from 1 to 5 bytes, depending on value size. types.String | Variable - length string. ObjectType(config) | A sequence of other objects. ArrayType() | A varied sequence of other objects.

No floating point numbers (yet).

Pass serialized objects over streams similar to TCP

Included Reader helper can help deserializing data transmitted over protocol such as TCP. It will automatically call onRead callback only when full object is available.

Writing to stream is easy:

// Send the buffer over your connection (for example, Node's TCP client):
client.write(buffer);

Reading is a bit more involved:

// Require "Reader" that deals with variable-length chunks in network. 
var Reader = require('binser').Reader;

// <...>

// Create one reader per connection:
var reader = new Reader();
reader.Type = types.String; // Or any other type serializer.
reader.onRead = function(obj) { // Set callback.
    console.log(obj);
};

// <...>

// On your "data" event, feed the buffer bytes into your reader:
reader.feed(buffer);