bunjs-docker-mastery
21
总安装量
10
周安装量
#17184
全站排名
安装命令
npx skills add https://github.com/modra40/claude-codex-skills-directory --skill bunjs-docker-mastery
Agent 安装分布
claude-code
7
gemini-cli
6
antigravity
6
trae
6
codex
6
Skill 文档
Bun.js + Docker Mastery
“Simplicity is the ultimate sophistication.” â Leonardo da Vinci
Filosofi Inti (20 Tahun Wisdom)
KISS – Keep It Stupid Simple
// â Over-engineering
class UserServiceFactoryAbstractSingletonProxyDecorator { ... }
// â
Simple & Clear
const userService = { create, update, delete, findById }
Less is More
- 1 file = 1 tanggung jawab (max 200 lines)
- 1 function = 1 task (max 20 lines)
- Jika butuh komentar panjang, refactor kodenya
- Nama yang jelas > komentar yang panjang
Red Flags (Warning Signs)
- File > 300 lines â split
- Function > 30 lines â extract
- Nested callback > 2 level â refactor
- Cyclomatic complexity > 10 â simplify
anytype â define proper types
Quick Start
Inisialisasi Project Baru
# Gunakan script init
./scripts/init-project.sh my-app
# Atau manual
bun init
bun add hono zod drizzle-orm pino
bun add -d @types/bun vitest
Struktur Folder Production-Ready
src/
âââ index.ts # Entry point ONLY (max 20 lines)
âââ app.ts # App setup & middleware registration
âââ config/
â âââ index.ts # Export semua config
â âââ env.ts # Environment validation dengan Zod
â âââ database.ts # Database config
âââ routes/
â âââ index.ts # Route aggregator
â âââ user.route.ts # User routes
â âââ auth.route.ts # Auth routes
âââ controllers/ # HTTP layer ONLY
âââ services/ # Business logic
âââ repositories/ # Data access layer
âââ middlewares/
â âââ auth.ts # Authentication
â âââ validate.ts # Request validation
â âââ error.ts # Error handler
âââ utils/
â âââ response.ts # Standard response helper
â âââ logger.ts # Pino logger setup
â âââ errors.ts # Custom error classes
âââ types/
âââ index.d.ts # Global types
âââ api.types.ts # API request/response types
Core Patterns
1. Entry Point yang Bersih
// src/index.ts - HANYA INI
import { app } from "./app"
import { env } from "./config/env"
import { logger } from "./utils/logger"
const server = Bun.serve({
port: env.PORT,
fetch: app.fetch,
})
logger.info(`ð Server running on ${server.url}`)
2. Environment Validation (WAJIB)
// src/config/env.ts
import { z } from "zod"
const envSchema = z.object({
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string().url(),
JWT_SECRET: z.string().min(32),
REDIS_URL: z.string().url().optional(),
})
export const env = envSchema.parse(process.env)
export type Env = z.infer<typeof envSchema>
3. Layered Architecture
HTTP Request â Controller â Service â Repository â Database
â â â
Validation Business Data Access
Logic (Drizzle)
4. Error Handling yang Proper
// src/utils/errors.ts
export class AppError extends Error {
constructor(
public message: string,
public statusCode: number = 500,
public code: string = "INTERNAL_ERROR"
) {
super(message)
Error.captureStackTrace(this, this.constructor)
}
}
export class NotFoundError extends AppError {
constructor(resource: string) {
super(`${resource} not found`, 404, "NOT_FOUND")
}
}
export class ValidationError extends AppError {
constructor(message: string) {
super(message, 400, "VALIDATION_ERROR")
}
}
5. Response Standard
// src/utils/response.ts
export const ok = <T>(data: T) => ({ success: true, data })
export const created = <T>(data: T) => ({ success: true, data })
export const error = (message: string, code: string) => ({
success: false,
error: { message, code }
})
Docker Best Practices
Multi-stage Build (Production)
# Build stage
FROM oven/bun:1-alpine AS builder
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile --production=false
COPY . .
RUN bun run build
# Production stage
FROM oven/bun:1-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# Security: Non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S bunjs -u 1001
USER bunjs
COPY /app/dist ./dist
COPY /app/node_modules ./node_modules
COPY /app/package.json ./
EXPOSE 3000
HEALTHCHECK \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
CMD ["bun", "run", "dist/index.js"]
Docker Compose (Development)
Lihat assets/project-template/docker/docker-compose.yml
Debugging Mastery
Common Crash Points & Solutions
| Issue | Penyebab | Solusi |
|---|---|---|
| Memory leak | Uncleared intervals/listeners | Cleanup di graceful shutdown |
| Connection pool exhausted | Tidak release connection | Gunakan using atau finally |
| Race condition | Async tanpa proper await | Promise.all dengan error handling |
| Uncaught Promise rejection | Missing try-catch | Global error handler |
Debugging Tools
// Bun native debugger
bun --inspect src/index.ts
// Memory profiling
bun --smol src/index.ts // Low memory mode
// Trace async operations
process.on("unhandledRejection", (reason, promise) => {
logger.error({ reason, promise }, "Unhandled Rejection")
})
References (Detailed Guides)
- Clean Code Patterns: See references/clean-code-patterns.md
- Debugging Deep Dive: See references/debugging-guide.md
- Library Arsenal (20+ recommended): See references/library-arsenal.md
- Docker Advanced Patterns: See references/docker-patterns.md
- Testing Strategy: See references/testing-strategy.md
Scripts
scripts/init-project.sh– Initialize new project dengan templatescripts/healthcheck.ts– Healthcheck endpoint template
Assets
assets/project-template/– Full project boilerplate siap pakai
Checklist Sebelum Production
- Environment variables validated dengan Zod
- Graceful shutdown implemented
- Health check endpoint
/health - Structured logging dengan Pino
- Error handling global
- Rate limiting
- CORS configured
- Security headers (helmet)
- Database connection pooling
- Docker multi-stage build
- CI/CD pipeline
- Monitoring & alerting ready