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.
Files changed (250) hide show
  1. package/.agent/ARCHITECTURE.md +99 -99
  2. package/.agent/GEMINI.md +52 -52
  3. package/.agent/agents/accessibility-reviewer.md +139 -86
  4. package/.agent/agents/ai-code-reviewer.md +160 -90
  5. package/.agent/agents/backend-specialist.md +164 -127
  6. package/.agent/agents/code-archaeologist.md +115 -73
  7. package/.agent/agents/database-architect.md +130 -110
  8. package/.agent/agents/debugger.md +137 -97
  9. package/.agent/agents/dependency-reviewer.md +78 -30
  10. package/.agent/agents/devops-engineer.md +161 -118
  11. package/.agent/agents/documentation-writer.md +151 -87
  12. package/.agent/agents/explorer-agent.md +117 -99
  13. package/.agent/agents/frontend-reviewer.md +127 -47
  14. package/.agent/agents/frontend-specialist.md +169 -109
  15. package/.agent/agents/game-developer.md +28 -164
  16. package/.agent/agents/logic-reviewer.md +87 -49
  17. package/.agent/agents/mobile-developer.md +151 -103
  18. package/.agent/agents/mobile-reviewer.md +133 -50
  19. package/.agent/agents/orchestrator.md +121 -110
  20. package/.agent/agents/penetration-tester.md +103 -77
  21. package/.agent/agents/performance-optimizer.md +136 -92
  22. package/.agent/agents/performance-reviewer.md +139 -69
  23. package/.agent/agents/product-manager.md +104 -70
  24. package/.agent/agents/product-owner.md +6 -25
  25. package/.agent/agents/project-planner.md +95 -95
  26. package/.agent/agents/qa-automation-engineer.md +174 -87
  27. package/.agent/agents/security-auditor.md +133 -129
  28. package/.agent/agents/seo-specialist.md +160 -99
  29. package/.agent/agents/sql-reviewer.md +132 -44
  30. package/.agent/agents/supervisor-agent.md +137 -109
  31. package/.agent/agents/swarm-worker-contracts.md +17 -17
  32. package/.agent/agents/swarm-worker-registry.md +46 -46
  33. package/.agent/agents/test-coverage-reviewer.md +132 -53
  34. package/.agent/agents/test-engineer.md +0 -21
  35. package/.agent/agents/type-safety-reviewer.md +143 -33
  36. package/.agent/patterns/generator.md +9 -9
  37. package/.agent/patterns/inversion.md +12 -12
  38. package/.agent/patterns/pipeline.md +9 -9
  39. package/.agent/patterns/reviewer.md +13 -13
  40. package/.agent/patterns/tool-wrapper.md +9 -9
  41. package/.agent/rules/GEMINI.md +63 -63
  42. package/.agent/scripts/__pycache__/auto_preview.cpython-311.pyc +0 -0
  43. package/.agent/scripts/__pycache__/bundle_analyzer.cpython-311.pyc +0 -0
  44. package/.agent/scripts/__pycache__/checklist.cpython-311.pyc +0 -0
  45. package/.agent/scripts/__pycache__/dependency_analyzer.cpython-311.pyc +0 -0
  46. package/.agent/scripts/__pycache__/security_scan.cpython-311.pyc +0 -0
  47. package/.agent/scripts/__pycache__/session_manager.cpython-311.pyc +0 -0
  48. package/.agent/scripts/__pycache__/skill_integrator.cpython-311.pyc +0 -0
  49. package/.agent/scripts/__pycache__/swarm_dispatcher.cpython-311.pyc +0 -0
  50. package/.agent/scripts/__pycache__/test_runner.cpython-311.pyc +0 -0
  51. package/.agent/scripts/__pycache__/verify_all.cpython-311.pyc +0 -0
  52. package/.agent/scripts/compress_skills.py +167 -0
  53. package/.agent/scripts/consolidate_skills.py +173 -0
  54. package/.agent/scripts/deep_compress.py +202 -0
  55. package/.agent/scripts/minify_context.py +80 -0
  56. package/.agent/scripts/security_scan.py +1 -1
  57. package/.agent/scripts/strip_tribunal.py +41 -0
  58. package/.agent/skills/agent-organizer/SKILL.md +60 -100
  59. package/.agent/skills/agentic-patterns/SKILL.md +0 -70
  60. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +108 -53
  61. package/.agent/skills/api-patterns/SKILL.md +197 -257
  62. package/.agent/skills/api-security-auditor/SKILL.md +125 -57
  63. package/.agent/skills/app-builder/SKILL.md +326 -50
  64. package/.agent/skills/app-builder/templates/SKILL.md +13 -15
  65. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +16 -16
  66. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +22 -22
  67. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +18 -18
  68. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +20 -20
  69. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +17 -17
  70. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +18 -18
  71. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +21 -21
  72. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +19 -19
  73. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +26 -26
  74. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +26 -26
  75. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +19 -19
  76. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +18 -18
  77. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +20 -20
  78. package/.agent/skills/appflow-wireframe/SKILL.md +71 -98
  79. package/.agent/skills/architecture/SKILL.md +161 -200
  80. package/.agent/skills/authentication-best-practices/SKILL.md +121 -54
  81. package/.agent/skills/bash-linux/SKILL.md +71 -166
  82. package/.agent/skills/behavioral-modes/SKILL.md +8 -69
  83. package/.agent/skills/brainstorming/SKILL.md +345 -127
  84. package/.agent/skills/building-native-ui/SKILL.md +125 -57
  85. package/.agent/skills/clean-code/SKILL.md +266 -149
  86. package/.agent/skills/code-review-checklist/SKILL.md +0 -62
  87. package/.agent/skills/config-validator/SKILL.md +73 -131
  88. package/.agent/skills/csharp-developer/SKILL.md +434 -73
  89. package/.agent/skills/database-design/SKILL.md +190 -275
  90. package/.agent/skills/deployment-procedures/SKILL.md +81 -158
  91. package/.agent/skills/devops-engineer/SKILL.md +255 -94
  92. package/.agent/skills/devops-incident-responder/SKILL.md +50 -69
  93. package/.agent/skills/doc.md +5 -5
  94. package/.agent/skills/documentation-templates/SKILL.md +19 -63
  95. package/.agent/skills/edge-computing/SKILL.md +75 -165
  96. package/.agent/skills/extract-design-system/SKILL.md +84 -58
  97. package/.agent/skills/framer-motion-expert/SKILL.md +195 -0
  98. package/.agent/skills/frontend-design/SKILL.md +151 -499
  99. package/.agent/skills/game-design-expert/SKILL.md +71 -0
  100. package/.agent/skills/game-engineering-expert/SKILL.md +88 -0
  101. package/.agent/skills/geo-fundamentals/SKILL.md +52 -178
  102. package/.agent/skills/github-operations/SKILL.md +197 -272
  103. package/.agent/skills/gsap-expert/SKILL.md +194 -0
  104. package/.agent/skills/i18n-localization/SKILL.md +60 -172
  105. package/.agent/skills/intelligent-routing/SKILL.md +123 -103
  106. package/.agent/skills/lint-and-validate/SKILL.md +8 -52
  107. package/.agent/skills/llm-engineering/SKILL.md +281 -195
  108. package/.agent/skills/local-first/SKILL.md +76 -159
  109. package/.agent/skills/mcp-builder/SKILL.md +48 -188
  110. package/.agent/skills/mobile-design/SKILL.md +213 -219
  111. package/.agent/skills/motion-engineering/SKILL.md +184 -0
  112. package/.agent/skills/nextjs-react-expert/SKILL.md +184 -203
  113. package/.agent/skills/nodejs-best-practices/SKILL.md +403 -185
  114. package/.agent/skills/observability/SKILL.md +211 -203
  115. package/.agent/skills/parallel-agents/SKILL.md +53 -146
  116. package/.agent/skills/performance-profiling/SKILL.md +171 -151
  117. package/.agent/skills/plan-writing/SKILL.md +49 -153
  118. package/.agent/skills/platform-engineer/SKILL.md +57 -103
  119. package/.agent/skills/playwright-best-practices/SKILL.md +110 -63
  120. package/.agent/skills/powershell-windows/SKILL.md +61 -179
  121. package/.agent/skills/python-patterns/SKILL.md +7 -35
  122. package/.agent/skills/python-pro/SKILL.md +273 -114
  123. package/.agent/skills/react-specialist/SKILL.md +227 -108
  124. package/.agent/skills/readme-builder/SKILL.md +15 -85
  125. package/.agent/skills/realtime-patterns/SKILL.md +216 -243
  126. package/.agent/skills/red-team-tactics/SKILL.md +10 -51
  127. package/.agent/skills/rust-pro/SKILL.md +525 -142
  128. package/.agent/skills/seo-fundamentals/SKILL.md +92 -153
  129. package/.agent/skills/server-management/SKILL.md +110 -166
  130. package/.agent/skills/shadcn-ui-expert/SKILL.md +154 -55
  131. package/.agent/skills/skill-creator/SKILL.md +18 -58
  132. package/.agent/skills/sql-pro/SKILL.md +543 -68
  133. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +28 -68
  134. package/.agent/skills/swiftui-expert/SKILL.md +124 -57
  135. package/.agent/skills/systematic-debugging/SKILL.md +49 -151
  136. package/.agent/skills/tailwind-patterns/SKILL.md +433 -149
  137. package/.agent/skills/tdd-workflow/SKILL.md +63 -169
  138. package/.agent/skills/test-result-analyzer/SKILL.md +33 -73
  139. package/.agent/skills/testing-patterns/SKILL.md +437 -130
  140. package/.agent/skills/trend-researcher/SKILL.md +30 -71
  141. package/.agent/skills/ui-ux-pro-max/SKILL.md +0 -41
  142. package/.agent/skills/ui-ux-researcher/SKILL.md +51 -91
  143. package/.agent/skills/vue-expert/SKILL.md +225 -119
  144. package/.agent/skills/vulnerability-scanner/SKILL.md +264 -226
  145. package/.agent/skills/web-accessibility-auditor/SKILL.md +141 -58
  146. package/.agent/skills/web-design-guidelines/SKILL.md +17 -61
  147. package/.agent/skills/webapp-testing/SKILL.md +71 -196
  148. package/.agent/skills/whimsy-injector/SKILL.md +58 -132
  149. package/.agent/skills/workflow-optimizer/SKILL.md +28 -68
  150. package/.agent/workflows/api-tester.md +96 -224
  151. package/.agent/workflows/audit.md +81 -122
  152. package/.agent/workflows/brainstorm.md +69 -105
  153. package/.agent/workflows/changelog.md +65 -97
  154. package/.agent/workflows/create.md +73 -88
  155. package/.agent/workflows/debug.md +80 -111
  156. package/.agent/workflows/deploy.md +119 -92
  157. package/.agent/workflows/enhance.md +80 -91
  158. package/.agent/workflows/fix.md +68 -97
  159. package/.agent/workflows/generate.md +165 -164
  160. package/.agent/workflows/migrate.md +106 -109
  161. package/.agent/workflows/orchestrate.md +103 -86
  162. package/.agent/workflows/performance-benchmarker.md +77 -268
  163. package/.agent/workflows/plan.md +120 -98
  164. package/.agent/workflows/preview.md +39 -96
  165. package/.agent/workflows/refactor.md +105 -97
  166. package/.agent/workflows/review-ai.md +63 -102
  167. package/.agent/workflows/review.md +71 -110
  168. package/.agent/workflows/session.md +53 -113
  169. package/.agent/workflows/status.md +42 -88
  170. package/.agent/workflows/strengthen-skills.md +90 -51
  171. package/.agent/workflows/swarm.md +114 -129
  172. package/.agent/workflows/test.md +125 -102
  173. package/.agent/workflows/tribunal-backend.md +60 -78
  174. package/.agent/workflows/tribunal-database.md +62 -100
  175. package/.agent/workflows/tribunal-frontend.md +62 -82
  176. package/.agent/workflows/tribunal-full.md +56 -100
  177. package/.agent/workflows/tribunal-mobile.md +65 -94
  178. package/.agent/workflows/tribunal-performance.md +62 -105
  179. package/.agent/workflows/ui-ux-pro-max.md +72 -121
  180. package/README.md +11 -15
  181. package/package.json +1 -1
  182. package/.agent/skills/api-patterns/api-style.md +0 -42
  183. package/.agent/skills/api-patterns/auth.md +0 -24
  184. package/.agent/skills/api-patterns/documentation.md +0 -26
  185. package/.agent/skills/api-patterns/graphql.md +0 -41
  186. package/.agent/skills/api-patterns/rate-limiting.md +0 -31
  187. package/.agent/skills/api-patterns/response.md +0 -37
  188. package/.agent/skills/api-patterns/rest.md +0 -40
  189. package/.agent/skills/api-patterns/security-testing.md +0 -122
  190. package/.agent/skills/api-patterns/trpc.md +0 -41
  191. package/.agent/skills/api-patterns/versioning.md +0 -22
  192. package/.agent/skills/app-builder/agent-coordination.md +0 -71
  193. package/.agent/skills/app-builder/feature-building.md +0 -53
  194. package/.agent/skills/app-builder/project-detection.md +0 -34
  195. package/.agent/skills/app-builder/scaffolding.md +0 -118
  196. package/.agent/skills/app-builder/tech-stack.md +0 -40
  197. package/.agent/skills/architecture/context-discovery.md +0 -43
  198. package/.agent/skills/architecture/examples.md +0 -94
  199. package/.agent/skills/architecture/pattern-selection.md +0 -68
  200. package/.agent/skills/architecture/patterns-reference.md +0 -50
  201. package/.agent/skills/architecture/trade-off-analysis.md +0 -77
  202. package/.agent/skills/brainstorming/dynamic-questioning.md +0 -360
  203. package/.agent/skills/database-design/database-selection.md +0 -43
  204. package/.agent/skills/database-design/indexing.md +0 -39
  205. package/.agent/skills/database-design/migrations.md +0 -48
  206. package/.agent/skills/database-design/optimization.md +0 -36
  207. package/.agent/skills/database-design/orm-selection.md +0 -30
  208. package/.agent/skills/database-design/schema-design.md +0 -56
  209. package/.agent/skills/dotnet-core-expert/SKILL.md +0 -103
  210. package/.agent/skills/framer-motion-animations/SKILL.md +0 -74
  211. package/.agent/skills/frontend-design/animation-guide.md +0 -331
  212. package/.agent/skills/frontend-design/color-system.md +0 -329
  213. package/.agent/skills/frontend-design/decision-trees.md +0 -418
  214. package/.agent/skills/frontend-design/motion-graphics.md +0 -306
  215. package/.agent/skills/frontend-design/typography-system.md +0 -363
  216. package/.agent/skills/frontend-design/ux-psychology.md +0 -1116
  217. package/.agent/skills/frontend-design/visual-effects.md +0 -383
  218. package/.agent/skills/game-development/2d-games/SKILL.md +0 -119
  219. package/.agent/skills/game-development/3d-games/SKILL.md +0 -135
  220. package/.agent/skills/game-development/SKILL.md +0 -236
  221. package/.agent/skills/game-development/game-art/SKILL.md +0 -185
  222. package/.agent/skills/game-development/game-audio/SKILL.md +0 -190
  223. package/.agent/skills/game-development/game-design/SKILL.md +0 -129
  224. package/.agent/skills/game-development/mobile-games/SKILL.md +0 -108
  225. package/.agent/skills/game-development/multiplayer/SKILL.md +0 -132
  226. package/.agent/skills/game-development/pc-games/SKILL.md +0 -144
  227. package/.agent/skills/game-development/vr-ar/SKILL.md +0 -123
  228. package/.agent/skills/game-development/web-games/SKILL.md +0 -150
  229. package/.agent/skills/intelligent-routing/router-manifest.md +0 -65
  230. package/.agent/skills/mobile-design/decision-trees.md +0 -516
  231. package/.agent/skills/mobile-design/mobile-backend.md +0 -491
  232. package/.agent/skills/mobile-design/mobile-color-system.md +0 -420
  233. package/.agent/skills/mobile-design/mobile-debugging.md +0 -122
  234. package/.agent/skills/mobile-design/mobile-design-thinking.md +0 -357
  235. package/.agent/skills/mobile-design/mobile-navigation.md +0 -458
  236. package/.agent/skills/mobile-design/mobile-performance.md +0 -767
  237. package/.agent/skills/mobile-design/mobile-testing.md +0 -356
  238. package/.agent/skills/mobile-design/mobile-typography.md +0 -433
  239. package/.agent/skills/mobile-design/platform-android.md +0 -666
  240. package/.agent/skills/mobile-design/platform-ios.md +0 -561
  241. package/.agent/skills/mobile-design/touch-psychology.md +0 -537
  242. package/.agent/skills/nextjs-react-expert/1-async-eliminating-waterfalls.md +0 -312
  243. package/.agent/skills/nextjs-react-expert/2-bundle-bundle-size-optimization.md +0 -240
  244. package/.agent/skills/nextjs-react-expert/3-server-server-side-performance.md +0 -490
  245. package/.agent/skills/nextjs-react-expert/4-client-client-side-data-fetching.md +0 -264
  246. package/.agent/skills/nextjs-react-expert/5-rerender-re-render-optimization.md +0 -581
  247. package/.agent/skills/nextjs-react-expert/6-rendering-rendering-performance.md +0 -432
  248. package/.agent/skills/nextjs-react-expert/7-js-javascript-performance.md +0 -684
  249. package/.agent/skills/nextjs-react-expert/8-advanced-advanced-patterns.md +0 -150
  250. 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 development principles and decision-making. Framework selection, async patterns, security, and architecture. Teaches thinking, not copying.
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: 1.0.0
6
- last-updated: 2026-03-12
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 Development Principles
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
- ## Framework Selection
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
- ## Modern Runtime Landscape (2025+)
16
+ ```json
17
+ // package.json — ALWAYS set type to module
18
+ {
19
+ "type": "module",
20
+ "engines": { "node": ">=22" }
21
+ }
22
+ ```
39
23
 
