msgpackr pack and unpack: working versions (tested 2026-10-16)
Round-trip objects with dates, maps and binary using msgpackr, exercise Packr/Unpackr options, and assert packed data is smaller than JSON for repetitive records, fully offline.
passed3 passed · 0 failed2026-10-16 15:21 UTC- Status
- passed
- Last passed
- 2026-10-16 15:21 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
msgpackr@2.1.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38923127324
The code
Exactly the files that passed. Your agent can pull them with get_recipe("msgpackr-pack-unpack") (install), or see them on GitHub.
package.json
{
"name": "msgpackr-pack-unpack",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"msgpackr": "2.1.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/codec.ts
import { pack, unpack, Packr, Unpackr } from "msgpackr";
export { pack, unpack, Packr, Unpackr };
export type PackrOptions = ConstructorParameters<typeof Packr>[0];
export type UnpackrOptions = ConstructorParameters<typeof Unpackr>[0];
export type EventRecord = {
id: string;
at: Date;
labels: Map<string, string>;
payload: Buffer;
};
export function encodeEvent(event: EventRecord, options?: PackrOptions): Buffer {
return new Packr(options).pack(event);
}
export function decodeEvent(data: Buffer, options?: UnpackrOptions): EventRecord {
return new Unpackr(options).unpack(data) as EventRecord;
}
export function packedSize(value: unknown): number {
return pack(value).length;
}
export function jsonSize(value: unknown): number {
return Buffer.byteLength(JSON.stringify(value), "utf8");
}
test/codec.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { Packr, Unpackr } from "msgpackr";
import {
decodeEvent,
encodeEvent,
packedSize,
jsonSize,
type EventRecord,
} from "../src/codec.ts";
test("roundtrips dates, maps and binary", () => {
const event: EventRecord = {
id: "evt-1",
at: new Date("2024-01-02T03:04:05.678Z"),
labels: new Map<string, string>([
["env", "prod"],
["region", "eu"],
]),
payload: Buffer.from([0, 1, 2, 255]),
};
const decoded = decodeEvent(encodeEvent(event));
assert.equal(decoded.id, "evt-1");
assert.ok(decoded.at instanceof Date);
assert.equal(decoded.at.getTime(), event.at.getTime());
assert.ok(decoded.labels instanceof Map);
assert.deepEqual([...decoded.labels], [...event.labels]);
assert.ok(Buffer.isBuffer(decoded.payload));
assert.deepEqual(decoded.payload, event.payload);
});
test("Packr and Unpackr honor options", () => {
const packr = new Packr({ useRecords: false });
const unpackr = new Unpackr({ useRecords: false, mapsAsObjects: true });
const packed = packr.pack({ a: 1, b: [2, 3] });
assert.deepEqual(unpackr.unpack(packed), { a: 1, b: [2, 3] });
});
test("packed repetitive data is smaller than JSON", () => {
const rows = Array.from({ length: 50 }, (_, index) => ({
kind: "user.activity.login.success",
region: "europe-west-1",
active: true,
index,
}));
assert.ok(packedSize(rows) < jsonSize(rows));
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.