@cratestack/cbor-web

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

wasm-bindgen CBOR codec for browser CrateStack RPC clients. Async one-time WASM init, synchronous encode/decode after — backed by the same Rust CborCodec the server and Rust client already use.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. With a generated CrateStack client
  4. Error handling
  5. Bundlers
  6. See Also
  7. License

@cratestack/cbor-web

A wasm-bindgen build of cratestack-codec-cbor's CborCodec for browser CrateStack RPC clients — the browser half of @cratestack/cbor-node's epic (#285). Same Rust encode/decode logic the server and Rust client already use, reachable from browser JavaScript with no CBOR reimplemented in TypeScript.

Installation

npm install @cratestack/cbor-web

Usage

createCborCodec() performs the one-time WASM module instantiation and resolves to a plain object satisfying CratestackRpcCodec from @cratestack/ts-types. Every encode/decode call on the returned object is synchronous — the async cost is paid once, not per call:

import { createCborCodec } from "@cratestack/cbor-web";

const codec = await createCborCodec();

const bytes = codec.encode({ hello: "world" });
const value = codec.decode(bytes);

With a generated CrateStack client

import { createCborCodec } from "@cratestack/cbor-web";
import { createClient } from "./generated/client.js";

const client = createClient({
  baseUrl: "https://api.example.com",
  codec: await createCborCodec(),
});

Error handling

Malformed CBOR input on decode, or a value encode can't represent, throws a catchable JS Error — never a WASM trap that would poison the module for subsequent calls (see crates/cratestack-cbor-wasm/src/wasm.rs for why that distinction matters).

Bundlers

Ships a wasm-pack --target web build: the .wasm asset is resolved via new URL('cratestack_cbor_wasm_bg.wasm', import.meta.url), which Vite, webpack 5, and Next.js all handle as a static asset without extra config. Verified against examples/embedded-browser-vite.

See Also

  • @cratestack/cbor-node — the Node/native counterpart (napi-rs).
  • crates/cratestack-cbor-wasm — the wasm-bindgen crate this package wraps.
  • crates/cratestack-codec-cbor — the underlying, unchanged Rust codec.

License

MIT

版本与发布记录

@cratestack/cbor-web 共发布 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-web 的常见问题

@cratestack/cbor-web 是什么?

用于浏览器CrateStack RPC客户端的wasm- binding cor编解码器。异步一次性WASM初始化,同步编码/解码后-由服务器和Rust客户端已经使用的相同Rust CborCodec支持。

如何安装 @cratestack/cbor-web?

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

@cratestack/cbor-web 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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