40
- The Node.js monopoly is ending. Understand constraints before picking a runtime:
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
- | Runtime | `fs` | `crypto` | `child_process` | `process.env` | Deploy Target |
43
- |---|---|---|---|---|---|
44
- | **Node.js** | | | | | Server, serverless |
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
- ```ts
50
- // ❌ Portability anti-pattern works in Node, breaks at edge
51
- import { createHash } from 'crypto'; // Node crypto — not available at edge
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
- // Web Platform APIs — work everywhere (Node ≥18, Bun, Deno, Edge)
55
- const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input));
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
- ### When to Consider Bun
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
- // package.json — identical, works in both
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
- // Gotcha: some native Node addons (e.g. bcrypt, canvas) need Bun alternatives
71
- // Use: @node-rs/bcrypt instead of bcrypt (N-API native, works in both)
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
- ## Async Patterns
61
+ ```typescript
62
+ import Fastify from "fastify";
63
+ import { z } from "zod";
77
64
 
78
- ### Always: Handle rejection
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
- Every Promise needs a rejection handler. Unhandled rejections crash Node.js processes in modern versions.
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
- ```ts
83
- // ❌ Unhandled rejection
84
- fetchData().then(process);
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
- // Handled
87
- fetchData().then(process).catch(handleError);
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
- // ✅ Or with async/await
90
- try {
91
- const data = await fetchData();
92
- process(data);
93
- } catch (err) {
94
- handleError(err);
95
- }
111
+ start();
96
112
  ```
