Skip to content

encoding

Terminal window
bun add @stopcock/encoding

Base64, Hex, and MessagePack as namespaces.

import { Base64, Hex, MsgPack } from '@stopcock/encoding'
Base64.encode(new Uint8Array([1, 2, 3])) // "AQID"
Base64.decode("AQID") // Uint8Array [1, 2, 3]
import { Base64 } from '@stopcock/encoding'
const fileData = new Uint8Array(await file.arrayBuffer())
const payload = JSON.stringify({ name: file.name, data: Base64.encode(fileData) })
await fetch('/api/upload', { method: 'POST', body: payload })
import { MsgPack } from '@stopcock/encoding'
// 40% smaller than JSON for structured data
const packed = MsgPack.encode({ type: 'move', x: 142.5, y: 87.3, ts: Date.now() })
websocket.send(packed)
// On receive
const msg = MsgPack.decode(new Uint8Array(event.data))
Base64.encode(data: Uint8Array): string
Base64.decode(str: string): Uint8Array
Hex.encode(data: Uint8Array): string
Hex.decode(str: string): Uint8Array

Binary serialization format. More compact than JSON for structured data.

MsgPack.encode(value: unknown): Uint8Array
MsgPack.decode(data: Uint8Array): unknown