Algorand MessagePack
This library, `algorand-msgpack`, is a TypeScript/JavaScript implementation of the MessagePack binary serialization format, currently at version 1.1.0. It's a fork of the popular `@msgpack/msgpack` library, maintaining a similar API but introducing several key enhancements. These include improved BigInt support that allows for greater interoperability with other MessagePack implementations by not solely encoding BigInts as int64/uint64, and the ability to decode raw strings as `Uint8Array`s to handle non-UTF-8 encoded strings. Additionally, it provides support for number and binary map keys within JavaScript Maps. The most recent significant update in v1.1.0 introduced the `RawBinaryString` class, enabling encoding byte arrays as MessagePack strings and offering corresponding decoding options. While it doesn't have a strict release cadence, updates appear to be driven by the needs of the Algorand ecosystem. It's designed to be a universal library, compatible with Node.js (v14 and above) and browser environments, and ships with comprehensive TypeScript type definitions.
Common errors
-
Module not found: Error: Can't resolve '@msgpack/msgpack' in '...' OR Failed to resolve module specifier "@msgpack/msgpack"
cause Attempting to import from the original `@msgpack/msgpack` package instead of the forked `algorand-msgpack`.fixUpdate all import statements in your code from `@msgpack/msgpack` to `algorand-msgpack`. -
TypeError: Cannot mix BigInt and other types, use explicit conversion
cause Mismatch in BigInt handling between `algorand-msgpack` and other MessagePack implementations, or expecting BigInts to be serialized without specific configuration.fixReview the BigInt support documentation for `algorand-msgpack`. You may need to use specific `EncoderOptions` or `DecoderOptions` to align BigInt serialization/deserialization with your requirements or other systems.
Warnings
- breaking This library is a fork of `@msgpack/msgpack` and introduces behavioral changes for BigInt handling, raw string decoding, and map key types. While the API is similar, direct replacement without understanding these differences may lead to subtle data corruption or unexpected behavior.
- gotcha The `RawBinaryString` class, introduced in v1.1.0, requires explicit decoder options (`useRawBinaryStringClass`, `rawBinaryStringKeys`, `rawBinaryStringValues`) to decode MessagePack strings back into `RawBinaryString` instances. Without these options, they will be decoded as standard JavaScript strings or `Uint8Array`s.
- gotcha This library explicitly requires Node.js version 14 or higher as per its `engines` field. Running in older Node.js environments may result in runtime errors or unexpected behavior.
Install
-
npm install algorand-msgpack -
yarn add algorand-msgpack -
pnpm add algorand-msgpack
Imports
- encode
import { encode } from '@msgpack/msgpack'import { encode } from 'algorand-msgpack' - decode
const { decode } = require('algorand-msgpack')import { decode } from 'algorand-msgpack' - RawBinaryString
import { RawBinaryString } from 'algorand-msgpack'
Quickstart
import { deepStrictEqual } from "assert";
import { encode, decode, RawBinaryString } from "algorand-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(),
// Example of using RawBinaryString introduced in v1.1.0
rawString: new RawBinaryString(Uint8Array.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]))
};
const encoded: Uint8Array = encode(object);
// When decoding, enable options if expecting RawBinaryString instances
const decoded = decode(encoded, {
useRawBinaryStringClass: true,
rawBinaryStringValues: true
});
deepStrictEqual(decoded, object);
console.log("MessagePack encoding and decoding successful!");