@computesdk/just-bash

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

just-bash provider for ComputeSDK - local sandboxed bash execution with virtual filesystem

原始使用文档
文档目录26 节
  1. Installation
  2. Quick Start
  3. Configuration
  4. Environment Variables
  5. Configuration Options
  6. Filesystem Backends
  7. InMemoryFs (default)
  8. OverlayFs (copy-on-write)
  9. ReadWriteFs (direct disk access)
  10. MountableFs (compose multiple filesystems)
  11. Custom Commands
  12. API Reference
  13. Code Execution
  14. Command Execution
  15. Filesystem Operations
  16. Sandbox Management
  17. Pre-populated Files
  18. Built-in Commands
  19. Error Handling
  20. Examples
  21. Data Pipeline
  22. JSON Processing
  23. Script Execution
  24. Limitations
  25. When to Use just-bash
  26. License

@computesdk/just-bash

just-bash provider for ComputeSDK - Local sandboxed bash execution with a virtual filesystem. No external services, containers, or authentication required.

Installation

npm install @computesdk/just-bash

Quick Start

import { justBash } from '@computesdk/just-bash';

const compute = justBash({});

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

const result = await sandbox.runCommand('echo "Hello" | tr a-z A-Z');
console.log(result.stdout); // "HELLO"

await sandbox.destroy();

Configuration

Environment Variables

No environment variables are required. just-bash runs entirely locally.

Configuration Options

interface JustBashConfig {
  /** Enable Python support via pyodide (disabled by default) */
  python?: boolean;
  /** Initial files to populate in the virtual filesystem */
  files?: Record<string, string>;
  /** Initial environment variables */
  env?: Record<string, string>;
  /** Working directory (defaults to /home/user) */
  cwd?: string;
  /** Custom filesystem implementation (see Filesystem Backends below) */
  fs?: IFileSystem;
  /** Custom commands created with defineCommand() (see Custom Commands below) */
  customCommands?: CustomCommand[];
  /** Network configuration for curl (disabled by default) */
  network?: NetworkConfig;
}

Filesystem Backends

By default, just-bash uses an InMemoryFs — a pure in-memory filesystem. You can swap in alternative backends depending on your use case:

InMemoryFs (default)

All files live in memory. Fast, deterministic, fully isolated.

const compute = justBash({}); // InMemoryFs is the default

OverlayFs (copy-on-write)

Reads from a real directory on disk; writes stay in memory. The underlying directory is never modified.

import { OverlayFs } from 'just-bash';

const compute = justBash({
  fs: new OverlayFs({ root: '/path/to/project' }),
  cwd: '/path/to/project',
});

ReadWriteFs (direct disk access)

Reads and writes go directly to a real directory. Use with caution — changes are real.

import { ReadWriteFs } from 'just-bash';

const compute = justBash({
  fs: new ReadWriteFs({ root: '/tmp/sandbox' }),
});

MountableFs (compose multiple filesystems)

Mount different filesystem backends at different paths.

import { MountableFs, InMemoryFs } from 'just-bash';
import { OverlayFs } from 'just-bash';

const compute = justBash({
  fs: new MountableFs({
    base: new InMemoryFs(),
    mounts: [
      { mountPoint: '/project', filesystem: new OverlayFs({ root: '/real/project' }) },
    ],
  }),
  cwd: '/project',
});

Custom Commands

You can extend just-bash with custom commands using defineCommand():

import { justBash } from '@computesdk/just-bash';
import { defineCommand } from 'just-bash';

const hello = defineCommand('hello', async (args, ctx) => {
  const name = args[0] || 'world';
  return {
    stdout: `Hello, ${name}!\n`,
    stderr: '',
    exitCode: 0,
  };
});

const compute = justBash({ customCommands: [hello] });
const sandbox = await compute.instances.create();

const result = await sandbox.runCommand('hello Alice');
console.log(result.stdout); // "Hello, Alice!\n"

Custom commands receive a context object (ctx) with access to:

  • ctx.fs — the virtual filesystem
  • ctx.cwd — current working directory
  • ctx.env — environment variables
  • ctx.stdin — standard input
  • ctx.exec(command) — run subcommands

API Reference

Code Execution

// Execute bash scripts
const result = await sandbox.runCommand(`
  for i in 1 2 3; do
    echo "Number: $i"
  done
`);
console.log(result.stdout);
// Number: 1
// Number: 2
// Number: 3

// Execute Python code (requires python: true in config)
const compute = justBash({ python: true });
const sandbox = await compute.instances.create();

const result = await sandbox.runCommand(`python - <<'PY'
import json
data = {"message": "Hello from Python"}
print(json.dumps(data))
PY`);

Command Execution

// Pipes and text processing
const result = await sandbox.runCommand('echo -e "banana\\napple\\ncherry" | sort');
console.log(result.stdout); // "apple\nbanana\ncherry\n"

// JSON processing with jq
await sandbox.filesystem.writeFile('/data.json', '[{"name":"Alice"},{"name":"Bob"}]');
const result = await sandbox.runCommand('cat /data.json | jq ".[].name"');

// Environment variables per command
const result = await sandbox.runCommand('echo $MY_VAR', { env: { MY_VAR: 'hello' } });

// Working directory per command
const result = await sandbox.runCommand('cat config.json', { cwd: '/app' });

