@enbox/dwn-sdk-js

中文文档
0.4.32发布于 2026/11/12Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2862
原始使用文档
文档目录9 节
  1. Overview
  2. Installation
  3. Usage
  4. Context role preference
  5. Custom Tenant Gating
  6. Custom Signer
  7. Architecture
  8. Development
  9. License

Decentralized Web Node (DWN) SDK

Research Preview — Enbox is under active development. APIs may change without notice.

Coverage

A TypeScript implementation of Decentralized Web Nodes — module-driven personal datastores that users control.

Overview

This package is the core DWN engine used by the rest of the Enbox stack. It handles message processing, module authorization, record storage, and encryption. Most applications should use @enbox/api rather than this package directly.

Installation

bun add @enbox/dwn-sdk-js

Usage

import { Dwn, DataStream, Jws, RecordsWrite } from '@enbox/dwn-sdk-js';
import { DataStoreLevel, MessageStoreLevel, ResumableTaskStoreLevel } from '@enbox/dwn-sdk-js/stores/level';

const messageStore = new MessageStoreLevel();
const dataStore = new DataStoreLevel();
const resumableTaskStore = new ResumableTaskStoreLevel();
const dwn = await Dwn.create({ messageStore, dataStore, resumableTaskStore });

// Create and process a RecordsWrite message
const data = new TextEncoder().encode('Hello, World!');
const recordsWrite = await RecordsWrite.create({
  data,
  dataFormat  : 'application/json',
  published   : true,
  schema      : 'example/post',
  signer      : Jws.createSigner(persona),
});

const dataStream = DataStream.fromBytes(data);
const result = await dwn.processMessage(persona.did, recordsWrite.message, { dataStream });
console.log(result.status); // { code: 202, detail: 'Accepted' }

await dwn.close();

Level-backed store implementations are exported from @enbox/dwn-sdk-js/stores/level so applications that only need message and module APIs do not load the Level store module graph by default. Shared blockstore helpers used by alternative store implementations are available from @enbox/dwn-sdk-js/stores/blockstore.

Context role preference

resolveModuleContextRoles(graph, contextPath) returns the root's direct-child role paths granting read, in $actions array order. That order declares client following preference for this exact root; object property order does not affect it. It does not change DWN authorization, imply role inheritance, or modify role assignments. The high-level shared-context API derives its role candidates from this same resolved module graph.

See context role preference for the definition convention and a notebook example with editor and viewer roles.

Custom Tenant Gating

By default all DIDs are allowed as tenants. Provide a custom TenantGate to restrict access:

import { ActiveTenantCheckResult, Dwn, TenantGate } from '@enbox/dwn-sdk-js';

class CustomTenantGate implements TenantGate {
  public async isActiveTenant(did: string): Promise<ActiveTenantCheckResult> {
    // custom logic
  }
}

const dwn = await Dwn.create({ messageStore, dataStore, resumableTaskStore, tenantGate: new CustomTenantGate() });

Custom Signer

Use PrivateKeySigner if you have a key available, or implement the Signer interface to integrate with an external signing service, HSM, etc.:

import type { Signer } from '@enbox/dwn-sdk-js';

class CustomSigner implements Signer {
  public keyId = 'did:example:alice#key1';
  public algorithm = 'EdDSA';
  public async sign(content: Uint8Array): Promise<Uint8Array> {
    // custom signing logic
  }
}

Architecture

DWN architecture diagram

The diagram is a conceptual view; actual component names in source may differ.

Development

# Build
bun run build

# Test
bun run test:node

# Test with grep filter (from this package directory)
GREP="ModulesConfigure" bun run test:node-grep

# Lint
bun run lint

License

Apache-2.0

版本与发布记录

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

版本发布日期距今
0.4.32latest2026/11/121 天前
0.4.312026/11/077 天前
0.4.302026/11/0113 天前
0.4.292026/10/2322 天前
0.4.282026/10/2124 天前
0.4.272026/09/181 个月前

关于 @enbox/dwn-sdk-js 的常见问题

@enbox/dwn-sdk-js 是什么?

A reference implementation of https://identity.foundation/decentralized-web-node/spec/

如何安装 @enbox/dwn-sdk-js?

在项目目录运行 npm install @enbox/dwn-sdk-js,或使用 pnpm add / yarn add / bun add。

@enbox/dwn-sdk-js 支持 TypeScript 吗?

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

@enbox/dwn-sdk-js 支持 ESM 还是 CommonJS?

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

@enbox/dwn-sdk-js 可以商用吗?

@enbox/dwn-sdk-js 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@enbox/dwn-sdk-js 的下载量有多少?

根据 npm 官方下载统计,@enbox/dwn-sdk-js 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,862 次,日均约 95 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@enbox/dwn-sdk-js 还在维护吗?

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