mcpField
B
bun

bun

github.com/marcioaltoe開発058 回インストール1 日前に更新
リポジトリ

bun について

Bun はアプリケーションの開発とテストに使う JavaScript ランタイムおよびツールキットです。このドキュメントスキルでは、パッケージ管理、サーバー、データベース、ファイル入出力、テスト、バンドル、実行ファイルのコンパイルを扱います。

主な機能

  1. 01パッケージとワークスペース管理
  2. 02HTTP・WebSocket サーバー
  3. 03SQL、SQLite、Redis の利用
  4. 04テストとカバレッジツール
  5. 05バンドルと実行ファイルのコンパイル

活用シーン

  1. 01依存関係のインストールと更新
  2. 02HTTP サービスの構築
  3. 03テストとカバレッジの実行
  4. 04アプリケーションのバンドルやコンパイル

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/marcioaltoe/skills --skill bun

インストール後、Claude Code で/bunを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

agents/
openai.yaml
references/
advanced.md
bundler.md
configuration.md
database.md
file-io.md
http-server.md
networking.md
shell.md
testing.md

詳細

Bun

クイックスタート

# Install
curl -fsSL https://bun.sh/install | bash

# Init project
bun init

# Run TypeScript directly
bun run index.ts

# Watch mode (hard restart on change)
bun --watch index.ts

# Hot reload (preserves global state, no restart)
bun --hot server.ts

パッケージ管理

bun install                    # install all deps
bun add express                # add dependency
bun add -d @types/node         # add dev dependency
bun remove lodash              # remove
bun update                     # update all
bun update --latest            # ignore semver ranges
bunx prettier --write .        # execute package binary (npx equivalent)
bun patch express              # patch a dependency in node_modules
ワークスペース
// root package.json
{ "workspaces": ["packages/*"] }

// child package.json
{ "dependencies": { "shared": "workspace:*" } }
bun install --filter "pkg-*"   # install filtered workspaces
環境変数

.env ファイルは次の順序で自動的に読み込まれます:.env → .env.$(NODE_ENV) → .env.local

Bun.env.API_KEY       // Bun-native
process.env.API_KEY   // Node.js compat
import.meta.env.API_KEY
bun --env-file=.env.staging run start

HTTP サーバー

Bun.serve({
  port: 3000,
  routes: {
    "/": new Response("Home"),
    "/users/:id": (req) => Response.json({ id: req.params.id }),
    "/api/posts": {
      GET: () => Response.json([]),
      POST: async (req) => Response.json(await req.json(), { status: 201 }),
    },
    "/api/*": Response.json({ error: "Not found" }, { status: 404 }),
    "/favicon.ico": Bun.file("./favicon.ico"),
  },
  fetch(req) {
    return new Response("Not Found", { status: 404 });
  },
});
WebSocket アップグレード
Bun.serve({
  fetch(req, server) {
    if (server.upgrade(req, { data: { userId: "123" } })) return;
    return new Response("Not a WebSocket", { status: 400 });
  },
  websocket: {
    open(ws) { ws.subscribe("chat"); },
    message(ws, msg) { ws.publish("chat", msg); },
    close(ws) {},
  },
});
フルスタック(HTML インポート)
import homepage from "./index.html";

Bun.serve({
  routes: { "/": homepage },
  development: true, // enables HMR
});

Cookie、静的ルート、TLS、サーバーのライフサイクル、メトリクスについては、references/http-server.md を参照してください。

データベース

Bun.sql(Postgres/MySQL/SQLite)
import { sql, SQL } from "bun";

// Auto-reads DATABASE_URL / POSTGRES_URL / MYSQL_URL
const users = await sql`SELECT * FROM users WHERE active = ${true}`;

// Explicit connection
const pg = new SQL("postgres://user:pass@localhost:5432/mydb");
const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
const sqlite = new SQL(":memory:");

// Insert with object helper
const [user] = await sql`INSERT INTO users ${sql({ name: "Alice", email: "[email protected]" })} RETURNING *`;

// Bulk insert
await sql`INSERT INTO users ${sql([user1, user2, user3])}`;

// Transactions
await sql.begin(async (tx) => {
  const [u] = await tx`INSERT INTO users ${sql({ name: "Bob" })} RETURNING *`;
  await tx`INSERT INTO accounts (user_id) VALUES (${u.id})`;
});
bun:sqlite(同期型、組み込み)
import { Database } from "bun:sqlite";

const db = new Database("app.db");
db.run("CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY, val TEXT)");
const row = db.query("SELECT * FROM kv WHERE key = ?").get("foo");
const all = db.query("SELECT * FROM kv").all();

// Class mapping
class User { id!: number; name!: string; }
const users = db.query("SELECT * FROM users").as(User).all();
Bun.redis
import { redis } from "bun";

await redis.set("key", "value", { ex: 60 });
const val = await redis.get("key");
await redis.del("key");
await redis.hmset("user:1", { name: "Alice", role: "admin" });

トランザクション、セーブポイント、MySQL/SQLite 固有の情報、Redis の Pub/Sub、接続オプションについては、references/database.md を参照してください。

シェル($)

import { $ } from "bun";

// Run and print to stdout
await $`echo "Hello"`;

// Capture output
const text = await $`ls -la`.text();
const data = await $`cat config.json`.json();
for await (const line of $`cat file.txt`.lines()) { }

// Pipes
await $`cat file.txt | grep "pattern" | wc -l`;

// Redirect from/to JS objects
await $`cat < ${new Response("data")} > ${Bun.file("out.txt")}`;