97
113
 
98
- ### Avoid: Blocking the event loop
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
- The event loop is single-threaded. Anything synchronous that takes more than a few ms blocks all other requests.
123
+ const app = new Hono();
101
124
 
102
- ```ts
103
- // Blocks event loop for large files
104
- const data = fs.readFileSync('huge.csv');
125
+ app.use("*", logger());
126
+ app.use("*", cors({ origin: "https://myapp.com" }));
105
127
 
106
- // Non-blocking
107
- const data = await fs.promises.readFile('huge.csv');
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
- // CPU-intensive work on main thread
110
- const result = computeHuge(dataset);
139
+ app.get("/health", (c) => c.json({ status: "ok" }));
111
140
 
112
- // Offload to worker thread
113
- const { Worker } = require('worker_threads');
141
+ export default app; // works in Node, Deno, Bun, Cloudflare Workers
114
142
  ```
115
143
 
116
- ### Concurrency vs. Parallelism
144
+ ---
117
145
 
118
- ```ts
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
- // Concurrent all start immediately, await all completions (use for independent operations)
125
- await Promise.all(ids.map(id => processItem(id)));
148
+ ### Global Error Handlers
126
149
 
127
- // Concurrent with limit — avoid overwhelming downstream services
128
- import pLimit from 'p-limit';
129
- const limit = pLimit(5); // max 5 concurrent
130
- await Promise.all(ids.map(id => limit(() => processItem(id))));
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
- ## Error Handling Architecture
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
- Structure errors so they carry meaning, not just messages:
171
+ ### Application Error Classes
138
172
 
139
- ```ts
140
- // Base application error
141
- class AppError extends Error {
173
+ ```typescript
174
+ export class AppError extends Error {
142
175
  constructor(
143
176
  message: string,
144
- public code: string,
145
- public statusCode: number,
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 = this.constructor.name;
182
+ this.name = "AppError";
183
+ Error.captureStackTrace(this, this.constructor);
150
184
  }
151
185
  }
152
186
 
153
- // Domain-specific errors
154
- class NotFoundError extends AppError {
187
+ export class NotFoundError extends AppError {
155
188
  constructor(resource: string, id: string) {
156
- super(`${resource} ${id} not found`, 'NOT_FOUND', 404);
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, 'VALIDATION_FAILED', 400);
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
- **Global error handler:**
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
- return res.status(err.statusCode).json({
172
- error: err.message,
173
- code: err.code,
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
- ## Security Baseline
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
- ```ts
187
- import helmet from 'helmet';
188
- import rateLimit from 'express-rate-limit';
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 — protect all routes
194
- app.use(rateLimit({
397
+ // Rate limiting
398
+ app.use("/api/", rateLimit({
195
399
  windowMs: 15 * 60 * 1000, // 15 minutes
196
- max: 100, // requests per window
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
- // Body size limit — prevent payload bombs
202
- app.use(express.json({ limit: '10kb' }));
203
-
204
- // Trust proxy correctly for rate limiting behind load balancer
205
- app.set('trust proxy', 1);
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
- **Never:**
209
- - Use `eval()` or `new Function()` with user input
210
- - Pass unvalidated user input to `exec()`, `spawn()`, or `child_process`
211
- - Log full request bodies (may contain credentials or PII)
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
- ## Project Structure
427
+ // Path traversal prevention
428
+ import { resolve, normalize } from "node:path";
216
429
 
217
- ```
218
- src/
219
- routes/ HTTP route definitions (thin — only parse and delegate)
220
- controllers/ Request handling, validation, response formatting
221
- services/ Business logic (no HTTP awareness)
222
- repositories/ Database access (no business logic)
223
- middleware/ Auth, rate limit, logging
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
- ## Output Format
441
+ ## Process Management
235
442
 
236
- When this skill produces or reviews code, structure your output as follows:
443
+ ### Graceful Shutdown
237
444
 
238
- ```
239
- ━━━ Nodejs Best Practices Report ━━━━━━━━━━━━━━━━━━━━━━━━
240
- Skill: Nodejs Best Practices
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
- **VBC (Verification-Before-Completion) is mandatory.**
253
- Do not mark status as VERIFIED until concrete terminal evidence is provided.
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
- ## 🏛️ Tribunal Integration (Anti-Hallucination)
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
- **Slash command: `/tribunal-backend`**
261
- **Active reviewers: `logic` · `security` · `dependency` · `type-safety`**
464
+ process.exit(0);
465
+ }
262
466
 
263
- ### Forbidden AI Tropes in Node.js
467
+ process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
468
+ process.on("SIGINT", () => gracefulShutdown("SIGINT"));
469
+ ```
264
470
 
265
- 1. **Blindly mixing `require` and `import`** — pick ESM or CommonJS and stick to it strictly based on `package.json`.
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
- ### ✅ Pre-Flight Self-Audit
473
+ ```typescript
474
+ import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
272
475
 
273
- Review these questions before generating Node.js code:
274
- ```
275
- Did I use the correct module system (CJS vs ESM) for this context?
276
- Is every Promise rejection properly handled?
277
- Did I block the event loop with synchronous FS or Crypto operations?
278
- Are all inputs validated before business logic runs?
279
- ✅ Is this code safe from memory leaks (e.g., unbounded arrays/maps)?
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
+ ---