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

buffercodec

v2.1.3

Published

Buffer codec able to encode or decode primary data types.

Downloads

30

Readme

Buffer Codec

BufferCodec is a lightweight low-level library that allows you to efficiently and easily translate between JSON and buffers by chaining together calls to write basic data types to buffers and other way around. It uses Typed Arrays which makes this package readily available for both, browsers and Node environments.

Build Status

Installation

You may install the package via:

  • npm npm install buffercodec
  • bower bower install buffercodec
  • git git clone https://github.com/emmorts/buffercodec

Documentation is available here

Quick start

Encoding to buffer is as simple as this:

import { BufferCodec } from "buffercodec";

const buffer = new BufferCodec()
  .uint8(0x1)
  .string('hello world')
  .uint16(Math.pow(2, 10))
  .uint16(Math.pow(2, 8))
  .float32(Math.PI)
  .result();

Decoding above buffer to a single object:

import { BufferCodec } from "buffercodec";

const object = BufferCodec
  .from(buffer)
  .parse({
    opcode: 'uint8',
    name: 'string',
    posX: 'uint16',
    posY: 'uint16',
    pi: 'float32'
  });

/*
object: {
  opcode: 0x1,
  name: 'hello world',
  posX: 1024,
  posY: 256,
  pi: 3.1415927410125732
}
*/

Top-level arrays are also supported, by providing the length of an array before its' items:

import { BufferCodec } from "buffercodec";

const length = 5;
const buffer = new BufferCodec().uint8(length);

for (let i = 1; i < length + 1; i++) {
  buffer
    .uint8(i)
    .uint16(i * i);
}

const result = BufferCodec
  .from(buffer.result())
  .parse([{
    id: 'uint8',
    value: 'uint16'
  }]);

/*
result: [
  {id: 1, value: 1},
  {id: 2, value: 4},
  {id: 3, value: 9},
  {id: 4, value: 16},
  {id: 5, value: 25}
]
*/

Types

BufferCodec supports the following types out of the box:

  • int8
  • uint8
  • int16 (littleEndian)
  • uint16 (littleEndian)
  • int32 (littleEndian)
  • uint32 (littleEndian)
  • float32 (littleEndian)
  • float64 (littleEndian)
  • string (utf8/utf16)

The properties in parentheses can be used in a template like so:

const result = BufferCodec
  .from(buffer)
  .parse({
    id: 'int16|littleEndian',
    value: 'float32',
    label: 'string|utf8'
  });

Types can be wrapped in an array to indicate that an array is expected for the property:

const result = BufferCodec
  .from(buffer)
  .parse({
    x: ['int16'],
    y: ['int16']
  });

Note: when encoding arrays using BufferCodec, you need to supply length encoded in a uint8 value before encoding the objects. It is therefore recommended to use BufferSchema, which does this for you.

Nested objects can also be used:

const result = BufferCodec
  .from(buffer)
  .parse({
    foo: {
      bar: {
        baz: ['uint8']
      }
    },
  });

Re-usable templates

Package includes class BufferSchema which allows you to define a schema per type and re-use it.

const pointSchema = new BufferSchema({
  x: 'float32',
  y: 'float32'
});

const buffer = pointSchema.encode({
  x: Math.PI,
  y: Math.PI
});

const point = pointSchema.decode(buffer);

Templates also support nullable types. You may also append a question mark to the type to indicate that the value is nullable, thus preserving space on the buffer in such event:

const result = new BufferSchema({
    id: 'int16|littleEndian',
    value: 'float32?',
    label: 'string?|utf8'
  });

Custom types

You can also add your custom strategy for encoding and decoding objects.

First, create a new strategy class implementing StrategyBase:

interface Point {
  x: number,
  y: number
}

class PointStrategy implements StrategyBase<Point> {

  supports(template: BufferValueTemplate): boolean {
    return typeof(template) === 'string' && template === 'point';
  }

  encode(point: Point, template: BufferValueTemplate, codec: BufferCodec) {
    codec.float32(point.x);
    codec.float32(point.y);
  }

  decode(template: BufferValueTemplate, codec: BufferCodec): Point {
    return {
      x: codec.decode({ type: 'float32' }),
      y: codec.decode({ type: 'float32' })
    }
  }
  
}

Add the strategy:

BufferStrategy.add(PointStrategy);

Now whenever you reference your type in BufferSchema, your provided strategy will be used.

const playerSchema = new BufferSchema({
  id: 'uint32',
  name: 'string',
  position: 'point'
});

playerSchema.encode({
  id: 42,
  name: 'AzureDiamond',
  position: {
    x: Math.PI,
    y: Math.PI
  }
});