@enbox/browser

中文文档
0.3.90发布于 2026/11/12Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2753

Enbox tools and features to use in the browser

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Wallet sign-in without a data session
  4. Browser runtime
  5. Project resources

Enbox Browser

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

Browser entrypoint for Enbox application APIs, auth, wallet connect, and DWeb features

NPM Package NPM Downloads Build Status Open Issues Code Coverage

Install

bun add @enbox/browser

Usage

@enbox/browser re-exports the high-level API, browser-safe auth surface, wallet connect handler, and DWeb helpers:

import { createApplication } from '@enbox/browser';
import { NotesStore } from './features/notes/store.js';

const application = createApplication({
  stores: { notes: NotesStore },
  wallet: { appName: 'Notes' },
  connection: { monitor: { autoRefresh: {} } },
});

await application.initialize();

// Invoke directly from a user action so the wallet popup opens.
async function connect(): Promise<void> {
  const snapshot = await application.connection.connect();
  if (snapshot.phase !== 'connected') {
    throw snapshot.error ?? new Error('Connection was not established.');
  }
  const notes = application.store(NotesStore);
  // Subscribe to notes' resources or run its domain commands.
}

Keep one application for the browser lifetime. It derives the manifest from feature modules and owns the connection, session-scoped resources, commands, and cleanup. application.connection.disconnect() signs out; application.dispose() permanently tears down the owner. React binds the same runtime through EnboxProvider application={application}.

For a custom host, supply connectHandler instead of wallet, or supply a connection: manifest => nativeConnection factory that owns all native configuration. The headless @enbox/api constructor requires that factory; the browser constructor supplies browser defaults around the same runtime. Native connection, record, and context primitives remain available for custom flows. defineStore's observe() factory owns all native view types through one subscription and cleanup mechanism.

Wallet sign-in without a data session

Call authenticateViaPopup() directly from a user action to request an owner authentication proof. Supply the wallet URL and a service-issued authentication: { version: 1, audience, challenge } request. The result is { connectedDid, authentication }, or undefined when denied; no DWN delegate session is created. The service verifies the returned proof against its exact pending request and consumes the challenge once before establishing login. Supply onAuthenticationRequest to register that exact generated request with the service before wallet delivery; the callback must resolve successfully before approval can proceed. A registration failure closes the popup.

Service-side verification requires an independent expected: { audience, challenge } from configuration and its issued transaction, together with the stored full request. Unknown or already-used challenges must be rejected. Owner authentication requires an HTTPS origin, including local development. Proofs expire 300 seconds after signing; the response envelope's separate 600-second lifetime does not extend that deadline. An expired proof fails login even if the wallet already approved grants in combined mode.

connectViaPopup() also accepts authentication to combine sign-in with its explicit DWN permission request. The existing application/connection-store APIs continue to manage data sessions. The Connect proof is an Enbox wallet primitive; OAuth/OIDC token issuance belongs to the receiving service.

Authentication capability discovery/negotiation is not implemented. Use combined mode only with known-capable wallets: older wallets can grant the requested data access and then return no owner proof, which fails login. Likewise, a final signing failure can leave previously written grants in place. Callers must reconcile or revoke that partial outcome before retrying; a rejected combined operation is not evidence of a DWN rollback.

Browser runtime

Every browser dapp must register an application-owned service worker that calls activatePolyfills(). The page owns the connection store, record views, and WebSockets; the worker owns DRL fetch interception and the offline shell. Keep the default Level/IndexedDB storage stack.

Before initializing the connection store, await prepareBrowser(workerUrl, { registrationOptions: { type: 'module' } }) for a module worker, or pass an existing registration from this page's navigator.serviceWorker when a PWA tool owns registration. The helper waits for that worker to control the page and activates page polyfills, with a timeout and abort signal. The application owns update activation. Worker-side activatePolyfills() claims clients during activation, but leaves waiting workers in place. Complete pending application work before your update handler requests skipWaiting() and reloads the page.

Current browser-conditioned packages work in page and worker builds without Enbox-specific Node-global, process, or IIFE shims. Follow the browser dapp guide for the canonical Vite, worker, startup, live-data, hosting, and verification setup. The rationale lives in Browser dapp architecture.

Project resources

ResourceDescription
AGENTS.mdContributor workflow and repository rules
LicenseApache License, Version 2.0

版本与发布记录

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

版本发布日期距今
0.3.90latest2026/11/121 天前
0.3.892026/11/077 天前
0.3.882026/11/077 天前
0.3.872026/11/058 天前
0.3.862026/11/0113 天前
0.3.852026/10/2916 天前

关于 @enbox/browser 的常见问题

@enbox/browser 是什么?

Enbox tools and features to use in the browser

如何安装 @enbox/browser?

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

@enbox/browser 支持 TypeScript 吗?

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

@enbox/browser 支持 ESM 还是 CommonJS?

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

@enbox/browser 可以商用吗?

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

@enbox/browser 的下载量有多少?

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

@enbox/browser 还在维护吗?

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