Skip to content

Commit 019c606

Browse files
committed
docs: add bun details
1 parent c3ff2a7 commit 019c606

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

‎.claude/rules/bun-native-apis.md‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
paths:
3+
- "packages/cli/**/*.ts"
4+
- "packages/server/**/*.ts"
5+
- "packages/skills/**/*.ts"
6+
- "packages/storage/**/*.ts"
7+
- "packages/harness/**/*.ts"
8+
- "packages/instrumentation/src/node/**/*.ts"
9+
- "packages/*/scripts/**/*.ts"
10+
---
11+
12+
# Bun-native APIs
13+
14+
This monorepo runs on Bun. In code that's only ever executed by Bun — the `contextbridge` binary, the local HTTP server, codegen and build scripts, the SQLite layer — reach for `Bun.*` globals before the Node equivalent. Training-data bias makes the Node API the default suggestion; this rule lists the cases where the Bun shape is materially shorter or more idiomatic and should win.
15+
16+
The trigger globs above already exclude browser packages (`annotation`, `website`, `ui`) and isomorphic ones (`context`, `shared`, `instrumentation/browser|shared`). If you find yourself in one of those, treat `Bun.*` as off-limits.
17+
18+
For anything not covered below, the canonical reference is <https://bun.sh/llms-full.txt> — it concatenates Bun's full docs into a single markdown file.
19+
20+
## File system reads
21+
22+
`Bun.file(path).text() / .json() / .bytes()` instead of `readFileSync` + `JSON.parse`. Reads are lazy: no I/O until the accessor is awaited.
23+
24+
```ts
25+
// Good
26+
const content = await Bun.file(argPath).text();
27+
const config = await Bun.file(configPath).json<Config>();
28+
29+
// Avoid
30+
const content = readFileSync(argPath, 'utf8');
31+
const config = JSON.parse(readFileSync(configPath, 'utf8')) as Config;
32+
```
33+
34+
In-repo example: `packages/cli/src/commands/open.ts`.
35+
36+
## File system writes
37+
38+
`Bun.write(path, data)` instead of `writeFileSync`. Auto-creates parent directories, accepts strings, `Bun.file` handles, `ArrayBuffer`, or `Response`.
39+
40+
```ts
41+
// Good
42+
await Bun.write(outPath, rendered);
43+
44+
// Avoid
45+
mkdirSync(dirname(outPath), { recursive: true });
46+
writeFileSync(outPath, rendered, 'utf8');
47+
```
48+
49+
## Globbing & directory walks
50+
51+
`new Bun.Glob(pattern).scanSync(root)` (or async `scan`) instead of `readdirSync({ recursive: true })` and instead of any hand-rolled recursive `walk()`. The recursive `readdirSync` flag works but doesn't pattern-filter; rolling a walk yourself is the most common Node carry-over and the most consistently corrected one in this repo's PR review.
52+
53+
```ts
54+
// Good
55+
const files = Array.from(new Bun.Glob('**/*.md').scanSync(rootDir)).sort();
56+
57+
// Avoid
58+
function walk(dir: string): string[] {
59+
/* recursive readdirSync... */
60+
}
61+
const files = walk(rootDir).filter((p) => p.endsWith('.md'));
62+
```
63+
64+
In-repo example: `packages/skills/src/handlebars.ts`.
65+
66+
## Subprocesses
67+
68+
`Bun.spawn` / `Bun.spawnSync` instead of `child_process.spawn` / `exec`. Use `Bun.which` instead of probing `$PATH` manually.
69+
70+
```ts
71+
// Good
72+
const proc = Bun.spawn({ cmd: [bin, ...args], stdout: 'pipe', stderr: 'pipe' });
73+
const stdout = await new Response(proc.stdout).text();
74+
const exitCode = await proc.exited;
75+
76+
// Avoid
77+
const { stdout } = await promisify(execFile)(bin, args);
78+
```
79+
80+
In-repo example: `packages/cli/src/CommandRunnerImpl.ts`.
81+
82+
## HTTP servers
83+
84+
`Bun.serve` instead of Express, Fastify, or `node:http`. New endpoints go on the existing `Bun.serve` instance in `packages/server`.
85+
86+
## SQLite
87+
88+
`bun:sqlite` instead of `better-sqlite3` or `node:sqlite`. The `@contextbridge/storage` package already does this — match its pattern when extending storage.
89+
90+
## Compile-time evaluation
91+
92+
Two related Bun-only import attribute forms; reach for them whenever the value is fixed at bundle time.
93+
94+
- `with { type: 'text' }` to embed a file's contents as a string literal at build. Used in `packages/skills/src/codex.ts` to bake rendered SKILL.md files into the CLI binary.
95+
- `with { type: 'macro' }` to evaluate a function at build time and inline its return value. Right shape for version stamps, fixture generation, build-time enumeration of files, and anything else that's a pure function of the source tree.
96+
97+
If you catch yourself writing a `readFileSync(...)` or a constant lookup at the top of a module to compute something the bundler could compute once, it's a macro.
98+
99+
## What not to use
100+
101+
- `fs.readdirSync(..., { recursive: true })` walks → use `Bun.Glob`.
102+
- Hand-rolled recursive `walk()` helpers → use `Bun.Glob`.
103+
- `child_process.exec` / `execFile` → use `Bun.spawn`.
104+
- `node:http` for new servers → use `Bun.serve`.
105+
- `better-sqlite3` / `node:sqlite` → use `bun:sqlite`.
106+
107+
`readFileSync` and `writeFileSync` are not banned outright — they're fine in a tight synchronous loop where `await` would force a redundant restructure — but `Bun.file` / `Bun.write` should be the default.

0 commit comments

Comments
 (0)