@earendil-works/djs

中文文档
0.1.0发布于 2026/11/06MIT OR Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 2903

A small, sandboxed JavaScript and TypeScript engine in WebAssembly: runs untrusted scripts with fuel and memory limits, and suspends runs into dumps that continue later.

原始使用文档
文档目录6 节
  1. Install
  2. Use
  3. Entry points
  4. Loading the module
  5. Documentation
  6. License

@earendil-works/djs

djs is a small JavaScript and TypeScript engine compiled to WebAssembly, made for running untrusted scripts inside a host: agent tool scripts, plugins, user code. Every sandbox is its own wasm instance with its own heap, so sandboxes share nothing. Scripts get only the host functions you give them, run under a fuel budget and a memory cap, and a run that waits on the host can be suspended into a dump, a plain JavaScript file, and continued later in another process.

The module has no imports and the package no dependencies: it runs in node (20 or newer), Bun, Deno, browsers and web workers.

Install

npm install @earendil-works/djs

Use

import { Sandbox } from "@earendil-works/djs";

const sandbox = await Sandbox.create({
  asyncFunctions: { "tools.search": async (q) => search(q) },
  maxFuel: 50_000_000,
  memoryLimit: 64 << 20,
});

// A script is the body of an async function: its parameters are the keys of the second
// argument, and `await` and `return` work at the top level.
const hits = await sandbox.runAsync("return (await tools.search(query)).slice(0, 3)", { query: "djs" });
sandbox.run("return a + b", { a: 40, b: 2 }); // 42
sandbox.dispose();

Options include typescript: true (scripts are TypeScript), console (where console.log goes), globals, deterministic (repeatable runs) and frozenBuiltins. Errors are SandboxErrors with a kind: threw, syntax, fuel, memory, ...

Entry points

importmodulefor
@earendil-works/djswasm/djs.wasmevery host
@earendil-works/djs/slimwasm/djs-slim.wasmhosts that count kilobytes: no typed arrays, BigInt, iterator helpers and other families scripts rarely use
@earendil-works/djs/dumpwasm/djs-dump.wasmhosts that suspend runs into dumps: the main module plus the dumper
@earendil-works/djs/dump-runtimethe helpers a dump needs to continue in a JavaScript engine other than djs

Each entry point exports the same API, with its own module as the default. To dump runs while running them in the smaller main module, give the sandbox the dump module as its dumpModule:

import { Sandbox } from "@earendil-works/djs";
import { loadModule as loadDumpModule } from "@earendil-works/djs/dump";

const sandbox = await Sandbox.create({ frozenBuiltins: true, dumpModule: await loadDumpModule(), asyncFunctions });
const task = sandbox.start(code, params);
const dump = await task.suspend(); // later, anywhere: (await Sandbox.resume(dump, options)).result

Loading the module

Sandbox.create loads its entry point's module once per process and caches it; loadModule(location) loads another one and new Sandbox({ module }) uses it. In node, Bun and Deno the module is read from the package's wasm/ directory ($DJS_WASM names another main module). In a browser it is fetched from the URL new URL("../wasm/djs.wasm", import.meta.url), a pattern that webpack and Vite recognize and copy into the build. In Vite's dev server, exclude the package from dependency pre-bundling, which would move the code away from its modules:

// vite.config.js
export default { optimizeDeps: { exclude: ["@earendil-works/djs"] } };

In Deno, allow reading the package's directory (--allow-read). With a bundler that does not know the pattern, serve the .wasm file yourself and pass its URL: Sandbox.create({ module: await loadModule("/assets/djs.wasm") }).

Documentation

The guide, with the whole API, the language djs runs, limits, determinism and dumps: https://earendil-works.github.io/djs/js-api.html

License

MIT or Apache-2.0, at your option.

版本与发布记录

@earendil-works/djs 共发布 3 个版本,首次发布于 2026/11/06;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.0latest2026/11/067 天前
0.0.12026/11/067 天前
0.0.0-stage2026/11/067 天前

关于 @earendil-works/djs 的常见问题

@earendil-works/djs 是什么?

A small, sandboxed JavaScript and TypeScript engine in WebAssembly: runs untrusted scripts with fuel and memory limits, and suspends runs into dumps that continue later.

如何安装 @earendil-works/djs?

在项目目录运行 npm install @earendil-works/djs,或使用 pnpm add / yarn add / bun add。

@earendil-works/djs 支持 TypeScript 吗?

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

@earendil-works/djs 支持 ESM 还是 CommonJS?

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

@earendil-works/djs 需要什么版本的 Node.js?

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

@earendil-works/djs 可以商用吗?

@earendil-works/djs 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@earendil-works/djs 的下载量有多少?

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

@earendil-works/djs 还在维护吗?

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