betting-brain-v3 / rules
Cursor rule8 starsChanged 14 months agoMITView on GitHub
Bun runtime patterns and best practices
- Installs packages
In this repository27 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/99-floor.mdcCursor rule
- .cursor/rules/ai-friendly-testing.mdcCursor rule
- .cursor/rules/analytics-stub-api.mdcCursor rule
- .cursor/rules/analytics-testing.mdcCursor rule
- .cursor/rules/api-patterns.mdcCursor rule
- .cursor/rules/browser-extension.mdcCursor rule
- .cursor/rules/cache-optimization.mdcCursor rule
- .cursor/rules/ci-integration.mdcCursor rule
- .cursor/rules/ci-patterns.mdcCursor rule
- .cursor/rules/cloudflare-infrastructure.mdcCursor rule
- .cursor/rules/cloudflare-workers.mdcCursor rule
- .cursor/rules/code-searchability.mdcCursor rule
- .cursor/rules/coverage-thresholds.mdcCursor rule
- .cursor/rules/database-patterns.mdcCursor rule
- .cursor/rules/documentation.mdcCursor rule
- .cursor/rules/endpoint-routing.mdcCursor rule
- .cursor/rules/file-naming.mdcCursor rule
- .cursor/rules/mcp-integration.mdcCursor rule
- .cursor/rules/process-management.mdcCursor rule
- .cursor/rules/production-security.mdcCursor rule
- .cursor/rules/quality-standards.mdcCursor rule
- .cursor/rules/root-organization.mdcCursor rule
- .cursor/rules/security-patterns.mdcCursor rule
- .cursor/rules/testing.mdcCursor rule
- .cursor/rules/testing-patterns.mdcCursor rule
- .cursor/rules/test-setup-patterns.mdcCursor rule
The file
Files
---
version: "5.0.0"
scope: ["typescript", "javascript", "json", "toml"]
alwaysApply: true
description: "Bun runtime patterns and best practices"
lastUpdated: "2025-10-08"
dependencies: ["quality-standards", "process-management"]
---
# Bun Runtime Rules
## 🔍 Code Searchability Patterns
### Find Bun Runtime Issues with ast-grep
```bash
# Find incorrect process spawning (should use processManager)
ast-grep --pattern 'Bun.spawn($$$)' src/
ast-grep --pattern 'import { spawn } from "bun"' src/
ast-grep --pattern 'import { spawn } from "child_process"' src/
sg -p 'Bun.spawn' src/
sg -p 'from "child_process"' src/
# Find Node.js fs usage (should use Bun file APIs)
ast-grep --pattern 'import fs from "fs"' src/
ast-grep --pattern 'import { readFile } from "fs/promises"' src/
sg -p 'from "fs"' src/
sg -p 'from "fs/promises"' src/
# Find incorrect test imports (should use bun:test)
ast-grep --pattern 'import { describe, test, expect } from "vitest"' tests/
ast-grep --pattern 'import { describe, test, expect } from "@jest/globals"' tests/
sg -p 'from "vitest"' tests/
sg -p 'from "@jest/globals"' tests/
# Find npm/yarn usage (should use bun)
ast-grep --pattern 'npm install' scripts/
ast-grep --pattern 'yarn install' scripts/
sg -p 'npm install' scripts/
sg -p 'yarn install' scripts/
```
### Bun Runtime Discovery Commands
```bash
# Find all Bun-specific patterns
sg search 'Bun.' src/
sg search 'bun:' src/
sg search 'processManager' src/
# Find Node.js anti-patterns
sg search 'from "fs"' src/
sg search 'from "child_process"' src/
sg search 'npm ' scripts/
sg search 'yarn ' scripts/
# Find Bun native APIs
sg search 'Bun.file' src/
sg search 'Bun.write' src/
sg search 'Bun.password' src/
```
## Process Spawning (CRITICAL)
### Always Use Process Manager
```typescript
// ❌ WRONG - Creates zombie processes
import { spawn } from 'bun';
const proc = Bun.spawn(['command']);
// ❌ WRONG - Old Node.js pattern
import { spawn } from 'child_process';
// ✅ CORRECT - Tracked and cleaned up
import processManager from './tests/utils/process-cleanup';
const proc = processManager.spawn(['command'], options);
```
See [Process Management Rules](mdc:.cursor/rules/process-management.mdc) for details.
## File Operations
**Use Bun APIs, not Node.js fs:**
```typescript
// ✅ CORRECT - Bun file API
const file = Bun.file('path/to/file.txt');
const contents = await file.text();
await Bun.write('output.txt', 'content');
// ❌ WRONG - Node.js fs
import fs from 'fs';
import { readFile } from 'fs/promises';
```
## Test Runner
**Use Bun Test, not Vitest or Jest:**
```typescript
// ✅ CORRECT - Bun Test
import { describe, test, expect } from 'bun:test';
// ❌ WRONG - Vitest/Jest imports
import { describe, test, expect } from 'vitest';
```
## Commands
**Always use Bun commands:**
```bash
✅ bun install # Install dependencies
✅ bun run dev # Development server
✅ bun test # Run tests
✅ bun run build # Build project
❌ npm install # WRONG
❌ yarn install # WRONG
❌ node script.js # WRONG - use: bun run script.ts
```
## Configuration
### TypeScript Config
```json
{
"compilerOptions": {
"module": "Preserve",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"types": ["@types/bun"]
}
}
```
### Bun Config (bunfig.toml)
```toml
[test]
timeout = 10000 # 10 seconds default
root = "."
[install]
# Package manager settings
```
## Native APIs
Bun provides native APIs for:
- Crypto
- Compression
- Encoding
- Streams
- Timers
- Workers
- Cron
- JWT
- Hash maps
- UUID
- Path
- Glob
- Watch
- Subprocess
- HTTP client
- WebSocket
- TLS
**Never install external packages for these features.**
## Key Files
- [Bun Config](mdc:bunfig.toml) - Runtime configuration
- [TypeScript Config](mdc:tsconfig.json) - TypeScript settings
- [Package.json](mdc:package.json) - Scripts and dependencies
- [Process Cleanup](mdc:tests/utils/process-cleanup.ts) - Process management
## Related Rules
- [Process Management](mdc:.cursor/rules/process-management.mdc) - Process spawning patterns
- [Testing Patterns](mdc:.cursor/rules/testing-patterns.mdc) - Bun Test usage
- [Security Patterns](mdc:.cursor/rules/security-patterns.mdc) - Security considerations
## Common Mistakes
### ❌ Wrong Imports
```typescript
// Don't use these
import { spawn } from 'bun'; // Wrong API
import { spawn } from 'child_process'; // Node.js
import fs from 'fs'; // Node.js
```
### ✅ Correct Imports
```typescript
// Use these
import processManager from './tests/utils/process-cleanup';
const file = Bun.file('path');
import { describe, test } from 'bun:test';
```
## Rules Summary
1. ✅ Use [processManager](mdc:tests/utils/process-cleanup.ts) for all process spawning
2. ✅ Use Bun file APIs (not Node.js fs)
3. ✅ Use Bun test runner (not Vitest/Jest)
4. ✅ Use bun commands (not npm/yarn)
5. ✅ Use Bun native APIs when available
6. ❌ Never use `Bun.spawn()` directly
7. ❌ Never use Node.js `child_process`
8. ❌ Never use Node.js `fs` module
9. ❌ Never install packages for Bun-native features
---
## Related Rules
- [Quality Standards](mdc:.cursor/rules/quality-standards.mdc) - Code quality standards ⭐ **NEW**
- [Process Management](mdc:.cursor/rules/process-management.mdc) - Zombie process prevention
- [Testing Patterns](mdc:.cursor/rules/testing-patterns.mdc) - Bun test patterns
- [Cloudflare Workers](mdc:.cursor/rules/cloudflare-workers.mdc) - Workers with Bun
## Related Documentation
- [docs/QUALITY_STANDARDS.md](mdc:docs/QUALITY_STANDARDS.md) - Complete quality guide ⭐ **NEW**
- [tests/utils/process-cleanup.ts](mdc:tests/utils/process-cleanup.ts) - Process manager
- [bunfig.toml](mdc:bunfig.toml) - Bun configuration
- [package.json](mdc:package.json) - Project configuration
- [tsconfig.json](mdc:tsconfig.json) - TypeScript configuration
---
**Status:** Bun runtime patterns established
**Last Updated:** 2025-10-08
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

