@computesdk/cloudflare

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

Cloudflare provider for ComputeSDK - edge code execution using Cloudflare Workers and Durable Objects

原始使用文档
文档目录16 节
  1. Installation
  2. Setup
  3. Preview URLs
  4. Snapshots
  5. Quick Start
  6. Usage
  7. Run Commands
  8. List Files
  9. File System
  10. Environment Variables
  11. Sandbox Size and Image
  12. Configuration
  13. Direct Mode
  14. Error Handling
  15. Limitations
  16. License

@computesdk/cloudflare

Cloudflare provider for ComputeSDK — execute code in secure, isolated containers on Cloudflare's edge network.

Installation

npm install @computesdk/cloudflare

Setup

Deploy the bridge Worker in worker/. It needs a Cloudflare account on the Workers Paid plan and Docker running locally; wrangler deploy builds the container images and pushes them to Cloudflare's registry.

git clone https://github.com/computesdk/computesdk.git
cd computesdk/packages/cloudflare/worker
npm ci
npx wrangler login
openssl rand -hex 32   # use the output as the API key
npx wrangler secret put SANDBOX_API_KEY
npx wrangler deploy

Then configure your application with the deployed Worker URL and the same API key:

CLOUDFLARE_SANDBOX_URL=https://computesdk-sandbox.<your-subdomain>.workers.dev
CLOUDFLARE_SANDBOX_API_KEY=<same value as SANDBOX_API_KEY>

You can also run:

npx @computesdk/cloudflare

to print these setup instructions.

A Worker deployed from the Cloudflare sandbox demo keeps working for commands and files. The adapter checks GET /v1/info and uses the bridge's native file transfer, preview URLs and snapshots only when the Worker lists them.

Preview URLs

With the bridge Worker, sandbox.getUrl({ port }) returns <sandboxUrl>/v1/sandbox/<id>/ports/<port>/, which forwards HTTP and WebSocket requests to that port. The server must listen on 0.0.0.0. Preview URLs need no API key: anyone with the URL can reach the port.

Snapshots

With the bridge Worker, compute.snapshot.create(sandboxId) saves the sandbox's filesystem and compute.instances.create({ snapshotId }) starts a new sandbox from it. Snapshots keep files only, not memory or running processes. Cloudflare keeps snapshot data for 30 days after its creation or last restore; snapshot.delete removes the handle from the bridge.

Quick Start

import { cloudflare } from '@computesdk/cloudflare';

const compute = cloudflare({
  sandboxUrl: process.env.CLOUDFLARE_SANDBOX_URL,
  sandboxApiKey: process.env.CLOUDFLARE_SANDBOX_API_KEY,
});

const sandbox = await compute.instances.create();

const result = await sandbox.runCommand(
  "node -e 'console.log(\"Hello from Cloudflare!\")'"
);

console.log(result.stdout);
await sandbox.destroy();

Usage

Run Commands

Use normal shell commands inside the sandbox:

await sandbox.runCommand('echo "Hello from Cloudflare"');
await sandbox.runCommand("node -e 'console.log(\"Hello Node.js\")'");

List Files

const result = await sandbox.runCommand('ls -la /tmp/app');
console.log(result.stdout);

File System

// Create directories
await sandbox.filesystem.mkdir('/tmp/app/data');

// Write and read files
await sandbox.filesystem.writeFile('/tmp/app/config.json', JSON.stringify({ key: 'value' }));
const content = await sandbox.filesystem.readFile('/tmp/app/config.json');

// List directory contents
const files = await sandbox.filesystem.readdir('/tmp/app');

// Check existence
const exists = await sandbox.filesystem.exists('/tmp/app/config.json');

// Remove files
await sandbox.filesystem.remove('/tmp/app/temp.txt');

Environment Variables

Pass environment variables to sandbox commands at initialization:

const compute = cloudflare({
  sandboxUrl: process.env.CLOUDFLARE_SANDBOX_URL,
  sandboxApiKey: process.env.CLOUDFLARE_SANDBOX_API_KEY,
  envVars: {
    API_KEY: 'your-api-key',
    DATABASE_URL: 'postgresql://localhost:5432/mydb',
  },
});

Or per-sandbox at creation time:

const sandbox = await compute.instances.create({
  envs: { NODE_ENV: 'production' },
});

Sandbox Size and Image

Choose the size and image of a new sandbox when you create it:

const sandbox = await compute.instances.create({
  vcpu: 2,
  memoryMib: 6144,
  diskMb: 12000,
  image: 'builder',
});
OptionDescription
vcpuvCPUs of a custom instance type
memoryMibMemory of the custom instance type, in MiB
diskMbDisk of the custom instance type, in MB
imageName of an image declared by the Worker. builder has Node.js 24 and common build tools.

Set vcpu, memoryMib, and diskMb together. Without them, the sandbox uses the Worker's default instance type. Without image, it uses the Worker's default image.

Configuration

interface CloudflareConfig {
  /** URL of the deployed bridge Worker (or sandbox demo Worker) */
  sandboxUrl?: string;
  /** API key that matches the Worker's SANDBOX_API_KEY secret */
  sandboxApiKey?: string;
  /** Deprecated compatibility alias for sandboxApiKey */
  sandboxSecret?: string;
  /** Durable Object binding to the Worker's Sandbox class (direct mode only) */
  sandboxBinding?: any;
  /** Execution timeout in milliseconds (default 30,000; maximum 900,000) */
  timeout?: number;
  /** Environment variables to pass to sandbox commands */
  envVars?: Record<string, string>;
}

Direct Mode

If your code already runs inside a Cloudflare Worker, bind directly to the Sandbox Durable Object exported by the Worker:

{
  "durable_objects": {
    "bindings": [
      {
        "name": "SANDBOX",
        "class_name": "Sandbox",
        "script_name": "sandbox"
      }
    ]
  }
}

Then pass that binding to the provider:

import { cloudflare } from '@computesdk/cloudflare';

const compute = cloudflare({
  sandboxBinding: env.SANDBOX,
});

Error Handling

const result = await sandbox.runCommand('command-that-does-not-exist');
if (result.exitCode !== 0) {
  console.error(result.stderr);
}

Limitations

  • Resource limits apply based on your Cloudflare plan
  • Some system calls may be restricted in the container environment
  • Preview URLs and snapshots need the bridge Worker, and are not available in direct mode
  • Listing all sandboxes is not supported — use getById to reconnect to a specific sandbox ID.

License

MIT

版本与发布记录

@computesdk/cloudflare 共发布 92 个版本,首次发布于 2025/09/13;过去 12 个月发布了 86 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.12latest2026/11/064 天前
2.1.112026/11/046 天前
2.1.102026/10/2812 天前
2.1.92026/10/2317 天前
2.1.82026/10/2318 天前
2.1.72026/10/2218 天前

关于 @computesdk/cloudflare 的常见问题

@computesdk/cloudflare 是什么?

Cloudflare provider for ComputeSDK - edge code execution using Cloudflare Workers and Durable Objects

如何安装 @computesdk/cloudflare?

在项目目录运行 npm install @computesdk/cloudflare,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 computesdk-cloudflare,可以用 npx computesdk-cloudflare 直接运行。

@computesdk/cloudflare 支持 TypeScript 吗?

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

@computesdk/cloudflare 支持 ESM 还是 CommonJS?

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

@computesdk/cloudflare 可以商用吗?

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

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

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

@computesdk/cloudflare 还在维护吗?

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