0.7.6 • Published 6 years ago

msgpuck v0.7.6

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

msgpuck

npm GitHub license Build Status Coverage Status

A fast and memory-efficient MessagePack serialization library.

Features

  • Fully compliant with the latest MessagePack specification
  • Supports low-level methods
  • Supports safe/big 64-bit integers handling
  • Supports custom Extension Types (Serialization Codecs)
  • Fully tested, including V8 JIT optimizations

Future works (not implemented yet)

  • Decoder low-level methods
  • Zero-copy stream handler
  • Web browsers, WebAssembly, Bundler plugins

Installation

# npm
npm install msgpuck

# yarn
yarn add msgpuck

Usage

Encoding

To encode values you can either use an instance of Encoder:

const { Encoder } = require('msgpuck');

const encoder = new Encoder();

...

encoder.encode(value); // string(encoded values)

A list of all low-level encoder methods:

encoder.encodeNil();                    // MP nil
encoder.encodeBool(true);               // MP bool
encoder.encodeInt(42);                  // MP int
encoder.encodeBigInt(42n);              // MP int64
encoder.encodeFloat(Math.PI);           // MP float
encoder.encodeStr('foo');               // MP str
encoder.encodeBin(Buffer.from([0x2a])); // MP bin
encoder.encodeArray([1, 2]);            // MP array
encoder.encodeObject({ key: 'value' }); // MP map
encoder.encodeMap(new Map([[1, 2]]);    // MP map
encoder.encodeExt(1, '\x2a');           // MP ext

Encoding options

The Encoder object supports options for fine-tuning the encoding process (defaults are in bold):

NameDescription
float: '64'Forces floats to be encoded as 64-bits MessagePack floats
float: '32'Forces floats to be encoded as 32-bits MessagePack floats
float: 'auto'Detects MessagePack floats type automatically
bufferMinLen: 15The minimum length of the string to use the Buffer
codecs: falseAn array of codecs

Decoding

To decode data (buffer) you can either use an instance of Decoder:

const { Decoder } = require('msgpuck');

const decoder = new Decoder();

...

decoder.decode(buffer);

Extensions

To define application-specific types use the Ext class:

const { Encoder, Decoder, Ext } = require('msgpuck');

const encoded = new Encoder().encode(Ext.make(42, '\x2a'));

const buffer = Buffer.from(encoded, 'binary');
const ext = new Decoder().decode(buffer);

console.log(ext.type === 42);     // bool(true)
console.log(ext.data === '\x2a'); // bool(true)

Tests

Run tests as follows:

npm run test

License

Copyright © 2018-present Alex Masterov <alex.masterow@gmail.com>

msgpuck is licensed under MIT and can be used for any personal or commercial project.

0.7.6

6 years ago

0.7.5

6 years ago

0.7.4

6 years ago

0.7.3

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.7

6 years ago

0.5.6

6 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago