@the-link/messagepack
v0.2.0
Published
Type-preserving MessagePack utilities for The Link.
Downloads
307
Readme
The Link MessagePack
Type-preserving MessagePack utilities for values carried through The Link. The package defines a serialization policy; no adapter is required to use it.
Install
npm install @the-link/messagepackUsage
import { deserialize, serialize } from "@the-link/messagepack"
const bytes = serialize({ createdAt: new Date(), data: new Uint8Array([1, 2, 3]) })
const value = deserialize(bytes)Adapters that expose codec selection can use the functions directly:
client.setSerialize(serialize)
client.setDeserialize(deserialize)The transmitted value model preserves undefined, bigint, Date, Map,
Set, RegExp, URL, Error, binary data, and, through a stream relay,
ReadableStream. Array buffers and typed-array
views are normalized to Uint8Array. Functions and symbols become null,
function-valued object properties are omitted, and ordinary class prototypes
are not transmitted. Transmitted<T> describes these transformations.
Attachments
Values already carried by a boundary can be referenced without encoding them inside the MessagePack payload:
const bytes = serialize({ file }, { attachments: [file] })
const value = deserialize(bytes, { attachments: [file] })The boundary owns attachment transport and must supply the same ordered list to both operations. The codec only records and resolves attachment indices.
Streams
A ReadableStream anywhere in a value travels as a reference. The boundary
supplies its stream relay, such as The Link Core's StreamRelay, on both
sides:
const bytes = serialize({ size, content: stream }, { streams: relay })
const value = deserialize(bytes, { streams: relay }) // value.content is a local ReadableStreamThe codec asks the relay for a reference for each stream it meets and for a local stream for each reference it reads; a stream met twice in one value stays one stream. The relay carries the chunks. Without a relay, serializing a stream throws.
Development
bun install --frozen-lockfile
bun run verifyLicense
MIT
