Giter Club home page Giter Club logo

msgpack5's Introduction

msgpack5  Build Status

A msgpack v5 implementation for node.js, with extension point support.

Install

npm install msgpack5 --save

Usage

var msgpack = require('msgpack5')() // namespace our extensions
  , a       = new MyType(2, 'a')
  , encode  = msgpack.encode
  , decode  = msgpack.decode

msgpack.register(0x42, MyType, mytipeEncode, mytipeDecode)

console.log(encode({ 'hello': 'world' }).toString('hex'))
// 81a568656c6c6fa5776f726c64
console.log(decode(encode({ 'hello': 'world' })))
// { hello: 'world' }
console.log(encode(a).toString('hex'))
// d5426161
console.log(decode(encode(a)) instanceof MyType)
// true
console.log(decode(encode(a)))
// { value: 'a', size: 2 }

function MyType(size, value) {
  this.value = value
  this.size  = size
}

function mytipeEncode(obj) {
  var buf = new Buffer(obj.size)
  buf.fill(obj.value)
  return buf
}

function mytipeDecode(data) {
  var result = new MyType(data.length, data.toString('utf8', 0, 1))
    , i

  for (i = 0; i < data.length; i++) {
    if (data.readUInt8(0) != data.readUInt8(i)) {
      throw new Error('should all be the same')
    }
  }

  return result
}

API

API


msgpack()

Creates a new instance on which you can register new types for being encoded.


encode(object)

Encodes object in msgpack, returns a bl.


decode(buf)

Decodes buf from in msgpack. buf can be a Buffer or a bl instance.


registerEncoder(check(obj), encode(obj))

Register a new custom object type for being automatically encoded. The arguments are:

  • check, a function that will be called to check if the passed object should be encoded with the encode function
  • encode, a function that will be called to encode an object in binary form; this function must return a Buffer which include the same type for registerDecoder.

registerDecoder(type, decode(buf))

Register a new custom objet type for being automatically decoded. The arguments are:

  • type, is a greater than zero integer identificating the type once serialized
  • decode, a function that will be called to decode the object from the passed Buffer

register(type, constructor, encode(obj), decode(buf))

Register a new custom objet type for being automatically encoded and decoded. The arguments are:

  • type, is a greater than zero integer identificating the type once serialized
  • constructor, the function that will be used to match the objects with instanceof
  • encode, a function that will be called to encode an object in binary form; this function must return a Buffer that can be deserialized by the decode function
  • decode, a function that will be called to decode the object from the passed Buffer

This is just a commodity that calls registerEncoder and registerDecoder internally.


encoder(opts)

Builds a stream in object mode that encodes msgpack. By default it writes an 4 byte length header containing the message length as a UInt32BE. This header can be disabled by passing { header: false } as an option.


decoder(opts)

Builds a stream in object mode that decodes msgpack. By default it expects msgpack to have a 4 byte length header containing the packaged length as a UInt32BE. This header can be disabled by passing { header: false } as an option.

Disclaimer

This library is built fully on JS and on bl to simplify the code. Every improvement that keeps the same API is welcome.

Acknowledgements

This project was kindly sponsored by nearForm.

This library was originally built as the data format for JSChan.

License

MIT

msgpack5's People

Contributors

mcollina avatar fritzy avatar pgte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.