A TypeScript project based on Cloudflare Workers and Cloudflare Containers (Durable Objects).
Leverages Cloudflare edge infrastructure to run and manage containerized workloads (AgentContainer).
| Command | Description | Notes |
|---|---|---|
pnpm dev |
Start local development server | Equivalent to wrangler dev |
pnpm start |
Alias for pnpm dev |
- |
pnpm deploy |
Deploy to Cloudflare | Equivalent to wrangler deploy |
pnpm cf-typegen |
Generate Cloudflare Bindings types | Must run after modifying wrangler.jsonc |
| Command | Description | Notes |
|---|---|---|
pnpm lint |
Run oxlint to check code | High-performance Rust linter |
pnpm lint:fix |
Auto-fix lint issues | Equivalent to oxlint --fix |
pnpm fmt |
Format code | Uses oxfmt (Biome fork) |
pnpm fmt:check |
Check formatting compliance | Used in CI, does not modify files |
Important: The repository currently has no test framework (Jest/Vitest).
- Do not attempt to run
pnpm test(command does not exist).- Validation methods: TypeScript compiler (
pnpm cf-typegen) +pnpm lint+ manual code review.- If adding tests, Vitest is recommended (good compatibility with Wrangler).
- Indentation: Tab indentation (managed by oxfmt).
- Semicolons: Do not use semicolons at end of statements (ASI style).
- Quotes: Prefer double quotes
"for strings. - Trailing Commas: Keep trailing commas in multi-line object/array definitions (ES2017+).
- Code Blocks: Always use braces
{ ... }, even for single-lineifstatements. - Line Endings: LF (
\n), not CRLF. - End of File: Keep one blank line at end of file.
| Type | Convention | Examples |
|---|---|---|
| Files | camelCase |
container.ts, sse.ts |
| Classes | PascalCase |
AgentContainer |
| Interface/Type | PascalCase |
SSEEvent, SSEEventHandler |
| Variables/Functions | camelCase |
verifyBasicAuth, processSSEStream |
| Constants | UPPER_CASE |
PORT, SINGLETON_CONTAINER_ID |
| Private Properties | _camelCase |
_watchPromise (optional prefix) |
| Boolean Variables | is/has/should prefix |
isAuthorized, hasError |
- Strict Mode: Strict mode is enabled (
tsconfig.json). Prohibit usingany, unless absolutely necessary with explanatory comments. - Type Definitions:
- Prefer
interfacefor defining object structures. - Use
typefor union types and function signatures. - Use
satisfieskeyword to validate exported objects:export default { ... } satisfies ExportedHandler.
- Prefer
- Environment Bindings: Access environment variables only via
import { env } from 'cloudflare:workers'. Prohibit usingprocess.env. - Null Handling: Prefer optional chaining
?.and nullish coalescing??. - Type Imports: Use
import type { ... }to explicitly distinguish pure type imports.
Maintain clear import layering (top to bottom):
// 1. External dependencies (Cloudflare standard library, third-party packages)
import { Container } from '@cloudflare/containers'
import { env } from 'cloudflare:workers'
// 2. Internal modules (local files)
import { forwardRequestToContainer } from './container'
import { processSSEStream } from './sse'
// 3. Type imports (explicit marking)
import type { SSEEvent } from './sse'src/index.tsis the Worker entry point.- It handles HTTP request routing, basic authentication (
verifyBasicAuth), and request forwarding. - Pattern: Functions return
nullto indicate "no error/continue processing", returnResponseobject to indicate "intercept/error".// Example pattern function checkAuth(req): Response | null { if (fail) return new Response('401', ...); return null; // Pass }
- Located in
src/container.ts. - Inherits from
Container(from@cloudflare/containers). - Singleton Pattern: Uses
idFromName(SINGLETON_CONTAINER_ID)to ensure globally unique instance for state management. - Lifecycle:
onStart: Container startup hook. Used to initialize background tasks.- Important: Background tasks (like
watchContainer) should not be awaited inonStartto avoid blocking the DO startup process, but must catch errors (fire-and-forget pattern).
- HTTP Errors: Prefer returning standard HTTP status code Responses (401, 403, 500).
- External I/O: All network requests (fetch, stream reading) must be wrapped in
try-catch. - SSE Streams: When handling Server-Sent Events, ensure proper Reader lifecycle management and gracefully exit on disconnect to avoid resource leaks.
When modifying this codebase as an AI Agent, strictly follow these rules:
-
Language Consistency:
- All code comments must use concise English.
- Git Commit Messages must use English.
- No mixed language comments.
-
Non-Destructive Modifications:
- Without tests, be extremely cautious when modifying code.
- After each modification, recommend running
pnpm cf-typegento ensure type definitions stay in sync withwrangler.jsonc. - Before modifying existing functionality, thoroughly understand its side effects.
-
Configuration is King:
wrangler.jsoncis the single source of truth for infrastructure configuration.- Do not hardcode configuration values (like ports, secrets); use Bindings or environment variables.
- If new variables are needed in code, must first update
wrangler.jsoncand runcf-typegen.
-
File Operations:
- Prefer modifying existing files; avoid creating too many fragmented small files.
worker-configuration.d.tsis auto-generated; do not manually modify it.- Keep directory structure flat; avoid excessive nesting.
-
Dependency Management:
- Only use dependencies defined in
package.json. - When introducing new libraries, first evaluate their compatibility in Cloudflare Workers Runtime (note: limited Node.js API support).
- Prefer Web Standard APIs (Request, Response, fetch, Streams) over Node.js-specific APIs.
- Only use dependencies defined in
| Tool | Description |
|---|---|
| Wrangler | Core CLI tool for deployment, dev, type generation |
| oxlint | High-performance Rust linter, replaces ESLint |
| oxfmt | Biome fork formatter, replaces Prettier |
| pnpm | Package manager (v10.28.1) |
# 1. Type check (if wrangler.jsonc was modified)
pnpm cf-typegen
# 2. Lint check
pnpm lint
# 3. Format check
pnpm fmt:check
# 4. Local development test
pnpm dev(No .cursorrules or .github/copilot-instructions.md files found in this project. If added later, please supplement relevant rules here)
This document is maintained by AI Agents to unify development standards and behavior guidelines.