Filesystem Operations

// Write file
await sandbox.filesystem.writeFile('/tmp/hello.txt', 'Hello World');

// Read file
const content = await sandbox.filesystem.readFile('/tmp/hello.txt');

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

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

// Check if file exists
const exists = await sandbox.filesystem.exists('/tmp/hello.txt');

// Remove file or directory
await sandbox.filesystem.remove('/tmp/hello.txt');

Sandbox Management

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

// List all active sandboxes
const sandboxes = await compute.instances.list();

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

// Destroy sandbox
await sandbox.destroy();

Pre-populated Files

const compute = justBash({
  files: {
    '/app/config.json': '{"port": 3000}',
    '/app/data.csv': 'name,age\nAlice,25\nBob,30',
  },
  env: {
    APP_ENV: 'production',
  },
  cwd: '/app',
});

const sandbox = await compute.instances.create();
const result = await sandbox.runCommand('cat config.json');
console.log(result.stdout); // {"port": 3000}

Built-in Commands

just-bash includes 60+ built-in commands:

CategoryCommands
File opscat, cp, ls, mkdir, mv, rm, touch, tree, ln, find
Text processingawk, grep, sed, cut, sort, uniq, wc, head, tail, tr
Data processingjq (JSON), yq (YAML/XML), sqlite3 (SQLite), CSV tools
Compressiongzip, tar
Utilitiesecho, printf, date, seq, timeout, basename, dirname
Shellexport, source, alias, test, read, set, unset

Error Handling

import { justBash } from '@computesdk/just-bash';

const compute = justBash({});
const sandbox = await compute.instances.create();

const result = await sandbox.runCommand('cat /nonexistent');
if (result.exitCode !== 0) {
  console.error('Command failed:', result.stderr);
}

// getUrl throws since just-bash has no network
try {
  await sandbox.getUrl({ port: 3000 });
} catch (error) {
  console.error(error.message); // "just-bash is a local sandbox without network capabilities..."
}

Examples

Data Pipeline

import { justBash } from '@computesdk/just-bash';

const compute = justBash({});
const sandbox = await compute.instances.create();

// Create CSV data
await sandbox.filesystem.writeFile('/data/sales.csv',
  'product,quantity,price\nWidget,100,9.99\nGadget,50,24.99\nDoohickey,200,4.99'
);

// Process with awk
const result = await sandbox.runCommand(
  'cat /data/sales.csv | tail -n +2 | awk -F, \'{ total += $2 * $3 } END { printf "Total revenue: $%.2f\\n", total }\''
);
console.log(result.stdout); // Total revenue: $3247.50

await sandbox.destroy();

JSON Processing

import { justBash } from '@computesdk/just-bash';

const compute = justBash({
  files: {
    '/data/users.json': JSON.stringify([
      { name: 'Alice', age: 30, role: 'admin' },
      { name: 'Bob', age: 25, role: 'user' },
      { name: 'Charlie', age: 35, role: 'admin' },
    ]),
  },
});
const sandbox = await compute.instances.create();

// Filter admins and extract names
const result = await sandbox.runCommand(
  'cat /data/users.json | jq \'[.[] | select(.role == "admin") | .name]\''
);
console.log(result.stdout); // ["Alice", "Charlie"]

await sandbox.destroy();

Script Execution

import { justBash } from '@computesdk/just-bash';

const compute = justBash({});
const sandbox = await compute.instances.create();

const result = await sandbox.runCommand(`
#!/bin/bash
count=0
for f in /proc/self/status /etc/hostname; do
  if test -f "$f"; then
    count=$((count + 1))
  fi
done
echo "Found $count system files"
`);

console.log(result.stdout);

await sandbox.destroy();

Limitations

  • No Network Access - getUrl() is not supported; curl requires explicit network config
  • No Real Processes - Commands are interpreted in TypeScript, not executed as real OS processes
  • No Native Node.js Runtime - use shell commands; this provider is bash-first and does not run a real Node.js VM
  • In-Memory by Default - Files don't persist unless you use OverlayFs, ReadWriteFs, or MountableFs
  • Python via Pyodide - Python support requires python: true and uses pyodide (WebAssembly-based)

When to Use just-bash

  • Testing & Development - Fast, no-cost sandbox for development and CI
  • AI Agent Tools - Secure bash execution for AI code generation
  • Offline Environments - No network or API keys required
  • Unit Tests - Deterministic, isolated test environments

License

MIT

版本与发布记录

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

版本发布日期距今
0.4.33latest2026/11/064 天前
0.4.322026/11/046 天前
0.4.312026/10/2813 天前
0.4.302026/10/2318 天前
0.4.292026/10/2318 天前
0.4.282026/10/2219 天前

关于 @computesdk/just-bash 的常见问题

@computesdk/just-bash 是什么?

just-bash provider for ComputeSDK - local sandboxed bash execution with virtual filesystem

如何安装 @computesdk/just-bash?

在项目目录运行 npm install @computesdk/just-bash,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:just-bash(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@computesdk/just-bash 支持 TypeScript 吗?

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

@computesdk/just-bash 支持 ESM 还是 CommonJS?

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

@computesdk/just-bash 可以商用吗?

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

@computesdk/just-bash 的下载量有多少?

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

@computesdk/just-bash 还在维护吗?

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