@namch/agent-assistant 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -522
- package/agents/backend-engineer.md +0 -8
- package/agents/brainstormer.md +0 -6
- package/agents/business-analyst.md +0 -5
- package/agents/database-architect.md +0 -6
- package/agents/debugger.md +0 -6
- package/agents/designer.md +0 -5
- package/agents/devops-engineer.md +0 -7
- package/agents/docs-manager.md +0 -6
- package/agents/frontend-engineer.md +0 -7
- package/agents/game-engineer.md +0 -7
- package/agents/mobile-engineer.md +0 -7
- package/agents/performance-engineer.md +0 -7
- package/agents/planner.md +0 -6
- package/agents/project-manager.md +0 -6
- package/agents/researcher.md +0 -5
- package/agents/reviewer.md +0 -6
- package/agents/scouter.md +0 -6
- package/agents/security-engineer.md +0 -7
- package/agents/tech-lead.md +0 -7
- package/agents/tester.md +0 -5
- package/cli/README.md +19 -10
- package/documents/business/business-features.md +1 -1
- package/documents/business/business-prd.md +4 -4
- package/documents/knowledge-architecture.md +1 -1
- package/documents/knowledge-domain.md +1 -1
- package/documents/knowledge-overview.md +14 -29
- package/documents/knowledge-source-base.md +14 -14
- package/package.json +1 -1
- package/rules/QUICK-REFERENCE.md +4 -1
- package/rules/SKILL-DISCOVERY.md +37 -14
- package/skills/active-directory-attacks/SKILL.md +383 -0
- package/skills/active-directory-attacks/references/advanced-attacks.md +382 -0
- package/skills/agent-evaluation/SKILL.md +64 -0
- package/skills/agent-memory-mcp/SKILL.md +82 -0
- package/skills/agent-memory-systems/SKILL.md +67 -0
- package/skills/agent-tool-builder/SKILL.md +53 -0
- package/skills/ai-agents-architect/SKILL.md +90 -0
- package/skills/ai-product/SKILL.md +54 -0
- package/skills/ai-wrapper-product/SKILL.md +273 -0
- package/skills/api-documentation-generator/SKILL.md +484 -0
- package/skills/api-fuzzing-bug-bounty/SKILL.md +433 -0
- package/skills/api-security-best-practices/SKILL.md +907 -0
- package/skills/autonomous-agent-patterns/SKILL.md +761 -0
- package/skills/autonomous-agents/SKILL.md +68 -0
- package/skills/aws-penetration-testing/SKILL.md +405 -0
- package/skills/aws-penetration-testing/references/advanced-aws-pentesting.md +469 -0
- package/skills/azure-functions/SKILL.md +42 -0
- package/skills/backend-dev-guidelines/SKILL.md +342 -0
- package/skills/backend-dev-guidelines/resources/architecture-overview.md +451 -0
- package/skills/backend-dev-guidelines/resources/async-and-errors.md +307 -0
- package/skills/backend-dev-guidelines/resources/complete-examples.md +638 -0
- package/skills/backend-dev-guidelines/resources/configuration.md +275 -0
- package/skills/backend-dev-guidelines/resources/database-patterns.md +224 -0
- package/skills/backend-dev-guidelines/resources/middleware-guide.md +213 -0
- package/skills/backend-dev-guidelines/resources/routing-and-controllers.md +756 -0
- package/skills/backend-dev-guidelines/resources/sentry-and-monitoring.md +336 -0
- package/skills/backend-dev-guidelines/resources/services-and-repositories.md +789 -0
- package/skills/backend-dev-guidelines/resources/testing-guide.md +235 -0
- package/skills/backend-dev-guidelines/resources/validation-patterns.md +754 -0
- package/skills/broken-authentication/SKILL.md +476 -0
- package/skills/bullmq-specialist/SKILL.md +57 -0
- package/skills/bun-development/SKILL.md +691 -0
- package/skills/burp-suite-testing/SKILL.md +380 -0
- package/skills/cloud-penetration-testing/SKILL.md +501 -0
- package/skills/cloud-penetration-testing/references/advanced-cloud-scripts.md +318 -0
- package/skills/computer-use-agents/SKILL.md +315 -0
- package/skills/content-creator/SKILL.md +248 -0
- package/skills/content-creator/assets/content_calendar_template.md +99 -0
- package/skills/content-creator/references/brand_guidelines.md +199 -0
- package/skills/content-creator/references/content_frameworks.md +534 -0
- package/skills/content-creator/references/social_media_optimization.md +317 -0
- package/skills/content-creator/scripts/brand_voice_analyzer.py +185 -0
- package/skills/content-creator/scripts/seo_optimizer.py +419 -0
- package/skills/context-window-management/SKILL.md +53 -0
- package/skills/conversation-memory/SKILL.md +61 -0
- package/skills/copy-editing/SKILL.md +439 -0
- package/skills/copywriting/SKILL.md +225 -0
- package/skills/crewai/SKILL.md +243 -0
- package/skills/discord-bot-architect/SKILL.md +277 -0
- package/skills/dispatching-parallel-agents/SKILL.md +180 -0
- package/skills/email-sequence/SKILL.md +925 -0
- package/skills/email-systems/SKILL.md +54 -0
- package/skills/ethical-hacking-methodology/SKILL.md +466 -0
- package/skills/executing-plans/SKILL.md +76 -0
- package/skills/file-path-traversal/SKILL.md +486 -0
- package/skills/finishing-a-development-branch/SKILL.md +200 -0
- package/skills/frontend-dev-guidelines/SKILL.md +359 -0
- package/skills/frontend-dev-guidelines/resources/common-patterns.md +331 -0
- package/skills/frontend-dev-guidelines/resources/complete-examples.md +872 -0
- package/skills/frontend-dev-guidelines/resources/component-patterns.md +502 -0
- package/skills/frontend-dev-guidelines/resources/data-fetching.md +767 -0
- package/skills/frontend-dev-guidelines/resources/file-organization.md +502 -0
- package/skills/frontend-dev-guidelines/resources/loading-and-error-states.md +501 -0
- package/skills/frontend-dev-guidelines/resources/performance.md +406 -0
- package/skills/frontend-dev-guidelines/resources/routing-guide.md +364 -0
- package/skills/frontend-dev-guidelines/resources/styling-guide.md +428 -0
- package/skills/frontend-dev-guidelines/resources/typescript-standards.md +418 -0
- package/skills/gcp-cloud-run/SKILL.md +288 -0
- package/skills/git-pushing/SKILL.md +33 -0
- package/skills/git-pushing/scripts/smart_commit.sh +19 -0
- package/skills/github-workflow-automation/SKILL.md +846 -0
- package/skills/html-injection-testing/SKILL.md +498 -0
- package/skills/idor-testing/SKILL.md +442 -0
- package/skills/inngest/SKILL.md +55 -0
- package/skills/javascript-mastery/SKILL.md +645 -0
- package/skills/kaizen/SKILL.md +730 -0
- package/skills/langfuse/SKILL.md +238 -0
- package/skills/langgraph/SKILL.md +287 -0
- package/skills/linux-privilege-escalation/SKILL.md +504 -0
- package/skills/llm-app-patterns/SKILL.md +760 -0
- package/skills/metasploit-framework/SKILL.md +478 -0
- package/skills/multi-agent-brainstorming/SKILL.md +256 -0
- package/skills/neon-postgres/SKILL.md +56 -0
- package/skills/nextjs-supabase-auth/SKILL.md +56 -0
- package/skills/nosql-expert/SKILL.md +111 -0
- package/skills/pentest-checklist/SKILL.md +334 -0
- package/skills/pentest-commands/SKILL.md +438 -0
- package/skills/plaid-fintech/SKILL.md +50 -0
- package/skills/planning-with-files/SKILL.md +211 -0
- package/skills/planning-with-files/examples.md +202 -0
- package/skills/planning-with-files/reference.md +218 -0
- package/skills/planning-with-files/scripts/check-complete.sh +44 -0
- package/skills/planning-with-files/scripts/init-session.sh +120 -0
- package/skills/planning-with-files/templates/findings.md +95 -0
- package/skills/planning-with-files/templates/progress.md +114 -0
- package/skills/planning-with-files/templates/task_plan.md +132 -0
- package/skills/privilege-escalation-methods/SKILL.md +333 -0
- package/skills/production-code-audit/SKILL.md +540 -0
- package/skills/prompt-caching/SKILL.md +61 -0
- package/skills/prompt-engineering/SKILL.md +171 -0
- package/skills/prompt-library/SKILL.md +322 -0
- package/skills/rag-engineer/SKILL.md +90 -0
- package/skills/rag-implementation/SKILL.md +63 -0
- package/skills/react-ui-patterns/SKILL.md +289 -0
- package/skills/red-team-tools/SKILL.md +310 -0
- package/skills/scanning-tools/SKILL.md +589 -0
- package/skills/shodan-reconnaissance/SKILL.md +503 -0
- package/skills/slack-bot-builder/SKILL.md +264 -0
- package/skills/smtp-penetration-testing/SKILL.md +500 -0
- package/skills/social-content/SKILL.md +807 -0
- package/skills/software-architecture/SKILL.md +75 -0
- package/skills/sql-injection-testing/SKILL.md +448 -0
- package/skills/sqlmap-database-pentesting/SKILL.md +400 -0
- package/skills/ssh-penetration-testing/SKILL.md +488 -0
- package/skills/stripe-integration/SKILL.md +69 -0
- package/skills/subagent-driven-development/SKILL.md +240 -0
- package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +20 -0
- package/skills/subagent-driven-development/implementer-prompt.md +78 -0
- package/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
- package/skills/tavily-web/SKILL.md +36 -0
- package/skills/telegram-bot-builder/SKILL.md +254 -0
- package/skills/test-driven-development/SKILL.md +371 -0
- package/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/skills/test-fixing/SKILL.md +119 -0
- package/skills/top-web-vulnerabilities/SKILL.md +543 -0
- package/skills/trigger-dev/SKILL.md +67 -0
- package/skills/twilio-communications/SKILL.md +295 -0
- package/skills/upstash-qstash/SKILL.md +68 -0
- package/skills/verification-before-completion/SKILL.md +139 -0
- package/skills/voice-agents/SKILL.md +68 -0
- package/skills/voice-ai-development/SKILL.md +302 -0
- package/skills/windows-privilege-escalation/SKILL.md +496 -0
- package/skills/wireshark-analysis/SKILL.md +497 -0
- package/skills/wordpress-penetration-testing/SKILL.md +485 -0
- package/skills/workflow-automation/SKILL.md +68 -0
- package/skills/xss-html-injection/SKILL.md +499 -0
- package/skills/zapier-make-patterns/SKILL.md +67 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: bun-development
|
|
3
|
+
description: "Modern JavaScript/TypeScript development with Bun runtime. Covers package management, bundling, testing, and migration from Node.js. Use when working with Bun, optimizing JS/TS development speed, or migrating from Node.js to Bun."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# ⚡ Bun Development
|
|
7
|
+
|
|
8
|
+
> Fast, modern JavaScript/TypeScript development with the Bun runtime, inspired by [oven-sh/bun](https://github.com/oven-sh/bun).
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
Use this skill when:
|
|
13
|
+
|
|
14
|
+
- Starting new JS/TS projects with Bun
|
|
15
|
+
- Migrating from Node.js to Bun
|
|
16
|
+
- Optimizing development speed
|
|
17
|
+
- Using Bun's built-in tools (bundler, test runner)
|
|
18
|
+
- Troubleshooting Bun-specific issues
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 1. Getting Started
|
|
23
|
+
|
|
24
|
+
### 1.1 Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# macOS / Linux
|
|
28
|
+
curl -fsSL https://bun.sh/install | bash
|
|
29
|
+
|
|
30
|
+
# Windows
|
|
31
|
+
powershell -c "irm bun.sh/install.ps1 | iex"
|
|
32
|
+
|
|
33
|
+
# Homebrew
|
|
34
|
+
brew tap oven-sh/bun
|
|
35
|
+
brew install bun
|
|
36
|
+
|
|
37
|
+
# npm (if needed)
|
|
38
|
+
npm install -g bun
|
|
39
|
+
|
|
40
|
+
# Upgrade
|
|
41
|
+
bun upgrade
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 1.2 Why Bun?
|
|
45
|
+
|
|
46
|
+
| Feature | Bun | Node.js |
|
|
47
|
+
| :-------------- | :------------- | :-------------------------- |
|
|
48
|
+
| Startup time | ~25ms | ~100ms+ |
|
|
49
|
+
| Package install | 10-100x faster | Baseline |
|
|
50
|
+
| TypeScript | Native | Requires transpiler |
|
|
51
|
+
| JSX | Native | Requires transpiler |
|
|
52
|
+
| Test runner | Built-in | External (Jest, Vitest) |
|
|
53
|
+
| Bundler | Built-in | External (Webpack, esbuild) |
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 2. Project Setup
|
|
58
|
+
|
|
59
|
+
### 2.1 Create New Project
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Initialize project
|
|
63
|
+
bun init
|
|
64
|
+
|
|
65
|
+
# Creates:
|
|
66
|
+
# ├── package.json
|
|
67
|
+
# ├── tsconfig.json
|
|
68
|
+
# ├── index.ts
|
|
69
|
+
# └── README.md
|
|
70
|
+
|
|
71
|
+
# With specific template
|
|
72
|
+
bun create <template> <project-name>
|
|
73
|
+
|
|
74
|
+
# Examples
|
|
75
|
+
bun create react my-app # React app
|
|
76
|
+
bun create next my-app # Next.js app
|
|
77
|
+
bun create vite my-app # Vite app
|
|
78
|
+
bun create elysia my-api # Elysia API
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 2.2 package.json
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"name": "my-bun-project",
|
|
86
|
+
"version": "1.0.0",
|
|
87
|
+
"module": "index.ts",
|
|
88
|
+
"type": "module",
|
|
89
|
+
"scripts": {
|
|
90
|
+
"dev": "bun run --watch index.ts",
|
|
91
|
+
"start": "bun run index.ts",
|
|
92
|
+
"test": "bun test",
|
|
93
|
+
"build": "bun build ./index.ts --outdir ./dist",
|
|
94
|
+
"lint": "bunx eslint ."
|
|
95
|
+
},
|
|
96
|
+
"devDependencies": {
|
|
97
|
+
"@types/bun": "latest"
|
|
98
|
+
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"typescript": "^5.0.0"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 2.3 tsconfig.json (Bun-optimized)
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"compilerOptions": {
|
|
110
|
+
"lib": ["ESNext"],
|
|
111
|
+
"module": "esnext",
|
|
112
|
+
"target": "esnext",
|
|
113
|
+
"moduleResolution": "bundler",
|
|
114
|
+
"moduleDetection": "force",
|
|
115
|
+
"allowImportingTsExtensions": true,
|
|
116
|
+
"noEmit": true,
|
|
117
|
+
"composite": true,
|
|
118
|
+
"strict": true,
|
|
119
|
+
"downlevelIteration": true,
|
|
120
|
+
"skipLibCheck": true,
|
|
121
|
+
"jsx": "react-jsx",
|
|
122
|
+
"allowSyntheticDefaultImports": true,
|
|
123
|
+
"forceConsistentCasingInFileNames": true,
|
|
124
|
+
"allowJs": true,
|
|
125
|
+
"types": ["bun-types"]
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 3. Package Management
|
|
133
|
+
|
|
134
|
+
### 3.1 Installing Packages
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# Install from package.json
|
|
138
|
+
bun install # or 'bun i'
|
|
139
|
+
|
|
140
|
+
# Add dependencies
|
|
141
|
+
bun add express # Regular dependency
|
|
142
|
+
bun add -d typescript # Dev dependency
|
|
143
|
+
bun add -D @types/node # Dev dependency (alias)
|
|
144
|
+
bun add --optional pkg # Optional dependency
|
|
145
|
+
|
|
146
|
+
# From specific registry
|
|
147
|
+
bun add lodash --registry https://registry.npmmirror.com
|
|
148
|
+
|
|
149
|
+
# Install specific version
|
|
150
|
+
bun add react@18.2.0
|
|
151
|
+
bun add react@latest
|
|
152
|
+
bun add react@next
|
|
153
|
+
|
|
154
|
+
# From git
|
|
155
|
+
bun add github:user/repo
|
|
156
|
+
bun add git+https://github.com/user/repo.git
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 3.2 Removing & Updating
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# Remove package
|
|
163
|
+
bun remove lodash
|
|
164
|
+
|
|
165
|
+
# Update packages
|
|
166
|
+
bun update # Update all
|
|
167
|
+
bun update lodash # Update specific
|
|
168
|
+
bun update --latest # Update to latest (ignore ranges)
|
|
169
|
+
|
|
170
|
+
# Check outdated
|
|
171
|
+
bun outdated
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 3.3 bunx (npx equivalent)
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# Execute package binaries
|
|
178
|
+
bunx prettier --write .
|
|
179
|
+
bunx tsc --init
|
|
180
|
+
bunx create-react-app my-app
|
|
181
|
+
|
|
182
|
+
# With specific version
|
|
183
|
+
bunx -p typescript@4.9 tsc --version
|
|
184
|
+
|
|
185
|
+
# Run without installing
|
|
186
|
+
bunx cowsay "Hello from Bun!"
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 3.4 Lockfile
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
# bun.lockb is a binary lockfile (faster parsing)
|
|
193
|
+
# To generate text lockfile for debugging:
|
|
194
|
+
bun install --yarn # Creates yarn.lock
|
|
195
|
+
|
|
196
|
+
# Trust existing lockfile
|
|
197
|
+
bun install --frozen-lockfile
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## 4. Running Code
|
|
203
|
+
|
|
204
|
+
### 4.1 Basic Execution
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
# Run TypeScript directly (no build step!)
|
|
208
|
+
bun run index.ts
|
|
209
|
+
|
|
210
|
+
# Run JavaScript
|
|
211
|
+
bun run index.js
|
|
212
|
+
|
|
213
|
+
# Run with arguments
|
|
214
|
+
bun run server.ts --port 3000
|
|
215
|
+
|
|
216
|
+
# Run package.json script
|
|
217
|
+
bun run dev
|
|
218
|
+
bun run build
|
|
219
|
+
|
|
220
|
+
# Short form (for scripts)
|
|
221
|
+
bun dev
|
|
222
|
+
bun build
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### 4.2 Watch Mode
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
# Auto-restart on file changes
|
|
229
|
+
bun --watch run index.ts
|
|
230
|
+
|
|
231
|
+
# With hot reloading
|
|
232
|
+
bun --hot run server.ts
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### 4.3 Environment Variables
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
// .env file is loaded automatically!
|
|
239
|
+
|
|
240
|
+
// Access environment variables
|
|
241
|
+
const apiKey = Bun.env.API_KEY;
|
|
242
|
+
const port = Bun.env.PORT ?? "3000";
|
|
243
|
+
|
|
244
|
+
// Or use process.env (Node.js compatible)
|
|
245
|
+
const dbUrl = process.env.DATABASE_URL;
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# Run with specific env file
|
|
250
|
+
bun --env-file=.env.production run index.ts
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## 5. Built-in APIs
|
|
256
|
+
|
|
257
|
+
### 5.1 File System (Bun.file)
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
// Read file
|
|
261
|
+
const file = Bun.file("./data.json");
|
|
262
|
+
const text = await file.text();
|
|
263
|
+
const json = await file.json();
|
|
264
|
+
const buffer = await file.arrayBuffer();
|
|
265
|
+
|
|
266
|
+
// File info
|
|
267
|
+
console.log(file.size); // bytes
|
|
268
|
+
console.log(file.type); // MIME type
|
|
269
|
+
|
|
270
|
+
// Write file
|
|
271
|
+
await Bun.write("./output.txt", "Hello, Bun!");
|
|
272
|
+
await Bun.write("./data.json", JSON.stringify({ foo: "bar" }));
|
|
273
|
+
|
|
274
|
+
// Stream large files
|
|
275
|
+
const reader = file.stream();
|
|
276
|
+
for await (const chunk of reader) {
|
|
277
|
+
console.log(chunk);
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### 5.2 HTTP Server (Bun.serve)
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
const server = Bun.serve({
|
|
285
|
+
port: 3000,
|
|
286
|
+
|
|
287
|
+
fetch(request) {
|
|
288
|
+
const url = new URL(request.url);
|
|
289
|
+
|
|
290
|
+
if (url.pathname === "/") {
|
|
291
|
+
return new Response("Hello World!");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (url.pathname === "/api/users") {
|
|
295
|
+
return Response.json([
|
|
296
|
+
{ id: 1, name: "Alice" },
|
|
297
|
+
{ id: 2, name: "Bob" },
|
|
298
|
+
]);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return new Response("Not Found", { status: 404 });
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
error(error) {
|
|
305
|
+
return new Response(`Error: ${error.message}`, { status: 500 });
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
console.log(`Server running at http://localhost:${server.port}`);
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### 5.3 WebSocket Server
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
const server = Bun.serve({
|
|
316
|
+
port: 3000,
|
|
317
|
+
|
|
318
|
+
fetch(req, server) {
|
|
319
|
+
// Upgrade to WebSocket
|
|
320
|
+
if (server.upgrade(req)) {
|
|
321
|
+
return; // Upgraded
|
|
322
|
+
}
|
|
323
|
+
return new Response("Upgrade failed", { status: 500 });
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
websocket: {
|
|
327
|
+
open(ws) {
|
|
328
|
+
console.log("Client connected");
|
|
329
|
+
ws.send("Welcome!");
|
|
330
|
+
},
|
|
331
|
+
|
|
332
|
+
message(ws, message) {
|
|
333
|
+
console.log(`Received: ${message}`);
|
|
334
|
+
ws.send(`Echo: ${message}`);
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
close(ws) {
|
|
338
|
+
console.log("Client disconnected");
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 5.4 SQLite (Bun.sql)
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
import { Database } from "bun:sqlite";
|
|
348
|
+
|
|
349
|
+
const db = new Database("mydb.sqlite");
|
|
350
|
+
|
|
351
|
+
// Create table
|
|
352
|
+
db.run(`
|
|
353
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
354
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
355
|
+
name TEXT NOT NULL,
|
|
356
|
+
email TEXT UNIQUE
|
|
357
|
+
)
|
|
358
|
+
`);
|
|
359
|
+
|
|
360
|
+
// Insert
|
|
361
|
+
const insert = db.prepare("INSERT INTO users (name, email) VALUES (?, ?)");
|
|
362
|
+
insert.run("Alice", "alice@example.com");
|
|
363
|
+
|
|
364
|
+
// Query
|
|
365
|
+
const query = db.prepare("SELECT * FROM users WHERE name = ?");
|
|
366
|
+
const user = query.get("Alice");
|
|
367
|
+
console.log(user); // { id: 1, name: "Alice", email: "alice@example.com" }
|
|
368
|
+
|
|
369
|
+
// Query all
|
|
370
|
+
const allUsers = db.query("SELECT * FROM users").all();
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### 5.5 Password Hashing
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
// Hash password
|
|
377
|
+
const password = "super-secret";
|
|
378
|
+
const hash = await Bun.password.hash(password);
|
|
379
|
+
|
|
380
|
+
// Verify password
|
|
381
|
+
const isValid = await Bun.password.verify(password, hash);
|
|
382
|
+
console.log(isValid); // true
|
|
383
|
+
|
|
384
|
+
// With algorithm options
|
|
385
|
+
const bcryptHash = await Bun.password.hash(password, {
|
|
386
|
+
algorithm: "bcrypt",
|
|
387
|
+
cost: 12,
|
|
388
|
+
});
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
## 6. Testing
|
|
394
|
+
|
|
395
|
+
### 6.1 Basic Tests
|
|
396
|
+
|
|
397
|
+
```typescript
|
|
398
|
+
// math.test.ts
|
|
399
|
+
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
|
|
400
|
+
|
|
401
|
+
describe("Math operations", () => {
|
|
402
|
+
it("adds two numbers", () => {
|
|
403
|
+
expect(1 + 1).toBe(2);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("subtracts two numbers", () => {
|
|
407
|
+
expect(5 - 3).toBe(2);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### 6.2 Running Tests
|
|
413
|
+
|
|
414
|
+
```bash
|
|
415
|
+
# Run all tests
|
|
416
|
+
bun test
|
|
417
|
+
|
|
418
|
+
# Run specific file
|
|
419
|
+
bun test math.test.ts
|
|
420
|
+
|
|
421
|
+
# Run matching pattern
|
|
422
|
+
bun test --grep "adds"
|
|
423
|
+
|
|
424
|
+
# Watch mode
|
|
425
|
+
bun test --watch
|
|
426
|
+
|
|
427
|
+
# With coverage
|
|
428
|
+
bun test --coverage
|
|
429
|
+
|
|
430
|
+
# Timeout
|
|
431
|
+
bun test --timeout 5000
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### 6.3 Matchers
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
import { expect, test } from "bun:test";
|
|
438
|
+
|
|
439
|
+
test("matchers", () => {
|
|
440
|
+
// Equality
|
|
441
|
+
expect(1).toBe(1);
|
|
442
|
+
expect({ a: 1 }).toEqual({ a: 1 });
|
|
443
|
+
expect([1, 2]).toContain(1);
|
|
444
|
+
|
|
445
|
+
// Comparisons
|
|
446
|
+
expect(10).toBeGreaterThan(5);
|
|
447
|
+
expect(5).toBeLessThanOrEqual(5);
|
|
448
|
+
|
|
449
|
+
// Truthiness
|
|
450
|
+
expect(true).toBeTruthy();
|
|
451
|
+
expect(null).toBeNull();
|
|
452
|
+
expect(undefined).toBeUndefined();
|
|
453
|
+
|
|
454
|
+
// Strings
|
|
455
|
+
expect("hello").toMatch(/ell/);
|
|
456
|
+
expect("hello").toContain("ell");
|
|
457
|
+
|
|
458
|
+
// Arrays
|
|
459
|
+
expect([1, 2, 3]).toHaveLength(3);
|
|
460
|
+
|
|
461
|
+
// Exceptions
|
|
462
|
+
expect(() => {
|
|
463
|
+
throw new Error("fail");
|
|
464
|
+
}).toThrow("fail");
|
|
465
|
+
|
|
466
|
+
// Async
|
|
467
|
+
await expect(Promise.resolve(1)).resolves.toBe(1);
|
|
468
|
+
await expect(Promise.reject("err")).rejects.toBe("err");
|
|
469
|
+
});
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### 6.4 Mocking
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
import { mock, spyOn } from "bun:test";
|
|
476
|
+
|
|
477
|
+
// Mock function
|
|
478
|
+
const mockFn = mock((x: number) => x * 2);
|
|
479
|
+
mockFn(5);
|
|
480
|
+
expect(mockFn).toHaveBeenCalled();
|
|
481
|
+
expect(mockFn).toHaveBeenCalledWith(5);
|
|
482
|
+
expect(mockFn.mock.results[0].value).toBe(10);
|
|
483
|
+
|
|
484
|
+
// Spy on method
|
|
485
|
+
const obj = {
|
|
486
|
+
method: () => "original",
|
|
487
|
+
};
|
|
488
|
+
const spy = spyOn(obj, "method").mockReturnValue("mocked");
|
|
489
|
+
expect(obj.method()).toBe("mocked");
|
|
490
|
+
expect(spy).toHaveBeenCalled();
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
---
|
|
494
|
+
|
|
495
|
+
## 7. Bundling
|
|
496
|
+
|
|
497
|
+
### 7.1 Basic Build
|
|
498
|
+
|
|
499
|
+
```bash
|
|
500
|
+
# Bundle for production
|
|
501
|
+
bun build ./src/index.ts --outdir ./dist
|
|
502
|
+
|
|
503
|
+
# With options
|
|
504
|
+
bun build ./src/index.ts \
|
|
505
|
+
--outdir ./dist \
|
|
506
|
+
--target browser \
|
|
507
|
+
--minify \
|
|
508
|
+
--sourcemap
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### 7.2 Build API
|
|
512
|
+
|
|
513
|
+
```typescript
|
|
514
|
+
const result = await Bun.build({
|
|
515
|
+
entrypoints: ["./src/index.ts"],
|
|
516
|
+
outdir: "./dist",
|
|
517
|
+
target: "browser", // or "bun", "node"
|
|
518
|
+
minify: true,
|
|
519
|
+
sourcemap: "external",
|
|
520
|
+
splitting: true,
|
|
521
|
+
format: "esm",
|
|
522
|
+
|
|
523
|
+
// External packages (not bundled)
|
|
524
|
+
external: ["react", "react-dom"],
|
|
525
|
+
|
|
526
|
+
// Define globals
|
|
527
|
+
define: {
|
|
528
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
529
|
+
},
|
|
530
|
+
|
|
531
|
+
// Naming
|
|
532
|
+
naming: {
|
|
533
|
+
entry: "[name].[hash].js",
|
|
534
|
+
chunk: "chunks/[name].[hash].js",
|
|
535
|
+
asset: "assets/[name].[hash][ext]",
|
|
536
|
+
},
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
if (!result.success) {
|
|
540
|
+
console.error(result.logs);
|
|
541
|
+
}
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### 7.3 Compile to Executable
|
|
545
|
+
|
|
546
|
+
```bash
|
|
547
|
+
# Create standalone executable
|
|
548
|
+
bun build ./src/cli.ts --compile --outfile myapp
|
|
549
|
+
|
|
550
|
+
# Cross-compile
|
|
551
|
+
bun build ./src/cli.ts --compile --target=bun-linux-x64 --outfile myapp-linux
|
|
552
|
+
bun build ./src/cli.ts --compile --target=bun-darwin-arm64 --outfile myapp-mac
|
|
553
|
+
|
|
554
|
+
# With embedded assets
|
|
555
|
+
bun build ./src/cli.ts --compile --outfile myapp --embed ./assets
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
---
|
|
559
|
+
|
|
560
|
+
## 8. Migration from Node.js
|
|
561
|
+
|
|
562
|
+
### 8.1 Compatibility
|
|
563
|
+
|
|
564
|
+
```typescript
|
|
565
|
+
// Most Node.js APIs work out of the box
|
|
566
|
+
import fs from "fs";
|
|
567
|
+
import path from "path";
|
|
568
|
+
import crypto from "crypto";
|
|
569
|
+
|
|
570
|
+
// process is global
|
|
571
|
+
console.log(process.cwd());
|
|
572
|
+
console.log(process.env.HOME);
|
|
573
|
+
|
|
574
|
+
// Buffer is global
|
|
575
|
+
const buf = Buffer.from("hello");
|
|
576
|
+
|
|
577
|
+
// __dirname and __filename work
|
|
578
|
+
console.log(__dirname);
|
|
579
|
+
console.log(__filename);
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### 8.2 Common Migration Steps
|
|
583
|
+
|
|
584
|
+
```bash
|
|
585
|
+
# 1. Install Bun
|
|
586
|
+
curl -fsSL https://bun.sh/install | bash
|
|
587
|
+
|
|
588
|
+
# 2. Replace package manager
|
|
589
|
+
rm -rf node_modules package-lock.json
|
|
590
|
+
bun install
|
|
591
|
+
|
|
592
|
+
# 3. Update scripts in package.json
|
|
593
|
+
# "start": "node index.js" → "start": "bun run index.ts"
|
|
594
|
+
# "test": "jest" → "test": "bun test"
|
|
595
|
+
|
|
596
|
+
# 4. Add Bun types
|
|
597
|
+
bun add -d @types/bun
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### 8.3 Differences from Node.js
|
|
601
|
+
|
|
602
|
+
```typescript
|
|
603
|
+
// ❌ Node.js specific (may not work)
|
|
604
|
+
require("module") // Use import instead
|
|
605
|
+
require.resolve("pkg") // Use import.meta.resolve
|
|
606
|
+
__non_webpack_require__ // Not supported
|
|
607
|
+
|
|
608
|
+
// ✅ Bun equivalents
|
|
609
|
+
import pkg from "pkg";
|
|
610
|
+
const resolved = import.meta.resolve("pkg");
|
|
611
|
+
Bun.resolveSync("pkg", process.cwd());
|
|
612
|
+
|
|
613
|
+
// ❌ These globals differ
|
|
614
|
+
process.hrtime() // Use Bun.nanoseconds()
|
|
615
|
+
setImmediate() // Use queueMicrotask()
|
|
616
|
+
|
|
617
|
+
// ✅ Bun-specific features
|
|
618
|
+
const file = Bun.file("./data.txt"); // Fast file API
|
|
619
|
+
Bun.serve({ port: 3000, fetch: ... }); // Fast HTTP server
|
|
620
|
+
Bun.password.hash(password); // Built-in hashing
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
---
|
|
624
|
+
|
|
625
|
+
## 9. Performance Tips
|
|
626
|
+
|
|
627
|
+
### 9.1 Use Bun-native APIs
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
// Slow (Node.js compat)
|
|
631
|
+
import fs from "fs/promises";
|
|
632
|
+
const content = await fs.readFile("./data.txt", "utf-8");
|
|
633
|
+
|
|
634
|
+
// Fast (Bun-native)
|
|
635
|
+
const file = Bun.file("./data.txt");
|
|
636
|
+
const content = await file.text();
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
### 9.2 Use Bun.serve for HTTP
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
// Don't: Express/Fastify (overhead)
|
|
643
|
+
import express from "express";
|
|
644
|
+
const app = express();
|
|
645
|
+
|
|
646
|
+
// Do: Bun.serve (native, 4-10x faster)
|
|
647
|
+
Bun.serve({
|
|
648
|
+
fetch(req) {
|
|
649
|
+
return new Response("Hello!");
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// Or use Elysia (Bun-optimized framework)
|
|
654
|
+
import { Elysia } from "elysia";
|
|
655
|
+
new Elysia().get("/", () => "Hello!").listen(3000);
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
### 9.3 Bundle for Production
|
|
659
|
+
|
|
660
|
+
```bash
|
|
661
|
+
# Always bundle and minify for production
|
|
662
|
+
bun build ./src/index.ts --outdir ./dist --minify --target node
|
|
663
|
+
|
|
664
|
+
# Then run the bundle
|
|
665
|
+
bun run ./dist/index.js
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
---
|
|
669
|
+
|
|
670
|
+
## Quick Reference
|
|
671
|
+
|
|
672
|
+
| Task | Command |
|
|
673
|
+
| :----------- | :----------------------------------------- |
|
|
674
|
+
| Init project | `bun init` |
|
|
675
|
+
| Install deps | `bun install` |
|
|
676
|
+
| Add package | `bun add <pkg>` |
|
|
677
|
+
| Run script | `bun run <script>` |
|
|
678
|
+
| Run file | `bun run file.ts` |
|
|
679
|
+
| Watch mode | `bun --watch run file.ts` |
|
|
680
|
+
| Run tests | `bun test` |
|
|
681
|
+
| Build | `bun build ./src/index.ts --outdir ./dist` |
|
|
682
|
+
| Execute pkg | `bunx <pkg>` |
|
|
683
|
+
|
|
684
|
+
---
|
|
685
|
+
|
|
686
|
+
## Resources
|
|
687
|
+
|
|
688
|
+
- [Bun Documentation](https://bun.sh/docs)
|
|
689
|
+
- [Bun GitHub](https://github.com/oven-sh/bun)
|
|
690
|
+
- [Elysia Framework](https://elysiajs.com/)
|
|
691
|
+
- [Bun Discord](https://bun.sh/discord)
|