@earendil-works/djs (英语)
djs 是一个小的 JavaScript 和 TypeScript 引擎编译到 Web Assembly, 用于运行 主机内部的不可信脚本:代理工具脚本,插件,用户代码. 每个沙盒 所以沙盒什么也没有 脚本只获得 运行在燃料预算和内存上限下,运行在 在主机上等待可被暂停为垃圾堆, 普通 JavaScript 文件, 并继续 稍后在另一个过程中.
模块没有导入,软件包没有依赖性:在节点运行(20个或更新的), Bun, Deno, 浏览器和网络工人.
安装
npm install @earendil-works/djs
使用
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();
选项包括 typescript: true (文字为TypeScript), console (何处)
console.log 继续, globals, (中文(简体) ), deterministic (可重复运行)和 frozenBuiltins。 。 。 .
错误是 SandboxErrors 与一个 kind数字 : threw, (中文(简体) ), syntax, (中文(简体) ), fuel, (中文(简体) ), memory,, , , , , , , , .
入境点
| 导入 | 模块 | 页:1 |
|---|---|---|
@earendil-works/djs | wasm/djs.wasm | 每个主机 |
@earendil-works/djs/slim | wasm/djs-slim.wasm | 计算千字节的主机: 没有键入数组, BigInt, 转发器助手和其他家庭脚本很少使用 |
@earendil-works/djs/dump | wasm/djs-dump.wasm | 挂起的主机会运行到垃圾堆: 主模块加垃圾堆 |
@earendil-works/djs/dump-runtime | 帮助者需要用 Djs 以外的 JavaScript 引擎继续堆放 |
每个切入点输出相同的API,以自己的模块为默认. 丢弃跑道
在较小的主模块中运行时,将倾卸模块作为沙箱
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
正在装入模块
Sandbox.create 每个进程装入其入口模块并缓存;
loadModule(location) 又装入一个 new Sandbox({ module }) 用这个 在节点,
Bun和Deno 模块是从软件包读取的 wasm/ 目录( e)$DJS_WASM 名称
)). . . 在浏览器中从 URL 中获取
new URL("../wasm/djs.wasm", import.meta.url),一个图案,网络包装和Vite识别
并抄入建筑。 在 Vite 的 Dev 服务器中, 将软件包排除在依赖之外
预捆绑,将代码从模块中移出:
// vite.config.js
export default { optimizeDeps: { exclude: ["@earendil-works/djs"] } };
在 Deno 中,允许读取软件包的目录(--allow-read) (中文(简体) ). 带着一个捆绑工
不知道模式,服务于 .wasm 存档并传递其 URL :
Sandbox.create({ module: await loadModule("/assets/djs.wasm") })。 。 。 .
文档
指南 整个API 语言 djs 运行,限制,决定主义和垃圾堆: https://earendil-works.github.io/djs/js-api.html 互联网档案馆的存檔,存档日期2013-12-22
许可证
麻省理工或Apache -2.0,由你选择.