@morojs/moro 1.2.1 → 1.4.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 (92) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +61 -7
  3. package/dist/core/config/file-loader.js +31 -25
  4. package/dist/core/config/file-loader.js.map +1 -1
  5. package/dist/core/config/schema.d.ts +2 -2
  6. package/dist/core/config/schema.js +1 -1
  7. package/dist/core/config/schema.js.map +1 -1
  8. package/dist/core/config/types.d.ts +147 -0
  9. package/dist/core/config/types.js +124 -0
  10. package/dist/core/config/types.js.map +1 -0
  11. package/dist/core/config/typescript-loader.d.ts +6 -0
  12. package/dist/core/config/typescript-loader.js +268 -0
  13. package/dist/core/config/typescript-loader.js.map +1 -0
  14. package/dist/core/config/validation.d.ts +18 -0
  15. package/dist/core/config/validation.js +134 -0
  16. package/dist/core/config/validation.js.map +1 -0
  17. package/dist/core/docs/openapi-generator.js +6 -6
  18. package/dist/core/docs/openapi-generator.js.map +1 -1
  19. package/dist/core/docs/schema-to-openapi.d.ts +7 -0
  20. package/dist/core/docs/schema-to-openapi.js +124 -0
  21. package/dist/core/docs/schema-to-openapi.js.map +1 -0
  22. package/dist/core/docs/zod-to-openapi.d.ts +2 -0
  23. package/dist/core/docs/zod-to-openapi.js.map +1 -1
  24. package/dist/core/events/event-bus.js +4 -0
  25. package/dist/core/events/event-bus.js.map +1 -1
  26. package/dist/core/framework.d.ts +29 -6
  27. package/dist/core/framework.js +117 -18
  28. package/dist/core/framework.js.map +1 -1
  29. package/dist/core/http/http-server.d.ts +33 -0
  30. package/dist/core/http/http-server.js +329 -28
  31. package/dist/core/http/http-server.js.map +1 -1
  32. package/dist/core/networking/adapters/index.d.ts +3 -0
  33. package/dist/core/networking/adapters/index.js +10 -0
  34. package/dist/core/networking/adapters/index.js.map +1 -0
  35. package/dist/core/networking/adapters/socketio-adapter.d.ts +16 -0
  36. package/dist/core/networking/adapters/socketio-adapter.js +244 -0
  37. package/dist/core/networking/adapters/socketio-adapter.js.map +1 -0
  38. package/dist/core/networking/adapters/ws-adapter.d.ts +54 -0
  39. package/dist/core/networking/adapters/ws-adapter.js +383 -0
  40. package/dist/core/networking/adapters/ws-adapter.js.map +1 -0
  41. package/dist/core/networking/websocket-adapter.d.ts +171 -0
  42. package/dist/core/networking/websocket-adapter.js +5 -0
  43. package/dist/core/networking/websocket-adapter.js.map +1 -0
  44. package/dist/core/networking/websocket-manager.d.ts +53 -17
  45. package/dist/core/networking/websocket-manager.js +166 -108
  46. package/dist/core/networking/websocket-manager.js.map +1 -1
  47. package/dist/core/routing/index.d.ts +13 -13
  48. package/dist/core/routing/index.js.map +1 -1
  49. package/dist/core/utilities/container.d.ts +1 -0
  50. package/dist/core/utilities/container.js +11 -1
  51. package/dist/core/utilities/container.js.map +1 -1
  52. package/dist/core/validation/adapters.d.ts +51 -0
  53. package/dist/core/validation/adapters.js +135 -0
  54. package/dist/core/validation/adapters.js.map +1 -0
  55. package/dist/core/validation/index.d.ts +14 -11
  56. package/dist/core/validation/index.js +37 -26
  57. package/dist/core/validation/index.js.map +1 -1
  58. package/dist/core/validation/schema-interface.d.ts +36 -0
  59. package/dist/core/validation/schema-interface.js +68 -0
  60. package/dist/core/validation/schema-interface.js.map +1 -0
  61. package/dist/index.d.ts +6 -1
  62. package/dist/index.js +14 -3
  63. package/dist/index.js.map +1 -1
  64. package/dist/moro.d.ts +8 -0
  65. package/dist/moro.js +339 -14
  66. package/dist/moro.js.map +1 -1
  67. package/dist/types/core.d.ts +17 -0
  68. package/package.json +42 -14
  69. package/src/core/config/file-loader.ts +34 -25
  70. package/src/core/config/schema.ts +1 -1
  71. package/src/core/config/types.ts +277 -0
  72. package/src/core/config/typescript-loader.ts +571 -0
  73. package/src/core/config/validation.ts +145 -0
  74. package/src/core/docs/openapi-generator.ts +7 -6
  75. package/src/core/docs/schema-to-openapi.ts +148 -0
  76. package/src/core/docs/zod-to-openapi.ts +2 -0
  77. package/src/core/events/event-bus.ts +5 -0
  78. package/src/core/framework.ts +121 -28
  79. package/src/core/http/http-server.ts +377 -28
  80. package/src/core/networking/adapters/index.ts +16 -0
  81. package/src/core/networking/adapters/socketio-adapter.ts +252 -0
  82. package/src/core/networking/adapters/ws-adapter.ts +425 -0
  83. package/src/core/networking/websocket-adapter.ts +217 -0
  84. package/src/core/networking/websocket-manager.ts +185 -127
  85. package/src/core/routing/index.ts +13 -13
  86. package/src/core/utilities/container.ts +14 -1
  87. package/src/core/validation/adapters.ts +147 -0
  88. package/src/core/validation/index.ts +60 -38
  89. package/src/core/validation/schema-interface.ts +100 -0
  90. package/src/index.ts +25 -2
  91. package/src/moro.ts +405 -15
  92. package/src/types/core.ts +18 -0
