fastypest 3.0.9 → 3.0.11

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.
@@ -0,0 +1,338 @@
1
+ ---
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."
4
+ risk: unknown
5
+ source: community
6
+ date_added: "2026-02-27"
7
+ ---
8
+
9
+ # Node.js Best Practices
10
+
11
+ > Principles and decision-making for Node.js development in 2025.
12
+ > **Learn to THINK, not memorize code patterns.**
13
+
14
+ ## When to Use
15
+ Use this skill when making Node.js architecture decisions, choosing frameworks, designing async patterns, or applying security and deployment best practices.
16
+
17
+ ---
18
+
19
+ ## ⚠️ How to Use This Skill
20
+
21
+ This skill teaches **decision-making principles**, not fixed code to copy.
22
+
23
+ - ASK user for preferences when unclear
24
+ - Choose framework/pattern based on CONTEXT
25
+ - Don't default to same solution every time
26
+
27
+ ---
28
+
29
+ ## 1. Framework Selection (2025)
30
+
31
+ ### Decision Tree
32
+
33
+ ```
34
+ What are you building?
35
+
36
+ ├── Edge/Serverless (Cloudflare, Vercel)
37
+ │ └── Hono (zero-dependency, ultra-fast cold starts)
38
+
39
+ ├── High Performance API
40
+ │ └── Fastify (2-3x faster than Express)
41
+
42
+ ├── Enterprise/Team familiarity
43
+ │ └── NestJS (structured, DI, decorators)
44
+
45
+ ├── Legacy/Stable/Maximum ecosystem
46
+ │ └── Express (mature, most middleware)
47
+
48
+ └── Full-stack with frontend
49
+ └── Next.js API Routes or tRPC
50
+ ```
51
+
52
+ ### Comparison Principles
53
+
54
+ | Factor | Hono | Fastify | Express |
55
+ |--------|------|---------|---------|
56
+ | **Best for** | Edge, serverless | Performance | Legacy, learning |
57
+ | **Cold start** | Fastest | Fast | Moderate |
58
+ | **Ecosystem** | Growing | Good | Largest |
59
+ | **TypeScript** | Native | Excellent | Good |
60
+ | **Learning curve** | Low | Medium | Low |
61
+
62
+ ### Selection Questions to Ask:
63
+ 1. What's the deployment target?
64
+ 2. Is cold start time critical?
65
+ 3. Does team have existing experience?
66
+ 4. Is there legacy code to maintain?
67
+
68
+ ---
69
+
70
+ ## 2. Runtime Considerations (2025)
71
+
72
+ ### Native TypeScript
73
+
74
+ ```
75
+ Node.js 22+: --experimental-strip-types
76
+ ├── Run .ts files directly
77
+ ├── No build step needed for simple projects
78
+ └── Consider for: scripts, simple APIs
79
+ ```
80
+
81
+ ### Module System Decision
82
+
83
+ ```
84
+ ESM (import/export)
85
+ ├── Modern standard
86
+ ├── Better tree-shaking
87
+ ├── Async module loading
88
+ └── Use for: new projects
89
+
90
+ CommonJS (require)
91
+ ├── Legacy compatibility
92
+ ├── More npm packages support
93
+ └── Use for: existing codebases, some edge cases
94
+ ```
95
+
96
+ ### Runtime Selection
97
+
98
+ | Runtime | Best For |
99
+ |---------|----------|
100
+ | **Node.js** | General purpose, largest ecosystem |
101
+ | **Bun** | Performance, built-in bundler |
102
+ | **Deno** | Security-first, built-in TypeScript |
103
+
104
+ ---
105
+
106
+ ## 3. Architecture Principles
107
+
108
+ ### Layered Structure Concept
109
+
110
+ ```
111
+ Request Flow:
112
+
113
+ ├── Controller/Route Layer
114
+ │ ├── Handles HTTP specifics
115
+ │ ├── Input validation at boundary
116
+ │ └── Calls service layer
117
+
118
+ ├── Service Layer
119
+ │ ├── Business logic
120
+ │ ├── Framework-agnostic
121
+ │ └── Calls repository layer
122
+
123
+ └── Repository Layer
124
+ ├── Data access only
125
+ ├── Database queries
126
+ └── ORM interactions
127
+ ```
128
+
129
+ ### Why This Matters:
130
+ - **Testability**: Mock layers independently
131
+ - **Flexibility**: Swap database without touching business logic
132
+ - **Clarity**: Each layer has single responsibility
133
+
134
+ ### When to Simplify:
135
+ - Small scripts → Single file OK
136
+ - Prototypes → Less structure acceptable
137
+ - Always ask: "Will this grow?"
138
+
139
+ ---
140
+
141
+ ## 4. Error Handling Principles
142
+
143
+ ### Centralized Error Handling
144
+
145
+ ```
146
+ Pattern:
147
+ ├── Create custom error classes
148
+ ├── Throw from any layer
149
+ ├── Catch at top level (middleware)
150
+ └── Format consistent response
151
+ ```
152
+
153
+ ### Error Response Philosophy
154
+
155
+ ```
156
+ Client gets:
157
+ ├── Appropriate HTTP status
158
+ ├── Error code for programmatic handling
159
+ ├── User-friendly message
160
+ └── NO internal details (security!)
161
+
162
+ Logs get:
163
+ ├── Full stack trace
164
+ ├── Request context
165
+ ├── User ID (if applicable)
166
+ └── Timestamp
167
+ ```
168
+
169
+ ### Status Code Selection
170
+
171
+ | Situation | Status | When |
172
+ |-----------|--------|------|
173
+ | Bad input | 400 | Client sent invalid data |
174
+ | No auth | 401 | Missing or invalid credentials |
175
+ | No permission | 403 | Valid auth, but not allowed |
176
+ | Not found | 404 | Resource doesn't exist |
177
+ | Conflict | 409 | Duplicate or state conflict |
178
+ | Validation | 422 | Schema valid but business rules fail |
179
+ | Server error | 500 | Our fault, log everything |
180
+
181
+ ---
182
+
183
+ ## 5. Async Patterns Principles
184
+
185
+ ### When to Use Each
186
+
187
+ | Pattern | Use When |
188
+ |---------|----------|
189
+ | `async/await` | Sequential async operations |
190
+ | `Promise.all` | Parallel independent operations |
191
+ | `Promise.allSettled` | Parallel where some can fail |
192
+ | `Promise.race` | Timeout or first response wins |
193
+
194
+ ### Event Loop Awareness
195
+
196
+ ```
197
+ I/O-bound (async helps):
198
+ ├── Database queries
199
+ ├── HTTP requests
200
+ ├── File system
201
+ └── Network operations
202
+
203
+ CPU-bound (async doesn't help):
204
+ ├── Crypto operations
205
+ ├── Image processing
206
+ ├── Complex calculations
207
+ └── → Use worker threads or offload
208
+ ```
209
+
210
+ ### Avoiding Event Loop Blocking
211
+
212
+ - Never use sync methods in production (fs.readFileSync, etc.)
213
+ - Offload CPU-intensive work
214
+ - Use streaming for large data
215
+
216
+ ---
217
+
218
+ ## 6. Validation Principles
219
+
220
+ ### Validate at Boundaries
221
+
222
+ ```
223
+ Where to validate:
224
+ ├── API entry point (request body/params)
225
+ ├── Before database operations
226
+ ├── External data (API responses, file uploads)
227
+ └── Environment variables (startup)
228
+ ```
229
+
230
+ ### Validation Library Selection
231
+
232
+ | Library | Best For |
233
+ |---------|----------|
234
+ | **Zod** | TypeScript first, inference |
235
+ | **Valibot** | Smaller bundle (tree-shakeable) |
236
+ | **ArkType** | Performance critical |
237
+ | **Yup** | Existing React Form usage |
238
+
239
+ ### Validation Philosophy
240
+
241
+ - Fail fast: Validate early
242
+ - Be specific: Clear error messages
243
+ - Don't trust: Even "internal" data
244
+
245
+ ---
246
+
247
+ ## 7. Security Principles
248
+
249
+ ### Security Checklist (Not Code)
250
+
251
+ - [ ] **Input validation**: All inputs validated
252
+ - [ ] **Parameterized queries**: No string concatenation for SQL
253
+ - [ ] **Password hashing**: bcrypt or argon2
254
+ - [ ] **JWT verification**: Always verify signature and expiry
255
+ - [ ] **Rate limiting**: Protect from abuse
256
+ - [ ] **Security headers**: Helmet.js or equivalent
257
+ - [ ] **HTTPS**: Everywhere in production
258
+ - [ ] **CORS**: Properly configured
259
+ - [ ] **Secrets**: Environment variables only
260
+ - [ ] **Dependencies**: Regularly audited
261
+
262
+ ### Security Mindset
263
+
264
+ ```
265
+ Trust nothing:
266
+ ├── Query params → validate
267
+ ├── Request body → validate
268
+ ├── Headers → verify
269
+ ├── Cookies → validate
270
+ ├── File uploads → scan
271
+ └── External APIs → validate response
272
+ ```
273
+
274
+ ---
275
+
276
+ ## 8. Testing Principles
277
+
278
+ ### Test Strategy Selection
279
+
280
+ | Type | Purpose | Tools |
281
+ |------|---------|-------|
282
+ | **Unit** | Business logic | node:test, Vitest |
283
+ | **Integration** | API endpoints | Supertest |
284
+ | **E2E** | Full flows | Playwright |
285
+
286
+ ### What to Test (Priorities)
287
+
288
+ 1. **Critical paths**: Auth, payments, core business
289
+ 2. **Edge cases**: Empty inputs, boundaries
290
+ 3. **Error handling**: What happens when things fail?
291
+ 4. **Not worth testing**: Framework code, trivial getters
292
+
293
+ ### Built-in Test Runner (Node.js 22+)
294
+
295
+ ```
296
+ node --test src/**/*.test.ts
297
+ ├── No external dependency
298
+ ├── Good coverage reporting
299
+ └── Watch mode available
300
+ ```
301
+
302
+ ---
303
+
304
+ ## 10. Anti-Patterns to Avoid
305
+
306
+ ### ❌ DON'T:
307
+ - Use Express for new edge projects (use Hono)
308
+ - Use sync methods in production code
309
+ - Put business logic in controllers
310
+ - Skip input validation
311
+ - Hardcode secrets
312
+ - Trust external data without validation
313
+ - Block event loop with CPU work
314
+
315
+ ### ✅ DO:
316
+ - Choose framework based on context
317
+ - Ask user for preferences when unclear
318
+ - Use layered architecture for growing projects
319
+ - Validate all inputs
320
+ - Use environment variables for secrets
321
+ - Profile before optimizing
322
+
323
+ ---
324
+
325
+ ## 11. Decision Checklist
326
+
327
+ Before implementing:
328
+
329
+ - [ ] **Asked user about stack preference?**
330
+ - [ ] **Chosen framework for THIS context?** (not just default)
331
+ - [ ] **Considered deployment target?**
332
+ - [ ] **Planned error handling strategy?**
333
+ - [ ] **Identified validation points?**
334
+ - [ ] **Considered security requirements?**
335
+
336
+ ---
337
+
338
+ > **Remember**: Node.js best practices are about decision-making, not memorizing patterns. Every project deserves fresh consideration based on its requirements.