Algorand MessagePack

1.1.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates basic encoding and decoding of a complex JavaScript object, including the use of `RawBinaryString` for explicit binary string handling, ensuring data integrity.

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!");

view raw JSON →