Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@msgpack/msgpack
Advanced tools
@msgpack/msgpack is an npm package that provides functionality for encoding and decoding data using the MessagePack format. MessagePack is a binary serialization format that is more efficient than JSON in terms of both size and speed. This package allows you to serialize JavaScript objects into a compact binary format and deserialize them back into JavaScript objects.
Encoding Data
This feature allows you to encode a JavaScript object into a MessagePack binary format. The `encode` function takes a JavaScript object and returns a Buffer containing the binary data.
const msgpack = require('@msgpack/msgpack');
const data = { foo: 'bar', num: 42 };
const encoded = msgpack.encode(data);
console.log(encoded);
Decoding Data
This feature allows you to decode a MessagePack binary format back into a JavaScript object. The `decode` function takes a Buffer containing the binary data and returns the original JavaScript object.
const msgpack = require('@msgpack/msgpack');
const encoded = Buffer.from([0x82, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72, 0xa3, 0x6e, 0x75, 0x6d, 0x2a]);
const decoded = msgpack.decode(encoded);
console.log(decoded);
Custom Extension Types
This feature allows you to define custom extension types for encoding and decoding. You can use `addExtPacker` to specify how to serialize a custom type and `addExtUnpacker` to specify how to deserialize it.
const msgpack = require('@msgpack/msgpack');
class MyType {
constructor(value) {
this.value = value;
}
}
msgpack.addExtPacker(0x01, MyType, (obj) => msgpack.encode(obj.value));
msgpack.addExtUnpacker(0x01, (data) => new MyType(msgpack.decode(data)));
const myObj = new MyType('custom data');
const encoded = msgpack.encode(myObj);
const decoded = msgpack.decode(encoded);
console.log(decoded);
msgpack-lite is another npm package for MessagePack serialization. It is designed to be lightweight and fast, with a focus on performance. Compared to @msgpack/msgpack, msgpack-lite may have fewer features but is optimized for speed and small bundle size.
notepack.io is a fast and small MessagePack implementation for JavaScript. It is designed to be highly efficient and is often used in performance-critical applications. Compared to @msgpack/msgpack, notepack.io is more focused on performance and may offer better speed at the cost of some additional features.
msgpack5 is a pure JavaScript implementation of the MessagePack format. It supports both encoding and decoding, as well as custom extension types. Compared to @msgpack/msgpack, msgpack5 offers similar functionality but may have different performance characteristics and API design.
This is the pure-JavaScript implementation of MessagePack, an efficient binary serilization format:
This is under development until v1.0.0. Any API will change without notice.
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);
This library is publised as @msgpack/msgpack in npmjs.com.
npm install @msgpack/msgpack
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.
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.
You can use polyfills for them.
If you use this library in NodeJS, v10 or later is required.
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 |
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 fileIf you use NodeJS and/or webpack, their module resolvers use the suitable one automatically.
Copyright 2019 The MessagePack Community.
This software is licensed under the ISC license:
https://opensource.org/licenses/ISC
See LICENSE for details.
FAQs
MessagePack for ECMA-262/JavaScript/TypeScript
The npm package @msgpack/msgpack receives a total of 233,696 weekly downloads. As such, @msgpack/msgpack popularity was classified as popular.
We found that @msgpack/msgpack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.