yinzerflow 0.2.8 → 0.2.10

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.
@@ -2,6 +2,8 @@
2
2
 
3
3
  YinzerFlow provides advanced configuration options for fine-tuning security, performance, and functionality. These options allow you to customize the framework's behavior for specific use cases while maintaining robust security defaults.
4
4
 
5
+ For common configuration patterns and best practices, see [Configuration Patterns](./configuration-patterns.md).
6
+
5
7
  ## Body Parser Configuration
6
8
 
7
9
  Body parsing handles all incoming request data with built-in security protections against DoS attacks, prototype pollution, and memory exhaustion vulnerabilities. See [Body Parsing Documentation](./body-parsing.md) for detailed setup, configuration options, and security considerations.
@@ -0,0 +1,500 @@
1
+ # Configuration Patterns
2
+
3
+ YinzerFlow provides flexible configuration options for different deployment environments and use cases. This document contains common configuration patterns and best practices.
4
+
5
+ For security principles and architecture, see [Security Overview](../security/security-overview.md).
6
+
7
+ ## Quick Configuration Reference
8
+
9
+ ### Minimal Configuration
10
+ ```typescript
11
+ import { YinzerFlow } from 'yinzerflow';
12
+
13
+ const app = new YinzerFlow({ port: 3000 });
14
+ ```
15
+
16
+ ### Basic API Configuration
17
+ ```typescript
18
+ const app = new YinzerFlow({
19
+ port: 3000,
20
+ cors: {
21
+ enabled: true,
22
+ origin: ['https://yourdomain.com']
23
+ }
24
+ });
25
+ ```
26
+
27
+ ### Production Configuration
28
+ ```typescript
29
+ const app = new YinzerFlow({
30
+ port: 443,
31
+ cors: {
32
+ enabled: true,
33
+ origin: ['https://yourdomain.com'],
34
+ credentials: true
35
+ },
36
+ bodyParser: {
37
+ json: {
38
+ maxSize: 262144, // 256KB
39
+ allowPrototypeProperties: false
40
+ }
41
+ },
42
+ ipSecurity: {
43
+ trustedProxies: ['127.0.0.1'],
44
+ detectSpoofing: true
45
+ }
46
+ });
47
+ ```
48
+
49
+ ## Security-First Configurations
50
+
51
+ These configurations prioritize security while maintaining functionality. For detailed security principles, see [Security Overview](../security/security-overview.md).
52
+
53
+ ### High-Security Configuration
54
+ For applications requiring maximum security:
55
+
56
+ ```typescript
57
+ const highSecurityConfig = {
58
+ port: 443,
59
+ cors: {
60
+ enabled: true,
61
+ origin: ['https://yourdomain.com'], // Specific domain only
62
+ credentials: true,
63
+ methods: ['GET', 'POST'], // Minimal methods
64
+ allowedHeaders: ['Content-Type']
65
+ },
66
+ bodyParser: {
67
+ json: {
68
+ maxSize: 32768, // 32KB only
69
+ maxDepth: 3,
70
+ maxKeys: 50
71
+ },
72
+ fileUploads: {
73
+ maxFileSize: 0, // No file uploads
74
+ maxFiles: 0
75
+ },
76
+ urlEncoded: {
77
+ maxSize: 8192, // 8KB forms only
78
+ maxFields: 20
79
+ }
80
+ },
81
+ ipSecurity: {
82
+ trustedProxies: [], // No trusted proxies
83
+ allowPrivateIps: false,
84
+ detectSpoofing: true
85
+ }
86
+ };
87
+ ```
88
+
89
+ ### Public API Configuration
90
+ For public APIs with controlled access:
91
+
92
+ ```typescript
93
+ const publicApiConfig = {
94
+ port: 3000,
95
+ cors: {
96
+ enabled: true,
97
+ origin: ['https://api.yourdomain.com'],
98
+ credentials: false, // No credentials for public API
99
+ methods: ['GET', 'POST', 'PUT', 'DELETE'],
100
+ allowedHeaders: ['Content-Type', 'Authorization']
101
+ },
102
+ bodyParser: {
103
+ json: {
104
+ maxSize: 262144, // 256KB
105
+ maxDepth: 10,
106
+ allowPrototypeProperties: false
107
+ },
108
+ fileUploads: {
109
+ maxFileSize: 10485760, // 10MB
110
+ allowedExtensions: ['.jpg', '.png', '.pdf'],
111
+ maxFiles: 5
112
+ }
113
+ }
114
+ };
115
+ ```
116
+
117
+ ## Environment-Specific Configurations
118
+
119
+ ### Development Configuration
120
+ ```typescript
121
+ const devConfig = {
122
+ port: 3000,
123
+ cors: {
124
+ enabled: true,
125
+ origin: '*', // Allow all origins in development
126
+ credentials: true
127
+ },
128
+ bodyParser: {
129
+ json: {
130
+ maxSize: 1048576, // 1MB for development
131
+ maxDepth: 20,
132
+ allowPrototypeProperties: false
133
+ },
134
+ fileUploads: {
135
+ maxFileSize: 52428800, // 50MB
136
+ maxFiles: 20,
137
+ allowedExtensions: [] // Allow all for development
138
+ }
139
+ },
140
+ ipSecurity: {
141
+ allowPrivateIps: true,
142
+ detectSpoofing: false // Disable for development
143
+ }
144
+ };
145
+ ```
146
+
147
+ ### Staging Configuration
148
+ ```typescript
149
+ const stagingConfig = {
150
+ port: 3000,
151
+ cors: {
152
+ enabled: true,
153
+ origin: ['https://staging.yourdomain.com'],
154
+ credentials: true
155
+ },
156
+ bodyParser: {
157
+ json: {
158
+ maxSize: 262144, // 256KB
159
+ maxDepth: 10,
160
+ allowPrototypeProperties: false
161
+ },
162
+ fileUploads: {
163
+ maxFileSize: 10485760, // 10MB
164
+ maxFiles: 10,
165
+ allowedExtensions: ['.jpg', '.png', '.pdf']
166
+ }
167
+ },
168
+ ipSecurity: {
169
+ trustedProxies: ['127.0.0.1'],
170
+ allowPrivateIps: true,
171
+ detectSpoofing: true
172
+ }
173
+ };
174
+ ```
175
+
176
+ ### Production Configuration
177
+ ```typescript
178
+ const productionConfig = {
179
+ port: 443,
180
+ cors: {
181
+ enabled: true,
182
+ origin: ['https://yourdomain.com'],
183
+ credentials: true,
184
+ methods: ['GET', 'POST', 'PUT', 'DELETE'],
185
+ allowedHeaders: ['Content-Type', 'Authorization']
186
+ },
187
+ bodyParser: {
188
+ json: {
189
+ maxSize: 262144, // 256KB
190
+ maxDepth: 10,
191
+ allowPrototypeProperties: false,
192
+ maxKeys: 1000
193
+ },
194
+ fileUploads: {
195
+ maxFileSize: 10485760, // 10MB
196
+ maxFiles: 10,
197
+ allowedExtensions: ['.jpg', '.png', '.pdf'],
198
+ blockedExtensions: ['.exe', '.bat', '.cmd']
199
+ },
200
+ urlEncoded: {
201
+ maxSize: 1048576, // 1MB
202
+ maxFields: 1000
203
+ }
204
+ },
205
+ ipSecurity: {
206
+ trustedProxies: ['127.0.0.1', '192.168.1.10'],
207
+ allowPrivateIps: false,
208
+ headerPreference: ['x-forwarded-for', 'x-real-ip'],
209
+ maxChainLength: 10,
210
+ detectSpoofing: true
211
+ }
212
+ };
213
+ ```
214
+
215
+ ### High-Security Configuration
216
+ ```typescript
217
+ const highSecurityConfig = {
218
+ port: 443,
219
+ cors: {
220
+ enabled: true,
221
+ origin: ['https://yourdomain.com'], // Specific domain only
222
+ credentials: true,
223
+ methods: ['GET', 'POST'], // Minimal methods
224
+ allowedHeaders: ['Content-Type']
225
+ },
226
+ bodyParser: {
227
+ json: {
228
+ maxSize: 32768, // 32KB only
229
+ maxDepth: 3,
230
+ maxKeys: 50
231
+ },
232
+ fileUploads: {
233
+ maxFileSize: 0, // No file uploads
234
+ maxFiles: 0
235
+ },
236
+ urlEncoded: {
237
+ maxSize: 8192, // 8KB forms only
238
+ maxFields: 20
239
+ }
240
+ },
241
+ ipSecurity: {
242
+ trustedProxies: [], // No trusted proxies
243
+ allowPrivateIps: false,
244
+ detectSpoofing: true
245
+ }
246
+ };
247
+ ```
248
+
249
+ ## Use Case Configurations
250
+
251
+ ### File Upload Service Configuration
252
+ ```typescript
253
+ const fileUploadConfig = {
254
+ port: 3000,
255
+ cors: {
256
+ enabled: true,
257
+ origin: ['https://upload.yourdomain.com'],
258
+ credentials: true
259
+ },
260
+ bodyParser: {
261
+ json: {
262
+ maxSize: 512000, // 500KB for metadata
263
+ maxDepth: 3,
264
+ allowPrototypeProperties: false
265
+ },
266
+ fileUploads: {
267
+ maxFileSize: 104857600, // 100MB per file
268
+ maxTotalSize: 524288000, // 500MB total
269
+ maxFiles: 20,
270
+ allowedExtensions: ['.jpg', '.jpeg', '.png', '.gif', '.mp4', '.webm', '.pdf'],
271
+ blockedExtensions: [], // Using allowlist instead
272
+ maxFilenameLength: 200
273
+ }
274
+ }
275
+ };
276
+ ```
277
+
278
+ ### Microservice Configuration
279
+ ```typescript
280
+ const microserviceConfig = {
281
+ port: 3000,
282
+ cors: {
283
+ enabled: false // No CORS for internal services
284
+ },
285
+ bodyParser: {
286
+ json: {
287
+ maxSize: 131072, // 128KB
288
+ maxDepth: 5,
289
+ allowPrototypeProperties: false
290
+ },
291
+ fileUploads: {
292
+ maxFileSize: 0, // No file uploads
293
+ maxFiles: 0
294
+ }
295
+ },
296
+ ipSecurity: {
297
+ trustedProxies: ['127.0.0.1', '10.0.0.0/8'],
298
+ allowPrivateIps: true,
299
+ detectSpoofing: true
300
+ }
301
+ };
302
+ ```
303
+
304
+ ### Load Balancer Configuration
305
+ ```typescript
306
+ const loadBalancerConfig = {
307
+ port: 3000,
308
+ cors: {
309
+ enabled: true,
310
+ origin: ['https://yourdomain.com'],
311
+ credentials: true
312
+ },
313
+ ipSecurity: {
314
+ trustedProxies: ['192.168.1.10', '192.168.1.11'], // Load balancer IPs
315
+ allowPrivateIps: true,
316
+ headerPreference: ['x-forwarded-for', 'x-real-ip'],
317
+ maxChainLength: 5,
318
+ detectSpoofing: true
319
+ }
320
+ };
321
+ ```
322
+
323
+ ### CDN Configuration
324
+ ```typescript
325
+ const cdnConfig = {
326
+ port: 3000,
327
+ cors: {
328
+ enabled: true,
329
+ origin: ['https://yourdomain.com'],
330
+ credentials: true
331
+ },
332
+ ipSecurity: {
333
+ trustedProxies: [
334
+ // Cloudflare IP ranges
335
+ '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22'
336
+ ],
337
+ headerPreference: ['cf-connecting-ip', 'x-forwarded-for'],
338
+ allowPrivateIps: false // Only real client IPs
339
+ }
340
+ };
341
+ ```
342
+
343
+ ## Configuration Best Practices
344
+
345
+ ### 1. Environment Variables
346
+ Use environment variables for sensitive configuration:
347
+
348
+ ```typescript
349
+ const app = new YinzerFlow({
350
+ port: parseInt(process.env.PORT || '3000'),
351
+ cors: {
352
+ enabled: true,
353
+ origin: process.env.ALLOWED_ORIGINS?.split(',') || ['http://localhost:3000']
354
+ },
355
+ bodyParser: {
356
+ json: {
357
+ maxSize: parseInt(process.env.MAX_JSON_SIZE || '262144')
358
+ }
359
+ }
360
+ });
361
+ ```
362
+
363
+ ### 2. Configuration Validation
364
+ Validate configuration at startup:
365
+
366
+ ```typescript
367
+ const validateConfig = (config: any) => {
368
+ if (!config.port || config.port < 1 || config.port > 65535) {
369
+ throw new Error('Invalid port configuration');
370
+ }
371
+
372
+ if (config.cors?.enabled && !config.cors?.origin) {
373
+ throw new Error('CORS enabled but no origin specified');
374
+ }
375
+
376
+ return config;
377
+ };
378
+
379
+ const config = validateConfig({
380
+ port: 3000,
381
+ cors: { enabled: true, origin: ['https://yourdomain.com'] }
382
+ });
383
+
384
+ const app = new YinzerFlow(config);
385
+ ```
386
+
387
+ ### 3. Configuration Composition
388
+ Compose configurations from smaller parts:
389
+
390
+ ```typescript
391
+ const baseConfig = {
392
+ port: 3000,
393
+ bodyParser: {
394
+ json: {
395
+ allowPrototypeProperties: false
396
+ }
397
+ }
398
+ };
399
+
400
+ const securityConfig = {
401
+ cors: {
402
+ enabled: true,
403
+ origin: ['https://yourdomain.com']
404
+ },
405
+ ipSecurity: {
406
+ detectSpoofing: true
407
+ }
408
+ };
409
+
410
+ const productionConfig = {
411
+ ...baseConfig,
412
+ ...securityConfig,
413
+ port: 443
414
+ };
415
+ ```
416
+
417
+ ### 4. Configuration Testing
418
+ Test your configuration:
419
+
420
+ ```typescript
421
+ const testConfig = async (config: any) => {
422
+ const app = new YinzerFlow(config);
423
+
424
+ try {
425
+ await app.listen();
426
+ console.log('✅ Configuration is valid');
427
+ await app.close();
428
+ } catch (error) {
429
+ console.error('❌ Configuration error:', error);
430
+ throw error;
431
+ }
432
+ };
433
+
434
+ await testConfig(productionConfig);
435
+ ```
436
+
437
+ ## Configuration Reference
438
+
439
+ For detailed configuration options, see:
440
+
441
+ - **[Advanced Configuration Options](./advanced-configuration-options.md)** - Complete configuration reference
442
+ - **[Security Overview](../security/security-overview.md)** - Security configuration patterns
443
+ - **[Body Parsing Security](../security/body-parsing.md)** - Body parser configuration
444
+ - **[CORS Security](../security/cors.md)** - CORS configuration
445
+ - **[IP Security](../security/ip-security.md)** - IP security configuration
446
+ - **[Logging Security](../security/logging.md)** - Logging configuration
447
+
448
+ ## Common Issues and Solutions
449
+
450
+ ### CORS Errors
451
+ **Problem**: Browser blocks requests due to CORS policy
452
+ **Solution**: Configure CORS with proper origins and credentials
453
+
454
+ ```typescript
455
+ cors: {
456
+ enabled: true,
457
+ origin: ['https://yourdomain.com'], // Specific origin
458
+ credentials: true, // If using cookies/auth
459
+ methods: ['GET', 'POST', 'PUT', 'DELETE']
460
+ }
461
+ ```
462
+
463
+ ### File Upload Failures
464
+ **Problem**: File uploads rejected or too large
465
+ **Solution**: Configure appropriate file upload limits
466
+
467
+ ```typescript
468
+ bodyParser: {
469
+ fileUploads: {
470
+ maxFileSize: 10485760, // 10MB per file
471
+ maxFiles: 10,
472
+ allowedExtensions: ['.jpg', '.png', '.pdf']
473
+ }
474
+ }
475
+ ```
476
+
477
+ ### IP Address Issues
478
+ **Problem**: Wrong client IP address detected
479
+ **Solution**: Configure trusted proxies for your infrastructure
480
+
481
+ ```typescript
482
+ ipSecurity: {
483
+ trustedProxies: ['127.0.0.1', '192.168.1.10'],
484
+ headerPreference: ['x-forwarded-for', 'x-real-ip']
485
+ }
486
+ ```
487
+
488
+ ### Memory Issues
489
+ **Problem**: Server runs out of memory with large requests
490
+ **Solution**: Reduce body parsing limits
491
+
492
+ ```typescript
493
+ bodyParser: {
494
+ json: {
495
+ maxSize: 131072, // 128KB instead of 256KB
496
+ maxDepth: 5, // Shallow nesting
497
+ maxKeys: 100 // Fewer object keys
498
+ }
499
+ }
500
+ ```
@@ -0,0 +1,92 @@
1
+ # Context Object
2
+
3
+ The Context object is the central interface for all YinzerFlow route handlers and hooks. It provides access to the request data, response controls, and maintains the complete request lifecycle state.
4
+
5
+ ## Configuration
6
+
7
+ Context objects are automatically created and provided to all handlers - no configuration required:
8
+
9
+ ```typescript
10
+ import { YinzerFlow } from 'yinzerflow';
11
+
12
+ const app = new YinzerFlow({ port: 3000 });
13
+
14
+ // Context is automatically provided to all handlers
15
+ app.get('/api/data', (ctx) => {
16
+ // Access request data
17
+ const { path, method, headers } = ctx.request;
18
+
19
+ // Control response
20
+ ctx.response.setStatusCode(200);
21
+
22
+ return { message: 'Success' };
23
+ });
24
+ ```
25
+
26
+ ## Examples
27
+
28
+ ### Basic Example
29
+
30
+ See [Basic Handler Pattern](./examples.md#basic-handler-pattern) for a complete example showing how to use the context object.
31
+
32
+ For detailed information about request properties and methods, see [Request Object Documentation](./request.md).
33
+ For detailed information about response methods and capabilities, see [Response Object Documentation](./response.md).
34
+
35
+ ## Common Use Cases
36
+
37
+ - **Request Data Access**: Extract headers, body, query parameters, and route parameters for processing
38
+ - **Response Control**: Set status codes, add headers, and control response formatting
39
+ - **Authentication**: Access authorization headers and client IP for security validation
40
+ - **Content Negotiation**: Handle Accept headers and Content-Type for proper response formatting
41
+ - **Error Handling**: Provide context to error handlers for detailed error responses
42
+ - **Logging and Monitoring**: Access request metadata for logging and analytics
43
+
44
+ ## Context Structure
45
+
46
+ The Context object contains two main properties:
47
+
48
+ ### Request Object (`ctx.request`)
49
+ Contains all incoming request data including headers, body, query parameters, route parameters, and metadata. See [Request Object Documentation](./request.md) for detailed information about request properties and methods.
50
+
51
+ ### Response Object (`ctx.response`)
52
+ Provides methods to control the HTTP response including status codes, headers, and response formatting. See [Response Object Documentation](./response.md) for detailed information about response methods and capabilities.
53
+
54
+ ## TypeScript Support
55
+
56
+ YinzerFlow provides full TypeScript support for context objects with generic type parameters.
57
+
58
+ See [TypeScript Pattern](./examples.md#typescript-pattern) for a complete example showing how to use TypeScript with the context object.
59
+
60
+ ## Error Handling Integration
61
+
62
+ Context objects are automatically passed to error handlers.
63
+
64
+ See [Error Handler Pattern](./examples.md#error-handler-pattern) for a complete example.
65
+
66
+ ## Hook Integration
67
+
68
+ Context objects are passed to all hooks (before/after hooks).
69
+
70
+ See [Hook Pattern](./examples.md#hook-pattern) for a complete example.
71
+
72
+ ## Security Considerations
73
+
74
+ YinzerFlow implements several security measures for context handling:
75
+
76
+ ### 🛡️ Input Validation
77
+ - **Problem**: Malicious request data can cause injection attacks or bypass security controls
78
+ - **YinzerFlow Solution**: All request properties are automatically validated and sanitized
79
+
80
+ ### 🛡️ Type Safety
81
+ - **Problem**: Untyped request data can lead to runtime errors and security vulnerabilities
82
+ - **YinzerFlow Solution**: Full TypeScript support with generic type parameters ensures type safety
83
+
84
+ ### 🛡️ Context Isolation
85
+ - **Problem**: Context objects could be modified maliciously to bypass security controls
86
+ - **YinzerFlow Solution**: Context objects are isolated and modifications are controlled
87
+
88
+ ### 🛡️ Header Security
89
+ - **Problem**: Malicious headers can cause parsing errors or security bypasses
90
+ - **YinzerFlow Solution**: Header validation and sanitization prevent header-based attacks
91
+
92
+ These security measures ensure YinzerFlow's context implementation follows security best practices and prevents common attack vectors while maintaining type safety and developer experience.