contextos-agents 1.5.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/.agents/AGENTS.md +421 -0
- package/.agents/adapters/aider/export.js +137 -0
- package/.agents/adapters/claude/export.js +55 -0
- package/.agents/adapters/copilot/export.js +107 -0
- package/.agents/adapters/cursor/export.js +210 -0
- package/.agents/adapters/gemini/export.js +115 -0
- package/.agents/adapters/shared.js +119 -0
- package/.agents/adapters/zed/export.js +111 -0
- package/.agents/core/profiles/backend.yaml +47 -0
- package/.agents/core/profiles/enterprise.yaml +46 -0
- package/.agents/core/profiles/frontend.yaml +46 -0
- package/.agents/core/profiles/hackathon.yaml +45 -0
- package/.agents/core/profiles/mvp.yaml +43 -0
- package/.agents/core/profiles/startup.yaml +48 -0
- package/.agents/core/skills/adapters/EXAMPLES.md +19 -0
- package/.agents/core/skills/adapters/SKILL.md +105 -0
- package/.agents/core/skills/adapters/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/adapters/VALIDATION.json +12 -0
- package/.agents/core/skills/adapters/skill.yaml +10 -0
- package/.agents/core/skills/architecture-diagrams/SKILL.md +108 -0
- package/.agents/core/skills/architecture-diagrams/VALIDATION.json +12 -0
- package/.agents/core/skills/architecture-diagrams/skill.yaml +8 -0
- package/.agents/core/skills/brutalist-design/SKILL.md +150 -0
- package/.agents/core/skills/brutalist-design/VALIDATION.json +12 -0
- package/.agents/core/skills/brutalist-design/skill.yaml +8 -0
- package/.agents/core/skills/context-manager/EXAMPLES.md +19 -0
- package/.agents/core/skills/context-manager/SKILL.md +126 -0
- package/.agents/core/skills/context-manager/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/context-manager/VALIDATION.json +12 -0
- package/.agents/core/skills/context-manager/skill.yaml +10 -0
- package/.agents/core/skills/context-os/EXAMPLES.md +21 -0
- package/.agents/core/skills/context-os/SKILL.md +169 -0
- package/.agents/core/skills/context-os/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/context-os/VALIDATION.json +12 -0
- package/.agents/core/skills/context-os/packs.yaml +59 -0
- package/.agents/core/skills/context-os/references/context-rules.md +68 -0
- package/.agents/core/skills/context-os/references/pipeline.md +119 -0
- package/.agents/core/skills/context-os/references/project-graph.md +103 -0
- package/.agents/core/skills/context-os/rules.yaml +135 -0
- package/.agents/core/skills/context-os/skill.yaml +10 -0
- package/.agents/core/skills/database/EXAMPLES.md +74 -0
- package/.agents/core/skills/database/SKILL.md +101 -0
- package/.agents/core/skills/database/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/database/VALIDATION.json +11 -0
- package/.agents/core/skills/database/skill.yaml +25 -0
- package/.agents/core/skills/ddd/EXAMPLES.md +42 -0
- package/.agents/core/skills/ddd/SKILL.md +247 -0
- package/.agents/core/skills/ddd/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ddd/VALIDATION.json +12 -0
- package/.agents/core/skills/ddd/ddd.md +178 -0
- package/.agents/core/skills/ddd/skill.yaml +10 -0
- package/.agents/core/skills/decisions/EXAMPLES.md +35 -0
- package/.agents/core/skills/decisions/SKILL.md +90 -0
- package/.agents/core/skills/decisions/TROUBLESHOOTING.md +13 -0
- package/.agents/core/skills/decisions/VALIDATION.json +12 -0
- package/.agents/core/skills/decisions/skill.yaml +10 -0
- package/.agents/core/skills/docker/EXAMPLES.md +56 -0
- package/.agents/core/skills/docker/SKILL.md +63 -0
- package/.agents/core/skills/docker/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/docker/VALIDATION.json +11 -0
- package/.agents/core/skills/docker/skill.yaml +23 -0
- package/.agents/core/skills/engineering-workflow/EXAMPLES.md +57 -0
- package/.agents/core/skills/engineering-workflow/SKILL.md +343 -0
- package/.agents/core/skills/engineering-workflow/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/engineering-workflow/VALIDATION.json +12 -0
- package/.agents/core/skills/engineering-workflow/skill.yaml +13 -0
- package/.agents/core/skills/fastapi/EXAMPLES.md +36 -0
- package/.agents/core/skills/fastapi/SKILL.md +148 -0
- package/.agents/core/skills/fastapi/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/fastapi/VALIDATION.json +12 -0
- package/.agents/core/skills/fastapi/fastapi.md +112 -0
- package/.agents/core/skills/fastapi/skill.yaml +10 -0
- package/.agents/core/skills/gemini-precision/SKILL.md +169 -0
- package/.agents/core/skills/gemini-precision/VALIDATION.json +12 -0
- package/.agents/core/skills/gemini-precision/skill.yaml +9 -0
- package/.agents/core/skills/generators/EXAMPLES.md +19 -0
- package/.agents/core/skills/generators/SKILL.md +112 -0
- package/.agents/core/skills/generators/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/generators/VALIDATION.json +12 -0
- package/.agents/core/skills/generators/skill.yaml +10 -0
- package/.agents/core/skills/generators/templates/API.md +77 -0
- package/.agents/core/skills/generators/templates/ARCHITECTURE.md +70 -0
- package/.agents/core/skills/generators/templates/DATABASE.md +42 -0
- package/.agents/core/skills/generators/templates/DECISION.md +46 -0
- package/.agents/core/skills/generators/templates/PRD.md +67 -0
- package/.agents/core/skills/generators/templates/PROJECT_GRAPH.md +56 -0
- package/.agents/core/skills/generators/templates/ROADMAP.md +51 -0
- package/.agents/core/skills/generators/templates/TASKS.md +43 -0
- package/.agents/core/skills/generators/templates/UI.md +73 -0
- package/.agents/core/skills/graphify/EXAMPLES.md +73 -0
- package/.agents/core/skills/graphify/SKILL.md +130 -0
- package/.agents/core/skills/graphify/VALIDATION.json +12 -0
- package/.agents/core/skills/graphify/skill.yaml +13 -0
- package/.agents/core/skills/gstack-roles/EXAMPLES.md +23 -0
- package/.agents/core/skills/gstack-roles/SKILL.md +152 -0
- package/.agents/core/skills/gstack-roles/TROUBLESHOOTING.md +13 -0
- package/.agents/core/skills/gstack-roles/VALIDATION.json +12 -0
- package/.agents/core/skills/gstack-roles/skill.yaml +12 -0
- package/.agents/core/skills/impeccable-design/EXAMPLES.md +26 -0
- package/.agents/core/skills/impeccable-design/SKILL.md +201 -0
- package/.agents/core/skills/impeccable-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/impeccable-design/VALIDATION.json +12 -0
- package/.agents/core/skills/impeccable-design/skill.yaml +14 -0
- package/.agents/core/skills/interview-me/SKILL.md +97 -0
- package/.agents/core/skills/interview-me/VALIDATION.json +12 -0
- package/.agents/core/skills/interview-me/skill.yaml +8 -0
- package/.agents/core/skills/microservices/EXAMPLES.md +38 -0
- package/.agents/core/skills/microservices/SKILL.md +164 -0
- package/.agents/core/skills/microservices/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/microservices/VALIDATION.json +12 -0
- package/.agents/core/skills/microservices/microservices.md +119 -0
- package/.agents/core/skills/microservices/skill.yaml +10 -0
- package/.agents/core/skills/minimalist-design/SKILL.md +113 -0
- package/.agents/core/skills/minimalist-design/VALIDATION.json +12 -0
- package/.agents/core/skills/minimalist-design/skill.yaml +8 -0
- package/.agents/core/skills/nestjs/EXAMPLES.md +40 -0
- package/.agents/core/skills/nestjs/SKILL.md +139 -0
- package/.agents/core/skills/nestjs/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/nestjs/VALIDATION.json +12 -0
- package/.agents/core/skills/nestjs/nestjs.md +103 -0
- package/.agents/core/skills/nestjs/skill.yaml +10 -0
- package/.agents/core/skills/nextjs/EXAMPLES.md +40 -0
- package/.agents/core/skills/nextjs/SKILL.md +163 -0
- package/.agents/core/skills/nextjs/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/nextjs/VALIDATION.json +12 -0
- package/.agents/core/skills/nextjs/nextjs.md +67 -0
- package/.agents/core/skills/nextjs/skill.yaml +10 -0
- package/.agents/core/skills/node/EXAMPLES.md +80 -0
- package/.agents/core/skills/node/SKILL.md +128 -0
- package/.agents/core/skills/node/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/node/VALIDATION.json +12 -0
- package/.agents/core/skills/node/node.md +87 -0
- package/.agents/core/skills/node/skill.yaml +10 -0
- package/.agents/core/skills/performance/EXAMPLES.md +30 -0
- package/.agents/core/skills/performance/SKILL.md +75 -0
- package/.agents/core/skills/performance/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/performance/VALIDATION.json +12 -0
- package/.agents/core/skills/performance/performance.md +52 -0
- package/.agents/core/skills/performance/skill.yaml +10 -0
- package/.agents/core/skills/ponytail-mindset/EXAMPLES.md +45 -0
- package/.agents/core/skills/ponytail-mindset/SKILL.md +193 -0
- package/.agents/core/skills/ponytail-mindset/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ponytail-mindset/VALIDATION.json +12 -0
- package/.agents/core/skills/ponytail-mindset/skill.yaml +14 -0
- package/.agents/core/skills/react/EXAMPLES.md +79 -0
- package/.agents/core/skills/react/SKILL.md +132 -0
- package/.agents/core/skills/react/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/react/VALIDATION.json +12 -0
- package/.agents/core/skills/react/react.md +93 -0
- package/.agents/core/skills/react/skill.yaml +10 -0
- package/.agents/core/skills/react-best-practices/SKILL.md +155 -0
- package/.agents/core/skills/react-best-practices/VALIDATION.json +12 -0
- package/.agents/core/skills/react-best-practices/skill.yaml +10 -0
- package/.agents/core/skills/redesign-audit/SKILL.md +117 -0
- package/.agents/core/skills/redesign-audit/VALIDATION.json +12 -0
- package/.agents/core/skills/redesign-audit/skill.yaml +8 -0
- package/.agents/core/skills/security/EXAMPLES.md +64 -0
- package/.agents/core/skills/security/SKILL.md +158 -0
- package/.agents/core/skills/security/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/security/VALIDATION.json +12 -0
- package/.agents/core/skills/security/security.md +106 -0
- package/.agents/core/skills/security/skill.yaml +10 -0
- package/.agents/core/skills/soft-design/SKILL.md +108 -0
- package/.agents/core/skills/soft-design/VALIDATION.json +12 -0
- package/.agents/core/skills/soft-design/skill.yaml +8 -0
- package/.agents/core/skills/state-management/EXAMPLES.md +56 -0
- package/.agents/core/skills/state-management/SKILL.md +48 -0
- package/.agents/core/skills/state-management/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/state-management/VALIDATION.json +11 -0
- package/.agents/core/skills/state-management/skill.yaml +22 -0
- package/.agents/core/skills/subagent-orchestrator/SKILL.md +100 -0
- package/.agents/core/skills/subagent-orchestrator/VALIDATION.json +12 -0
- package/.agents/core/skills/subagent-orchestrator/skill.yaml +8 -0
- package/.agents/core/skills/system-design/EXAMPLES.md +75 -0
- package/.agents/core/skills/system-design/SKILL.md +419 -0
- package/.agents/core/skills/system-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/system-design/VALIDATION.json +12 -0
- package/.agents/core/skills/system-design/skill.yaml +13 -0
- package/.agents/core/skills/system-design/system-design.md +112 -0
- package/.agents/core/skills/testing/EXAMPLES.md +71 -0
- package/.agents/core/skills/testing/SKILL.md +70 -0
- package/.agents/core/skills/testing/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/testing/VALIDATION.json +11 -0
- package/.agents/core/skills/testing/skill.yaml +26 -0
- package/.agents/core/skills/typescript/EXAMPLES.md +64 -0
- package/.agents/core/skills/typescript/SKILL.md +112 -0
- package/.agents/core/skills/typescript/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/typescript/VALIDATION.json +12 -0
- package/.agents/core/skills/typescript/skill.yaml +10 -0
- package/.agents/core/skills/typescript/typescript.md +71 -0
- package/.agents/core/skills/ui-design/EXAMPLES.md +21 -0
- package/.agents/core/skills/ui-design/SKILL.md +124 -0
- package/.agents/core/skills/ui-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ui-design/VALIDATION.json +12 -0
- package/.agents/core/skills/ui-design/skill.yaml +10 -0
- package/.agents/core/skills/ui-design/ui.md +88 -0
- package/.agents/core/skills/ui-ux-pro/EXAMPLES.md +62 -0
- package/.agents/core/skills/ui-ux-pro/SKILL.md +375 -0
- package/.agents/core/skills/ui-ux-pro/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ui-ux-pro/VALIDATION.json +12 -0
- package/.agents/core/skills/ui-ux-pro/skill.yaml +13 -0
- package/.agents/core/skills/ux-design/EXAMPLES.md +36 -0
- package/.agents/core/skills/ux-design/SKILL.md +116 -0
- package/.agents/core/skills/ux-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ux-design/VALIDATION.json +12 -0
- package/.agents/core/skills/ux-design/skill.yaml +10 -0
- package/.agents/core/skills/ux-design/ux.md +80 -0
- package/.agents/core/skills/vercel-optimize/SKILL.md +83 -0
- package/.agents/core/skills/vercel-optimize/VALIDATION.json +12 -0
- package/.agents/core/skills/vercel-optimize/skill.yaml +10 -0
- package/.agents/core/skills/web-accessibility/EXAMPLES.md +39 -0
- package/.agents/core/skills/web-accessibility/SKILL.md +170 -0
- package/.agents/core/skills/web-accessibility/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/web-accessibility/VALIDATION.json +12 -0
- package/.agents/core/skills/web-accessibility/accessibility.md +63 -0
- package/.agents/core/skills/web-accessibility/skill.yaml +10 -0
- package/.agents/ctx.js +370 -0
- package/.agents/generated/claude/skills/adapters/SKILL.md +126 -0
- package/.agents/generated/claude/skills/architecture-diagrams/SKILL.md +101 -0
- package/.agents/generated/claude/skills/brutalist-design/SKILL.md +145 -0
- package/.agents/generated/claude/skills/context-manager/SKILL.md +147 -0
- package/.agents/generated/claude/skills/context-os/SKILL.md +191 -0
- package/.agents/generated/claude/skills/database/SKILL.md +191 -0
- package/.agents/generated/claude/skills/ddd/SKILL.md +305 -0
- package/.agents/generated/claude/skills/decisions/SKILL.md +134 -0
- package/.agents/generated/claude/skills/docker/SKILL.md +135 -0
- package/.agents/generated/claude/skills/engineering-workflow/SKILL.md +414 -0
- package/.agents/generated/claude/skills/fastapi/SKILL.md +200 -0
- package/.agents/generated/claude/skills/gemini-precision/SKILL.md +161 -0
- package/.agents/generated/claude/skills/generators/SKILL.md +133 -0
- package/.agents/generated/claude/skills/graphify/SKILL.md +198 -0
- package/.agents/generated/claude/skills/gstack-roles/SKILL.md +184 -0
- package/.agents/generated/claude/skills/impeccable-design/SKILL.md +241 -0
- package/.agents/generated/claude/skills/interview-me/SKILL.md +90 -0
- package/.agents/generated/claude/skills/microservices/SKILL.md +218 -0
- package/.agents/generated/claude/skills/minimalist-design/SKILL.md +108 -0
- package/.agents/generated/claude/skills/nestjs/SKILL.md +195 -0
- package/.agents/generated/claude/skills/nextjs/SKILL.md +219 -0
- package/.agents/generated/claude/skills/node/SKILL.md +224 -0
- package/.agents/generated/claude/skills/performance/SKILL.md +121 -0
- package/.agents/generated/claude/skills/ponytail-mindset/SKILL.md +252 -0
- package/.agents/generated/claude/skills/react/SKILL.md +227 -0
- package/.agents/generated/claude/skills/react-best-practices/SKILL.md +146 -0
- package/.agents/generated/claude/skills/redesign-audit/SKILL.md +112 -0
- package/.agents/generated/claude/skills/security/SKILL.md +237 -0
- package/.agents/generated/claude/skills/soft-design/SKILL.md +103 -0
- package/.agents/generated/claude/skills/state-management/SKILL.md +120 -0
- package/.agents/generated/claude/skills/subagent-orchestrator/SKILL.md +93 -0
- package/.agents/generated/claude/skills/system-design/SKILL.md +507 -0
- package/.agents/generated/claude/skills/testing/SKILL.md +157 -0
- package/.agents/generated/claude/skills/typescript/SKILL.md +192 -0
- package/.agents/generated/claude/skills/ui-design/SKILL.md +161 -0
- package/.agents/generated/claude/skills/ui-ux-pro/SKILL.md +451 -0
- package/.agents/generated/claude/skills/ux-design/SKILL.md +168 -0
- package/.agents/generated/claude/skills/vercel-optimize/SKILL.md +76 -0
- package/.agents/generated/claude/skills/web-accessibility/SKILL.md +225 -0
- package/.agents/generated/gemini/skills/adapters/SKILL.md +135 -0
- package/.agents/generated/gemini/skills/architecture-diagrams/SKILL.md +107 -0
- package/.agents/generated/gemini/skills/brutalist-design/SKILL.md +151 -0
- package/.agents/generated/gemini/skills/context-manager/SKILL.md +156 -0
- package/.agents/generated/gemini/skills/context-os/SKILL.md +200 -0
- package/.agents/generated/gemini/skills/database/SKILL.md +200 -0
- package/.agents/generated/gemini/skills/ddd/SKILL.md +314 -0
- package/.agents/generated/gemini/skills/decisions/SKILL.md +143 -0
- package/.agents/generated/gemini/skills/docker/SKILL.md +144 -0
- package/.agents/generated/gemini/skills/engineering-workflow/SKILL.md +423 -0
- package/.agents/generated/gemini/skills/fastapi/SKILL.md +209 -0
- package/.agents/generated/gemini/skills/gemini-precision/SKILL.md +167 -0
- package/.agents/generated/gemini/skills/generators/SKILL.md +142 -0
- package/.agents/generated/gemini/skills/graphify/SKILL.md +205 -0
- package/.agents/generated/gemini/skills/gstack-roles/SKILL.md +193 -0
- package/.agents/generated/gemini/skills/impeccable-design/SKILL.md +250 -0
- package/.agents/generated/gemini/skills/interview-me/SKILL.md +96 -0
- package/.agents/generated/gemini/skills/microservices/SKILL.md +227 -0
- package/.agents/generated/gemini/skills/minimalist-design/SKILL.md +114 -0
- package/.agents/generated/gemini/skills/nestjs/SKILL.md +204 -0
- package/.agents/generated/gemini/skills/nextjs/SKILL.md +298 -0
- package/.agents/generated/gemini/skills/node/SKILL.md +323 -0
- package/.agents/generated/gemini/skills/performance/SKILL.md +185 -0
- package/.agents/generated/gemini/skills/ponytail-mindset/SKILL.md +261 -0
- package/.agents/generated/gemini/skills/react/SKILL.md +332 -0
- package/.agents/generated/gemini/skills/react-best-practices/SKILL.md +152 -0
- package/.agents/generated/gemini/skills/redesign-audit/SKILL.md +118 -0
- package/.agents/generated/gemini/skills/security/SKILL.md +355 -0
- package/.agents/generated/gemini/skills/soft-design/SKILL.md +109 -0
- package/.agents/generated/gemini/skills/state-management/SKILL.md +129 -0
- package/.agents/generated/gemini/skills/subagent-orchestrator/SKILL.md +99 -0
- package/.agents/generated/gemini/skills/system-design/SKILL.md +631 -0
- package/.agents/generated/gemini/skills/testing/SKILL.md +166 -0
- package/.agents/generated/gemini/skills/typescript/SKILL.md +275 -0
- package/.agents/generated/gemini/skills/ui-design/SKILL.md +170 -0
- package/.agents/generated/gemini/skills/ui-ux-pro/SKILL.md +460 -0
- package/.agents/generated/gemini/skills/ux-design/SKILL.md +177 -0
- package/.agents/generated/gemini/skills/vercel-optimize/SKILL.md +82 -0
- package/.agents/generated/gemini/skills/web-accessibility/SKILL.md +300 -0
- package/.agents/mcp/runtime.py +454 -0
- package/.agents/mcp/server.mjs +189373 -0
- package/.agents/plugins.js +574 -0
- package/.agents/profiles.js +267 -0
- package/.agents/resolver.js +336 -0
- package/.agents/skills-index.json +200 -0
- package/.agents/skills.json +6 -0
- package/.agents/validate.js +500 -0
- package/LICENSE +21 -0
- package/README.md +415 -0
- package/benchmarks/gemini-issues.js +533 -0
- package/bin/index.js +458 -0
- package/package.json +70 -0
- package/registry.json +53 -0
- package/registry.schema.json +32 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Next.js
|
|
3
|
+
description: >
|
|
4
|
+
ContextOS skill for Next.js
|
|
5
|
+
---
|
|
6
|
+
# Next.js App Router Best Practices
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Enforces high-performance architectural patterns for Next.js App Router based on Vercel Engineering guidelines: React Server Components (RSC), zero-waterfall async pipelines, request deduplication via `React.cache()`, bundle optimization, and secure Server Actions.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
Activate whenever building, refactoring, or reviewing Next.js pages, layouts, Route Handlers (`app/api`), Server Actions, or components in the `app/` directory.
|
|
15
|
+
|
|
16
|
+
## Negative Constraints (What NOT to Do)
|
|
17
|
+
|
|
18
|
+
1. **NEVER use barrel imports for UI libraries**: Avoid `import { Button, Dialog } from '@/components'`. Import directly from the exact file (`import { Button } from '@/components/ui/button'`) to prevent bundler tree-shaking failures and trace bloat.
|
|
19
|
+
2. **NEVER trust client-provided data or session state in Server Actions**: Always authenticate session and authorize tenant ownership inside the Server Action handler itself before mutating data.
|
|
20
|
+
3. **NEVER introduce sequential `await` waterfalls for independent data**: Always use `Promise.all()` or parallel streaming `<Suspense>` boundaries.
|
|
21
|
+
4. **NEVER pass large unneeded serialized data from Server to Client Components**: Only pass the specific primitive fields required by the client component (`server-dedup-props`).
|
|
22
|
+
5. **NEVER use `useEffect` for data fetching**: Fetch directly in Server Components or use TanStack Query / SWR for client-side queries.
|
|
23
|
+
6. **NEVER import server-only modules in client components**: Use the `server-only` package in data access layers to catch accidental client imports at build time.
|
|
24
|
+
|
|
25
|
+
## Rules & Patterns
|
|
26
|
+
|
|
27
|
+
### 1. Eliminating Async Waterfalls (Critical)
|
|
28
|
+
|
|
29
|
+
- **Parallel Fetching**: Fetch independent data concurrently at the top of the route or component.
|
|
30
|
+
- **Granular Streaming**: Wrap slow, non-critical subtrees in `<Suspense fallback={<Skeleton />}>` so critical above-the-fold content streams immediately.
|
|
31
|
+
- **Defer Awaits**: Check cheap synchronous conditions before awaiting remote resources.
|
|
32
|
+
|
|
33
|
+
### 2. Request Deduplication & Caching (`server-cache-react`)
|
|
34
|
+
|
|
35
|
+
- Use `React.cache()` to deduplicate identical database or service calls across multiple components rendered in the same server request lifecycle.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { cache } from 'react';
|
|
39
|
+
import { db } from '@/lib/db';
|
|
40
|
+
|
|
41
|
+
export const getCurrentUser = cache(async (userId: string) => {
|
|
42
|
+
return await db.user.findUnique({
|
|
43
|
+
where: { id: userId },
|
|
44
|
+
select: { id: true, name: true, role: true, email: true }
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 3. Secure Server Actions (`server-auth-actions`)
|
|
50
|
+
|
|
51
|
+
- Treat every Server Action as a public HTTP endpoint. Always validate session, authorization, and input schema with Zod.
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
'use server';
|
|
55
|
+
|
|
56
|
+
import { z } from 'zod';
|
|
57
|
+
import { auth } from '@/lib/auth';
|
|
58
|
+
import { db } from '@/lib/db';
|
|
59
|
+
import { revalidatePath } from 'next/cache';
|
|
60
|
+
|
|
61
|
+
const UpdateProfileSchema = z.object({
|
|
62
|
+
name: z.string().min(2).max(50),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export async function updateProfile(formData: FormData) {
|
|
66
|
+
const session = await auth();
|
|
67
|
+
if (!session?.userId) throw new Error('Unauthorized');
|
|
68
|
+
|
|
69
|
+
const result = UpdateProfileSchema.safeParse({ name: formData.get('name') });
|
|
70
|
+
if (!result.success) return { error: 'Invalid input', issues: result.error.flatten() };
|
|
71
|
+
|
|
72
|
+
await db.user.update({
|
|
73
|
+
where: { id: session.userId },
|
|
74
|
+
data: { name: result.data.name },
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
revalidatePath('/settings');
|
|
78
|
+
return { success: true };
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 4. Bundle Optimization & Dynamic Imports (`bundle-dynamic-imports`)
|
|
83
|
+
|
|
84
|
+
- Heavy interactive client components (charts, rich-text editors, video players) must be dynamically loaded with `next/dynamic`.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import dynamic from 'next/dynamic';
|
|
88
|
+
|
|
89
|
+
const AnalyticsChart = dynamic(
|
|
90
|
+
() => import('@/components/analytics/chart').then(mod => mod.AnalyticsChart),
|
|
91
|
+
{
|
|
92
|
+
loading: () => <div className="h-64 animate-pulse bg-muted rounded-lg" />,
|
|
93
|
+
ssr: false,
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 5. Next.js 15+ Async Request APIs (`async-params`)
|
|
99
|
+
|
|
100
|
+
In Next.js 15+, `params`, `searchParams`, `cookies()`, and `headers()` are asynchronous and must be awaited:
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
// [GOOD] Next.js 15+ Page Component
|
|
104
|
+
interface PageProps {
|
|
105
|
+
params: Promise<{ id: string }>;
|
|
106
|
+
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default async function UserPage({ params, searchParams }: PageProps) {
|
|
110
|
+
const { id } = await params;
|
|
111
|
+
const { tab } = await searchParams;
|
|
112
|
+
const user = await getUser(id);
|
|
113
|
+
|
|
114
|
+
return <UserProfile user={user} activeTab={tab as string} />;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 6. Non-Blocking Background Tasks with `after()`
|
|
119
|
+
|
|
120
|
+
To execute logging, analytics, or cache priming without delaying the user's HTTP response:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { after } from 'next/server';
|
|
124
|
+
|
|
125
|
+
export async function POST(request: Request) {
|
|
126
|
+
const data = await request.json();
|
|
127
|
+
const result = await processOrder(data);
|
|
128
|
+
|
|
129
|
+
// Executes asynchronously AFTER the response stream has completed
|
|
130
|
+
after(async () => {
|
|
131
|
+
await sendSlackNotification(result);
|
|
132
|
+
await indexOrderInSearch(result.id);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return Response.json({ success: true, orderId: result.id });
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## Code Examples
|
|
142
|
+
|
|
143
|
+
See `EXAMPLES.md` for detailed code examples and component templates.
|
|
144
|
+
|
|
145
|
+
## Validation Checklist
|
|
146
|
+
|
|
147
|
+
- [ ] All database queries in RSC layers use `React.cache()` if called across multiple components.
|
|
148
|
+
- [ ] No barrel imports (`from '@/components'`); all imports point to exact component modules.
|
|
149
|
+
- [ ] Server Actions have explicit auth checks and Zod input validation.
|
|
150
|
+
- [ ] Heavy client widgets (charts, editors) use `next/dynamic`.
|
|
151
|
+
- [ ] Images use `next/image` with explicit `sizes` and `priority` on LCP elements.
|
|
152
|
+
|
|
153
|
+
## Common Mistakes
|
|
154
|
+
|
|
155
|
+
- Using `'use client'` at page level instead of leaf components.
|
|
156
|
+
- Relying on client-side authentication checks for Server Actions without server-side validation.
|
|
157
|
+
- Chaining sequential awaits for independent data models.
|
|
158
|
+
|
|
159
|
+
## Integration Notes
|
|
160
|
+
|
|
161
|
+
- Pairs with `react` and `ui-ux-pro` for component design and state management.
|
|
162
|
+
- Pairs with `security` for session authorization and input sanitization.
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
<!-- Source: nextjs.md -->
|
|
166
|
+
|
|
167
|
+
# Next.js — Best Practices (App Router)
|
|
168
|
+
|
|
169
|
+
## Routing
|
|
170
|
+
|
|
171
|
+
- Use **App Router** (`app/` directory) — not Pages Router
|
|
172
|
+
- **Layouts** — shared UI in `layout.tsx`, nested layouts for sections
|
|
173
|
+
- **Loading states** — `loading.tsx` for Suspense boundaries
|
|
174
|
+
- **Error handling** — `error.tsx` for error boundaries per route
|
|
175
|
+
- **Not found** — `not-found.tsx` for 404 pages
|
|
176
|
+
|
|
177
|
+
## Server vs Client Components
|
|
178
|
+
|
|
179
|
+
- **Default to Server Components** — they're server by default
|
|
180
|
+
- **Use `'use client'`** only when you need: event handlers, useState, useEffect, browser APIs
|
|
181
|
+
- **Push client boundaries down** — keep as much as possible on the server
|
|
182
|
+
- **Don't pass functions** from Server to Client components
|
|
183
|
+
|
|
184
|
+
## Data Fetching
|
|
185
|
+
|
|
186
|
+
- **Server Components** — fetch directly, no useEffect
|
|
187
|
+
- **Server Actions** — for mutations (`'use server'`)
|
|
188
|
+
- **Route Handlers** — `app/api/` for REST endpoints
|
|
189
|
+
- **Parallel fetching** — use Promise.all for independent requests
|
|
190
|
+
- **Caching** — leverage Next.js cache, revalidate strategically
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
// Server Component — direct fetch
|
|
194
|
+
async function UserProfile({ id }: { id: string }) {
|
|
195
|
+
const user = await getUser(id); // No useEffect needed
|
|
196
|
+
return <div>{user.name}</div>;
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## File Structure
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
app/
|
|
204
|
+
├── layout.tsx # Root layout
|
|
205
|
+
├── page.tsx # Home page
|
|
206
|
+
├── globals.css
|
|
207
|
+
├── (auth)/ # Route group (no URL impact)
|
|
208
|
+
│ ├── login/page.tsx
|
|
209
|
+
│ └── register/page.tsx
|
|
210
|
+
├── dashboard/
|
|
211
|
+
│ ├── layout.tsx # Dashboard layout
|
|
212
|
+
│ ├── page.tsx # Dashboard home
|
|
213
|
+
│ └── settings/page.tsx
|
|
214
|
+
├── api/
|
|
215
|
+
│ └── users/route.ts # API route
|
|
216
|
+
└── components/ # Shared components
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Performance
|
|
220
|
+
|
|
221
|
+
- **Image optimization** — always use `next/image`
|
|
222
|
+
- **Font optimization** — use `next/font`
|
|
223
|
+
- **Metadata** — export metadata object from pages
|
|
224
|
+
- **Static generation** — prefer SSG over SSR when possible
|
|
225
|
+
- **Edge runtime** — for latency-sensitive routes
|
|
226
|
+
|
|
227
|
+
## Anti-Patterns
|
|
228
|
+
|
|
229
|
+
- [FAIL] Using `useEffect` for data fetching in Server Components
|
|
230
|
+
- [FAIL] Making everything a Client Component
|
|
231
|
+
- [FAIL] Not using `loading.tsx` and `error.tsx`
|
|
232
|
+
- [FAIL] Importing server-only code in Client Components
|
|
233
|
+
- [FAIL] Not leveraging caching and revalidation
|
|
234
|
+
|
|
235
|
+
<!-- Source: EXAMPLES.md -->
|
|
236
|
+
|
|
237
|
+
# nextjs Examples — Anti-patterns vs ContextOS Standard
|
|
238
|
+
|
|
239
|
+
## Example 1: Server Components vs Client Components
|
|
240
|
+
|
|
241
|
+
### Anti-pattern: Marking the Entire Page as Client Component
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
// BAD: app/dashboard/page.tsx with 'use client' at top
|
|
245
|
+
// Bloats client bundle, loses SEO benefits, eliminates direct DB access
|
|
246
|
+
'use client';
|
|
247
|
+
|
|
248
|
+
export default function DashboardPage() {
|
|
249
|
+
const [data, setData] = useState(null);
|
|
250
|
+
useEffect(() => { fetch('/api/dashboard').then(...) }, []);
|
|
251
|
+
return <div>...</div>;
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Best practice: ContextOS Standard (RSC by Default, Client Leaf Nodes)
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
// GOOD: Server Component fetches data directly with zero bundle cost
|
|
259
|
+
// app/dashboard/page.tsx (Server Component)
|
|
260
|
+
import { Suspense } from 'react';
|
|
261
|
+
import { db } from '@/lib/db';
|
|
262
|
+
import { InteractiveChart } from './InteractiveChart'; // 'use client' leaf component
|
|
263
|
+
|
|
264
|
+
export default async function DashboardPage() {
|
|
265
|
+
const stats = await db.analytics.getStats();
|
|
266
|
+
return (
|
|
267
|
+
<main>
|
|
268
|
+
<h1>Dashboard</h1>
|
|
269
|
+
<p>Total Revenue: {stats.revenue}</p>
|
|
270
|
+
<Suspense fallback={<ChartSkeleton />}>
|
|
271
|
+
<InteractiveChart initialData={stats.chartData} />
|
|
272
|
+
</Suspense>
|
|
273
|
+
</main>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
<!-- Source: TROUBLESHOOTING.md -->
|
|
279
|
+
|
|
280
|
+
# nextjs Troubleshooting & Common Mistakes
|
|
281
|
+
|
|
282
|
+
## 1. Hydration Mismatch Errors
|
|
283
|
+
|
|
284
|
+
- **Symptom**: "Text content does not match server-rendered HTML".
|
|
285
|
+
- **Root Cause**: Rendering dates, window dimensions, or local storage data that differs between server render and client hydration.
|
|
286
|
+
- **Fix**: Use suppressHydrationWarning on localized timestamps or load client-only state inside a useEffect after mount.
|
|
287
|
+
|
|
288
|
+
## 2. Accidental Server Code Bundled to Client
|
|
289
|
+
|
|
290
|
+
- **Symptom**: "Module not found: Can't resolve 'fs' or 'pg' in client bundle".
|
|
291
|
+
- **Root Cause**: Client component importing a utility that transitively imports server-only database code.
|
|
292
|
+
- **Fix**: Separate server utilities into *.server.ts and install import 'server-only'; at the top of server files.
|
|
293
|
+
|
|
294
|
+
## 3. Waterfall Fetches in Server Components
|
|
295
|
+
|
|
296
|
+
- **Symptom**: Page takes 3 seconds to load due to sequential await statements.
|
|
297
|
+
- **Root Cause**: Awaiting independent data sources one after another.
|
|
298
|
+
- **Fix**: Use Promise.all([fetchUsers(), fetchProducts()]) or separate into nested <Suspense> boundaries.
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Node.js
|
|
3
|
+
description: >
|
|
4
|
+
ContextOS skill for Node.js
|
|
5
|
+
---
|
|
6
|
+
# Node.js
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Production-grade Node.js runtime and server standard. Enforces async event loop non-blocking hygiene, graceful shutdown, structured JSON logging with correlation IDs, and unhandled rejection guards.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
Activate when developing Node.js HTTP servers, Express/Fastify APIs, background workers, CLI tools, or stream-based data pipelines.
|
|
15
|
+
|
|
16
|
+
## Negative Constraints (What NOT to Do)
|
|
17
|
+
|
|
18
|
+
1. **NEVER execute synchronous filesystem/crypto calls in request handlers (`fs.readFileSync`)**: Always use async promises (`fs.promises.*`) to avoid blocking the event loop.
|
|
19
|
+
2. **NEVER leave uncaught promise rejections**: Every async route must use `express-async-errors` or wrap operations in try/catch calling `next(err)`.
|
|
20
|
+
3. **NEVER buffer large files/payloads entirely in memory (`fs.readFile`)**: Always use Streams or Pipelines (`stream.pipeline`) for processing large files.
|
|
21
|
+
4. **NEVER store in-memory session or user state on a single process instance**: Use Redis or an external state store to allow multi-instance scaling.
|
|
22
|
+
5. **NEVER ignore `SIGTERM` / `SIGINT` shutdown signals**: Always implement graceful shutdown to close open DB pools and drain active HTTP connections.
|
|
23
|
+
|
|
24
|
+
## Rules & Patterns
|
|
25
|
+
|
|
26
|
+
## Architecture
|
|
27
|
+
|
|
28
|
+
- **Layered architecture**: Routes → Controllers → Services → Repositories
|
|
29
|
+
- **Dependency injection** — don't import dependencies directly in services
|
|
30
|
+
- **Config from environment** — never hardcode secrets, use env variables
|
|
31
|
+
- **Graceful shutdown** — handle SIGTERM, close connections, drain requests
|
|
32
|
+
|
|
33
|
+
## Error Handling
|
|
34
|
+
|
|
35
|
+
- **Never swallow errors** — always handle or re-throw
|
|
36
|
+
- **Custom error classes** — extend Error with HTTP status codes
|
|
37
|
+
- **Global error handler** — catch unhandled rejections and uncaught exceptions
|
|
38
|
+
- **Structured logging** — JSON logs with request ID, timestamp, level
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
class AppError extends Error {
|
|
42
|
+
constructor(
|
|
43
|
+
public message: string,
|
|
44
|
+
public statusCode: number = 500,
|
|
45
|
+
public code: string = 'INTERNAL_ERROR'
|
|
46
|
+
) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.name = 'AppError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API Design
|
|
54
|
+
|
|
55
|
+
- **RESTful conventions** — GET (read), POST (create), PUT (full update), PATCH (partial), DELETE
|
|
56
|
+
- **Consistent response format** — `{ data, error, pagination }`
|
|
57
|
+
- **Validation at the edge** — validate request body/params with Zod or Joi
|
|
58
|
+
- **Rate limiting** — protect against abuse
|
|
59
|
+
- **CORS** — configure explicitly, never use `*` in production
|
|
60
|
+
|
|
61
|
+
## Security
|
|
62
|
+
|
|
63
|
+
- **Helmet.js** — security headers
|
|
64
|
+
- **Input validation** — never trust client input
|
|
65
|
+
- **SQL injection** — always use parameterized queries
|
|
66
|
+
- **XSS** — sanitize output, use Content-Security-Policy
|
|
67
|
+
- **Authentication** — JWT with short expiry + refresh tokens
|
|
68
|
+
- **Secrets** — environment variables, never in code
|
|
69
|
+
|
|
70
|
+
## Database
|
|
71
|
+
|
|
72
|
+
- **Connection pooling** — don't create connections per request
|
|
73
|
+
- **Migrations** — version-controlled schema changes
|
|
74
|
+
- **Transactions** — for multi-step operations
|
|
75
|
+
- **Indexes** — add indexes for frequently queried columns
|
|
76
|
+
|
|
77
|
+
## Performance
|
|
78
|
+
|
|
79
|
+
- **Async/await** — never block the event loop
|
|
80
|
+
- **Streaming** — for large files and data sets
|
|
81
|
+
- **Caching** — Redis for frequently accessed data
|
|
82
|
+
- **Clustering** — use PM2 or cluster module for multi-core
|
|
83
|
+
|
|
84
|
+
## Testing
|
|
85
|
+
|
|
86
|
+
- **Unit tests** — services and utilities
|
|
87
|
+
- **Integration tests** — API endpoints with test database
|
|
88
|
+
- **Test isolation** — each test should be independent
|
|
89
|
+
- **Fixtures** — use factories, not shared state
|
|
90
|
+
|
|
91
|
+
## File Structure
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
src/
|
|
95
|
+
├── config/ # Configuration
|
|
96
|
+
├── modules/ # Feature modules
|
|
97
|
+
│ └── users/
|
|
98
|
+
│ ├── users.controller.ts
|
|
99
|
+
│ ├── users.service.ts
|
|
100
|
+
│ ├── users.repository.ts
|
|
101
|
+
│ ├── users.routes.ts
|
|
102
|
+
│ ├── users.types.ts
|
|
103
|
+
│ └── users.test.ts
|
|
104
|
+
├── shared/ # Shared utilities
|
|
105
|
+
│ ├── middleware/
|
|
106
|
+
│ ├── errors/
|
|
107
|
+
│ └── utils/
|
|
108
|
+
├── types/ # Global types
|
|
109
|
+
└── app.ts # App entry point
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
## Code Examples
|
|
114
|
+
|
|
115
|
+
See `EXAMPLES.md` for detailed code examples.
|
|
116
|
+
|
|
117
|
+
## Validation Checklist
|
|
118
|
+
|
|
119
|
+
What to verify during the review phase before completing the task.
|
|
120
|
+
|
|
121
|
+
## Common Mistakes
|
|
122
|
+
|
|
123
|
+
Anti-patterns and things to explicitly avoid. See `TROUBLESHOOTING.md`.
|
|
124
|
+
|
|
125
|
+
## Integration Notes
|
|
126
|
+
|
|
127
|
+
How this skill interacts with other skills.
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
<!-- Source: node.md -->
|
|
131
|
+
|
|
132
|
+
# Node.js — Best Practices
|
|
133
|
+
|
|
134
|
+
## Architecture
|
|
135
|
+
|
|
136
|
+
- **Layered architecture**: Routes → Controllers → Services → Repositories
|
|
137
|
+
- **Dependency injection** — don't import dependencies directly in services
|
|
138
|
+
- **Config from environment** — never hardcode secrets, use env variables
|
|
139
|
+
- **Graceful shutdown** — handle SIGTERM, close connections, drain requests
|
|
140
|
+
|
|
141
|
+
## Error Handling
|
|
142
|
+
|
|
143
|
+
- **Never swallow errors** — always handle or re-throw
|
|
144
|
+
- **Custom error classes** — extend Error with HTTP status codes
|
|
145
|
+
- **Global error handler** — catch unhandled rejections and uncaught exceptions
|
|
146
|
+
- **Structured logging** — JSON logs with request ID, timestamp, level
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
class AppError extends Error {
|
|
150
|
+
constructor(
|
|
151
|
+
public message: string,
|
|
152
|
+
public statusCode: number = 500,
|
|
153
|
+
public code: string = 'INTERNAL_ERROR'
|
|
154
|
+
) {
|
|
155
|
+
super(message);
|
|
156
|
+
this.name = 'AppError';
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## API Design
|
|
162
|
+
|
|
163
|
+
- **RESTful conventions** — GET (read), POST (create), PUT (full update), PATCH (partial), DELETE
|
|
164
|
+
- **Consistent response format** — `{ data, error, pagination }`
|
|
165
|
+
- **Validation at the edge** — validate request body/params with Zod or Joi
|
|
166
|
+
- **Rate limiting** — protect against abuse
|
|
167
|
+
- **CORS** — configure explicitly, never use `*` in production
|
|
168
|
+
|
|
169
|
+
## Security
|
|
170
|
+
|
|
171
|
+
- **Helmet.js** — security headers
|
|
172
|
+
- **Input validation** — never trust client input
|
|
173
|
+
- **SQL injection** — always use parameterized queries
|
|
174
|
+
- **XSS** — sanitize output, use Content-Security-Policy
|
|
175
|
+
- **Authentication** — JWT with short expiry + refresh tokens
|
|
176
|
+
- **Secrets** — environment variables, never in code
|
|
177
|
+
|
|
178
|
+
## Database
|
|
179
|
+
|
|
180
|
+
- **Connection pooling** — don't create connections per request
|
|
181
|
+
- **Migrations** — version-controlled schema changes
|
|
182
|
+
- **Transactions** — for multi-step operations
|
|
183
|
+
- **Indexes** — add indexes for frequently queried columns
|
|
184
|
+
|
|
185
|
+
## Performance
|
|
186
|
+
|
|
187
|
+
- **Async/await** — never block the event loop
|
|
188
|
+
- **Streaming** — for large files and data sets
|
|
189
|
+
- **Caching** — Redis for frequently accessed data
|
|
190
|
+
- **Clustering** — use PM2 or cluster module for multi-core
|
|
191
|
+
|
|
192
|
+
## Testing
|
|
193
|
+
|
|
194
|
+
- **Unit tests** — services and utilities
|
|
195
|
+
- **Integration tests** — API endpoints with test database
|
|
196
|
+
- **Test isolation** — each test should be independent
|
|
197
|
+
- **Fixtures** — use factories, not shared state
|
|
198
|
+
|
|
199
|
+
## File Structure
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
src/
|
|
203
|
+
├── config/ # Configuration
|
|
204
|
+
├── modules/ # Feature modules
|
|
205
|
+
│ └── users/
|
|
206
|
+
│ ├── users.controller.ts
|
|
207
|
+
│ ├── users.service.ts
|
|
208
|
+
│ ├── users.repository.ts
|
|
209
|
+
│ ├── users.routes.ts
|
|
210
|
+
│ ├── users.types.ts
|
|
211
|
+
│ └── users.test.ts
|
|
212
|
+
├── shared/ # Shared utilities
|
|
213
|
+
│ ├── middleware/
|
|
214
|
+
│ ├── errors/
|
|
215
|
+
│ └── utils/
|
|
216
|
+
├── types/ # Global types
|
|
217
|
+
└── app.ts # App entry point
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
<!-- Source: EXAMPLES.md -->
|
|
221
|
+
|
|
222
|
+
# Node.js Examples — Anti-patterns vs ContextOS Standard
|
|
223
|
+
|
|
224
|
+
## Example 1: Graceful Process Shutdown
|
|
225
|
+
|
|
226
|
+
### Anti-pattern: Anti-pattern (Abruptly killing process and dropping in-flight requests)
|
|
227
|
+
|
|
228
|
+
```javascript
|
|
229
|
+
// BAD: drops active database transactions and in-flight HTTP connections
|
|
230
|
+
process.on('SIGTERM', () => {
|
|
231
|
+
process.exit(0);
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Best practice: ContextOS Standard (Graceful connection draining)
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
// GOOD: drains active requests, closes database connections, and exits safely
|
|
239
|
+
import http from 'http';
|
|
240
|
+
import { prisma } from './db';
|
|
241
|
+
import { logger } from './logger';
|
|
242
|
+
|
|
243
|
+
export function setupGracefulShutdown(server: http.Server) {
|
|
244
|
+
const shutdown = async (signal: string) => {
|
|
245
|
+
logger.info(`Received ${signal}. Starting graceful shutdown...`);
|
|
246
|
+
|
|
247
|
+
server.close(async () => {
|
|
248
|
+
logger.info('HTTP server closed.');
|
|
249
|
+
try {
|
|
250
|
+
await prisma.$disconnect();
|
|
251
|
+
logger.info('Database pool closed.');
|
|
252
|
+
process.exit(0);
|
|
253
|
+
} catch (err) {
|
|
254
|
+
logger.error('Error during database disconnect:', err);
|
|
255
|
+
process.exit(1);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Force shutdown after timeout if connections hang
|
|
260
|
+
setTimeout(() => {
|
|
261
|
+
logger.error('Forceful shutdown timeout reached.');
|
|
262
|
+
process.exit(1);
|
|
263
|
+
}, 10_000).unref();
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
267
|
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Example 2: Stream-based File Processing
|
|
274
|
+
|
|
275
|
+
### Anti-pattern: Anti-pattern (Loading entire 500MB file into buffer)
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
// BAD: easily causes Out Of Memory (OOM) crashes under concurrency
|
|
279
|
+
app.get('/download/:file', async (req, res) => {
|
|
280
|
+
const data = await fs.promises.readFile(`/uploads/${req.params.file}`);
|
|
281
|
+
res.send(data);
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Best practice: ContextOS Standard (Piping read stream with pipeline)
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// GOOD: constant memory usage (O(1) RAM) regardless of file size
|
|
289
|
+
import fs from 'fs';
|
|
290
|
+
import { pipeline } from 'stream/promises';
|
|
291
|
+
|
|
292
|
+
app.get('/download/:file', async (req, res, next) => {
|
|
293
|
+
try {
|
|
294
|
+
const filePath = `/uploads/${req.params.file}`;
|
|
295
|
+
const readStream = fs.createReadStream(filePath);
|
|
296
|
+
await pipeline(readStream, res);
|
|
297
|
+
} catch (err) {
|
|
298
|
+
next(err);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
<!-- Source: TROUBLESHOOTING.md -->
|
|
304
|
+
|
|
305
|
+
# node Troubleshooting & Common Mistakes
|
|
306
|
+
|
|
307
|
+
## 1. Unhandled Promise Rejections Crashing the Process
|
|
308
|
+
|
|
309
|
+
- **Symptom**: Node process crashes abruptly without clear stack trace in production.
|
|
310
|
+
- **Root Cause**: Missing `process.on('unhandledRejection')` handler in Node.js >= 15.
|
|
311
|
+
- **Fix**: Always register top-level unhandledRejection and uncaughtException logging before exiting cleanly.
|
|
312
|
+
|
|
313
|
+
## 2. Event Loop Starvation
|
|
314
|
+
|
|
315
|
+
- **Symptom**: API endpoints stop responding or latency spikes to 10+ seconds.
|
|
316
|
+
- **Root Cause**: Heavy synchronous operations (`JSON.parse` on a 50MB file, sync bcrypt hashing, or regex catastrophic backtracking).
|
|
317
|
+
- **Fix**: Offload CPU-heavy tasks to Worker Threads or use async worker queues.
|
|
318
|
+
|
|
319
|
+
## 3. Memory Leaks in Event Emitters
|
|
320
|
+
|
|
321
|
+
- **Symptom**: "MaxListenersExceededWarning: Possible EventEmitter memory leak detected".
|
|
322
|
+
- **Root Cause**: Adding listeners inside request handlers without removing them on close.
|
|
323
|
+
- **Fix**: Remove listeners in cleanup callbacks or use `AbortController` signals.
|