# Repository: eggjs/egg # Stars: 18997 ## CLAUDE.md # CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Project Overview This is the **Eggjs** framework - a progressive Node.js framework for building enterprise-class server-side applications. Built on top of Koa.js, it provides a plugin system, conventions over configuration, and enterprise-grade features like clustering, logging, and security. **This project is structured as a pnpm monorepo** with multiple packages and uses pnpm workspaces for dependency management. ### Node.js Requirements **IMPORTANT: All packages in this monorepo require Node.js >= 22.18.0**. This minimum version is enforced across all packages to ensure compatibility with modern JavaScript features and optimal performance. > Node.js will be able to execute TypeScript files without additional configuration. See https://nodejs.org/en/blog/release/v22.18.0 ## Monorepo Structure ### Packages - **`packages/egg/`** - Main Eggjs framework package - `src/` - TypeScript source code - `test/` - Comprehensive test suite with fixtures - **`packages/core/`** - Core plugin framework (merged from @eggjs/core) - `src/` - Core TypeScript source code - `test/` - Core framework test suite with vitest - **`packages/utils/`** - Utility functions (merged from @eggjs/utils) - `src/` - Utils TypeScript source code - `test/` - Utils test suite - **`packages/mock/`** - Testing utilities (merged from @eggjs/mock) - `src/` - Mock TypeScript source code - `test/` - Mock test suite - **`packages/cluster/`** - Cluster management (merged from @eggjs/cluster) - `src/` - Cluster TypeScript source code - `test/` - Cluster test suite - **`packages/cookies/`** - Cookie handling utilities (merged from @eggjs/cookies) - `src/` - Cookies TypeScript source code - `test/` - Cookies test suite with Mocha - **`packages/koa/`** - Koa web framework (merged from @eggjs/koa) - `src/` - Koa TypeScript source code - `test/` - Koa test suite - **`packages/supertest/`** - HTTP testing utilities (merged from @eggjs/supertest) - `src/` - Supertest TypeScript source code - `test/` - Supertest test suite - **`packages/extend2/`** - Object extension utility (merged from extend2) - `src/` - Extend2 TypeScript source code - `test/` - Extend2 test suite - **`packages/koa-static-cache/`** - Static file serving with cache (merged from @eggjs/koa-static-cache) - `src/` - TypeScript source code for static cache middleware - `test/` - Test suite with Vitest - **`packages/router/`** - Router middleware for Koa/Egg (merged from @eggjs/router) - `src/` - TypeScript source code for router implementation - `test/` - Test suite with Vitest - Provides RESTful resource routing and middleware composition - Supports route parameter matching with path-to-regexp - Includes EggRouter class with additional convenience methods - **`plugins/`** - Egg framework plugins (all plugins should be located here) - `development/` - Development plugin for local development (merged from @eggjs/development) - Provides development tools and auto-reload functionality - Only enabled in local environment - `watcher/` - File watcher plugin (merged from @eggjs/watcher) - Provides file system watching capabilities - Supports multiple event sources for different environments - Used by development plugin for auto-reload functionality - `schedule/` - Task scheduling plugin (merged from @eggjs/schedule) - Provides cron-based task scheduling capabilities - Supports interval and cron expression scheduling - Manages scheduled tasks across worker processes - `static/` - Static file serving plugin (merged from @eggjs/static) - Provides static file serving middleware - Supports multiple static directories - Includes cache control and range request support - Built on top of koa-static-cache - `security/` - Security plugin (merged from @eggjs/security) - Provides comprehensive security middleware and helpers - CSRF protection with token and referer validation - XSS prevention with content filtering and escape utilities - Path traversal protection and safe redirects - Security headers (HSTS, CSP, X-Frame-Options, etc.) - SSRF protection for HTTP client requests - Configurable security policies per environment - `session/` - Session management plugin (merged from @eggjs/session) - Provides session middleware based on koa-session - Supports both cookie and external session stores - Built-in memory store for development - Configurable session options (maxAge, renew, etc.) - Session encryption and signing - `logrotator/` - Log rotation plugin (merged from @eggjs/logrotator) - Provides automatic log file rotation based on time or size - Supports daily rotation with configurable patterns - Includes log cleanup to remove old files based on maxDays - Manages all application and custom logger files - Integrates with schedule plugin for automated rotation tasks - `multipart/` - Multipart form data handling plugin (merged from @eggjs/multipart) - Provides multipart/form-data parsing for file uploads - Supports both stream and file modes for handling uploads - Built-in file size and type validation - Automatic temporary file cleanup - Configurable whitelist/blacklist for file extensions - Integration with schedule plugin for tmpdir cleanup - `i18n/` - Internationalization plugin (merged from @eggjs/i18n) - Provides internationalization support for multi-language applications - Supports multiple file formats (JSON, JS, YAML, Properties, INI) - Automatic locale detection from query, cookie, or header - Context-aware translation helpers - Domain-specific locale configurations - Built-in pluralization support - `view/` - Base view plugin (merged from @eggjs/view) - Provides template rendering infrastructure for Egg applications - Supports multiple template engines through plugin system - Implements ViewManager for managing template engine instances - Extends context with render() method for template rendering - Built-in mapping configuration for file extensions to engines - Template engine agnostic - works with ejs, nunjucks, handlebars, etc. - `view-nunjucks/` - Nunjucks template engine plugin (merged from egg-view-nunjucks) - Provides Nunjucks template engine integration for Egg applications - Built on Mozilla's Nunjucks templating engine - Extends ViewHelper with Nunjucks-specific safe string helpers - Auto-injects CSRF tokens and CSP nonce attributes - Supports custom filters and template caching - Includes sandbox protection against prototype pollution attacks - Depends on security and view plugins - `tracer/` - Request tracing plugin (merged from @eggjs/tracer) - Provides distributed tracing capabilities for Egg applications - Automatically generates and tracks traceId, spanId, and parentSpanId - Extends context with tracer object for request correlation - Supports custom tracer implementations via Class configuration - Integrates with application and agent processes for full tracing coverage - `typebox-validate/` - TypeBox validation plugin (merged from egg-typebox-validate) - Provides schema validation for TypeScript Egg projects - Built on Ajv with TypeBox schema definitions - Extends context with tValidate() and tValidateWithoutThrow() methods - Includes decorator support for controller validation - Supports custom formats and validation rules - Re-exports TypeBox for schema definitions - `redis/` - Redis/Valkey plugin (merged from @eggjs/redis) - Provides Redis and Valkey client integration for Egg applications - Built on ioredis for full Redis feature support - Supports single instance, cluster, and sentinel configurations - Multi-client support with singleton pattern - Weak dependency mode for optional Redis connections - Extends Application and Agent with redis property - **`packages/skills/`** - AI agent skills for Egg framework (@eggjs/skills) - Pure markdown documentation package (no source code) - Provides structured guidance for AI assistants working with Egg - `egg/` - Entry point skill that routes to specialized skills - `egg-controller/` - Controller implementation skill (HTTP, MCP, Schedule) - `egg-core/` - Core framework concepts skill (modules, DI, lifecycle) - **`examples/`** - Example applications - `helloworld-commonjs/` - CommonJS example - `helloworld-typescript/` - TypeScript example - **`tools/egg-bin/`** - CLI development tool package (@eggjs/bin) - `src/` - TypeScript source code for CLI commands - `test/` - Comprehensive test suite with mocha - **`site/`** - Documentation website built with VitePress v2 ### Core Architecture (packages/egg/) - **`src/lib/`** - Core framework classes and utilities - `application.ts` - Main Application class extending EggApplicationCore - `agent.ts` - Agent process manager - `egg.ts` - Core EggApplicationCore with shared functionality - `start.ts` - Application startup logic for cluster/single mode - **`src/app/extend/`** - Framework extensions (context, helper, request, response) - **`src/config/`** - Default configurations and plugins - **`src/lib/core/`** - Core components (httpclient, logger, messenger, base classes) - **`src/lib/loader/`** - Application loaders (AppWorkerLoader, AgentWorkerLoader) ### Key Classes - **EggApplicationCore** - Base application class with core functionality - **Application** - Main app class for worker processes - **Agent** - Agent process class for background tasks - **Context** - Extended Koa context with Egg-specific features - **BaseContextClass** - Base for controllers, services, subscriptions - **AppWorkerLoader/AgentWorkerLoader** - Load app components in convention order ### Loading Convention The framework follows a specific loading order: 1. Plugin system 2. Configurations 3. Application/Request/Response/Context extensions 4. Custom loaders 5. Services 6. Middlewares 7. Controllers 8. Router ## Development Commands ### Monorepo Management - `pnpm install` - Install dependencies for all packages - `pnpm -r run