// Error handling
const { exitCode } = await $`may-fail`.nothrow().quiet();

// Config
$.cwd("/tmp");
$.env({ ...process.env, NODE_ENV: "production" });

セキュリティ: 補間された変数は自動的にエスケープされます(シェルインジェクションは発生しません)。回避するには { raw: str } を使用してください。

組み込み機能、コマンド置換、ブレース展開については、references/shell.md を参照してください。

ファイル I/O と S3

// Read
const file = Bun.file("data.json");
const json = await file.json();       // also: .text(), .bytes(), .stream(), .arrayBuffer()
console.log(file.size, file.type);    // size in bytes, MIME type

// Write
await Bun.write("out.txt", "hello");
await Bun.write(Bun.file("copy.bin"), Bun.file("src.bin")); // copy file

// Incremental write
const writer = Bun.file("log.txt").writer();
writer.write("line 1\n");
writer.write("line 2\n");
writer.end();

// S3
import { s3 } from "bun";
const obj = s3.file("data.json");
const data = await obj.json();
await obj.write(JSON.stringify({ ok: true }));
const url = obj.presign({ expiresIn: 3600, method: "PUT" });
await obj.delete();

// s3:// protocol
const res = await fetch("s3://bucket/file.txt");

// Glob
const glob = new Bun.Glob("**/*.ts");
for await (const path of glob.scan(".")) console.log(path);

S3 認証情報、マルチパートアップロード、ストリーム、ハッシュ化、semver については、references/file-io.md を参照してください。

テスト

import { test, expect, describe, mock, spyOn, beforeEach } from "bun:test";

describe("math", () => {
  test("adds", () => expect(1 + 1).toBe(2));

  test.each([
    [1, 2, 3],
    [4, 5, 9],
  ])("%i + %i = %i", (a, b, expected) => {
    expect(a + b).toBe(expected);
  });

  test.skip("wip", () => {});
  test.todo("implement later");
});

// Mock functions
const fn = mock(() => 42);
fn();
expect(fn).toHaveBeenCalled();

// Module mocking
mock.module("./db", () => ({
  query: mock(() => []),
}));

// Spies
const spy = spyOn(console, "log");
console.log("test");
expect(spy).toHaveBeenCalledWith("test");

// Snapshots
test("snap", () => {
  expect({ a: 1, b: "hello" }).toMatchSnapshot();
});
bun test                          # run all tests
bun test --watch                  # watch mode
bun test --coverage               # with coverage
bun test --update-snapshots       # update snapshots
bun test --bail                   # stop on first failure
bun test --test-name-pattern "add" # filter by name

すべてのマッチャー、ライフサイクルフック、型テスト、再試行/繰り返し、DOM テストについては、references/testing.md を参照してください。

バンドラーとコンパイル

// Bundle for browser
const result = await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  splitting: true,
  minify: true,
  sourcemap: "linked",
  target: "browser", // or "bun", "node"
});

if (!result.success) {
  for (const log of result.logs) console.error(log);
}
# Compile to standalone executable
bun build --compile --minify --sourcemap --bytecode ./app.ts --outfile myapp

# Cross-compile
bun build --compile --target=bun-linux-x64 ./app.ts --outfile myapp-linux
bun build --compile --target=bun-darwin-arm64 ./app.ts --outfile myapp-mac
// Embed files in executable
import icon from "./icon.png" with { type: "file" };
import db from "./data.db" with { type: "sqlite", embed: "true" };

すべてのビルドオプション、プラグイン、クロスコンパイルのターゲット、Windows オプション、Transpiler API については、references/bundler.md を参照してください。

子プロセス

// Async
const proc = Bun.spawn(["ls", "-la"], {
  cwd: "/tmp",
  stdout: "pipe",
});
const output = await new Response(proc.stdout).text();
await proc.exited;

// Sync
const { stdout, exitCode } = Bun.spawnSync(["echo", "hi"]);

// Timeout + abort
const ctrl = new AbortController();
Bun.spawn(["sleep", "100"], { signal: ctrl.signal, timeout: 5000 });

// IPC between bun processes
const child = Bun.spawn(["bun", "worker.ts"], {
  ipc(msg) { console.log("from child:", msg); },
});
child.send({ type: "start" });

設定(bunfig.toml)

# Common options
preload = ["./setup.ts"]
logLevel = "warn"

[run]
shell = "bun"                    # use Bun's shell instead of system shell
bun = true                       # alias node to bun in scripts

[test]
preload = ["./test-setup.ts"]
coverage = true
coverageThreshold = 0.8
retry = 2

[install]
exact = true                     # pin exact versions
frozenLockfile = true            # CI: fail if lockfile out of date
auto = "fallback"                # auto-install missing packages

bunfig.toml の全項目については、references/configuration.md を参照してください。

リファレンス一覧

トピックリファレンス
HTTP サーバー、ルーティング、WebSocket、Cookie、フルスタックreferences/http-server.md
Bun.sql、bun:sqlite、Bun.redisreferences/database.md
TCP、UDP、DNS、fetchreferences/networking.md
Bun Shell($)references/shell.md
bun:test、モック、スナップショット、カバレッジreferences/testing.md
Bun.build、実行可能ファイルへのコンパイル、プラグインreferences/bundler.md
Bun.file、S3、Glob、ストリーム、ハッシュ化、semverreferences/file-io.md
bunfig.toml の全項目references/configuration.md
Workers、HTMLRewriter、FFI、C コンパイラー、シークレット、Node.js 互換性references/advanced.md

コメント