@computesdk/microsandbox

中文文档
0.1.26发布于 2026/11/10MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1万

Microsandbox provider for ComputeSDK with local and cloud microVM backends

原始使用文档
文档目录7 节
  1. Requirements
  2. Installation
  3. Cloud quick start
  4. Local quick start
  5. Configuration
  6. Backend support
  7. License

@computesdk/microsandbox

Microsandbox provider for ComputeSDK. It runs the same sandbox API against hardware-isolated local microVMs or microsandbox cloud.

Requirements

  • Node.js 22 or newer
  • Cloud backend: microsandbox cloud access and an API key
  • Local backend: macOS on Apple Silicon, Linux with KVM, or Windows with Windows Hypervisor Platform

Installation

npm install computesdk @computesdk/microsandbox

Cloud quick start

Cloud is the default backend. Pass an API key directly or omit it to use the microsandbox SDK's MSB_API_KEY, MSB_PROFILE, or active-profile resolution:

import { compute } from 'computesdk';
import { microsandbox } from '@computesdk/microsandbox';

compute.setConfig({
  provider: microsandbox({
    apiKey: process.env.MSB_API_KEY,
    image: 'node:22',
  }),
});

const sandbox = await compute.instances.create();
const result = await sandbox.runCommand('node --version');
console.log(result.stdout);
await sandbox.destroy();

Local quick start

Select the local backend explicitly when the sandbox should run on the calling machine. Local mode does not require an account or API key:

import { compute } from 'computesdk';
import { microsandbox } from '@computesdk/microsandbox';

compute.setConfig({
  provider: microsandbox({
    backend: 'local',
    image: 'node:22',
    ports: [{ host: 3000, guest: 3000 }],
  }),
});

const sandbox = await compute.instances.create();
await sandbox.filesystem.writeFile('/tmp/hello.js', 'console.log("hello")');
console.log((await sandbox.runCommand('node /tmp/hello.js')).stdout);
await sandbox.destroy();

Calling microsandbox() with no configuration uses cloud through the microsandbox SDK's standard environment and active-profile resolution. If no cloud credentials resolve, the provider reports how to configure cloud or opt into backend: 'local'; it never silently falls back to local.

Use a named cloud profile instead of an API key when appropriate:

const provider = microsandbox({ profile: 'production' });

Configuration

interface MicrosandboxConfig {
  backend?: 'cloud' | 'local'; // Defaults to cloud
  apiKey?: string;             // Falls back to MSB_API_KEY
  apiUrl?: string;             // Optional endpoint override; requires apiKey
  profile?: string;            // Named cloud profile; mutually exclusive with apiKey/apiUrl
  image?: string;
  cpus?: number;
  memoryMib?: number;
  rootDiskMib?: number;
  ephemeral?: boolean;         // Delete on stop; defaults to true
  workdir?: string;
  namePrefix?: string;
  ports?: Array<number | { host: number; guest: number; bind?: string }>;
  timeout?: number;           // Idle and command timeout in milliseconds; defaults to 900_000 (15 minutes)
  pullPolicy?: 'always' | 'if-missing' | 'never';
  networkEnabled?: boolean;
}

Per-sandbox image, templateId, snapshotId, cpus, vcpus, memory, memoryMb, memoryMiB, memoryMib, memMiB, rootDiskMib, ephemeral, timeout, name, envs, metadata, and ports options override or extend provider defaults where applicable.

Backend support

Operations run concurrently using the selected process-wide backend. Do not change the native SDK default backend while provider operations are running.

MethodLocalCloud
create, getById, list, destroyYesYes
runCommand and live output callbacksYesYes
filesystemYesYes
getInfoYesYes
getUrlYes, for ports declared before createNot currently available
snapshotYes, disk stateNot currently available

Local snapshots stop the sandbox, capture its writable root disk, and restart it. Files on that disk survive the restore, but tmpfs paths such as /tmp and running processes are not captured. Microsandbox cloud does not currently support published ports or disk snapshots, so those methods return explicit unsupported errors on cloud.

License

MIT

Sandboxes are ephemeral by default and are deleted when they stop. Their default idle timeout is 15 minutes, configured through the native SDK’s idleTimeout(900). Set ephemeral: false to retain a sandbox after stopping, and use timeout (milliseconds) to override the idle timeout and default command timeout. The adapter does not set a maximum lifetime.

Use one backend configuration per process. Concurrent operations share that configuration; the first successful backend selection is retained for the lifetime of the process. Later requests with different credentials, endpoints, profiles, or backend selection are rejected before changing the SDK backend. Use the same explicit configuration everywhere, or consistently use SDK environment/profile resolution.

版本与发布记录

@computesdk/microsandbox 共发布 26 个版本,首次发布于 2026/08/19;过去 12 个月发布了 26 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.26latest2026/11/10今天
0.1.252026/11/064 天前
0.1.242026/11/046 天前
0.1.232026/10/3110 天前
0.1.222026/10/2812 天前
0.1.212026/10/2813 天前

关于 @computesdk/microsandbox 的常见问题

@computesdk/microsandbox 是什么?

Microsandbox provider for ComputeSDK with local and cloud microVM backends

如何安装 @computesdk/microsandbox?

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

@computesdk/microsandbox 支持 TypeScript 吗?

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

@computesdk/microsandbox 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@computesdk/microsandbox 需要什么版本的 Node.js?

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

@computesdk/microsandbox 可以商用吗?

@computesdk/microsandbox 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@computesdk/microsandbox 的下载量有多少?

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

@computesdk/microsandbox 还在维护吗?

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