encoding
bun add @stopcock/encodingBase64, 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]Real-world patterns
Section titled “Real-world patterns”File upload as Base64
Section titled “File upload as Base64”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 })Binary protocol with MsgPack
Section titled “Binary protocol with MsgPack”import { MsgPack } from '@stopcock/encoding'
// 40% smaller than JSON for structured dataconst packed = MsgPack.encode({ type: 'move', x: 142.5, y: 87.3, ts: Date.now() })websocket.send(packed)
// On receiveconst msg = MsgPack.decode(new Uint8Array(event.data))Base64
Section titled “Base64”Base64.encode(data: Uint8Array): stringBase64.decode(str: string): Uint8ArrayHex.encode(data: Uint8Array): stringHex.decode(str: string): Uint8ArrayMsgPack
Section titled “MsgPack”Binary serialization format. More compact than JSON for structured data.
MsgPack.encode(value: unknown): Uint8ArrayMsgPack.decode(data: Uint8Array): unknown