@computesdk/neevcloud (英语)
尼夫克卢德 提供方 计算SDK ——在安全的云沙箱中运行命令并管理文件.
安装
npm install @computesdk/neevcloud
设置
从 NeevCloud 控制台获取一个 API 密钥, 并记录 Org 和投影 沙盒应该居住, 然后设定 :
export NEEV_API_KEY=sk-nc-...
export NEEV_ORG_ID=org-...
export NEEV_PROJECT_ID=prj-...
快速启动
import { neevcloud } from '@computesdk/neevcloud';
const compute = neevcloud();
const sandbox = await compute.instances.create();
const res = await sandbox.runCommand('echo "Hello from NeevCloud!"');
console.log(res.stdout); // "Hello from NeevCloud!"
await sandbox.filesystem.writeFile('app.txt', 'hi');
console.log(await sandbox.filesystem.readFile('app.txt')); // "hi"
const url = await sandbox.getUrl({ port: 3000 });
console.log(url); // https://3000-<id>.<region>.neevsandbox.app
await sandbox.destroy();
配置
neevcloud(config) 接受 { apiKey?, orgId?, projectId?, timeout? }。每个字段都是可选的,省略时从匹配的环境变量中读取,所以大多数呼叫者都可以使用 neevcloud() 没有争论.
interface NeevCloudConfig {
/** NeevCloud API key. Read from NEEV_API_KEY when omitted. */
apiKey?: string;
/** Org the sandboxes belong to. Read from NEEV_ORG_ID when omitted. */
orgId?: string;
/** Project the sandboxes belong to. Read from NEEV_PROJECT_ID when omitted. */
projectId?: string;
/** Request timeout in milliseconds. */
timeout?: number;
}
特征
- 命令执行 ——运行 shell 命令,缓冲或流出,前景或背景.
- 文件系统访问 ——读取,写出,列出,stat,并删除文件.
- 预览 URL ——在公共HTTPS URL上曝光任何端口
getUrl({ port })。 。 。 . - 弹性靴子 ——从目录模板开始,原始OCI图像,快照,或平台默认,有名称,env vars,大小,和寿命.
- 抓图 ——保存运行中的沙盒的内存和文件系统,再从中提供出新的沙盒.
- 模板 ——列出沙盒模板目录.
API 参考
命令执行
// Buffered — resolves with the full result
const res = await sandbox.runCommand('ls -la');
console.log(res.stdout, res.exitCode, res.durationMs);
// Streamed — pass output callbacks to receive chunks as they arrive
await sandbox.runCommand('npm install', {
onStdout: (chunk) => process.stdout.write(chunk),
onStderr: (chunk) => process.stderr.write(chunk),
});
// Background — returns immediately, leaves the process running
await sandbox.runCommand('python3 -m http.server 3000', { background: true });
命令会从工作空间根穿过一个 shell(/workspace),所以管道和重定向工作. 一个亲戚 cwd 从工作空间根解析;使用绝对根。 流出输出来自NeevCloud exec API,所以沙盒内的任何端口都不得被接通. 一个超过它的命令 timeout 返回退出代码 124 和它迄今为止的产出.
文件系统操作
await sandbox.filesystem.writeFile('data/input.csv', csv);
const content = await sandbox.filesystem.readFile('data/input.csv');
await sandbox.filesystem.mkdir('data/output');
const entries = await sandbox.filesystem.readdir('data');
const present = await sandbox.filesystem.exists('data/input.csv');
await sandbox.filesystem.remove('data/input.csv');
路径意味着相同的东西 filesystem 电话和在 runCommand: 从工作空间根解决相对路径(/workspace),并使用绝对路径。 工作空间内部的路径通过 NeevCloud 文件 API; 其外的绝对路径, 如 /tmp/x穿过外壳.
预览 URL
// Start a server, then expose its port over public HTTPS
await sandbox.runCommand('python3 -m http.server 3000', { background: true });
const url = await sandbox.getUrl({ port: 3000 });
沙盒管理
const sandbox = await compute.instances.create({ templateId: 'sb-ubuntu-24-04-minimal' });
const sandbox = await compute.instances.create({ image: 'docker.io/library/python:3.12' });
// Name, env vars, size (cpu cores, memory in MB) and lifetime (ms; deleted when it elapses)
const sandbox = await compute.instances.create({
name: 'build-42',
envs: { NODE_ENV: 'production' },
cpu: 2,
memory: 4096,
timeout: 30 * 60 * 1000,
});
const info = await sandbox.getInfo(); // { id, provider, status, createdAt, timeout, metadata }
const all = await compute.instances.list();
const one = await compute.instances.getById(id); // null if not found
await sandbox.destroy();
templateId 财务报告和审定财务报表 image 相互排斥; 省略两个平台默认值。 通过一个 AbortSignal 作为 signal 取消创建并删除已请求的沙盒.
抓图
// Save memory and filesystem; resolves once the snapshot is ready to restore
const snapshot = await compute.snapshot.create(sandbox.instanceId, { name: 'after-install' });
// Boot a new sandbox from it
const copy = await compute.instances.create({ snapshotId: snapshot.id });
const forOne = await compute.snapshot.list({ instanceId: sandbox.instanceId });
const all = await compute.snapshot.list(); // visits every sandbox in the project
await compute.snapshot.delete(snapshot.id);
模板
const templates = await compute.template.list();
模板目录由NeevCloud管理,所以 template.create 财务报告和审定财务报表 template.delete 掷出. 从您自己的图像启动 create({ image }),或用 snapshot.create。 。 。 .
在计算SDK表面之外
对于计算SDK合同之外的能力——暂停/恢复、交互式PTYs、流程监督、Egreat-limits、分叉和回滚以及审计日志——达到基本 @neevcloud/sdk 控件 :
const native = sandbox.getInstance();
await native.pause();
await native.resume();
const copy = await native.fork('copy-of-build');
许可证
麻省理工学院