@ computesdk/ mosaic (帮助)
计算SDK的 Mosaic 提供者 。 Mosaic为以火花为主的沙盒环境提供了命令执行,工作空间文件系统,预览URL,快照,以及从容器图像所构建的环境.
安装
npm install computesdk @computesdk/mosaic
快开始
import { compute } from 'computesdk';
import { mosaic } from '@computesdk/mosaic';
compute.setConfig({
provider: mosaic({
baseUrl: process.env.MOSAIC_API_URL,
apiKey: process.env.MOSAIC_API_TOKEN,
}),
});
const sandbox = await compute.instances.create({ templateId: 'node-20' });
await sandbox.filesystem.writeFile('/workspace/server.js', 'require("http").createServer((_, r) => r.end("ok")).listen(3000)');
await sandbox.runCommand('node /workspace/server.js', { background: true });
console.log(await sandbox.getUrl({ port: 3000 }));
await sandbox.destroy();
提供者还读到 MOSAIC_API_URL 财务报告和审定财务报表 MOSAIC_API_TOKEN 当这些值从配置中删除时. baseUrl 应当指出Mosaic API的部署,以及 apiKey 以无记名的迹象形式发出的.
配置
interface MosaicConfig {
baseUrl?: string;
apiKey?: string;
template?: string;
memoryMb?: number;
vcpu?: number;
requestTimeoutMs?: number;
/** Outbound network access. On by default. */
networkEnabled?: boolean;
/** Lifetime of a URL from getUrl, in seconds. Defaults to 3600. */
previewExpiresInSeconds?: number;
}
单放箱 templateId, (中文(简体) ), runtime, (中文(简体) ), memoryMb, (中文(简体) ), memoryMiB, (中文(简体) ), vcpus, (中文(简体) ), cpus,以及 metadata 选项覆盖提供者默认值.
模板、快照和图像
node-20 财务报告和审定财务报表 python-3.11 是Mosaic的股票模板。 其他一切——a templateId 这不是股票,a snapshotId,或一个 image ——是您自己的环境之一,通过id或您给它取的名字进行处理:
const provider = mosaic({});
// Build an environment from any linux/amd64 registry image (minutes, once).
await provider.template.create({ name: 'my-env', image: 'python:3.12-slim' });
// Sandboxes from it restore in about a second, like any other template.
const sandbox = await compute.instances.create({ templateId: 'my-env' });
// Or checkpoint a sandbox you have already set up.
const snapshot = await provider.snapshot.create(sandbox.instanceId, { name: 'my-toolchain' });
await compute.instances.create({ snapshotId: 'my-toolchain' });
template.create 接受 image 加可选 retentionSeconds 财务报告和审定财务报表 registryUsername编号 :registryPassword 一个私人形象。 登记册证书用于这一拉动,从未储存.
支助行动
| 方法 | 支助 | 页:1 |
|---|---|---|
create | ✅ | 库存模板、快照或图像环境;资源和元数据覆盖. |
getById | ✅ | 回返 null 当找不到沙盒时. |
list | ✅ | 列出 API 令牌可见的沙盒 . |
destroy | ✅ | 缺少沙盒的同上. |
runCommand | ✅ | 工作目录,环境,超时,以及背景执行. |
getInfo | ✅ | 返回生命周期状态和资源元数据. |
getUrl | ✅ | 终止 HTTPS 为来宾端口预览 URL . |
filesystem | ✅ | 读取,写出,mkdir,读出,存在,去除. |
snapshot | ✅ | 创建,列表,并删除;可以名取名并恢复快照. |
template | ✅ | 从容器图像中构建、列出和删除环境. |
页:1
background: true通过 shell 进程级别发射命令,因此一个dev 服务器比启动它的请求有效,而沙盒仍然可以被暂停. 回来的stdout是贝壳工作编号.- 文件系统在内部调用
/workspace使用 Mosaic 二进制安全文件 API;其外出路径会倒回外壳. - 图像必须是
linux/amd64并包含/bin/sh,所以不动和刮伤的图像被拒绝. - Mosaic的边缘为HTTPS上空提供预览;
getUrl拒绝任何其他协议.
许可证
麻省理工学院