@@ -1,6 +1,5 @@
1
1
  // src/core/http-server.ts
2
2
  import { IncomingMessage, ServerResponse, createServer, Server } from 'http';
3
- import { URL } from 'url';
4
3
  import * as zlib from 'zlib';
5
4
  import { promisify } from 'util';
6
5
  import { createFrameworkLogger } from '../logger';
@@ -17,9 +16,114 @@ export class MoroHttpServer {
17
16
  private compressionThreshold = 1024;
18
17
  private logger = createFrameworkLogger('HttpServer');
19
18
  private hookManager: any;
19
+ private requestCounter = 0;
20
+
21
+ // Minimal object pooling for frequently created objects
22
+ private paramObjectPool: Record<string, string>[] = [];
23
+ private bufferPool: Buffer[] = [];
24
+ private readonly maxPoolSize = 50;
25
+
26
+ // Request handler pooling to avoid function creation overhead
27
+ private middlewareExecutionCache = new Map<string, Function>();
28
+
29
+ // String interning for common values (massive memory savings)
30
+ private static readonly INTERNED_METHODS = new Map([
31
+ ['GET', 'GET'],
32
+ ['POST', 'POST'],
33
+ ['PUT', 'PUT'],
34
+ ['DELETE', 'DELETE'],
35
+ ['PATCH', 'PATCH'],
36
+ ['HEAD', 'HEAD'],
37
+ ['OPTIONS', 'OPTIONS'],
38
+ ]);
39
+
40
+ private static readonly INTERNED_HEADERS = new Map([
41
+ ['content-type', 'content-type'],
42
+ ['content-length', 'content-length'],
43
+ ['authorization', 'authorization'],
44
+ ['accept', 'accept'],
45
+ ['user-agent', 'user-agent'],
46
+ ['host', 'host'],
47
+ ['connection', 'connection'],
48
+ ['cache-control', 'cache-control'],
49
+ ]);
50
+
51
+ // Pre-compiled response templates for ultra-common responses
52
+ private static readonly RESPONSE_TEMPLATES = {
53
+ notFound: Buffer.from('{"success":false,"error":"Not found"}'),
54
+ unauthorized: Buffer.from('{"success":false,"error":"Unauthorized"}'),
55
+ forbidden: Buffer.from('{"success":false,"error":"Forbidden"}'),
56
+ internalError: Buffer.from('{"success":false,"error":"Internal server error"}'),
57
+ methodNotAllowed: Buffer.from('{"success":false,"error":"Method not allowed"}'),
58
+ rateLimited: Buffer.from('{"success":false,"error":"Rate limit exceeded"}'),
59
+ };
60
+
61
+ // Ultra-fast buffer pool for zero-copy operations (Rust-level performance)
62
+ private static readonly BUFFER_SIZES = [64, 256, 1024, 4096, 16384];
63
+ private static readonly BUFFER_POOLS = new Map<number, Buffer[]>();
64
+
65
+ static {
66
+ // Pre-allocate buffer pools for zero-allocation responses
67
+ for (const size of MoroHttpServer.BUFFER_SIZES) {
68
+ MoroHttpServer.BUFFER_POOLS.set(size, []);
69
+ for (let i = 0; i < 50; i++) {
70
+ // 50 buffers per size
71
+ MoroHttpServer.BUFFER_POOLS.get(size)!.push(Buffer.allocUnsafe(size));
72
+ }
73
+ }
74
+ }
75
+
76
+ private static getOptimalBuffer(size: number): Buffer {
77
+ // Find the smallest buffer that fits
78
+ for (const poolSize of MoroHttpServer.BUFFER_SIZES) {
79
+ if (size <= poolSize) {
80
+ const pool = MoroHttpServer.BUFFER_POOLS.get(poolSize)!;
81
+ return pool.length > 0 ? pool.pop()! : Buffer.allocUnsafe(poolSize);
82
+ }
83
+ }
84
+ return Buffer.allocUnsafe(size);
85
+ }
86
+
87
+ private static returnBuffer(buffer: Buffer): void {
88
+ // Return buffer to appropriate pool
89
+ const size = buffer.length;
90
+ if (MoroHttpServer.BUFFER_POOLS.has(size)) {
91
+ const pool = MoroHttpServer.BUFFER_POOLS.get(size)!;
92
+ if (pool.length < 50) {
93
+ // Don't let pools grow too large
94
+ pool.push(buffer);
95
+ }
96
+ }
97
+ }
20
98
 
21
99
  constructor() {
22
100
  this.server = createServer(this.handleRequest.bind(this));
101
+
102
+ // Optimize server for high performance (conservative settings for compatibility)
103
+ this.server.keepAliveTimeout = 5000; // 5 seconds
104
+ this.server.headersTimeout = 6000; // 6 seconds
105
+ this.server.timeout = 30000; // 30 seconds request timeout
106
+ }
107
+
108
+ // Configure server for maximum performance (can disable all overhead)
109
+ configurePerformance(
110
+ config: {
111
+ compression?: { enabled: boolean; threshold?: number };
112
+ minimal?: boolean;
113
+ } = {}
114
+ ) {
115
+ if (config.compression !== undefined) {
116
+ this.compressionEnabled = config.compression.enabled;
117
+ if (config.compression.threshold !== undefined) {
118
+ this.compressionThreshold = config.compression.threshold;
119
+ }
120
+ }
121
+
122
+ // Minimal mode - disable ALL overhead for pure speed
123
+ if (config.minimal) {
124
+ this.compressionEnabled = false;
125
+ this.compressionThreshold = Infinity; // Never compress
126
+ }
23
127
  }
24
128
 
25
129
  // Middleware management
@@ -58,14 +162,34 @@ export class MoroHttpServer {
58
162
  const handler = handlers.pop() as HttpHandler;
59
163
  const middleware = handlers as Middleware[];
60
164
 
61
- this.routes.push({
165
+ const route = {
62
166
  method,
63
167
  path,
64
168
  pattern,
65
169
  paramNames,
66
170
  handler,
67
171
  middleware,
68
- });
172
+ };
173
+
174
+ this.routes.push(route);
175
+
176
+ // Organize routes for optimal lookup
177
+ if (paramNames.length === 0) {
178
+ // Static route - O(1) lookup
179
+ const staticKey = `${method}:${path}`;
180
+ this.staticRoutes.set(staticKey, route);
181
+ } else {
182
+ // Dynamic route - organize by segment count for faster matching
183
+ this.dynamicRoutes.push(route);
184
+
185
+ const segments = path.split('/').filter(s => s.length > 0);
186
+ const segmentCount = segments.length;
187
+
188
+ if (!this.routesBySegmentCount.has(segmentCount)) {
189
+ this.routesBySegmentCount.set(segmentCount, []);
190
+ }
191
+ this.routesBySegmentCount.get(segmentCount)!.push(route);
192
+ }
69
193
  }
70
194
 
71
195
  private pathToRegex(path: string): { pattern: RegExp; paramNames: string[] } {
@@ -90,13 +214,23 @@ export class MoroHttpServer {
90
214
  const httpRes = this.enhanceResponse(res);
91
215
 
92
216
  try {
93
- // Parse URL and query parameters
94
- const url = new URL(req.url!, `http://${req.headers.host}`);
95
- httpReq.path = url.pathname;
96
- httpReq.query = Object.fromEntries(url.searchParams);
217
+ // Optimized URL and query parsing
218
+ const urlString = req.url!;
219
+ const queryIndex = urlString.indexOf('?');
220
+
221
+ if (queryIndex === -1) {
222
+ // No query string - fast path
223
+ httpReq.path = urlString;
224
+ httpReq.query = {};
225
+ } else {
226
+ // Has query string - parse efficiently
227
+ httpReq.path = urlString.substring(0, queryIndex);
228
+ httpReq.query = this.parseQueryString(urlString.substring(queryIndex + 1));
229
+ }
97
230
 
98
- // Parse body for POST/PUT/PATCH requests
99
- if (['POST', 'PUT', 'PATCH'].includes(req.method!)) {
231
+ // Ultra-fast method checking - avoid array includes
232
+ const method = req.method!;
233
+ if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
100
234
  httpReq.body = await this.parseBody(req);
101
235
  }
102
236
 
@@ -119,14 +253,19 @@ export class MoroHttpServer {
119
253
  // Find matching route
120
254
  const route = this.findRoute(req.method!, httpReq.path);
121
255
  if (!route) {
122
- httpRes.status(404).json({ success: false, error: 'Not found' });
256
+ // Ultra-fast 404 response with pre-compiled buffer
257
+ httpRes.statusCode = 404;
258
+ httpRes.setHeader('Content-Type', 'application/json; charset=utf-8');
259
+ httpRes.setHeader('Content-Length', MoroHttpServer.RESPONSE_TEMPLATES.notFound.length);
260
+ httpRes.end(MoroHttpServer.RESPONSE_TEMPLATES.notFound);
123
261
  return;
124
262
  }
125
263
 
126
- // Extract path parameters
264
+ // Extract path parameters - optimized with object pooling
127
265
  const matches = httpReq.path.match(route.pattern);
128
266
  if (matches) {
129
- httpReq.params = {};
267
+ // Use pooled object for parameters
268
+ httpReq.params = this.acquireParamObject();
130
269
  route.paramNames.forEach((name, index) => {
131
270
  httpReq.params[name] = matches[index + 1];
132
271
  });
@@ -191,14 +330,82 @@ export class MoroHttpServer {
191
330
  }
192
331
  }
193
332
 
333
+ // Object pooling for parameter objects
334
+ private acquireParamObject(): Record<string, string> {
335
+ const obj = this.paramObjectPool.pop();
336
+ if (obj) {
337
+ // Clear existing properties
338
+ Object.keys(obj).forEach(key => delete obj[key]);
339
+ return obj;
340
+ }
341
+ return {};
342
+ }
343
+
344
+ private releaseParamObject(params: Record<string, string>): void {
345
+ if (this.paramObjectPool.length < this.maxPoolSize) {
346
+ this.paramObjectPool.push(params);
347
+ }
348
+ }
349
+
350
+ private acquireBuffer(size: number): Buffer {
351
+ const buffer = this.bufferPool.find(b => b.length >= size);
352
+ if (buffer) {
353
+ this.bufferPool.splice(this.bufferPool.indexOf(buffer), 1);
354
+ return buffer.subarray(0, size);
355
+ }
356
+ return Buffer.allocUnsafe(size);
357
+ }
358
+
359
+ private releaseBuffer(buffer: Buffer): void {
360
+ if (this.bufferPool.length < this.maxPoolSize && buffer.length <= 8192) {
361
+ this.bufferPool.push(buffer);
362
+ }
363
+ }
364
+
365
+ private streamLargeResponse(res: any, data: any): void {
366
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
367
+ res.setHeader('Transfer-Encoding', 'chunked');
368
+
369
+ // Stream the response in chunks
370
+ const jsonString = JSON.stringify(data);
371
+ const chunkSize = 8192; // 8KB chunks
372
+
373
+ for (let i = 0; i < jsonString.length; i += chunkSize) {
374
+ const chunk = jsonString.substring(i, i + chunkSize);
375
+ res.write(chunk);
376
+ }
377
+ res.end();
378
+ }
379
+
380
+ private normalizePath(path: string): string {
381
+ // Check cache first
382
+ if (this.pathNormalizationCache.has(path)) {
383
+ return this.pathNormalizationCache.get(path)!;
384
+ }
385
+
386
+ // Fast normalization: remove trailing slash (except root), decode once
387
+ let normalized = path;
388
+ if (normalized.length > 1 && normalized.endsWith('/')) {
389
+ normalized = normalized.slice(0, -1);
390
+ }
391
+
392
+ // Cache result (limit cache size)
393
+ if (this.pathNormalizationCache.size < 200) {
394
+ this.pathNormalizationCache.set(path, normalized);
395
+ }
396
+
397
+ return normalized;
398
+ }
399
+
194
400
  private enhanceRequest(req: IncomingMessage): HttpRequest {
195
401
  const httpReq = req as HttpRequest;
196
- httpReq.params = {};
402
+ httpReq.params = this.acquireParamObject();
197
403
  httpReq.query = {};
198
404
  httpReq.body = null;
199
405
  httpReq.path = '';
200
406
  httpReq.ip = req.socket.remoteAddress || '';
201
- httpReq.requestId = Math.random().toString(36).substring(7);
407
+ // Faster request ID generation
408
+ httpReq.requestId = Date.now().toString(36) + (++this.requestCounter).toString(36);
202
409
  httpReq.headers = req.headers as Record<string, string>;
203
410
 
204
411
  // Parse cookies
@@ -233,32 +440,94 @@ export class MoroHttpServer {
233
440
  httpRes.json = async (data: any) => {
234
441
  if (httpRes.headersSent) return;
235
442
 
236
- const jsonString = JSON.stringify(data);
237
- const buffer = Buffer.from(jsonString);
443
+ // Ultra-fast JSON serialization with zero-copy buffers
444
+ let jsonString: string;
445
+
446
+ // Enhanced JSON optimization for common API patterns
447
+ if (data && typeof data === 'object' && 'success' in data) {
448
+ if ('data' in data && 'error' in data && !('total' in data)) {
449
+ // {success, data, error} pattern
450
+ jsonString = `{"success":${data.success},"data":${JSON.stringify(data.data)},"error":${JSON.stringify(data.error)}}`;
451
+ } else if ('data' in data && 'total' in data && !('error' in data)) {
452
+ // {success, data, total} pattern
453
+ jsonString = `{"success":${data.success},"data":${JSON.stringify(data.data)},"total":${data.total}}`;
454
+ } else if ('data' in data && !('error' in data) && !('total' in data)) {
455
+ // {success, data} pattern
456
+ jsonString = `{"success":${data.success},"data":${JSON.stringify(data.data)}}`;
457
+ } else if ('error' in data && !('data' in data) && !('total' in data)) {
458
+ // {success, error} pattern
459
+ jsonString = `{"success":${data.success},"error":${JSON.stringify(data.error)}}`;
460
+ } else {
461
+ // Complex object - use standard JSON.stringify
462
+ jsonString = JSON.stringify(data);
463
+ }
464
+ } else {
465
+ jsonString = JSON.stringify(data);
466
+ }
467
+
468
+ // Use buffer pool for zero-allocation responses
469
+ const estimatedSize = jsonString.length;
470
+ if (estimatedSize > 32768) {
471
+ // Large response - stream it
472
+ return this.streamLargeResponse(httpRes, data);
473
+ }
474
+
475
+ const buffer = MoroHttpServer.getOptimalBuffer(estimatedSize);
476
+ const actualLength = buffer.write(jsonString, 0, 'utf8');
238
477
 
239
- httpRes.setHeader('Content-Type', 'application/json; charset=utf-8');
478
+ // Slice to actual size to avoid sending extra bytes
479
+ const finalBuffer =
480
+ actualLength === buffer.length ? buffer : buffer.subarray(0, actualLength);
240
481
 
241
- // Compression
242
- if (this.compressionEnabled && buffer.length > this.compressionThreshold) {
482
+ // Optimized header setting - set multiple headers at once when possible
483
+ const headers: Record<string, string | number> = {
484
+ 'Content-Type': 'application/json; charset=utf-8',
485
+ };
486
+
487
+ // Compression with buffer pool
488
+ if (this.compressionEnabled && finalBuffer.length > this.compressionThreshold) {
243
489
  const acceptEncoding = httpRes.req.headers['accept-encoding'] || '';
244
490
 
245
491
  if (acceptEncoding.includes('gzip')) {
246
- const compressed = await gzip(buffer);
247
- httpRes.setHeader('Content-Encoding', 'gzip');
248
- httpRes.setHeader('Content-Length', compressed.length);
492
+ const compressed = await gzip(finalBuffer);
493
+ headers['Content-Encoding'] = 'gzip';
494
+ headers['Content-Length'] = compressed.length;
495
+
496
+ // Set all headers at once
497
+ Object.entries(headers).forEach(([key, value]) => {
498
+ httpRes.setHeader(key, value);
499
+ });
500
+
249
501
  httpRes.end(compressed);
502
+ // Return buffer to pool after response
503
+ process.nextTick(() => MoroHttpServer.returnBuffer(buffer));
250
504
  return;
251
505
  } else if (acceptEncoding.includes('deflate')) {
252
- const compressed = await deflate(buffer);
253
- httpRes.setHeader('Content-Encoding', 'deflate');
254
- httpRes.setHeader('Content-Length', compressed.length);
506
+ const compressed = await deflate(finalBuffer);
507
+ headers['Content-Encoding'] = 'deflate';
508
+ headers['Content-Length'] = compressed.length;
509
+
510
+ Object.entries(headers).forEach(([key, value]) => {
511
+ httpRes.setHeader(key, value);
512
+ });
513
+
255
514
  httpRes.end(compressed);
515
+ // Return buffer to pool after response
516
+ process.nextTick(() => MoroHttpServer.returnBuffer(buffer));
256
517
  return;
257
518
  }
258
519
  }
259
520
 
260
- httpRes.setHeader('Content-Length', buffer.length);
261
- httpRes.end(buffer);
521
+ headers['Content-Length'] = finalBuffer.length;
522
+
523
+ // Set all headers at once for better performance
524
+ Object.entries(headers).forEach(([key, value]) => {
525
+ httpRes.setHeader(key, value);
526
+ });
527
+
528
+ httpRes.end(finalBuffer);
529
+ // Return buffer to pool after response (zero-copy achievement!)
530
+ process.nextTick(() => MoroHttpServer.returnBuffer(buffer));
262
531
  };
263
532
 
264
533
  httpRes.send = (data: string | Buffer) => {
@@ -485,8 +754,85 @@ export class MoroHttpServer {
485
754
  return result;
486
755
  }
487
756
 
757
+ private parseQueryString(queryString: string): Record<string, string> {
758
+ const result: Record<string, string> = {};
759
+ if (!queryString) return result;
760
+
761
+ const pairs = queryString.split('&');
762
+ for (const pair of pairs) {
763
+ const equalIndex = pair.indexOf('=');
764
+ if (equalIndex === -1) {
765
+ result[decodeURIComponent(pair)] = '';
766
+ } else {
767
+ const key = decodeURIComponent(pair.substring(0, equalIndex));
768
+ const value = decodeURIComponent(pair.substring(equalIndex + 1));
769
+ result[key] = value;
770
+ }
771
+ }
772
+ return result;
773
+ }
774
+
775
+ // Advanced route optimization: cache + static routes + segment grouping
776
+ private routeCache = new Map<string, RouteEntry | null>();
777
+ private staticRoutes = new Map<string, RouteEntry>();
778
+ private dynamicRoutes: RouteEntry[] = [];
779
+ private routesBySegmentCount = new Map<number, RouteEntry[]>();
780
+ private pathNormalizationCache = new Map<string, string>();
781
+
782
+ // Ultra-fast CPU cache-friendly optimizations (Rust-level performance)
783
+ private routeHitCount = new Map<string, number>(); // Track route popularity for cache optimization
784
+ private static readonly HOT_ROUTE_THRESHOLD = 100; // Routes accessed 100+ times get hot path treatment
785
+
488
786
  private findRoute(method: string, path: string): RouteEntry | null {
489
- return this.routes.find(route => route.method === method && route.pattern.test(path)) || null;
787
+ // Normalize path for consistent matching
788
+ const normalizedPath = this.normalizePath(path);
789
+ const cacheKey = `${method}:${normalizedPath}`;
790
+
791
+ // Track route popularity for hot path optimization
792
+ const hitCount = (this.routeHitCount.get(cacheKey) || 0) + 1;
793
+ this.routeHitCount.set(cacheKey, hitCount);
794
+
795
+ // Check cache first (hot path optimization)
796
+ if (this.routeCache.has(cacheKey)) {
797
+ const cachedRoute = this.routeCache.get(cacheKey)!;
798
+
799
+ // Promote frequently accessed routes to front of cache (LRU-like)
800
+ if (hitCount > MoroHttpServer.HOT_ROUTE_THRESHOLD && this.routeCache.size > 100) {
801
+ this.routeCache.delete(cacheKey);
802
+ this.routeCache.set(cacheKey, cachedRoute); // Move to end (most recent)
803
+ }
804
+
805
+ return cachedRoute;
806
+ }
807
+
808
+ // Phase 1: O(1) static route lookup
809
+ const staticRoute = this.staticRoutes.get(cacheKey);
810
+ if (staticRoute) {
811
+ this.routeCache.set(cacheKey, staticRoute);
812
+ return staticRoute;
813
+ }
814
+
815
+ // Phase 2: Optimized dynamic route matching by segment count
816
+ let route: RouteEntry | null = null;
817
+ if (this.dynamicRoutes.length > 0) {
818
+ const segments = normalizedPath.split('/').filter(s => s.length > 0);
819
+ const candidateRoutes = this.routesBySegmentCount.get(segments.length) || this.dynamicRoutes;
820
+
821
+ // Only test routes with matching method and segment count
822
+ for (const candidateRoute of candidateRoutes) {
823
+ if (candidateRoute.method === method && candidateRoute.pattern.test(normalizedPath)) {
824
+ route = candidateRoute;
825
+ break;
826
+ }
827
+ }
828
+ }
829
+
830
+ // Cache result (limit cache size to prevent memory leaks)
831
+ if (this.routeCache.size < 500) {
832
+ this.routeCache.set(cacheKey, route);
833
+ }
834
+
835
+ return route;
490
836
  }
491
837
 
492
838
  private async executeMiddleware(
@@ -495,6 +841,9 @@ export class MoroHttpServer {
495
841
  res: HttpResponse
496
842
  ): Promise<void> {
497
843
  for (const mw of middleware) {
844
+ // Short-circuit if response already sent
845
+ if (res.headersSent) return;
846
+
498
847
  await new Promise<void>((resolve, reject) => {
499
848
  let nextCalled = false;
500
849
 
@@ -0,0 +1,16 @@
1
+ // WebSocket Adapters for Moro Framework
2
+ // Export all available adapters from this centralized location
3
+
4
+ export { SocketIOAdapter } from './socketio-adapter';
5
+ export { WSAdapter } from './ws-adapter';
6
+
7
+ // Re-export the adapter interface for convenience
8
+ export type {
9
+ WebSocketAdapter,
10
+ WebSocketAdapterOptions,
11
+ WebSocketNamespace,
12
+ WebSocketConnection,
13
+ WebSocketEmitter,
14
+ WebSocketMiddleware,
15
+ WebSocketEventHandler,
16
+ } from '../websocket-adapter';