bunjs-docker-mastery

bunjs-docker-mastery

Senior/Lead Developer Bun.js + Docker dengan pengalaman 20 tahun. Skill ini digunakan ketika: (1) Membuat project Bun.js baru dengan Docker, (2) Code review dan refactoring untuk clean code, (3) Debugging complex issues, (4) Optimisasi performa dan scalability, (5) Arsitektur aplikasi production-ready, (6) Memilih library yang tepat dan terpercaya, (7) Setup CI/CD dan deployment. Trigger keywords: "bun", "bunjs", "bun.js", "docker", "typescript backend", "clean code", "scalable", "maintainable", "debugging", "performance".

0estrelas
0forks
Atualizado 12/27/2025
SKILL.md
readonlyread-only
name
bunjs-docker-mastery
description

|

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
  • any type → 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 --from=builder --chown=bunjs:nodejs /app/dist ./dist
COPY --from=builder --chown=bunjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=bunjs:nodejs /app/package.json ./

EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  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)

Scripts

  • scripts/init-project.sh - Initialize new project dengan template
  • scripts/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

You Might Also Like

Related Skills

coding-agent

coding-agent

179Kdev-codegen

Run Codex CLI, Claude Code, OpenCode, or Pi Coding Agent via background process for programmatic control.

openclaw avataropenclaw
Obter
add-uint-support

add-uint-support

97Kdev-codegen

Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.

pytorch avatarpytorch
Obter
at-dispatch-v2

at-dispatch-v2

97Kdev-codegen

Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.

pytorch avatarpytorch
Obter
skill-writer

skill-writer

97Kdev-codegen

Guide users through creating Agent Skills for Claude Code. Use when the user wants to create, write, author, or design a new Skill, or needs help with SKILL.md files, frontmatter, or skill structure.

pytorch avatarpytorch
Obter

Implements JavaScript classes in C++ using JavaScriptCore. Use when creating new JS classes with C++ bindings, prototypes, or constructors.

oven-sh avataroven-sh
Obter

Creates JavaScript classes using Bun's Zig bindings generator (.classes.ts). Use when implementing new JS APIs in Zig with JSC integration.

oven-sh avataroven-sh
Obter