@computesdk/codesandbox

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

计算SDK 的 CodeSandbox 提供者 - 有 npm 和 Python 支持的快速浏览器兼容的沙盒

中文使用文档
文档目录14 节
  1. 安装
  2. 设置
  3. 快速启动
  4. 配置
  5. 环境变量
  6. 配置选项
  7. 特征
  8. API 参考
  9. 命令执行
  10. 文件系统操作
  11. 沙盒管理
  12. 最佳做法
  13. 支助
  14. 许可证

@ computesdk/ 编码框

ComputeSDK的代码Sandbox提供商 - 在安全,孤立的代码Sandbox环境中执行代码,拥有完整的文件系统和发展环境支持.

安装

npm install @computesdk/codesandbox

设置

  1. 获取您的代码Sandbox API 密钥 代码和框.io/t/api
  2. 设置环境变量 :
export CSB_API_KEY=your_api_key_here

快速启动

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

const compute = codesandbox({
  apiKey: process.env.CSB_API_KEY,
  templateId: 'universal', // Optional: specify template
  timeout: 600000 // 10 minutes
});

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

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

await sandbox.destroy();

配置

环境变量

export CSB_API_KEY=your_api_key_here

配置选项

interface CodesandboxConfig {
  /** CodeSandbox API key - if not provided, will use CSB_API_KEY env var */
  apiKey?: string;
  /** Template to use for new sandboxes (defaults to universal template) */
  templateId?: string;
  /** Default runtime environment (e.g. 'node', 'python') */
  runtime?: string;
  /** Execution timeout in milliseconds */
  timeout?: number;
}

特征

  • ✅ 命令执行 - 在沙盒中运行 shell 命令
  • ✅ 文件系统操作 - 通过CodeSandbox API全面访问文件系统
  • ✅ 模板支持 - 从自定义模板创建沙盒
  • ✅ 发展环境 - 与软件包管理者全面开发设置
  • ✅ 持久性 - 档案在休眠/复发周期内持续存在
  • ✅ 快照/续作 - 快速沙盒从快照修复

API 参考

命令执行

// Run Node.js code via heredoc
const result = await sandbox.runCommand(`node - <<'JS'
const fs = require('fs');
const data = { timestamp: Date.now() };
console.log('Processing data:', JSON.stringify(data));
JS`);

// Run Python code via heredoc
const result = await sandbox.runCommand(`python - <<'PY'
import datetime
import json

data = {'timestamp': datetime.datetime.now().isoformat()}
print('Processing data:', json.dumps(data))
PY`);

// List files
const result = await sandbox.runCommand('ls -la');

// Install Node.js packages
const result = await sandbox.runCommand('npm install lodash');

// Install Python packages
const result = await sandbox.runCommand('pip install requests');

// Run development server
const result = await sandbox.runCommand('npm run dev');

文件系统操作

// Write file
await sandbox.filesystem.writeFile('/project/workspace/app.js', `
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello from CodeSandbox!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
`);

// Read file
const content = await sandbox.filesystem.readFile('/project/workspace/package.json');

// Create directory
await sandbox.filesystem.mkdir('/project/workspace/src');

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

// Check if file exists
const exists = await sandbox.filesystem.exists('/project/workspace/app.js');

// Remove file or directory
await sandbox.filesystem.remove('/project/workspace/temp.txt');

沙盒管理

// Get sandbox info
const info = await sandbox.getInfo();
console.log(info.id, info.provider, info.status);

// Resume existing sandbox
const existing = await compute.instances.getById('sandbox-id');

// Hibernate sandbox (saves state)
await compute.instances.destroy('sandbox-id'); // Actually hibernates

// Note: CodeSandbox doesn't support listing all sandboxes
// Each sandbox is managed individually

最佳做法

  1. 资源管理: 使用休眠而不是破坏沙盒来保存状态
  2. 处理错误: 使用试捕取区块进行稳健的错误处理
  3. 超时: 为长期任务设置适当的超时
  4. 文件组织: 组织文件 /project/workspace/ 目录
  5. 模板使用: 为您的项目类型使用适当的模板
  6. API 密钥安全: 从不向版本控制输入 API 密钥
  7. 快照管理: 利用代码Sandbox的快照/重现能力

支助

许可证

麻省理工学院

版本与发布记录

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

版本发布日期距今
1.5.70latest2026/11/064 天前
1.5.692026/11/046 天前
1.5.682026/10/2812 天前
1.5.672026/10/2317 天前
1.5.662026/10/2318 天前
1.5.652026/10/2218 天前

关于 @computesdk/codesandbox 的常见问题

@computesdk/codesandbox 是什么?

计算SDK 的 CodeSandbox 提供者 - 有 npm 和 Python 支持的快速浏览器兼容的沙盒

如何安装 @computesdk/codesandbox?

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

@computesdk/codesandbox 支持 TypeScript 吗?

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

@computesdk/codesandbox 支持 ESM 还是 CommonJS?

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

@computesdk/codesandbox 可以商用吗?

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

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

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

@computesdk/codesandbox 还在维护吗?

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