From 844192df421fae98d1330ebb30e392a5cea78e46 Mon Sep 17 00:00:00 2001 From: Enoch Groot Date: Thu, 10 Sep 2026 02:32:50 +0000 Subject: [PATCH] feat: ship TypeScript types for pack, unpack, and Stream Adds index.d.ts matching the 2.0.0 runtime: pack(...values) returns Buffer, unpack.bytes_remaining is per-thread, Stream wraps a duplex. Fixes msgpack/msgpack-node#39 --- index.d.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 52 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3e9a37e --- /dev/null +++ b/index.d.ts @@ -0,0 +1,51 @@ +// Type definitions for msgpack 2.0.0 +// Project: https://github.com/enochgroot/msgpack-node + +/// + +import { EventEmitter } from 'events'; + +/** + * Serialize values to MessagePack. + * + * A single argument is packed as itself; two or more are packed as an array + * of that many elements. + */ +export function pack(...values: any[]): Buffer; + +/** + * Deserialize the first MessagePack value in `buf`. + * + * Returns `null` when the buffer holds an incomplete value, in which case + * `unpack.bytes_remaining` equals `buf.length`. Throws on malformed input or + * when a container/string/bin header exceeds the decoder's limits. + */ +export function unpack(buf: Buffer): any; + +export namespace unpack { + /** + * Bytes left in the buffer passed to the most recent `unpack()` call on + * this thread. Read it immediately after `unpack()`: the next call + * overwrites it. + */ + let bytes_remaining: number; +} + +/** + * Frames MessagePack messages over a stream. + * + * Emits `'msg'` with each decoded value, and `'error'` if a packet cannot be + * decoded (the buffered data is then dropped). + */ +export class Stream extends EventEmitter { + constructor(s: NodeJS.ReadWriteStream); + + /** Buffered bytes not yet forming a complete message, or `null`. */ + buf: Buffer | null; + + /** + * Pack `m` and write it to the underlying stream. Extra arguments are + * forwarded to `stream.write()` (encoding, callback). + */ + send(m: any, ...args: any[]): boolean; +} diff --git a/package.json b/package.json index 5caa3b9..0374719 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "url": "https://github.com/enochgroot/msgpack-node.git" }, "main": "./lib/msgpack", + "types": "index.d.ts", "directories": { "lib": "lib" },