JSPM

  • Created
  • Published
  • Downloads 907803
  • Score
    100M100P100Q208566F
  • License ISC

MessagePack implementation in JavaScript

Package Exports

  • @msgpack/msgpack

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@msgpack/msgpack) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

MessagePack for JavaScript npm version Build Status

This is the pure-JavaScript implementation of MessagePack, an efficient binary serilization format:

https://msgpack.org/

Stability

This is under development until v1.0.0. Any API will change without notice.

Synopsis

import { deepStrictEqual } from "assert";
import { encode, decode } from "@msgpack/msgpack";

const object = {
  nil: null,
  integer: 1,
  float: Math.PI,
  string: "Hello, world!",
  binary: Uint8Array.from([1, 2, 3]),
  array: [10, 20, 30],
  map: { foo: "bar" },
  timestampExt: new Date(),
};

const encoded = encode(object);
// encoded is an Uint8Array instance

deepStrictEqual(decode(encoded), object);

Install

This library is publised as @msgpack/msgpack in npmjs.com.

npm install @msgpack/msgpack

Encode and Decode

encode(data: unknown, options?): Uint8Array

It encodes data and returns a byte array as Uint8Array.

decode(buffer: ArrayLike<number> | Uint8Array, options?): unknown

It decodes buffer in a byte buffer and returns decoded data as uknown.

decodeAsync(stream: AsyncIterable<ArrayLike<number> | Uint8Array>, options?): Promise<unknown>

It decodes stream in an async iterable of byte arrays and returns decoded data as uknown wrapped in Promise. This function works asyncronously.

Extension Types

To handle MessagePack Extension Types, this library provides ExtensionCodec class.

Here is an example to setup custom extension types that handles Map and Set classes in TypeScript:

import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";

const extensionCodec = new ExtensionCodec();

// Set<T>
extensionCodec.register({
  type: 0,
  encode: (object: unknown): Uint8Array | null => {
    if (object instanceof Set) {
      return encode([...object]);
    } else {
      return null;
    }
  },
  decode: (data: Uint8Array) => {
    const array = decode(data) as Array<any>;
    return new Set(array);
  },
});

// Map<T>
extensionCodec.register({
  type: 1,
  encode: (object: unknown): Uint8Array => {
    if (object instanceof Map) {
      return encode([...object]);
    } else {
      return null;
    }
  },
  decode: (data: Uint8Array) => {
    const array = decode(data) as Array<[unknown, unknown]>;
    return new Map(array);
  },
});

// and later
import { encode, decode } from "@msgpack/msgpack";

const encoded = = encode([new Set<any>(), new Map<any, any>()], { extensionCodec });
const decoded = decode(encoded, { extensionCodec });

Not that extension types for custom objects must be [0, 127], while [-1, -128] is reserved to MessagePack itself.

Prerequsites

You can use polyfills for them.

NodeJS

If you use this library in NodeJS, v10 or later is required.

Benchmark

Benchmark on NodeJS/v12.1.0

operation op ms op/s
buf = Buffer.from(JSON.stringify(obj)); 493600 5000 98720
buf = JSON.stringify(obj); 959600 5000 191920
obj = JSON.parse(buf); 346100 5000 69220
buf = require("msgpack-lite").encode(obj); 358300 5000 71660
obj = require("msgpack-lite").decode(buf); 270400 5000 54080
buf = require("@msgpack/msgpack").encode(obj); 594300 5000 118860
obj = require("@msgpack/msgpack").decode(buf); 343100 5000 68620

Distrubition

The NPM package distributed in npmjs.com includes both ES2015+ and ES5 files:

  • /dist is compiled into ES2015+
  • /dist.es5 is compiled into ES5 and bundled to singile file

If you use NodeJS and/or webpack, their module resolvers use the suitable one automatically.

License

Copyright 2019 The MessagePack Community.

This software is licensed under the ISC license:

https://opensource.org/licenses/ISC

See LICENSE for details.