@cratestack/cbor-node

中文文档
0.17.2发布于 2026/11/01MITTypeScriptES Module零依赖构建来源已验证

Native N-API CBOR codec for CrateStack's generated TypeScript clients, wrapping the framework's own cratestack-codec-cbor Rust crate (crates/cratestack-cbor-napi) for byte-identical wire behavior with the server and Rust client.

原始使用文档
文档目录5 节
  1. Usage
  2. Null handling
  3. Error handling
  4. Scope
  5. Supported platforms

@cratestack/cbor-node

Native N-API CBOR codec for CrateStack's generated TypeScript RPC clients — a CratestackRpcCodec backed by the framework's own cratestack-codec-cbor Rust crate via native bindings (crates/cratestack-cbor-napi), not a JS reimplementation. Encode/decode output is byte-identical to what the CrateStack server and Rust client already produce for application/cbor.

Ships prebuilt per-platform binaries via optionalDependencies (the same pattern esbuild/@swc/core use) — no Rust toolchain needed to install or use this package.

See epic #285 and issue #286 for the full design context.

Usage

import { cborCodec } from "@cratestack/cbor-node";

const bytes = cborCodec.encode({ hello: "world", count: 1 });
const value = cborCodec.decode(bytes);

cborCodec structurally satisfies CratestackRpcCodec from @cratestack/ts-types, so it can be passed anywhere a generated client's runtime expects a codec — no explicit dependency on @cratestack/ts-types required at runtime, since it's checked via satisfies, not a type re-export.

Null handling

CBOR has a real null (RFC 8949 §3.3, simple value 22, byte 0xf6). cratestack-codec-cbor's underlying minicbor-serde backend has a documented quirk where a bare Rust unit type () encodes as the CBOR empty array marker (0x80) instead — this package's encode translates every JSON null (top-level or nested inside an object/array) through the code path that produces real CBOR null, matching CborCodec's own Option::None behavior byte-for-byte. See crates/cratestack-cbor-napi/src/json_value.rs for the detail.

Error handling

Malformed CBOR bytes passed to decode throw a catchable JS Error — never a native crash. Both native entry points also use napi-rs's catch_unwind to convert any unexpected Rust panic into a catchable exception instead of aborting the Node process.

Scope

Single-item encode/decode only — no CBOR-seq/streaming support (that's a separate concern; see epic #285). Since issue #746, @cratestack/cbor (this package's Node half, via the umbrella @cratestack/cbor re-export) is the default codec for a generated TypeScript RPC client — --no-native-cbor (TypeScriptGeneratorConfig::native_cbor: false) falls back to the pure-TypeScript jsonRpcCodec, needed on platforms this package doesn't ship a napi binary for (win32-arm64 — see napi.targets in package.json). REST-transport generated clients are unaffected either way: the REST runtime has no codec seam at all.

Supported platforms

napi.targets in package.json is the source of truth; as of cratestack#850 it is:

PlatformTarget triplenpm platform package
macOS x64x86_64-apple-darwin@cratestack/cbor-node-darwin-x64
macOS arm64aarch64-apple-darwin@cratestack/cbor-node-darwin-arm64
Linux x64 (glibc)x86_64-unknown-linux-gnu@cratestack/cbor-node-linux-x64-gnu
Linux arm64 (glibc)aarch64-unknown-linux-gnu@cratestack/cbor-node-linux-arm64-gnu
Linux x64 (musl / Alpine)x86_64-unknown-linux-musl@cratestack/cbor-node-linux-x64-musl
Linux arm64 (musl / Alpine)aarch64-unknown-linux-musl@cratestack/cbor-node-linux-arm64-musl
Windows x64x86_64-pc-windows-msvc@cratestack/cbor-node-win32-x64-msvc

Both musl binaries are load-tested in CI, not just built: each build-cbor-node musl leg runs on the runner architecture it targets and, before uploading anything, loads the .node it just produced under node:22-alpine and asserts a fixed encode/decode vector through this package's own native.mjs. So "works on Alpine" is checked on x64 and arm64, on real hardware rather than emulation, on every release build.

Still not covered: win32-arm64. There is also no wasm/wasi fallback — the generated native.mjs loader contains a wasm32-wasi branch, but nothing publishes a .wasi.cjs or a @cratestack/cbor-node-wasm32-wasi package, so that branch can only ever add another Cannot find module to the error chain. On an unsupported platform the loader raises the generic "Cannot find native binding. npm has a bug related to optional dependencies…" error, which blames npm rather than naming the real cause; --no-native-cbor is the escape hatch.

The per-platform packages above are an optimisation, not a requirement: this package's own tarball bundles every .node binary (files: ["*.node"]), and the loader tries the bundled ./cratestack-cbor-node.<platform>.node before the @cratestack/cbor-node-<platform> dependency. A platform whose subpackage has not been published yet still resolves a working binding.

版本与发布记录

@cratestack/cbor-node 共发布 57 个版本,首次发布于 2026/08/03;过去 12 个月发布了 57 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.17.2latest2026/11/012 天前
0.17.12026/10/312 天前
0.17.02026/10/286 天前
0.16.02026/10/2310 天前
0.15.42026/10/1222 天前
0.15.32026/10/011 个月前

关于 @cratestack/cbor-node 的常见问题

@cratestack/cbor-node 是什么?

本机N-API cor编解码器,用于CrateStack生成的TypeScript客户端,封装框架自己的cratstack -codec- cor Rust crate (crate / cratstack - cor -napi),用于与服务器和Rust客户端进行字节相同的连接行为。

如何安装 @cratestack/cbor-node?

在项目目录运行 npm install @cratestack/cbor-node,或使用 pnpm add / yarn add / bun add。

@cratestack/cbor-node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cratestack/cbor-node 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cratestack/cbor-node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cratestack/cbor-node 可以商用吗?

@cratestack/cbor-node 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cratestack/cbor-node 还在维护吗?

最新版本 0.17.2 发布于 2026/11/01(2 天前);至今共发布 57 个版本,其中过去 12 个月发布了 57 个;首次发布于 2026/08/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。