tribunal-kit 2.4.6 → 3.1.0
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/.agent/ARCHITECTURE.md +99 -99
- package/.agent/GEMINI.md +52 -52
- package/.agent/agents/accessibility-reviewer.md +139 -86
- package/.agent/agents/ai-code-reviewer.md +160 -90
- package/.agent/agents/backend-specialist.md +164 -127
- package/.agent/agents/code-archaeologist.md +115 -73
- package/.agent/agents/database-architect.md +130 -110
- package/.agent/agents/debugger.md +137 -97
- package/.agent/agents/dependency-reviewer.md +78 -30
- package/.agent/agents/devops-engineer.md +161 -118
- package/.agent/agents/documentation-writer.md +151 -87
- package/.agent/agents/explorer-agent.md +117 -99
- package/.agent/agents/frontend-reviewer.md +127 -47
- package/.agent/agents/frontend-specialist.md +169 -109
- package/.agent/agents/game-developer.md +28 -164
- package/.agent/agents/logic-reviewer.md +87 -49
- package/.agent/agents/mobile-developer.md +151 -103
- package/.agent/agents/mobile-reviewer.md +133 -50
- package/.agent/agents/orchestrator.md +121 -110
- package/.agent/agents/penetration-tester.md +103 -77
- package/.agent/agents/performance-optimizer.md +136 -92
- package/.agent/agents/performance-reviewer.md +139 -69
- package/.agent/agents/product-manager.md +104 -70
- package/.agent/agents/product-owner.md +6 -25
- package/.agent/agents/project-planner.md +95 -95
- package/.agent/agents/qa-automation-engineer.md +174 -87
- package/.agent/agents/security-auditor.md +133 -129
- package/.agent/agents/seo-specialist.md +160 -99
- package/.agent/agents/sql-reviewer.md +132 -44
- package/.agent/agents/supervisor-agent.md +137 -109
- package/.agent/agents/swarm-worker-contracts.md +17 -17
- package/.agent/agents/swarm-worker-registry.md +46 -46
- package/.agent/agents/test-coverage-reviewer.md +132 -53
- package/.agent/agents/test-engineer.md +0 -21
- package/.agent/agents/type-safety-reviewer.md +143 -33
- package/.agent/patterns/generator.md +9 -9
- package/.agent/patterns/inversion.md +12 -12
- package/.agent/patterns/pipeline.md +9 -9
- package/.agent/patterns/reviewer.md +13 -13
- package/.agent/patterns/tool-wrapper.md +9 -9
- package/.agent/rules/GEMINI.md +63 -63
- package/.agent/scripts/__pycache__/auto_preview.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/bundle_analyzer.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/checklist.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/dependency_analyzer.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/security_scan.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/session_manager.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/skill_integrator.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/swarm_dispatcher.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/test_runner.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/verify_all.cpython-311.pyc +0 -0
- package/.agent/scripts/compress_skills.py +167 -0
- package/.agent/scripts/consolidate_skills.py +173 -0
- package/.agent/scripts/deep_compress.py +202 -0
- package/.agent/scripts/minify_context.py +80 -0
- package/.agent/scripts/security_scan.py +1 -1
- package/.agent/scripts/strip_tribunal.py +41 -0
- package/.agent/skills/agent-organizer/SKILL.md +60 -100
- package/.agent/skills/agentic-patterns/SKILL.md +0 -70
- package/.agent/skills/ai-prompt-injection-defense/SKILL.md +108 -53
- package/.agent/skills/api-patterns/SKILL.md +197 -257
- package/.agent/skills/api-security-auditor/SKILL.md +125 -57
- package/.agent/skills/app-builder/SKILL.md +326 -50
- package/.agent/skills/app-builder/templates/SKILL.md +13 -15
- package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +16 -16
- package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +22 -22
- package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +18 -18
- package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +20 -20
- package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +17 -17
- package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +18 -18
- package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +21 -21
- package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +19 -19
- package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +26 -26
- package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +26 -26
- package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +19 -19
- package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +18 -18
- package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +20 -20
- package/.agent/skills/appflow-wireframe/SKILL.md +71 -98
- package/.agent/skills/architecture/SKILL.md +161 -200
- package/.agent/skills/authentication-best-practices/SKILL.md +121 -54
- package/.agent/skills/bash-linux/SKILL.md +71 -166
- package/.agent/skills/behavioral-modes/SKILL.md +8 -69
- package/.agent/skills/brainstorming/SKILL.md +345 -127
- package/.agent/skills/building-native-ui/SKILL.md +125 -57
- package/.agent/skills/clean-code/SKILL.md +266 -149
- package/.agent/skills/code-review-checklist/SKILL.md +0 -62
- package/.agent/skills/config-validator/SKILL.md +73 -131
- package/.agent/skills/csharp-developer/SKILL.md +434 -73
- package/.agent/skills/database-design/SKILL.md +190 -275
- package/.agent/skills/deployment-procedures/SKILL.md +81 -158
- package/.agent/skills/devops-engineer/SKILL.md +255 -94
- package/.agent/skills/devops-incident-responder/SKILL.md +50 -69
- package/.agent/skills/doc.md +5 -5
- package/.agent/skills/documentation-templates/SKILL.md +19 -63
- package/.agent/skills/edge-computing/SKILL.md +75 -165
- package/.agent/skills/extract-design-system/SKILL.md +84 -58
- package/.agent/skills/framer-motion-expert/SKILL.md +195 -0
- package/.agent/skills/frontend-design/SKILL.md +151 -499
- package/.agent/skills/game-design-expert/SKILL.md +71 -0
- package/.agent/skills/game-engineering-expert/SKILL.md +88 -0
- package/.agent/skills/geo-fundamentals/SKILL.md +52 -178
- package/.agent/skills/github-operations/SKILL.md +197 -272
- package/.agent/skills/gsap-expert/SKILL.md +194 -0
- package/.agent/skills/i18n-localization/SKILL.md +60 -172
- package/.agent/skills/intelligent-routing/SKILL.md +123 -103
- package/.agent/skills/lint-and-validate/SKILL.md +8 -52
- package/.agent/skills/llm-engineering/SKILL.md +281 -195
- package/.agent/skills/local-first/SKILL.md +76 -159
- package/.agent/skills/mcp-builder/SKILL.md +48 -188
- package/.agent/skills/mobile-design/SKILL.md +213 -219
- package/.agent/skills/motion-engineering/SKILL.md +184 -0
- package/.agent/skills/nextjs-react-expert/SKILL.md +184 -203
- package/.agent/skills/nodejs-best-practices/SKILL.md +403 -185
- package/.agent/skills/observability/SKILL.md +211 -203
- package/.agent/skills/parallel-agents/SKILL.md +53 -146
- package/.agent/skills/performance-profiling/SKILL.md +171 -151
- package/.agent/skills/plan-writing/SKILL.md +49 -153
- package/.agent/skills/platform-engineer/SKILL.md +57 -103
- package/.agent/skills/playwright-best-practices/SKILL.md +110 -63
- package/.agent/skills/powershell-windows/SKILL.md +61 -179
- package/.agent/skills/python-patterns/SKILL.md +7 -35
- package/.agent/skills/python-pro/SKILL.md +273 -114
- package/.agent/skills/react-specialist/SKILL.md +227 -108
- package/.agent/skills/readme-builder/SKILL.md +15 -85
- package/.agent/skills/realtime-patterns/SKILL.md +216 -243
- package/.agent/skills/red-team-tactics/SKILL.md +10 -51
- package/.agent/skills/rust-pro/SKILL.md +525 -142
- package/.agent/skills/seo-fundamentals/SKILL.md +92 -153
- package/.agent/skills/server-management/SKILL.md +110 -166
- package/.agent/skills/shadcn-ui-expert/SKILL.md +154 -55
- package/.agent/skills/skill-creator/SKILL.md +18 -58
- package/.agent/skills/sql-pro/SKILL.md +543 -68
- package/.agent/skills/supabase-postgres-best-practices/SKILL.md +28 -68
- package/.agent/skills/swiftui-expert/SKILL.md +124 -57
- package/.agent/skills/systematic-debugging/SKILL.md +49 -151
- package/.agent/skills/tailwind-patterns/SKILL.md +433 -149
- package/.agent/skills/tdd-workflow/SKILL.md +63 -169
- package/.agent/skills/test-result-analyzer/SKILL.md +33 -73
- package/.agent/skills/testing-patterns/SKILL.md +437 -130
- package/.agent/skills/trend-researcher/SKILL.md +30 -71
- package/.agent/skills/ui-ux-pro-max/SKILL.md +0 -41
- package/.agent/skills/ui-ux-researcher/SKILL.md +51 -91
- package/.agent/skills/vue-expert/SKILL.md +225 -119
- package/.agent/skills/vulnerability-scanner/SKILL.md +264 -226
- package/.agent/skills/web-accessibility-auditor/SKILL.md +141 -58
- package/.agent/skills/web-design-guidelines/SKILL.md +17 -61
- package/.agent/skills/webapp-testing/SKILL.md +71 -196
- package/.agent/skills/whimsy-injector/SKILL.md +58 -132
- package/.agent/skills/workflow-optimizer/SKILL.md +28 -68
- package/.agent/workflows/api-tester.md +96 -224
- package/.agent/workflows/audit.md +81 -122
- package/.agent/workflows/brainstorm.md +69 -105
- package/.agent/workflows/changelog.md +65 -97
- package/.agent/workflows/create.md +73 -88
- package/.agent/workflows/debug.md +80 -111
- package/.agent/workflows/deploy.md +119 -92
- package/.agent/workflows/enhance.md +80 -91
- package/.agent/workflows/fix.md +68 -97
- package/.agent/workflows/generate.md +165 -164
- package/.agent/workflows/migrate.md +106 -109
- package/.agent/workflows/orchestrate.md +103 -86
- package/.agent/workflows/performance-benchmarker.md +77 -268
- package/.agent/workflows/plan.md +120 -98
- package/.agent/workflows/preview.md +39 -96
- package/.agent/workflows/refactor.md +105 -97
- package/.agent/workflows/review-ai.md +63 -102
- package/.agent/workflows/review.md +71 -110
- package/.agent/workflows/session.md +53 -113
- package/.agent/workflows/status.md +42 -88
- package/.agent/workflows/strengthen-skills.md +90 -51
- package/.agent/workflows/swarm.md +114 -129
- package/.agent/workflows/test.md +125 -102
- package/.agent/workflows/tribunal-backend.md +60 -78
- package/.agent/workflows/tribunal-database.md +62 -100
- package/.agent/workflows/tribunal-frontend.md +62 -82
- package/.agent/workflows/tribunal-full.md +56 -100
- package/.agent/workflows/tribunal-mobile.md +65 -94
- package/.agent/workflows/tribunal-performance.md +62 -105
- package/.agent/workflows/ui-ux-pro-max.md +72 -121
- package/README.md +11 -15
- package/package.json +1 -1
- package/.agent/skills/api-patterns/api-style.md +0 -42
- package/.agent/skills/api-patterns/auth.md +0 -24
- package/.agent/skills/api-patterns/documentation.md +0 -26
- package/.agent/skills/api-patterns/graphql.md +0 -41
- package/.agent/skills/api-patterns/rate-limiting.md +0 -31
- package/.agent/skills/api-patterns/response.md +0 -37
- package/.agent/skills/api-patterns/rest.md +0 -40
- package/.agent/skills/api-patterns/security-testing.md +0 -122
- package/.agent/skills/api-patterns/trpc.md +0 -41
- package/.agent/skills/api-patterns/versioning.md +0 -22
- package/.agent/skills/app-builder/agent-coordination.md +0 -71
- package/.agent/skills/app-builder/feature-building.md +0 -53
- package/.agent/skills/app-builder/project-detection.md +0 -34
- package/.agent/skills/app-builder/scaffolding.md +0 -118
- package/.agent/skills/app-builder/tech-stack.md +0 -40
- package/.agent/skills/architecture/context-discovery.md +0 -43
- package/.agent/skills/architecture/examples.md +0 -94
- package/.agent/skills/architecture/pattern-selection.md +0 -68
- package/.agent/skills/architecture/patterns-reference.md +0 -50
- package/.agent/skills/architecture/trade-off-analysis.md +0 -77
- package/.agent/skills/brainstorming/dynamic-questioning.md +0 -360
- package/.agent/skills/database-design/database-selection.md +0 -43
- package/.agent/skills/database-design/indexing.md +0 -39
- package/.agent/skills/database-design/migrations.md +0 -48
- package/.agent/skills/database-design/optimization.md +0 -36
- package/.agent/skills/database-design/orm-selection.md +0 -30
- package/.agent/skills/database-design/schema-design.md +0 -56
- package/.agent/skills/dotnet-core-expert/SKILL.md +0 -103
- package/.agent/skills/framer-motion-animations/SKILL.md +0 -74
- package/.agent/skills/frontend-design/animation-guide.md +0 -331
- package/.agent/skills/frontend-design/color-system.md +0 -329
- package/.agent/skills/frontend-design/decision-trees.md +0 -418
- package/.agent/skills/frontend-design/motion-graphics.md +0 -306
- package/.agent/skills/frontend-design/typography-system.md +0 -363
- package/.agent/skills/frontend-design/ux-psychology.md +0 -1116
- package/.agent/skills/frontend-design/visual-effects.md +0 -383
- package/.agent/skills/game-development/2d-games/SKILL.md +0 -119
- package/.agent/skills/game-development/3d-games/SKILL.md +0 -135
- package/.agent/skills/game-development/SKILL.md +0 -236
- package/.agent/skills/game-development/game-art/SKILL.md +0 -185
- package/.agent/skills/game-development/game-audio/SKILL.md +0 -190
- package/.agent/skills/game-development/game-design/SKILL.md +0 -129
- package/.agent/skills/game-development/mobile-games/SKILL.md +0 -108
- package/.agent/skills/game-development/multiplayer/SKILL.md +0 -132
- package/.agent/skills/game-development/pc-games/SKILL.md +0 -144
- package/.agent/skills/game-development/vr-ar/SKILL.md +0 -123
- package/.agent/skills/game-development/web-games/SKILL.md +0 -150
- package/.agent/skills/intelligent-routing/router-manifest.md +0 -65
- package/.agent/skills/mobile-design/decision-trees.md +0 -516
- package/.agent/skills/mobile-design/mobile-backend.md +0 -491
- package/.agent/skills/mobile-design/mobile-color-system.md +0 -420
- package/.agent/skills/mobile-design/mobile-debugging.md +0 -122
- package/.agent/skills/mobile-design/mobile-design-thinking.md +0 -357
- package/.agent/skills/mobile-design/mobile-navigation.md +0 -458
- package/.agent/skills/mobile-design/mobile-performance.md +0 -767
- package/.agent/skills/mobile-design/mobile-testing.md +0 -356
- package/.agent/skills/mobile-design/mobile-typography.md +0 -433
- package/.agent/skills/mobile-design/platform-android.md +0 -666
- package/.agent/skills/mobile-design/platform-ios.md +0 -561
- package/.agent/skills/mobile-design/touch-psychology.md +0 -537
- package/.agent/skills/nextjs-react-expert/1-async-eliminating-waterfalls.md +0 -312
- package/.agent/skills/nextjs-react-expert/2-bundle-bundle-size-optimization.md +0 -240
- package/.agent/skills/nextjs-react-expert/3-server-server-side-performance.md +0 -490
- package/.agent/skills/nextjs-react-expert/4-client-client-side-data-fetching.md +0 -264
- package/.agent/skills/nextjs-react-expert/5-rerender-re-render-optimization.md +0 -581
- package/.agent/skills/nextjs-react-expert/6-rendering-rendering-performance.md +0 -432
- package/.agent/skills/nextjs-react-expert/7-js-javascript-performance.md +0 -684
- package/.agent/skills/nextjs-react-expert/8-advanced-advanced-patterns.md +0 -150
- package/.agent/skills/vulnerability-scanner/checklists.md +0 -121
|
@@ -1,280 +1,498 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: nodejs-best-practices
|
|
3
|
-
description: Node.js
|
|
3
|
+
description: Node.js 22+ production mastery. ES Modules, async patterns, Express/Fastify/Hono, middleware architecture, error handling, streaming, worker threads, environment config, security hardening, process management, and deployment patterns. Use when building Node.js servers, APIs, CLI tools, or any server-side JavaScript.
|
|
4
4
|
allowed-tools: Read, Write, Edit, Glob, Grep
|
|
5
|
-
version:
|
|
6
|
-
last-updated: 2026-
|
|
5
|
+
version: 2.0.0
|
|
6
|
+
last-updated: 2026-04-01
|
|
7
7
|
applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
|
|
8
8
|
---
|
|
9
9
|
|
|
10
|
-
# Node.js
|
|
11
|
-
|
|
12
|
-
> Node.js is not a problem — unpredictable async behavior is.
|
|
13
|
-
> Understand the event loop and half of Node.js "gotchas" disappear.
|
|
10
|
+
# Node.js Best Practices — Node 22+ Production Mastery
|
|
14
11
|
|
|
15
12
|
---
|
|
16
13
|
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
| Context | Recommended | Why |
|
|
20
|
-
|---|---|---|
|
|
21
|
-
| REST API, standard patterns | Express + TypeScript | Mature, flexible, most hiring knowledge |
|
|
22
|
-
| REST API, speed + TypeScript-first | Fastify | 2x Express throughput, built-in validation |
|
|
23
|
-
| Full-stack React | Next.js API routes | Colocated API and UI, serverless-friendly |
|
|
24
|
-
| REST + tRPC | Next.js + tRPC | Type-safe end-to-end with no code generation |
|
|
25
|
-
| RPC across services | gRPC | Binary protocol, contract-first |
|
|
26
|
-
| Edge function / Cloudflare Worker | Hono | Tiny, Web Platform API-native, zero cold start |
|
|
27
|
-
| Scripts / tooling / bun-native | Bun | Built-in bundler, test runner, near-Node compat |
|
|
28
|
-
|
|
29
|
-
**Questions to ask before choosing:**
|
|
30
|
-
|
|
31
|
-
- Is this public-facing or internal?
|
|
32
|
-
- Do we control the deployment environment (server vs. serverless vs. edge)?
|
|
33
|
-
- Is the team already familiar with a framework?
|
|
34
|
-
- Does this need to compose with an existing TypeScript frontend?
|
|
35
|
-
|
|
36
|
-
---
|
|
14
|
+
## ES Modules (Mandatory)
|
|
37
15
|
|
|
38
|
-
|
|
16
|
+
```json
|
|
17
|
+
// package.json — ALWAYS set type to module
|
|
18
|
+
{
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": { "node": ">=22" }
|
|
21
|
+
}
|
|
22
|
+
```
|
|
39
23
|
|
|
40
|
-
|
|
24
|
+
```typescript
|
|
25
|
+
// ✅ ESM imports (modern Node.js)
|
|
26
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
27
|
+
import { join, resolve } from "node:path";
|
|
28
|
+
import { createServer } from "node:http";
|
|
29
|
+
import { EventEmitter } from "node:events";
|
|
41
30
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
| **Bun** | ✅ | ✅ | ✅ | ✅ | Server (Node-compatible) |
|
|
46
|
-
| **Deno** | ✅ (explicit perm) | ✅ | ✅ (explicit perm) | ✅ | Server, Deno Deploy |
|
|
47
|
-
| **Edge (Cloudflare Workers)** | ❌ | Web only | ❌ | via `env` binding | Edge (global) |
|
|
31
|
+
// ❌ HALLUCINATION TRAP: Use node: protocol prefix for built-in modules
|
|
32
|
+
// ❌ import fs from "fs"; ← ambiguous (could be npm package)
|
|
33
|
+
// ✅ import fs from "node:fs"; ← explicitly a Node.js built-in
|
|
48
34
|
|
|
49
|
-
|
|
50
|
-
// ❌
|
|
51
|
-
|
|
52
|
-
import fs from 'fs'; // No filesystem at edge
|
|
35
|
+
// ❌ HALLUCINATION TRAP: Do NOT use require() in ESM projects
|
|
36
|
+
// ❌ const fs = require("fs"); ← CommonJS (legacy)
|
|
37
|
+
// ✅ import fs from "node:fs/promises"; ← ESM
|
|
53
38
|
|
|
54
|
-
//
|
|
55
|
-
const
|
|
56
|
-
const response = await fetch('https://api.example.com/data');
|
|
39
|
+
// Dynamic imports (for conditional loading)
|
|
40
|
+
const module = await import("./heavy-module.js");
|
|
57
41
|
```
|
|
58
42
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
```ts
|
|
62
|
-
// Bun is Node-compatible but: starts faster, ships a bundler + test runner built-in
|
|
63
|
-
// Use Bun when: scripts, tooling, new greenfield backends, or test-heavy projects
|
|
43
|
+
---
|
|
64
44
|
|
|
65
|
-
|
|
66
|
-
// bun install → 10x faster than npm install
|
|
67
|
-
// bun test → built-in Vitest-compatible runner
|
|
68
|
-
// bun run → direct TypeScript execution, no transpile step
|
|
45
|
+
## Framework Selection
|
|
69
46
|
|
|
70
|
-
|
|
71
|
-
|
|
47
|
+
```
|
|
48
|
+
┌────────────────────────────────────────────────────────────┐
|
|
49
|
+
│ When to Use What │
|
|
50
|
+
├────────────────────────────────────────────────────────────┤
|
|
51
|
+
│ Express │ Legacy projects, extensive middleware ecosystem│
|
|
52
|
+
│ Fastify │ Performance-critical APIs, schema validation │
|
|
53
|
+
│ Hono │ Edge/serverless, multi-runtime (Node/Deno/Bun)│
|
|
54
|
+
│ tRPC │ Full-stack TypeScript (Next.js + React Query) │
|
|
55
|
+
│ Raw http │ Learning, minimal proxies, health checks │
|
|
56
|
+
└────────────────────────────────────────────────────────────┘
|
|
72
57
|
```
|
|
73
58
|
|
|
74
|
-
|
|
59
|
+
### Fastify (Recommended)
|
|
75
60
|
|
|
76
|
-
|
|
61
|
+
```typescript
|
|
62
|
+
import Fastify from "fastify";
|
|
63
|
+
import { z } from "zod";
|
|
77
64
|
|
|
78
|
-
|
|
65
|
+
const app = Fastify({
|
|
66
|
+
logger: {
|
|
67
|
+
level: process.env.LOG_LEVEL ?? "info",
|
|
68
|
+
transport: process.env.NODE_ENV === "development"
|
|
69
|
+
? { target: "pino-pretty" }
|
|
70
|
+
: undefined,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
79
73
|
|
|
80
|
-
|
|
74
|
+
// Schema validation with Zod → Fastify schema
|
|
75
|
+
const CreateUserSchema = z.object({
|
|
76
|
+
name: z.string().min(2).max(100),
|
|
77
|
+
email: z.string().email(),
|
|
78
|
+
role: z.enum(["admin", "user"]).default("user"),
|
|
79
|
+
});
|
|
81
80
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
type CreateUserBody = z.infer<typeof CreateUserSchema>;
|
|
82
|
+
|
|
83
|
+
app.post<{ Body: CreateUserBody }>("/users", {
|
|
84
|
+
schema: {
|
|
85
|
+
body: {
|
|
86
|
+
type: "object",
|
|
87
|
+
required: ["name", "email"],
|
|
88
|
+
properties: {
|
|
89
|
+
name: { type: "string", minLength: 2 },
|
|
90
|
+
email: { type: "string", format: "email" },
|
|
91
|
+
role: { type: "string", enum: ["admin", "user"] },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
}, async (request, reply) => {
|
|
96
|
+
const validated = CreateUserSchema.parse(request.body);
|
|
97
|
+
const user = await createUser(validated);
|
|
98
|
+
return reply.status(201).send(user);
|
|
99
|
+
});
|
|
85
100
|
|
|
86
|
-
//
|
|
87
|
-
|
|
101
|
+
// Graceful shutdown
|
|
102
|
+
const start = async () => {
|
|
103
|
+
try {
|
|
104
|
+
await app.listen({ port: 3000, host: "0.0.0.0" });
|
|
105
|
+
} catch (err) {
|
|
106
|
+
app.log.error(err);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
88
110
|
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
const data = await fetchData();
|
|
92
|
-
process(data);
|
|
93
|
-
} catch (err) {
|
|
94
|
-
handleError(err);
|
|
95
|
-
}
|
|
111
|
+
start();
|
|
96
112
|
```
|
|
97
113
|
|
|
98
|
-
###
|
|
114
|
+
### Hono (Edge-First)
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { Hono } from "hono";
|
|
118
|
+
import { cors } from "hono/cors";
|
|
119
|
+
import { logger } from "hono/logger";
|
|
120
|
+
import { zValidator } from "@hono/zod-validator";
|
|
121
|
+
import { z } from "zod";
|
|
99
122
|
|
|
100
|
-
|
|
123
|
+
const app = new Hono();
|
|
101
124
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const data = fs.readFileSync('huge.csv');
|
|
125
|
+
app.use("*", logger());
|
|
126
|
+
app.use("*", cors({ origin: "https://myapp.com" }));
|
|
105
127
|
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
const createUserSchema = z.object({
|
|
129
|
+
name: z.string().min(2),
|
|
130
|
+
email: z.string().email(),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
app.post("/users", zValidator("json", createUserSchema), async (c) => {
|
|
134
|
+
const body = c.req.valid("json");
|
|
135
|
+
const user = await createUser(body);
|
|
136
|
+
return c.json(user, 201);
|
|
137
|
+
});
|
|
108
138
|
|
|
109
|
-
|
|
110
|
-
const result = computeHuge(dataset);
|
|
139
|
+
app.get("/health", (c) => c.json({ status: "ok" }));
|
|
111
140
|
|
|
112
|
-
//
|
|
113
|
-
const { Worker } = require('worker_threads');
|
|
141
|
+
export default app; // works in Node, Deno, Bun, Cloudflare Workers
|
|
114
142
|
```
|
|
115
143
|
|
|
116
|
-
|
|
144
|
+
---
|
|
117
145
|
|
|
118
|
-
|
|
119
|
-
// Sequential — each awaits the previous (use when order matters or requests are dependent)
|
|
120
|
-
for (const id of ids) {
|
|
121
|
-
await processItem(id);
|
|
122
|
-
}
|
|
146
|
+
## Error Handling
|
|
123
147
|
|
|
124
|
-
|
|
125
|
-
await Promise.all(ids.map(id => processItem(id)));
|
|
148
|
+
### Global Error Handlers
|
|
126
149
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
150
|
+
```typescript
|
|
151
|
+
// ✅ MANDATORY: Handle unhandled rejections and exceptions
|
|
152
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
153
|
+
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
|
154
|
+
// Log to error tracking service (Sentry, etc.)
|
|
155
|
+
// Gracefully shutdown
|
|
156
|
+
process.exit(1);
|
|
157
|
+
});
|
|
132
158
|
|
|
133
|
-
|
|
159
|
+
process.on("uncaughtException", (error) => {
|
|
160
|
+
console.error("Uncaught Exception:", error);
|
|
161
|
+
// Log to error tracking service
|
|
162
|
+
process.exit(1); // MUST exit — state is corrupted
|
|
163
|
+
});
|
|
134
164
|
|
|
135
|
-
|
|
165
|
+
// ❌ HALLUCINATION TRAP: After uncaughtException, the process MUST exit
|
|
166
|
+
// The process is in an undefined state — continuing is dangerous
|
|
167
|
+
// ❌ process.on("uncaughtException", (err) => { console.log(err); }); // continues running
|
|
168
|
+
// ✅ process.on("uncaughtException", (err) => { log(err); process.exit(1); });
|
|
169
|
+
```
|
|
136
170
|
|
|
137
|
-
|
|
171
|
+
### Application Error Classes
|
|
138
172
|
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
class AppError extends Error {
|
|
173
|
+
```typescript
|
|
174
|
+
export class AppError extends Error {
|
|
142
175
|
constructor(
|
|
143
176
|
message: string,
|
|
144
|
-
public
|
|
145
|
-
public
|
|
146
|
-
public isOperational = true
|
|
177
|
+
public statusCode: number = 500,
|
|
178
|
+
public code: string = "INTERNAL_ERROR",
|
|
179
|
+
public isOperational: boolean = true,
|
|
147
180
|
) {
|
|
148
181
|
super(message);
|
|
149
|
-
this.name =
|
|
182
|
+
this.name = "AppError";
|
|
183
|
+
Error.captureStackTrace(this, this.constructor);
|
|
150
184
|
}
|
|
151
185
|
}
|
|
152
186
|
|
|
153
|
-
|
|
154
|
-
class NotFoundError extends AppError {
|
|
187
|
+
export class NotFoundError extends AppError {
|
|
155
188
|
constructor(resource: string, id: string) {
|
|
156
|
-
super(`${resource} ${id} not found`,
|
|
189
|
+
super(`${resource} '${id}' not found`, 404, "NOT_FOUND");
|
|
157
190
|
}
|
|
158
191
|
}
|
|
159
192
|
|
|
160
|
-
class ValidationError extends AppError {
|
|
161
|
-
constructor(message: string) {
|
|
162
|
-
super(message,
|
|
193
|
+
export class ValidationError extends AppError {
|
|
194
|
+
constructor(message: string, public errors: Record<string, string[]> = {}) {
|
|
195
|
+
super(message, 400, "VALIDATION_ERROR");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export class UnauthorizedError extends AppError {
|
|
200
|
+
constructor(message = "Authentication required") {
|
|
201
|
+
super(message, 401, "UNAUTHORIZED");
|
|
163
202
|
}
|
|
164
203
|
}
|
|
165
|
-
```
|
|
166
204
|
|
|
167
|
-
|
|
168
|
-
```ts
|
|
205
|
+
// Express error middleware
|
|
169
206
|
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
|
170
207
|
if (err instanceof AppError && err.isOperational) {
|
|
171
|
-
|
|
172
|
-
error: err.message,
|
|
173
|
-
|
|
208
|
+
res.status(err.statusCode).json({
|
|
209
|
+
error: { code: err.code, message: err.message },
|
|
210
|
+
});
|
|
211
|
+
} else {
|
|
212
|
+
// Programmer error — log and return generic message
|
|
213
|
+
console.error("Unexpected error:", err);
|
|
214
|
+
res.status(500).json({
|
|
215
|
+
error: { code: "INTERNAL_ERROR", message: "Something went wrong" },
|
|
174
216
|
});
|
|
175
217
|
}
|
|
176
|
-
// Non-operational errors — log fully, don't expose details
|
|
177
|
-
logger.error('Unexpected error', { err, url: req.url });
|
|
178
|
-
res.status(500).json({ error: 'Internal server error' });
|
|
179
218
|
});
|
|
180
219
|
```
|
|
181
220
|
|
|
182
221
|
---
|
|
183
222
|
|
|
184
|
-
##
|
|
223
|
+
## Async Patterns
|
|
224
|
+
|
|
225
|
+
### Parallel vs Sequential
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
// ❌ SEQUENTIAL: Each await blocks the next
|
|
229
|
+
const users = await getUsers(); // 200ms
|
|
230
|
+
const posts = await getPosts(); // 200ms
|
|
231
|
+
const stats = await getStats(); // 200ms
|
|
232
|
+
// Total: 600ms
|
|
233
|
+
|
|
234
|
+
// ✅ PARALLEL: All start simultaneously
|
|
235
|
+
const [users, posts, stats] = await Promise.all([
|
|
236
|
+
getUsers(), // 200ms
|
|
237
|
+
getPosts(), // 200ms (concurrent)
|
|
238
|
+
getStats(), // 200ms (concurrent)
|
|
239
|
+
]);
|
|
240
|
+
// Total: ~200ms
|
|
241
|
+
|
|
242
|
+
// Promise.allSettled — when some can fail
|
|
243
|
+
const results = await Promise.allSettled([
|
|
244
|
+
fetchCriticalData(),
|
|
245
|
+
fetchOptionalData(),
|
|
246
|
+
fetchAnalytics(),
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
for (const result of results) {
|
|
250
|
+
if (result.status === "fulfilled") {
|
|
251
|
+
process(result.value);
|
|
252
|
+
} else {
|
|
253
|
+
console.error("Failed:", result.reason);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Retry Pattern
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
async function withRetry<T>(
|
|
262
|
+
fn: () => Promise<T>,
|
|
263
|
+
options: { maxRetries?: number; baseDelay?: number; maxDelay?: number } = {},
|
|
264
|
+
): Promise<T> {
|
|
265
|
+
const { maxRetries = 3, baseDelay = 1000, maxDelay = 10000 } = options;
|
|
266
|
+
|
|
267
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
268
|
+
try {
|
|
269
|
+
return await fn();
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (attempt === maxRetries) throw error;
|
|
272
|
+
|
|
273
|
+
const delay = Math.min(baseDelay * 2 ** attempt, maxDelay);
|
|
274
|
+
const jitter = delay * (0.5 + Math.random() * 0.5);
|
|
275
|
+
console.warn(`Attempt ${attempt + 1} failed, retrying in ${jitter}ms`);
|
|
276
|
+
await new Promise((resolve) => setTimeout(resolve, jitter));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
throw new Error("Unreachable");
|
|
280
|
+
}
|
|
185
281
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
282
|
+
// Usage:
|
|
283
|
+
const data = await withRetry(() => fetch("https://api.flaky.com/data"), {
|
|
284
|
+
maxRetries: 3,
|
|
285
|
+
baseDelay: 500,
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### AbortController (Timeouts & Cancellation)
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
async function fetchWithTimeout(url: string, timeoutMs = 5000): Promise<Response> {
|
|
293
|
+
const controller = new AbortController();
|
|
294
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
const response = await fetch(url, { signal: controller.signal });
|
|
298
|
+
return response;
|
|
299
|
+
} catch (error) {
|
|
300
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
301
|
+
throw new Error(`Request to ${url} timed out after ${timeoutMs}ms`);
|
|
302
|
+
}
|
|
303
|
+
throw error;
|
|
304
|
+
} finally {
|
|
305
|
+
clearTimeout(timeoutId);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
## Streaming
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { Readable, Transform, pipeline } from "node:stream/promises";
|
|
316
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
317
|
+
import { createGzip } from "node:zlib";
|
|
318
|
+
|
|
319
|
+
// Stream large file processing (no memory issues)
|
|
320
|
+
async function processLargeCSV(inputPath: string, outputPath: string) {
|
|
321
|
+
const transform = new Transform({
|
|
322
|
+
transform(chunk, encoding, callback) {
|
|
323
|
+
const processed = chunk.toString().toUpperCase();
|
|
324
|
+
callback(null, processed);
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
await pipeline(
|
|
329
|
+
createReadStream(inputPath),
|
|
330
|
+
transform,
|
|
331
|
+
createGzip(),
|
|
332
|
+
createWriteStream(outputPath),
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Streaming HTTP response
|
|
337
|
+
app.get("/export", async (req, res) => {
|
|
338
|
+
res.setHeader("Content-Type", "text/csv");
|
|
339
|
+
res.setHeader("Content-Disposition", "attachment; filename=export.csv");
|
|
340
|
+
|
|
341
|
+
const cursor = db.collection("users").find().stream();
|
|
342
|
+
cursor.on("data", (user) => {
|
|
343
|
+
res.write(`${user.id},${user.name},${user.email}\n`);
|
|
344
|
+
});
|
|
345
|
+
cursor.on("end", () => res.end());
|
|
346
|
+
cursor.on("error", (err) => {
|
|
347
|
+
console.error(err);
|
|
348
|
+
res.status(500).end();
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// ❌ HALLUCINATION TRAP: Use stream/promises (not callbacks)
|
|
353
|
+
// ❌ const { pipeline } = require("stream"); ← callback-based
|
|
354
|
+
// ✅ import { pipeline } from "node:stream/promises"; ← async/await
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
---
|
|
358
|
+
|
|
359
|
+
## Environment & Config
|
|
360
|
+
|
|
361
|
+
```typescript
|
|
362
|
+
// config.ts — centralized, validated configuration
|
|
363
|
+
import { z } from "zod";
|
|
364
|
+
|
|
365
|
+
const envSchema = z.object({
|
|
366
|
+
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
|
|
367
|
+
PORT: z.coerce.number().default(3000),
|
|
368
|
+
DATABASE_URL: z.string().url(),
|
|
369
|
+
REDIS_URL: z.string().url().optional(),
|
|
370
|
+
JWT_SECRET: z.string().min(32),
|
|
371
|
+
CORS_ORIGIN: z.string().default("http://localhost:5173"),
|
|
372
|
+
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
export const config = envSchema.parse(process.env);
|
|
376
|
+
|
|
377
|
+
// ❌ HALLUCINATION TRAP: Validate env vars at startup — fail FAST
|
|
378
|
+
// ❌ process.env.DATABASE_URL! ← crashes at runtime, not startup
|
|
379
|
+
// ✅ Validate with Zod on app init — crash immediately with clear error
|
|
380
|
+
|
|
381
|
+
// ❌ HALLUCINATION TRAP: Never hardcode secrets
|
|
382
|
+
// ❌ const JWT_SECRET = "my-secret-key"; ← in source code
|
|
383
|
+
// ✅ const JWT_SECRET = process.env.JWT_SECRET; ← from environment
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
## Security Hardening
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
import helmet from "helmet";
|
|
392
|
+
import rateLimit from "express-rate-limit";
|
|
189
393
|
|
|
190
394
|
// Security headers
|
|
191
395
|
app.use(helmet());
|
|
192
396
|
|
|
193
|
-
// Rate limiting
|
|
194
|
-
app.use(rateLimit({
|
|
397
|
+
// Rate limiting
|
|
398
|
+
app.use("/api/", rateLimit({
|
|
195
399
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
196
|
-
max: 100,
|
|
400
|
+
max: 100, // 100 requests per window
|
|
197
401
|
standardHeaders: true,
|
|
198
402
|
legacyHeaders: false,
|
|
403
|
+
message: { error: "Too many requests, try again later" },
|
|
199
404
|
}));
|
|
200
405
|
|
|
201
|
-
//
|
|
202
|
-
app.use(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
```
|
|
406
|
+
// Auth rate limiting (stricter)
|
|
407
|
+
app.use("/api/auth/", rateLimit({
|
|
408
|
+
windowMs: 15 * 60 * 1000,
|
|
409
|
+
max: 5, // only 5 login attempts per 15 min
|
|
410
|
+
}));
|
|
207
411
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
412
|
+
// Input validation (ALWAYS validate)
|
|
413
|
+
app.post("/api/users", async (req, res, next) => {
|
|
414
|
+
try {
|
|
415
|
+
const data = CreateUserSchema.parse(req.body); // Zod validates
|
|
416
|
+
const user = await createUser(data);
|
|
417
|
+
res.status(201).json(user);
|
|
418
|
+
} catch (error) {
|
|
419
|
+
next(error);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
212
422
|
|
|
213
|
-
|
|
423
|
+
// SQL injection prevention (covered by parameterized queries)
|
|
424
|
+
// ❌ db.query(`SELECT * FROM users WHERE id = ${req.params.id}`);
|
|
425
|
+
// ✅ db.query("SELECT * FROM users WHERE id = $1", [req.params.id]);
|
|
214
426
|
|
|
215
|
-
|
|
427
|
+
// Path traversal prevention
|
|
428
|
+
import { resolve, normalize } from "node:path";
|
|
216
429
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
lib/ Shared utilities (date, crypto, validation)
|
|
225
|
-
types/ TypeScript interfaces and type exports
|
|
226
|
-
config/ Environment config with validation
|
|
430
|
+
function safePath(userInput: string, baseDir: string): string {
|
|
431
|
+
const resolved = resolve(baseDir, normalize(userInput));
|
|
432
|
+
if (!resolved.startsWith(baseDir)) {
|
|
433
|
+
throw new Error("Path traversal detected");
|
|
434
|
+
}
|
|
435
|
+
return resolved;
|
|
436
|
+
}
|
|
227
437
|
```
|
|
228
438
|
|
|
229
|
-
**Dependency direction:** routes → controllers → services → repositories
|
|
230
|
-
**Never:** repositories calling services, or services knowing about HTTP
|
|
231
|
-
|
|
232
439
|
---
|
|
233
440
|
|
|
234
|
-
##
|
|
441
|
+
## Process Management
|
|
235
442
|
|
|
236
|
-
|
|
443
|
+
### Graceful Shutdown
|
|
237
444
|
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
Language: [detected language / framework]
|
|
242
|
-
Scope: [N files · N functions]
|
|
243
|
-
─────────────────────────────────────────────────
|
|
244
|
-
✅ Passed: [checks that passed, or "All clean"]
|
|
245
|
-
⚠️ Warnings: [non-blocking issues, or "None"]
|
|
246
|
-
❌ Blocked: [blocking issues requiring fix, or "None"]
|
|
247
|
-
─────────────────────────────────────────────────
|
|
248
|
-
VBC status: PENDING → VERIFIED
|
|
249
|
-
Evidence: [test output / lint pass / compile success]
|
|
250
|
-
```
|
|
445
|
+
```typescript
|
|
446
|
+
async function gracefulShutdown(signal: string) {
|
|
447
|
+
console.log(`\n${signal} received — shutting down gracefully`);
|
|
251
448
|
|
|
252
|
-
|
|
253
|
-
|
|
449
|
+
// Stop accepting new connections
|
|
450
|
+
server.close();
|
|
254
451
|
|
|
452
|
+
// Close database connections
|
|
453
|
+
await db.end();
|
|
255
454
|
|
|
256
|
-
|
|
455
|
+
// Close Redis
|
|
456
|
+
await redis.quit();
|
|
257
457
|
|
|
258
|
-
|
|
458
|
+
// Allow in-flight requests 10s to complete
|
|
459
|
+
setTimeout(() => {
|
|
460
|
+
console.error("Forceful shutdown after timeout");
|
|
461
|
+
process.exit(1);
|
|
462
|
+
}, 10000);
|
|
259
463
|
|
|
260
|
-
|
|
261
|
-
|
|
464
|
+
process.exit(0);
|
|
465
|
+
}
|
|
262
466
|
|
|
263
|
-
|
|
467
|
+
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
|
|
468
|
+
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
|
|
469
|
+
```
|
|
264
470
|
|
|
265
|
-
|
|
266
|
-
2. **"Catch-all and ignore" error handling** — e.g., `catch (e) { console.log(e); }` without throwing or returning an error response.
|
|
267
|
-
3. **Assuming Express** — if the project is Next.js, Fastify, or NestJS, do not hallucinate Express code.
|
|
268
|
-
4. **Unparameterized queries** — never interpolate strings into SQL.
|
|
269
|
-
5. **No `any` types** — unless an external library leaves no choice, type all request bodies and responses.
|
|
471
|
+
### Worker Threads (CPU-Bound)
|
|
270
472
|
|
|
271
|
-
|
|
473
|
+
```typescript
|
|
474
|
+
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
|
272
475
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
476
|
+
if (isMainThread) {
|
|
477
|
+
// Main thread — offload CPU work
|
|
478
|
+
function runWorker(data: unknown): Promise<unknown> {
|
|
479
|
+
return new Promise((resolve, reject) => {
|
|
480
|
+
const worker = new Worker(new URL(import.meta.url), { workerData: data });
|
|
481
|
+
worker.on("message", resolve);
|
|
482
|
+
worker.on("error", reject);
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const result = await runWorker({ input: largeDataSet });
|
|
487
|
+
} else {
|
|
488
|
+
// Worker thread — CPU-intensive work
|
|
489
|
+
const result = heavyComputation(workerData.input);
|
|
490
|
+
parentPort?.postMessage(result);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// ❌ HALLUCINATION TRAP: Worker threads are for CPU-bound tasks
|
|
494
|
+
// For I/O-bound tasks (network, file), use async/await — NOT workers
|
|
495
|
+
// Workers have overhead (serialization, memory) — don't overuse
|
|
280
496
|
```
|
|
497
|
+
|
|
498
|
+
---
|