@stacksjs/bun-router 0.0.5 → 0.0.7

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 (269) hide show
  1. package/dist/auth.d.ts +134 -0
  2. package/dist/cache/lru-cache.d.ts +139 -0
  3. package/dist/cache/middleware-memoization.d.ts +151 -0
  4. package/dist/cache/route-cache-warmer.d.ts +161 -0
  5. package/dist/cache/sqlite-cache.d.ts +209 -0
  6. package/dist/cache/streaming-cache.d.ts +132 -0
  7. package/dist/cli/colors.d.ts +15 -0
  8. package/dist/cli/index.d.ts +10 -0
  9. package/dist/cli/middleware.d.ts +31 -0
  10. package/dist/cli/openapi.d.ts +17 -0
  11. package/dist/cli/router.d.ts +15 -0
  12. package/dist/cli/routes.d.ts +29 -0
  13. package/dist/cli/utils.d.ts +74 -0
  14. package/dist/cli.d.ts +1 -1
  15. package/dist/cli.js +1 -1
  16. package/dist/config.d.ts +7 -0
  17. package/dist/container/container.d.ts +273 -0
  18. package/dist/container/contextual-binding.d.ts +240 -0
  19. package/dist/container/decorators.d.ts +141 -0
  20. package/dist/container/service-provider.d.ts +285 -0
  21. package/dist/development/hot-reload.d.ts +187 -0
  22. package/dist/development/index.d.ts +167 -0
  23. package/dist/development/performance-profiler.d.ts +217 -0
  24. package/dist/development/route-debugger.d.ts +154 -0
  25. package/dist/development/route-inspector.d.ts +211 -0
  26. package/dist/development/typescript-utilities.d.ts +209 -0
  27. package/dist/docs.d.ts +11 -0
  28. package/dist/errors/circuit-breaker.d.ts +195 -0
  29. package/dist/errors/error-handler.d.ts +88 -0
  30. package/dist/errors/error-reporting.d.ts +144 -0
  31. package/dist/errors/exceptions.d.ts +184 -0
  32. package/dist/errors/graceful-degradation.d.ts +154 -0
  33. package/dist/errors/index.d.ts +9 -0
  34. package/dist/errors/router-errors.d.ts +110 -0
  35. package/dist/file-serving/static-files.d.ts +143 -0
  36. package/dist/index.d.ts +2 -0
  37. package/dist/index.js +375 -227
  38. package/dist/middleware/auth.d.ts +63 -0
  39. package/dist/middleware/content_security_policy.d.ts +53 -0
  40. package/dist/middleware/cors.d.ts +5 -0
  41. package/dist/middleware/csrf.d.ts +7 -0
  42. package/dist/middleware/ddos_protection.d.ts +39 -0
  43. package/dist/middleware/file_security.d.ts +26 -0
  44. package/dist/middleware/file_upload.d.ts +38 -0
  45. package/dist/middleware/helmet.d.ts +54 -0
  46. package/dist/middleware/index.d.ts +44 -0
  47. package/dist/middleware/input_validation.d.ts +45 -0
  48. package/dist/middleware/json_body.d.ts +4 -0
  49. package/dist/middleware/performance_alerting.d.ts +87 -0
  50. package/dist/middleware/performance_dashboard.d.ts +87 -0
  51. package/dist/middleware/performance_monitor.d.ts +209 -0
  52. package/dist/middleware/pipeline.d.ts +131 -0
  53. package/dist/middleware/rate_limit.d.ts +36 -0
  54. package/dist/middleware/request_id.d.ts +4 -0
  55. package/dist/middleware/request_signing.d.ts +153 -0
  56. package/dist/middleware/request_tracer.d.ts +72 -0
  57. package/dist/middleware/response_cache.d.ts +97 -0
  58. package/dist/middleware/security.d.ts +69 -0
  59. package/dist/middleware/security_suite.d.ts +46 -0
  60. package/dist/middleware/session.d.ts +23 -0
  61. package/dist/model-binding/index.d.ts +2 -0
  62. package/dist/model-binding/model-middleware.d.ts +118 -0
  63. package/dist/model-binding/model-registry.d.ts +164 -0
  64. package/dist/model-binding.d.ts +185 -0
  65. package/dist/model-resolver-factory.d.ts +31 -0
  66. package/dist/observability/correlation.d.ts +187 -0
  67. package/dist/observability/health-checks.d.ts +186 -0
  68. package/dist/observability/index.d.ts +60 -0
  69. package/dist/observability/integration.d.ts +147 -0
  70. package/dist/observability/metrics.d.ts +184 -0
  71. package/dist/observability/tracing.d.ts +187 -0
  72. package/dist/optimization/bun-utilities.d.ts +224 -0
  73. package/dist/query-builder-integration.d.ts +36 -0
  74. package/dist/request/context.d.ts +34 -0
  75. package/dist/request/enhanced-request.d.ts +213 -0
  76. package/dist/request/macros.d.ts +300 -0
  77. package/dist/response/macros.d.ts +245 -0
  78. package/dist/response/response-factory.d.ts +133 -0
  79. package/dist/router/api-routes.d.ts +5 -0
  80. package/dist/router/file-based-routing.d.ts +118 -0
  81. package/dist/router/file-streaming.d.ts +5 -0
  82. package/dist/router/fluent-router.d.ts +315 -0
  83. package/dist/router/fluent-routing.d.ts +271 -0
  84. package/dist/router/group-organization.d.ts +5 -0
  85. package/dist/router/handler-resolver.d.ts +24 -0
  86. package/dist/router/http-methods.d.ts +10 -0
  87. package/dist/router/index.d.ts +113 -0
  88. package/dist/router/middleware-groups.d.ts +94 -0
  89. package/dist/router/middleware-integration.d.ts +112 -0
  90. package/dist/router/middleware.d.ts +5 -0
  91. package/dist/router/model-binding.d.ts +5 -0
  92. package/dist/router/optimized-route-matching.d.ts +6 -0
  93. package/dist/router/route-building.d.ts +5 -0
  94. package/dist/router/route-compiler.d.ts +153 -0
  95. package/dist/router/route-matching.d.ts +5 -0
  96. package/dist/router/route-trie.d.ts +114 -0
  97. package/dist/router/router.d.ts +276 -0
  98. package/dist/router/server.d.ts +5 -0
  99. package/dist/router/validation-integration.d.ts +170 -0
  100. package/dist/router/view-rendering.d.ts +5 -0
  101. package/dist/router/websocket.d.ts +5 -0
  102. package/dist/routing/route-caching.d.ts +129 -0
  103. package/dist/routing/route-throttling.d.ts +150 -0
  104. package/dist/routing/subdomain-routing.d.ts +206 -0
  105. package/dist/session/database-store.d.ts +51 -0
  106. package/dist/session/file-store.d.ts +22 -0
  107. package/dist/session/index.d.ts +106 -0
  108. package/dist/session/memory-store.d.ts +21 -0
  109. package/dist/session/redis-store.d.ts +31 -0
  110. package/dist/streaming/index.d.ts +3 -0
  111. package/dist/streaming/sse-handler.d.ts +138 -0
  112. package/dist/streaming/stream-handler.d.ts +114 -0
  113. package/dist/testing/auth-testing.d.ts +156 -0
  114. package/dist/testing/file-upload-testing.d.ts +190 -0
  115. package/dist/testing/index.d.ts +10 -0
  116. package/dist/testing/middleware-testing.d.ts +138 -0
  117. package/dist/testing/model-binding-testing.d.ts +186 -0
  118. package/dist/testing/performance-testing.d.ts +235 -0
  119. package/dist/testing/test-client.d.ts +117 -0
  120. package/dist/testing/test-request.d.ts +85 -0
  121. package/dist/testing/test-response.d.ts +90 -0
  122. package/dist/testing/types.d.ts +207 -0
  123. package/dist/testing/websocket-testing.d.ts +227 -0
  124. package/dist/types/controller-types.d.ts +208 -0
  125. package/dist/types/core.d.ts +543 -0
  126. package/dist/types/middleware-types.d.ts +227 -0
  127. package/dist/types/request-response-augmentation.d.ts +261 -0
  128. package/dist/types/route-inference.d.ts +168 -0
  129. package/dist/types.d.ts +1776 -0
  130. package/dist/url.d.ts +56 -0
  131. package/dist/utils/index.d.ts +1 -0
  132. package/dist/utils/query-preservation.d.ts +48 -0
  133. package/dist/utils.d.ts +69 -0
  134. package/dist/validation/validator.d.ts +140 -0
  135. package/dist/websocket/clustering.d.ts +185 -0
  136. package/package.json +3 -2
  137. package/src/auth.ts +469 -0
  138. package/src/cache/lru-cache.ts +457 -0
  139. package/src/cache/middleware-memoization.ts +531 -0
  140. package/src/cache/route-cache-warmer.ts +486 -0
  141. package/src/cache/sqlite-cache.ts +783 -0
  142. package/src/cache/streaming-cache.ts +572 -0
  143. package/src/cli/colors.ts +29 -0
  144. package/src/cli/index.ts +287 -0
  145. package/src/cli/middleware.ts +291 -0
  146. package/src/cli/openapi.ts +407 -0
  147. package/src/cli/router.ts +188 -0
  148. package/src/cli/routes.ts +265 -0
  149. package/src/cli/utils.ts +531 -0
  150. package/src/cli.ts +5 -0
  151. package/src/config.ts +322 -0
  152. package/src/container/container.ts +740 -0
  153. package/src/container/contextual-binding.ts +603 -0
  154. package/src/container/decorators.ts +359 -0
  155. package/src/container/service-provider.ts +596 -0
  156. package/src/development/hot-reload.ts +673 -0
  157. package/src/development/index.ts +499 -0
  158. package/src/development/performance-profiler.ts +717 -0
  159. package/src/development/route-debugger.ts +527 -0
  160. package/src/development/route-inspector.ts +749 -0
  161. package/src/development/typescript-utilities.ts +678 -0
  162. package/src/docs.ts +397 -0
  163. package/src/errors/circuit-breaker.ts +732 -0
  164. package/src/errors/error-handler.ts +569 -0
  165. package/src/errors/error-reporting.ts +672 -0
  166. package/src/errors/exceptions.ts +536 -0
  167. package/src/errors/graceful-degradation.ts +621 -0
  168. package/src/errors/index.ts +21 -0
  169. package/src/errors/router-errors.ts +632 -0
  170. package/src/file-serving/static-files.ts +581 -0
  171. package/src/index.ts +14 -0
  172. package/src/middleware/auth.ts +219 -0
  173. package/src/middleware/content_security_policy.ts +215 -0
  174. package/src/middleware/cors.ts +74 -0
  175. package/src/middleware/csrf.ts +108 -0
  176. package/src/middleware/ddos_protection.ts +255 -0
  177. package/src/middleware/file_security.ts +191 -0
  178. package/src/middleware/file_upload.ts +264 -0
  179. package/src/middleware/helmet.ts +268 -0
  180. package/src/middleware/index.ts +117 -0
  181. package/src/middleware/input_validation.ts +449 -0
  182. package/src/middleware/json_body.ts +37 -0
  183. package/src/middleware/performance_alerting.ts +538 -0
  184. package/src/middleware/performance_dashboard.ts +661 -0
  185. package/src/middleware/performance_monitor.ts +943 -0
  186. package/src/middleware/pipeline.ts +489 -0
  187. package/src/middleware/rate_limit.ts +245 -0
  188. package/src/middleware/request_id.ts +36 -0
  189. package/src/middleware/request_signing.ts +636 -0
  190. package/src/middleware/request_tracer.ts +636 -0
  191. package/src/middleware/response_cache.ts +743 -0
  192. package/src/middleware/security.ts +482 -0
  193. package/src/middleware/security_suite.ts +257 -0
  194. package/src/middleware/session.ts +91 -0
  195. package/src/model-binding/index.ts +18 -0
  196. package/src/model-binding/model-middleware.ts +425 -0
  197. package/src/model-binding/model-registry.ts +550 -0
  198. package/src/model-binding.ts +370 -0
  199. package/src/model-resolver-factory.ts +106 -0
  200. package/src/observability/correlation.ts +691 -0
  201. package/src/observability/health-checks.ts +729 -0
  202. package/src/observability/index.ts +184 -0
  203. package/src/observability/integration.ts +548 -0
  204. package/src/observability/metrics.ts +753 -0
  205. package/src/observability/tracing.ts +638 -0
  206. package/src/optimization/bun-utilities.ts +778 -0
  207. package/src/query-builder-integration.ts +137 -0
  208. package/src/request/context.ts +62 -0
  209. package/src/request/enhanced-request.ts +857 -0
  210. package/src/request/macros.ts +688 -0
  211. package/src/response/macros.ts +612 -0
  212. package/src/response/response-factory.ts +588 -0
  213. package/src/router/api-routes.ts +243 -0
  214. package/src/router/file-based-routing.ts +670 -0
  215. package/src/router/file-streaming.ts +372 -0
  216. package/src/router/fluent-router.ts +927 -0
  217. package/src/router/fluent-routing.ts +797 -0
  218. package/src/router/group-organization.ts +213 -0
  219. package/src/router/handler-resolver.ts +291 -0
  220. package/src/router/http-methods.ts +377 -0
  221. package/src/router/index.ts +185 -0
  222. package/src/router/middleware-groups.ts +222 -0
  223. package/src/router/middleware-integration.ts +399 -0
  224. package/src/router/middleware.ts +231 -0
  225. package/src/router/model-binding.ts +215 -0
  226. package/src/router/optimized-route-matching.ts +253 -0
  227. package/src/router/route-building.ts +212 -0
  228. package/src/router/route-compiler.ts +702 -0
  229. package/src/router/route-matching.ts +349 -0
  230. package/src/router/route-trie.ts +464 -0
  231. package/src/router/router.ts +1524 -0
  232. package/src/router/server.ts +429 -0
  233. package/src/router/validation-integration.ts +443 -0
  234. package/src/router/view-rendering.ts +233 -0
  235. package/src/router/websocket.ts +100 -0
  236. package/src/routing/route-caching.ts +402 -0
  237. package/src/routing/route-throttling.ts +469 -0
  238. package/src/routing/subdomain-routing.ts +492 -0
  239. package/src/session/database-store.ts +109 -0
  240. package/src/session/file-store.ts +148 -0
  241. package/src/session/index.ts +244 -0
  242. package/src/session/memory-store.ts +88 -0
  243. package/src/session/redis-store.ts +93 -0
  244. package/src/streaming/index.ts +17 -0
  245. package/src/streaming/sse-handler.ts +482 -0
  246. package/src/streaming/stream-handler.ts +552 -0
  247. package/src/testing/auth-testing.ts +446 -0
  248. package/src/testing/file-upload-testing.ts +543 -0
  249. package/src/testing/index.ts +10 -0
  250. package/src/testing/middleware-testing.ts +322 -0
  251. package/src/testing/model-binding-testing.ts +645 -0
  252. package/src/testing/performance-testing.ts +738 -0
  253. package/src/testing/test-client.ts +310 -0
  254. package/src/testing/test-request.ts +315 -0
  255. package/src/testing/test-response.ts +332 -0
  256. package/src/testing/types.ts +224 -0
  257. package/src/testing/websocket-testing.ts +590 -0
  258. package/src/types/controller-types.ts +385 -0
  259. package/src/types/core.ts +683 -0
  260. package/src/types/middleware-types.ts +419 -0
  261. package/src/types/request-response-augmentation.ts +489 -0
  262. package/src/types/route-inference.ts +357 -0
  263. package/src/types.ts +2049 -0
  264. package/src/url.ts +126 -0
  265. package/src/utils/index.ts +1 -0
  266. package/src/utils/query-preservation.ts +201 -0
  267. package/src/utils.ts +327 -0
  268. package/src/validation/validator.ts +685 -0
  269. package/src/websocket/clustering.ts +762 -0
@@ -0,0 +1,1524 @@
1
+ import type { Server } from 'bun'
2
+ import type { MiddlewareDependency, MiddlewarePipeline, MiddlewarePipelineStats, MiddlewareSkipCondition } from '../middleware/pipeline'
3
+ import type {
4
+ ActionHandler,
5
+ CookieOptions,
6
+ CookieToSet,
7
+ EnhancedRequest,
8
+ MiddlewareHandler,
9
+ NextFunction,
10
+ Route,
11
+ RouteGroup,
12
+ RouteHandler,
13
+ RouterConfig,
14
+ ThrottlePattern,
15
+ WebSocketConfig,
16
+ WebSocketData,
17
+ } from '../types'
18
+ import { createRateLimitMiddleware, parseThrottleString } from '../routing/route-throttling'
19
+ import { registerNamedRoute } from '../url'
20
+ import { extractParamNames, joinPaths, matchPath } from '../utils'
21
+
22
+ /**
23
+ * Route compiler interface for pattern matching
24
+ */
25
+ export interface RouteCompiler {
26
+ compile: (path: string) => RegExp
27
+ match: (path: string, pattern: RegExp) => Record<string, string> | null
28
+ }
29
+
30
+ /**
31
+ * Middleware cache info interface
32
+ */
33
+ export interface MiddlewareCacheInfo {
34
+ size: number
35
+ routes: string[]
36
+ hitRate: number
37
+ }
38
+
39
+ // Re-export types for module augmentation
40
+ export type {
41
+ ActionHandler,
42
+ MiddlewareHandler,
43
+ Route,
44
+ RouteGroup,
45
+ RouteHandler,
46
+ RouterConfig,
47
+ WebSocketConfig,
48
+ }
49
+
50
+ // Middleware condition type
51
+ export type MiddlewareCondition = (_req: EnhancedRequest) => boolean
52
+
53
+ /**
54
+ * Unified Router class with advanced middleware patterns
55
+ */
56
+ export class Router {
57
+ routes: Route[] = []
58
+ currentGroup: RouteGroup | null = null
59
+ globalMiddleware: MiddlewareHandler[] = []
60
+ namedRoutes: Map<string, Route> = new Map()
61
+ fallbackHandler: ActionHandler | null = null
62
+ patterns: Map<string, string> = new Map()
63
+ currentDomain: string | null = null
64
+ domains: Record<string, Route[]> = {}
65
+ serverInstance: Server<WebSocketData> | null = null
66
+ wsConfig: WebSocketConfig | null = null
67
+ errorHandler: ((_error: Error) => Response | Promise<Response>) | null = null
68
+ templateCache: Map<string, string> = new Map<string, string>()
69
+ routeCache: Map<string, { route: Route, params: Record<string, string> }> = new Map()
70
+ staticRoutes: Map<string, Map<string, Route>> = new Map()
71
+ staticResponses: Map<string, Response> = new Map()
72
+ precompiledPatterns: Map<string, RegExp> = new Map()
73
+ domainPatternCache: Map<string, RegExp> = new Map()
74
+ routeCompiler: RouteCompiler | null = null
75
+
76
+ // Advanced middleware features
77
+ private middlewareGroups: Map<string, MiddlewareHandler[]> = new Map()
78
+ private namedMiddleware: Map<string, (params?: string) => MiddlewareHandler> = new Map()
79
+ private conditionalMiddleware: Array<{ condition: MiddlewareCondition, middleware: MiddlewareHandler[] }> = []
80
+
81
+ // Middleware pipeline for advanced features
82
+ _middlewarePipeline?: MiddlewarePipeline
83
+
84
+ config: RouterConfig = {
85
+ verbose: false,
86
+ routesPath: 'routes',
87
+ apiRoutesPath: 'routes/api.ts',
88
+ webRoutesPath: 'routes/web.ts',
89
+ apiPrefix: '/api',
90
+ webPrefix: '',
91
+ actionsPath: 'actions',
92
+ controllersPath: 'controllers',
93
+ defaultMiddleware: {
94
+ api: [],
95
+ web: [],
96
+ },
97
+ }
98
+
99
+ constructor(config: Partial<RouterConfig> = {}) {
100
+ this.routes = []
101
+ this.config = { ...this.config, ...config }
102
+ this.initializeDefaultMiddleware()
103
+ }
104
+
105
+ /**
106
+ * Initialize default named middleware
107
+ */
108
+ private initializeDefaultMiddleware(): void {
109
+ // Auth middleware
110
+ this.namedMiddleware.set('auth', () => async (req: EnhancedRequest, next: NextFunction) => {
111
+ const token = req.headers.get('authorization')?.replace('Bearer ', '')
112
+ if (!token) {
113
+ return new Response('Unauthorized', { status: 401 })
114
+ }
115
+ return await next()
116
+ })
117
+
118
+ // Throttle middleware with parameters
119
+ this.namedMiddleware.set('throttle', (params?: string): MiddlewareHandler => {
120
+ const config = params ? parseThrottleString(params as ThrottlePattern) : { maxAttempts: 60, windowMs: 60000 }
121
+ return createRateLimitMiddleware({
122
+ maxAttempts: config.maxAttempts || 60,
123
+ windowMs: config.windowMs,
124
+ keyGenerator: (req: EnhancedRequest) => req.headers.get('x-forwarded-for') || 'anonymous',
125
+ })
126
+ })
127
+
128
+ // CORS middleware
129
+ this.namedMiddleware.set('cors', () => async (_req: EnhancedRequest, next: NextFunction) => {
130
+ const response = await next()
131
+ if (response) {
132
+ response.headers.set('Access-Control-Allow-Origin', '*')
133
+ response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
134
+ response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')
135
+ }
136
+ return response
137
+ })
138
+ }
139
+
140
+ /**
141
+ * Register a middleware group
142
+ */
143
+ middlewareGroup(name: string, middlewareNames: string[]): this {
144
+ const middlewareHandlers: MiddlewareHandler[] = []
145
+
146
+ for (const middlewareName of middlewareNames) {
147
+ const [name, params] = middlewareName.split(':')
148
+ const middlewareFactory = this.namedMiddleware.get(name)
149
+
150
+ if (middlewareFactory) {
151
+ middlewareHandlers.push(middlewareFactory(params))
152
+ }
153
+ }
154
+
155
+ this.middlewareGroups.set(name, middlewareHandlers)
156
+ return this
157
+ }
158
+
159
+ /**
160
+ * Apply middleware group to routes
161
+ */
162
+ middlewareGroupRoutes(name: string): RouteGroupBuilder {
163
+ const middlewareHandlers = this.middlewareGroups.get(name) || []
164
+ return new RouteGroupBuilder(this, middlewareHandlers)
165
+ }
166
+
167
+ /**
168
+ * Conditional middleware execution
169
+ */
170
+ when(condition: MiddlewareCondition): ConditionalBuilder {
171
+ return new ConditionalBuilder(this, condition)
172
+ }
173
+
174
+ /**
175
+ * Apply middleware with parameters
176
+ */
177
+ middleware(middlewareName: string): MiddlewareBuilder {
178
+ const [name, params] = middlewareName.split(':')
179
+ const middlewareFactory = this.namedMiddleware.get(name)
180
+
181
+ if (!middlewareFactory) {
182
+ throw new Error(`Unknown middleware: ${name}`)
183
+ }
184
+
185
+ const middlewareHandler = middlewareFactory(params)
186
+ return new MiddlewareBuilder(this, [middlewareHandler])
187
+ }
188
+
189
+ /**
190
+ * Get the server instance
191
+ */
192
+ getServer(): Server<WebSocketData> | null {
193
+ return this.serverInstance
194
+ }
195
+
196
+ /**
197
+ * Extend the router with custom methods
198
+ */
199
+ extend(methods: Record<string, (...args: unknown[]) => unknown>): Router {
200
+ for (const [name, method] of Object.entries(methods)) {
201
+ if (typeof method === 'function') {
202
+ // @ts-expect-error - dynamically extending the object
203
+ this[name] = method.bind(this)
204
+ }
205
+ }
206
+ return this
207
+ }
208
+
209
+ /**
210
+ * Register routes from a package or module file.
211
+ * Loads the route file within an optional group (prefix + middleware).
212
+ */
213
+ async register(routePath: string, options?: { prefix?: string, middleware?: MiddlewareHandler[] }): Promise<Router> {
214
+ const callback = async () => {
215
+ await import(routePath)
216
+ }
217
+
218
+ if (options?.prefix || (options?.middleware && options.middleware.length > 0)) {
219
+ await this.group({
220
+ prefix: options.prefix,
221
+ middleware: options.middleware,
222
+ }, callback)
223
+ }
224
+ else {
225
+ await callback()
226
+ }
227
+
228
+ return this
229
+ }
230
+
231
+ /**
232
+ * Invalidate route caches
233
+ */
234
+ invalidateCache(): void {
235
+ this.routeCache.clear()
236
+ }
237
+
238
+ /**
239
+ * Internal method to add a route with full HTTP method support
240
+ * This is the core route registration method used by get/post/put/patch/delete
241
+ */
242
+ registerRoute(
243
+ method: string,
244
+ path: string,
245
+ handler: ActionHandler,
246
+ type?: 'api' | 'web',
247
+ name?: string,
248
+ middleware?: (string | MiddlewareHandler)[],
249
+ ): Router {
250
+ // Apply current group settings if in a group
251
+ let routePath = path
252
+ let routeMiddleware: MiddlewareHandler[] = []
253
+ const routeType = type || 'web'
254
+
255
+ if (this.currentGroup) {
256
+ // Apply prefix if it exists
257
+ if (this.currentGroup.prefix) {
258
+ routePath = joinPaths(this.currentGroup.prefix, path)
259
+ }
260
+
261
+ // Apply middleware if it exists
262
+ if (this.currentGroup.middleware && this.currentGroup.middleware.length > 0) {
263
+ routeMiddleware = [...this.currentGroup.middleware] as MiddlewareHandler[]
264
+ }
265
+ }
266
+
267
+ // Apply route-specific middleware if provided
268
+ if (middleware && middleware.length > 0) {
269
+ for (const middlewareItem of middleware) {
270
+ const resolved = this.resolveMiddleware(middlewareItem)
271
+ if (resolved) {
272
+ routeMiddleware.push(resolved)
273
+ }
274
+ }
275
+ }
276
+
277
+ // Apply API/Web path prefixes
278
+ if (routeType === 'api' && this.config.apiPrefix) {
279
+ routePath = joinPaths(this.config.apiPrefix, routePath)
280
+ }
281
+ else if (routeType === 'web' && this.config.webPrefix) {
282
+ routePath = joinPaths(this.config.webPrefix, routePath)
283
+ }
284
+
285
+ // Apply domain if in a domain group
286
+ let domain: string | undefined
287
+ if (this.currentDomain) {
288
+ domain = this.currentDomain
289
+ }
290
+
291
+ // Create the route
292
+ const route: Route = {
293
+ method: method.toUpperCase(),
294
+ path: routePath,
295
+ handler,
296
+ domain,
297
+ params: {},
298
+ middleware: routeMiddleware,
299
+ }
300
+
301
+ // Apply constraints from patterns map
302
+ const paramNames = extractParamNames(routePath)
303
+ const constraints: Record<string, string> = {}
304
+
305
+ paramNames.forEach((param: string) => {
306
+ // Remove optional marker for constraint lookup
307
+ const baseParam = param.replace('?', '')
308
+ if (this.patterns.has(baseParam)) {
309
+ constraints[baseParam] = this.patterns.get(baseParam)!
310
+ }
311
+ })
312
+
313
+ if (Object.keys(constraints).length > 0) {
314
+ route.constraints = constraints
315
+ }
316
+
317
+ // Add pattern property for route matching
318
+ route.pattern = {
319
+ exec: (url: URL): { pathname: { groups: Record<string, string> } } | null => {
320
+ const params: Record<string, string> = {}
321
+ // Pass constraints directly to matchPath for more efficient matching
322
+ const constraintsRecord = route.constraints && !Array.isArray(route.constraints)
323
+ ? route.constraints as Record<string, string>
324
+ : undefined
325
+
326
+ const isMatch = matchPath(routePath, url.pathname, params, constraintsRecord)
327
+
328
+ if (!isMatch) {
329
+ return null
330
+ }
331
+
332
+ return {
333
+ pathname: {
334
+ groups: params,
335
+ },
336
+ }
337
+ },
338
+ }
339
+
340
+ // Add to the appropriate collection
341
+ if (domain) {
342
+ if (!this.domains[domain]) {
343
+ this.domains[domain] = []
344
+ }
345
+ this.domains[domain].push(route)
346
+ }
347
+ else {
348
+ this.routes.push(route)
349
+ }
350
+
351
+ // Add to static routes map for fast lookup if it's a static route
352
+ if (!routePath.includes('{') && !routePath.includes('*')) {
353
+ if (!this.staticRoutes.has(method.toUpperCase())) {
354
+ this.staticRoutes.set(method.toUpperCase(), new Map())
355
+ }
356
+ this.staticRoutes.get(method.toUpperCase())!.set(routePath, route)
357
+ }
358
+
359
+ // Add to named routes if name is provided
360
+ if (name) {
361
+ route.name = name
362
+ this.namedRoutes.set(name, route)
363
+ registerNamedRoute(name, route.path)
364
+ }
365
+
366
+ // Clear route cache when new routes are added
367
+ this.routeCache.clear()
368
+
369
+ return this
370
+ }
371
+
372
+ /**
373
+ * Resolve middleware from string or handler (synchronous)
374
+ */
375
+ resolveMiddleware(middleware: string | MiddlewareHandler): MiddlewareHandler | null {
376
+ if (typeof middleware === 'function') {
377
+ return middleware
378
+ }
379
+
380
+ // Parse middleware string like "auth:api" or "throttle:60,1"
381
+ const [name, params] = middleware.split(':')
382
+ const middlewareFactory = this.namedMiddleware.get(name)
383
+
384
+ if (middlewareFactory) {
385
+ return middlewareFactory(params)
386
+ }
387
+
388
+ return null
389
+ }
390
+
391
+ /**
392
+ * HTTP GET method
393
+ */
394
+ get(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
395
+ return this.registerRoute('GET', path, handler, type, name, middleware)
396
+ }
397
+
398
+ /**
399
+ * HTTP POST method
400
+ */
401
+ post(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
402
+ return this.registerRoute('POST', path, handler, type, name, middleware)
403
+ }
404
+
405
+ /**
406
+ * HTTP PUT method
407
+ */
408
+ put(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
409
+ return this.registerRoute('PUT', path, handler, type, name, middleware)
410
+ }
411
+
412
+ /**
413
+ * HTTP PATCH method
414
+ */
415
+ patch(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
416
+ return this.registerRoute('PATCH', path, handler, type, name, middleware)
417
+ }
418
+
419
+ /**
420
+ * HTTP DELETE method
421
+ */
422
+ delete(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
423
+ return this.registerRoute('DELETE', path, handler, type, name, middleware)
424
+ }
425
+
426
+ /**
427
+ * HTTP OPTIONS method
428
+ */
429
+ options(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
430
+ return this.registerRoute('OPTIONS', path, handler, type, name, middleware)
431
+ }
432
+
433
+ /**
434
+ * Register route for multiple HTTP methods
435
+ */
436
+ match(methods: string[], path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
437
+ for (const method of methods) {
438
+ this.registerRoute(method, path, handler, type, name, middleware)
439
+ }
440
+ return this
441
+ }
442
+
443
+ /**
444
+ * Register route for any HTTP method
445
+ */
446
+ any(path: string, handler: ActionHandler, type?: 'api' | 'web', name?: string, middleware?: (string | MiddlewareHandler)[]): Router {
447
+ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']
448
+ return this.match(methods, path, handler, type, name, middleware)
449
+ }
450
+
451
+ /**
452
+ * Set fallback handler for unmatched routes
453
+ */
454
+ fallback(handler: ActionHandler): Router {
455
+ this.fallbackHandler = handler
456
+ return this
457
+ }
458
+
459
+ /**
460
+ * Generate URL for a named route
461
+ */
462
+ route(name: string, params: Record<string, string> = {}): string {
463
+ const route = this.namedRoutes.get(name)
464
+ if (!route) {
465
+ throw new Error(`Route with name "${name}" not found`)
466
+ }
467
+
468
+ let url = route.path
469
+ // Replace path parameters
470
+ for (const [param, value] of Object.entries(params)) {
471
+ url = url.replace(`{${param}}`, encodeURIComponent(value))
472
+ url = url.replace(`{${param}?}`, encodeURIComponent(value))
473
+ }
474
+
475
+ return url
476
+ }
477
+
478
+ /**
479
+ * Set error handler
480
+ */
481
+ onError(handler: (error: Error) => Response | Promise<Response>): Router {
482
+ this.errorHandler = handler
483
+ return this
484
+ }
485
+
486
+ /**
487
+ * Create redirect response
488
+ */
489
+ redirect(url: string, status: 301 | 302 | 303 | 307 | 308 = 302): Response {
490
+ const headers = new Headers()
491
+ headers.set('Location', url)
492
+ return new Response(null, {
493
+ status,
494
+ headers,
495
+ })
496
+ }
497
+
498
+ /**
499
+ * Create permanent redirect response
500
+ */
501
+ permanentRedirect(url: string): Response {
502
+ return this.redirect(url, 301)
503
+ }
504
+
505
+ /**
506
+ * Register redirect route
507
+ */
508
+ redirectRoute(from: string, to: string, status: 301 | 302 | 303 | 307 | 308 = 302): Router {
509
+ this.get(from, (_req: EnhancedRequest) => {
510
+ return this.redirect(to, status)
511
+ })
512
+ return this
513
+ }
514
+
515
+ /**
516
+ * Start the HTTP server
517
+ */
518
+ async serve(options?: { port?: number, hostname?: string }): Promise<Server<WebSocketData>> {
519
+ // Invalidate route cache before starting server
520
+ this.invalidateCache()
521
+
522
+ // Create server options - use type assertion for Bun.serve compatibility
523
+ const serverOptions = {
524
+ ...options,
525
+ fetch: this.handleRequest.bind(this),
526
+ websocket: this.wsConfig,
527
+ } as Parameters<typeof Bun.serve>[0]
528
+
529
+ // Start the server
530
+ this.serverInstance = Bun.serve(serverOptions) as Server<WebSocketData>
531
+
532
+ if (this.config.verbose) {
533
+ const port = this.serverInstance.port
534
+ const hostname = this.serverInstance.hostname
535
+ console.log(`\n🚀 Server running at http://${hostname}:${port}\n`)
536
+ }
537
+
538
+ return this.serverInstance
539
+ }
540
+
541
+ /**
542
+ * Handle an HTTP request
543
+ */
544
+ async handleRequest(req: Request): Promise<Response> {
545
+ try {
546
+ // Create URL for route matching
547
+ const url = new URL(req.url)
548
+
549
+ if (this.config.verbose) {
550
+ console.log(`${req.method} ${url.pathname}`)
551
+ }
552
+
553
+ // Handle CORS preflight OPTIONS requests - but check for registered OPTIONS routes first
554
+ // This ensures explicitly registered OPTIONS routes work while still providing CORS support
555
+ if (req.method === 'OPTIONS') {
556
+ const hostname = url.hostname || req.headers.get('host')?.split(':')[0] || 'localhost'
557
+ const optionsMatch = this.matchRoute(url.pathname, 'OPTIONS', hostname)
558
+ if (!optionsMatch) {
559
+ // No explicit OPTIONS route - return generic CORS preflight response
560
+ return new Response(null, {
561
+ status: 204,
562
+ headers: {
563
+ 'Access-Control-Allow-Origin': '*',
564
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
565
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With',
566
+ 'Access-Control-Max-Age': '86400',
567
+ },
568
+ })
569
+ }
570
+ // Let the registered OPTIONS route handle it (fall through to normal route matching)
571
+ }
572
+
573
+ // Get domain from the host header
574
+ const hostname = url.hostname || req.headers.get('host')?.split(':')[0] || 'localhost'
575
+
576
+ // Find a matching route
577
+ const match = this.matchRoute(url.pathname, req.method as any, hostname)
578
+
579
+ // Enhance the request with params and other utilities
580
+ const enhancedReq = this.enhanceRequest(req, match?.params || {})
581
+
582
+ if (match) {
583
+ // Add the matched route to the request
584
+ enhancedReq.route = match.route
585
+
586
+ // Collect all middleware to run
587
+ const middlewareStack = [...this.globalMiddleware]
588
+
589
+ // Add route-specific middleware
590
+ if (match.route.middleware && match.route.middleware.length > 0) {
591
+ middlewareStack.push(...match.route.middleware)
592
+ }
593
+
594
+ // Create a final middleware that executes the route handler
595
+ const routeHandlerMiddleware = async (req: EnhancedRequest, _next: NextFunction) => {
596
+ return await this.resolveHandler(match.route.handler, req)
597
+ }
598
+
599
+ // Add the route handler as the final middleware
600
+ middlewareStack.push(routeHandlerMiddleware)
601
+
602
+ // Run middleware stack with the route handler at the end
603
+ const response = await this.runMiddleware(enhancedReq, middlewareStack)
604
+
605
+ // Apply modified cookies to the response
606
+ if (response) {
607
+ return this.applyModifiedCookies(response, enhancedReq)
608
+ }
609
+
610
+ // This should not happen since we're always returning a response now
611
+ return new Response('No response from middleware chain', { status: 500 })
612
+ }
613
+
614
+ // No route found - check if the path exists with a different method (405 vs 404).
615
+ // 404/405 bodies now include path + method so client-side debugging (typo'd
616
+ // endpoint, stale SPA cache, missing route registration) is one grep away.
617
+ // Both responses still flow through globalMiddleware so user middleware
618
+ // (X-Request-ID, audit, custom CORS) sees them.
619
+ const allowedMethods = this.getAllowedMethods(url.pathname, hostname)
620
+
621
+ if (allowedMethods.length > 0) {
622
+ const methodNotAllowedHandler = async (_req: EnhancedRequest, _next: NextFunction) => {
623
+ return new Response(JSON.stringify({
624
+ error: 'Method Not Allowed',
625
+ path: url.pathname,
626
+ method: req.method,
627
+ allowed: allowedMethods,
628
+ }), {
629
+ status: 405,
630
+ headers: {
631
+ 'Content-Type': 'application/json',
632
+ 'Allow': allowedMethods.join(', '),
633
+ },
634
+ })
635
+ }
636
+
637
+ if (this.globalMiddleware.length > 0) {
638
+ const middlewareStack = [...this.globalMiddleware, methodNotAllowedHandler]
639
+ const response = await this.runMiddleware(enhancedReq, middlewareStack)
640
+ if (response) {
641
+ return this.applyModifiedCookies(response, enhancedReq)
642
+ }
643
+ }
644
+
645
+ return methodNotAllowedHandler(enhancedReq, async () => new Response(null))
646
+ }
647
+
648
+ // No route found with any method - still run global middleware for CORS headers on 404
649
+ if (this.globalMiddleware.length > 0) {
650
+ const notFoundHandler = async (_req: EnhancedRequest, _next: NextFunction) => {
651
+ if (this.fallbackHandler) {
652
+ return await this.resolveHandler(this.fallbackHandler, enhancedReq)
653
+ }
654
+ return new Response(JSON.stringify({
655
+ error: 'Not Found',
656
+ path: url.pathname,
657
+ method: req.method,
658
+ }), {
659
+ status: 404,
660
+ headers: { 'Content-Type': 'application/json' },
661
+ })
662
+ }
663
+ const middlewareStack = [...this.globalMiddleware, notFoundHandler]
664
+ const response = await this.runMiddleware(enhancedReq, middlewareStack)
665
+ if (response) {
666
+ return this.applyModifiedCookies(response, enhancedReq)
667
+ }
668
+ }
669
+
670
+ // No global middleware, try the fallback handler directly
671
+ if (this.fallbackHandler) {
672
+ const response = await this.resolveHandler(this.fallbackHandler, enhancedReq)
673
+ return this.applyModifiedCookies(response, enhancedReq)
674
+ }
675
+
676
+ // No fallback handler, return a 404 with path context
677
+ return new Response(JSON.stringify({
678
+ error: 'Not Found',
679
+ path: url.pathname,
680
+ method: req.method,
681
+ }), {
682
+ status: 404,
683
+ headers: { 'Content-Type': 'application/json' },
684
+ })
685
+ }
686
+ catch (error) {
687
+ console.error('Error handling request:', error)
688
+
689
+ // Still run global middleware for CORS headers on errors
690
+ if (this.globalMiddleware.length > 0) {
691
+ const enhancedReq = this.enhanceRequest(req, {})
692
+ const errorHandler = async (_req: EnhancedRequest, _next: NextFunction) => {
693
+ if (this.errorHandler) {
694
+ return this.errorHandler(error as Error)
695
+ }
696
+ return new Response(JSON.stringify({
697
+ error: 'Internal Server Error',
698
+ message: error instanceof Error ? error.message : String(error),
699
+ }), {
700
+ status: 500,
701
+ headers: { 'Content-Type': 'application/json' },
702
+ })
703
+ }
704
+ const middlewareStack = [...this.globalMiddleware, errorHandler]
705
+ try {
706
+ const response = await this.runMiddleware(enhancedReq, middlewareStack)
707
+ if (response) {
708
+ return response
709
+ }
710
+ }
711
+ catch {
712
+ // Middleware itself failed, fall through to default error
713
+ }
714
+ }
715
+
716
+ // Use custom error handler if available
717
+ if (this.errorHandler) {
718
+ return this.errorHandler(error as Error)
719
+ }
720
+
721
+ // Default error response
722
+ return new Response(JSON.stringify({
723
+ error: 'Internal Server Error',
724
+ message: error instanceof Error ? error.message : String(error),
725
+ }), {
726
+ status: 500,
727
+ headers: { 'Content-Type': 'application/json' },
728
+ })
729
+ }
730
+ }
731
+
732
+ /**
733
+ * Get all allowed HTTP methods for a given path
734
+ * Used to determine if a 405 Method Not Allowed should be returned instead of 404
735
+ */
736
+ getAllowedMethods(path: string, domain?: string): string[] {
737
+ const url = new URL(path, 'http://localhost')
738
+ const methods: Set<string> = new Set()
739
+ const allMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']
740
+
741
+ for (const method of allMethods) {
742
+ // Check static routes
743
+ if (this.staticRoutes.has(method)) {
744
+ const staticRoute = this.staticRoutes.get(method)!.get(url.pathname)
745
+ if (staticRoute && (!domain || !staticRoute.domain || staticRoute.domain === domain)) {
746
+ methods.add(method)
747
+ continue
748
+ }
749
+ }
750
+
751
+ // Get potential routes
752
+ const potentialRoutes: Route[] = domain && this.domains[domain]
753
+ ? this.domains[domain]
754
+ : this.routes
755
+
756
+ // Filter routes to only those matching the HTTP method
757
+ const methodRoutes = potentialRoutes.filter((route: Route) => route.method === method)
758
+
759
+ // Check exact match
760
+ for (const route of methodRoutes) {
761
+ if (route.path === url.pathname) {
762
+ methods.add(method)
763
+ break
764
+ }
765
+ }
766
+
767
+ // Check pattern match if not already found
768
+ if (!methods.has(method)) {
769
+ for (const route of methodRoutes) {
770
+ if (route.pattern) {
771
+ const match = route.pattern.exec(url)
772
+ if (match) {
773
+ methods.add(method)
774
+ break
775
+ }
776
+ }
777
+ }
778
+ }
779
+
780
+ // Check wildcard routes if not already found
781
+ if (!methods.has(method)) {
782
+ const wildcardRoutes = potentialRoutes.filter((route: Route) =>
783
+ route.method === method && route.path.endsWith('*'),
784
+ )
785
+
786
+ for (const route of wildcardRoutes) {
787
+ const basePath = route.path.slice(0, -1)
788
+ if (url.pathname.startsWith(basePath)) {
789
+ methods.add(method)
790
+ break
791
+ }
792
+ }
793
+ }
794
+ }
795
+
796
+ // If GET is allowed, HEAD is implicitly allowed too
797
+ if (methods.has('GET')) {
798
+ methods.add('HEAD')
799
+ }
800
+
801
+ return Array.from(methods)
802
+ }
803
+
804
+ /**
805
+ * Match a route based on the path, method, and domain
806
+ */
807
+ matchRoute(path: string, method: string, domain?: string): { route: Route, params: Record<string, string> } | undefined {
808
+ const url = new URL(path, 'http://localhost')
809
+
810
+ // Generate cache key
811
+ const cacheKey = `${domain || ''}:${method}:${url.pathname}`
812
+
813
+ // Check cache first
814
+ if (this.routeCache.has(cacheKey)) {
815
+ return this.routeCache.get(cacheKey)
816
+ }
817
+
818
+ // Fast path for static routes
819
+ if (this.staticRoutes.has(method)) {
820
+ const staticRoute = this.staticRoutes.get(method)!.get(url.pathname)
821
+ if (staticRoute && (!domain || !staticRoute.domain || staticRoute.domain === domain)) {
822
+ const result = {
823
+ route: staticRoute,
824
+ params: {},
825
+ }
826
+ this.routeCache.set(cacheKey, result)
827
+ return result
828
+ }
829
+ }
830
+
831
+ // Get potential routes - either all routes or domain-specific routes
832
+ const potentialRoutes: Route[] = domain && this.domains[domain]
833
+ ? this.domains[domain]
834
+ : this.routes
835
+
836
+ // Filter routes to only those matching the HTTP method
837
+ const methodRoutes = potentialRoutes.filter((route: Route) => route.method === method)
838
+
839
+ // First, try to find an exact match
840
+ for (const route of methodRoutes) {
841
+ if (route.path === url.pathname) {
842
+ const result = {
843
+ route,
844
+ params: {},
845
+ }
846
+ this.routeCache.set(cacheKey, result)
847
+ return result
848
+ }
849
+ }
850
+
851
+ // If no exact match, try matching patterns
852
+ for (const route of methodRoutes) {
853
+ if (route.pattern) {
854
+ const match = route.pattern.exec(url)
855
+ if (match) {
856
+ const result = {
857
+ route,
858
+ params: match.pathname.groups,
859
+ }
860
+ this.routeCache.set(cacheKey, result)
861
+ return result
862
+ }
863
+ }
864
+ }
865
+
866
+ // If still no match, try domain-specific * (wildcard) routes
867
+ if (domain && this.domains[domain]) {
868
+ const wildcardRoutes = this.domains[domain].filter((route: Route) =>
869
+ route.method === method && route.path.endsWith('*'),
870
+ )
871
+
872
+ for (const route of wildcardRoutes) {
873
+ const basePath = route.path.slice(0, -1) // Remove the '*'
874
+ if (url.pathname.startsWith(basePath)) {
875
+ const result = {
876
+ route,
877
+ params: {
878
+ wildcard: url.pathname.slice(basePath.length),
879
+ },
880
+ }
881
+ this.routeCache.set(cacheKey, result)
882
+ return result
883
+ }
884
+ }
885
+ }
886
+
887
+ // If no match in domain-specific routes, try global wildcard routes
888
+ const globalWildcardRoutes = this.routes.filter((route: Route) =>
889
+ route.method === method && route.path.endsWith('*') && (!domain || !route.domain),
890
+ )
891
+
892
+ for (const route of globalWildcardRoutes) {
893
+ const basePath = route.path.slice(0, -1) // Remove the '*'
894
+ if (url.pathname.startsWith(basePath)) {
895
+ const result = {
896
+ route,
897
+ params: {
898
+ wildcard: url.pathname.slice(basePath.length),
899
+ },
900
+ }
901
+ this.routeCache.set(cacheKey, result)
902
+ return result
903
+ }
904
+ }
905
+
906
+ // If no match for specific method, try HEAD for GET requests
907
+ if (method === 'HEAD') {
908
+ return this.matchRoute(path, 'GET', domain)
909
+ }
910
+
911
+ // No matching route found
912
+ return undefined
913
+ }
914
+
915
+ /**
916
+ * Enhance a request with params and other utilities
917
+ */
918
+ enhanceRequest(req: Request, params: Record<string, string> = {}): EnhancedRequest {
919
+ // Lazy cookie parsing
920
+ let parsedCookies: Record<string, string> | null = null
921
+
922
+ const getCookies = () => {
923
+ if (parsedCookies === null) {
924
+ parsedCookies = {}
925
+ const cookieHeader = req.headers.get('cookie') || ''
926
+
927
+ cookieHeader.split(';').forEach((cookie) => {
928
+ const parts = cookie.trim().split('=')
929
+ if (parts.length >= 2) {
930
+ const name = parts[0].trim()
931
+ const value = parts.slice(1).join('=').trim()
932
+ parsedCookies![name] = decodeURIComponent(value)
933
+ }
934
+ })
935
+ }
936
+ return parsedCookies
937
+ }
938
+
939
+ // Parse query string
940
+ const url = new URL(req.url)
941
+ const query: Record<string, string> = {}
942
+ url.searchParams.forEach((value, key) => {
943
+ query[key] = value
944
+ })
945
+
946
+ // Create a wrapper object that proxies to the original request
947
+ // This is necessary because native Request objects don't allow property assignment in Bun
948
+ const enhancedReq = {
949
+ // Proxy the native Request properties/methods
950
+ get url() { return req.url },
951
+ get method() { return req.method },
952
+ get headers() { return req.headers },
953
+ get body() { return req.body },
954
+ get bodyUsed() { return req.bodyUsed },
955
+ get cache() { return req.cache },
956
+ get credentials() { return req.credentials },
957
+ get destination() { return req.destination },
958
+ get integrity() { return req.integrity },
959
+ get keepalive() { return req.keepalive },
960
+ get mode() { return req.mode },
961
+ get redirect() { return req.redirect },
962
+ get referrer() { return req.referrer },
963
+ get referrerPolicy() { return req.referrerPolicy },
964
+ get signal() { return req.signal },
965
+ arrayBuffer: () => req.arrayBuffer(),
966
+ blob: () => req.blob(),
967
+ clone: () => req.clone(),
968
+ formData: () => req.formData(),
969
+ json: () => req.json(),
970
+ text: () => req.text(),
971
+
972
+ // Enhanced properties
973
+ params,
974
+ query,
975
+ jsonBody: null as any,
976
+ formBody: null as any,
977
+ _cookiesToSet: [] as CookieToSet[],
978
+ _cookiesToDelete: [] as { name: string, options: CookieOptions }[],
979
+
980
+ } as EnhancedRequest
981
+
982
+ // Helper to get all input data - needs to be defined before adding to object
983
+ const getAllInput = (): Record<string, any> => {
984
+ const input: Record<string, any> = {}
985
+
986
+ // Query parameters
987
+ for (const [key, value] of Object.entries(query)) {
988
+ input[key] = value
989
+ }
990
+
991
+ // JSON body
992
+ if (enhancedReq.jsonBody && typeof enhancedReq.jsonBody === 'object') {
993
+ for (const [key, value] of Object.entries(enhancedReq.jsonBody)) {
994
+ input[key] = value
995
+ }
996
+ }
997
+
998
+ // Form body
999
+ if (enhancedReq.formBody && typeof enhancedReq.formBody === 'object') {
1000
+ for (const [key, value] of Object.entries(enhancedReq.formBody)) {
1001
+ input[key] = value
1002
+ }
1003
+ }
1004
+
1005
+ // Route params
1006
+ for (const [key, value] of Object.entries(params)) {
1007
+ input[key] = value
1008
+ }
1009
+
1010
+ return input
1011
+ }
1012
+
1013
+ // Add cookie utilities
1014
+ enhancedReq.cookies = {
1015
+ get: (name: string) => getCookies()[name],
1016
+ set: (name: string, value: string, options: CookieOptions = {}) => {
1017
+ enhancedReq._cookiesToSet!.push({ name, value, options })
1018
+ },
1019
+ delete: (name: string, options: CookieOptions = {}) => {
1020
+ enhancedReq._cookiesToDelete!.push({ name, options })
1021
+ },
1022
+ getAll: () => ({ ...getCookies() }),
1023
+ }
1024
+
1025
+ // Add Laravel-style request methods directly to the wrapper object
1026
+ // These are the methods that provide Laravel-like request handling
1027
+ ;(enhancedReq as any).get = <T = any>(key: string, defaultValue?: T): T => {
1028
+ const input = getAllInput()
1029
+ const value = input[key]
1030
+ return (value !== undefined ? value : defaultValue) as T
1031
+ }
1032
+
1033
+ ;(enhancedReq as any).input = <T = any>(key: string, defaultValue?: T): T => {
1034
+ const input = getAllInput()
1035
+ const value = input[key]
1036
+ return (value !== undefined ? value : defaultValue) as T
1037
+ }
1038
+
1039
+ ;(enhancedReq as any).all = (): Record<string, any> => getAllInput()
1040
+
1041
+ ;(enhancedReq as any).only = <T extends Record<string, unknown>>(keys: string[]): T => {
1042
+ const input = getAllInput()
1043
+ const result = {} as T
1044
+ for (const key of keys) {
1045
+ if (key in input) {
1046
+ (result as any)[key] = input[key]
1047
+ }
1048
+ }
1049
+ return result
1050
+ }
1051
+
1052
+ ;(enhancedReq as any).except = <T extends Record<string, unknown>>(keys: string[]): T => {
1053
+ const input = getAllInput()
1054
+ const result = { ...input } as T
1055
+ for (const key of keys) {
1056
+ delete (result as any)[key]
1057
+ }
1058
+ return result
1059
+ }
1060
+
1061
+ ;(enhancedReq as any).has = (key: string | string[]): boolean => {
1062
+ const input = getAllInput()
1063
+ if (Array.isArray(key)) {
1064
+ return key.every(k => k in input && input[k] !== undefined)
1065
+ }
1066
+ return key in input && input[key] !== undefined
1067
+ }
1068
+
1069
+ ;(enhancedReq as any).hasAny = (keys: string[]): boolean => {
1070
+ const input = getAllInput()
1071
+ return keys.some(k => k in input && input[k] !== undefined)
1072
+ }
1073
+
1074
+ ;(enhancedReq as any).filled = (key: string | string[]): boolean => {
1075
+ const input = getAllInput()
1076
+ const isFilled = (k: string): boolean => {
1077
+ const value = input[k]
1078
+ return value !== undefined && value !== null && value !== '' && !(Array.isArray(value) && value.length === 0)
1079
+ }
1080
+ if (Array.isArray(key)) {
1081
+ return key.every(isFilled)
1082
+ }
1083
+ return isFilled(key)
1084
+ }
1085
+
1086
+ ;(enhancedReq as any).missing = (key: string | string[]): boolean => {
1087
+ const input = getAllInput()
1088
+ if (Array.isArray(key)) {
1089
+ return key.every(k => !(k in input) || input[k] === undefined)
1090
+ }
1091
+ return !(key in input) || input[key] === undefined
1092
+ }
1093
+
1094
+ ;(enhancedReq as any).string = (key: string, defaultValue: string = ''): string => {
1095
+ const input = getAllInput()
1096
+ const value = input[key]
1097
+ return value !== undefined && value !== null ? String(value) : defaultValue
1098
+ }
1099
+
1100
+ ;(enhancedReq as any).integer = (key: string, defaultValue: number = 0): number => {
1101
+ const input = getAllInput()
1102
+ const value = input[key]
1103
+ const parsed = Number.parseInt(String(value), 10)
1104
+ return Number.isNaN(parsed) ? defaultValue : parsed
1105
+ }
1106
+
1107
+ ;(enhancedReq as any).float = (key: string, defaultValue: number = 0): number => {
1108
+ const input = getAllInput()
1109
+ const value = input[key]
1110
+ const parsed = Number.parseFloat(String(value))
1111
+ return Number.isNaN(parsed) ? defaultValue : parsed
1112
+ }
1113
+
1114
+ ;(enhancedReq as any).boolean = (key: string, defaultValue: boolean = false): boolean => {
1115
+ const input = getAllInput()
1116
+ const value = input[key]
1117
+ if (value === undefined || value === null)
1118
+ return defaultValue
1119
+ if (typeof value === 'boolean')
1120
+ return value
1121
+ if (value === 'true' || value === '1' || value === 1)
1122
+ return true
1123
+ if (value === 'false' || value === '0' || value === 0)
1124
+ return false
1125
+ return defaultValue
1126
+ }
1127
+
1128
+ ;(enhancedReq as any).array = <T = unknown>(key: string): T[] => {
1129
+ const input = getAllInput()
1130
+ const value = input[key]
1131
+ if (Array.isArray(value))
1132
+ return value as T[]
1133
+ return value !== undefined && value !== null ? [value as T] : []
1134
+ }
1135
+
1136
+ ;(enhancedReq as any).bearerToken = (): string | null => {
1137
+ const authHeader
1138
+ = req.headers.get('authorization')
1139
+ || req.headers.get('Authorization')
1140
+ || ''
1141
+ if (authHeader.startsWith('Bearer '))
1142
+ return authHeader.substring(7)
1143
+ return null
1144
+ }
1145
+
1146
+ ;(enhancedReq as any).header = (name: string): string | null => {
1147
+ return req.headers.get(name) || req.headers.get(name.toLowerCase()) || null
1148
+ }
1149
+
1150
+ ;(enhancedReq as any).getParam = <T = string>(name: string, defaultValue?: T): T | undefined => {
1151
+ const value = params?.[name] as T | undefined
1152
+ return value !== undefined ? value : defaultValue
1153
+ }
1154
+
1155
+ ;(enhancedReq as any).params = params || {}
1156
+
1157
+ return enhancedReq as EnhancedRequest
1158
+ }
1159
+
1160
+ /**
1161
+ * Apply modified cookies to a response
1162
+ */
1163
+ applyModifiedCookies(response: Response, req: EnhancedRequest): Response {
1164
+ // Clone the response to modify headers
1165
+ const newResponse = new Response(response.body, {
1166
+ status: response.status,
1167
+ statusText: response.statusText,
1168
+ headers: response.headers,
1169
+ })
1170
+
1171
+ // Apply cookies to set
1172
+ if (req._cookiesToSet && req._cookiesToSet.length > 0) {
1173
+ for (const { name, value, options } of req._cookiesToSet) {
1174
+ const cookieString = this.serializeCookie(name, value, options)
1175
+ newResponse.headers.append('Set-Cookie', cookieString)
1176
+ }
1177
+ }
1178
+
1179
+ // Apply cookies to delete
1180
+ if (req._cookiesToDelete && req._cookiesToDelete.length > 0) {
1181
+ for (const { name, options } of req._cookiesToDelete) {
1182
+ const deletionOptions = {
1183
+ ...options,
1184
+ expires: new Date(0), // Set expiration to past date
1185
+ maxAge: 0,
1186
+ }
1187
+ const cookieString = this.serializeCookie(name, '', deletionOptions)
1188
+ newResponse.headers.append('Set-Cookie', cookieString)
1189
+ }
1190
+ }
1191
+
1192
+ return newResponse
1193
+ }
1194
+
1195
+ /**
1196
+ * Serialize a cookie for the Set-Cookie header
1197
+ */
1198
+ serializeCookie(name: string, value: string, options: CookieOptions = {}): string {
1199
+ let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
1200
+
1201
+ if (options.maxAge !== undefined) {
1202
+ cookie += `; Max-Age=${options.maxAge}`
1203
+ }
1204
+
1205
+ if (options.expires && options.expires instanceof Date) {
1206
+ cookie += `; Expires=${options.expires.toUTCString()}`
1207
+ }
1208
+
1209
+ if (options.path) {
1210
+ cookie += `; Path=${options.path}`
1211
+ }
1212
+ else {
1213
+ cookie += '; Path=/'
1214
+ }
1215
+
1216
+ if (options.domain) {
1217
+ cookie += `; Domain=${options.domain}`
1218
+ }
1219
+
1220
+ if (options.secure) {
1221
+ cookie += '; Secure'
1222
+ }
1223
+
1224
+ if (options.httpOnly) {
1225
+ cookie += '; HttpOnly'
1226
+ }
1227
+
1228
+ if (options.sameSite) {
1229
+ const sameSite = options.sameSite.toLowerCase()
1230
+ cookie += `; SameSite=${sameSite.charAt(0).toUpperCase() + sameSite.slice(1)}`
1231
+ }
1232
+
1233
+ return cookie
1234
+ }
1235
+
1236
+ /**
1237
+ * Build an optimized middleware chain
1238
+ */
1239
+ buildMiddlewareChain(middlewares: MiddlewareHandler[]): (req: EnhancedRequest) => Promise<Response | null> {
1240
+ if (middlewares.length === 0) {
1241
+ return async (_req: EnhancedRequest) => null
1242
+ }
1243
+
1244
+ // Build the chain from the end to start for better performance
1245
+ let chain = async (_req: EnhancedRequest): Promise<Response | null> => null
1246
+
1247
+ for (let i = middlewares.length - 1; i >= 0; i--) {
1248
+ const middleware = middlewares[i]
1249
+ const nextChain = chain
1250
+ chain = async (req: EnhancedRequest): Promise<Response | null> => {
1251
+ const next = async (): Promise<Response> => {
1252
+ const result = await nextChain(req)
1253
+ return result || new Response(null, { status: 200 })
1254
+ }
1255
+ return middleware(req, next)
1256
+ }
1257
+ }
1258
+
1259
+ return chain
1260
+ }
1261
+
1262
+ /**
1263
+ * Run middleware stack for a request
1264
+ */
1265
+ async runMiddleware(req: EnhancedRequest, middlewareStack: MiddlewareHandler[]): Promise<Response | null> {
1266
+ if (middlewareStack.length === 0) {
1267
+ return null // No middleware to run
1268
+ }
1269
+
1270
+ try {
1271
+ // Build and execute optimized middleware chain
1272
+ const chain = this.buildMiddlewareChain(middlewareStack)
1273
+ return await chain(req)
1274
+ }
1275
+ catch (error) {
1276
+ if (this.errorHandler) {
1277
+ return this.errorHandler(error as Error)
1278
+ }
1279
+ throw error
1280
+ }
1281
+ }
1282
+
1283
+ /**
1284
+ * Resolve an action handler
1285
+ */
1286
+ async resolveHandler(handler: ActionHandler, req: EnhancedRequest): Promise<Response> {
1287
+ // If it's a function, call it with the request
1288
+ if (typeof handler === 'function' && !(handler as { prototype?: { handle?: unknown } }).prototype?.handle) {
1289
+ return await (handler as (req: EnhancedRequest) => Response | Promise<Response>)(req)
1290
+ }
1291
+
1292
+ // If it's a class constructor, instantiate it and call handle
1293
+ if (typeof handler === 'function' && (handler as { prototype?: { handle?: unknown } }).prototype?.handle) {
1294
+ const HandlerClass = handler as new () => { handle: (req: EnhancedRequest) => Response | Promise<Response> }
1295
+ const handlerInstance = new HandlerClass()
1296
+ return await handlerInstance.handle(req)
1297
+ }
1298
+
1299
+ // If it's an object with a handle method
1300
+ if (handler && typeof (handler as unknown as { handle?: unknown }).handle === 'function') {
1301
+ return await (handler as unknown as { handle: (req: EnhancedRequest) => Response | Promise<Response> }).handle(req)
1302
+ }
1303
+
1304
+ throw new Error(`Invalid action handler: ${typeof handler}`)
1305
+ }
1306
+
1307
+ /**
1308
+ * Add middleware to the router
1309
+ */
1310
+ use(...middleware: (string | MiddlewareHandler)[]): Router {
1311
+ for (const mw of middleware) {
1312
+ const resolvedMiddleware = this.resolveMiddleware(mw)
1313
+ if (resolvedMiddleware) {
1314
+ this.globalMiddleware.push(resolvedMiddleware)
1315
+ }
1316
+ }
1317
+ return this
1318
+ }
1319
+
1320
+ /**
1321
+ * Create a route group with prefix and middleware
1322
+ */
1323
+ group(options: { prefix?: string, middleware?: (string | MiddlewareHandler)[] }, callback: () => void | Promise<void>): Router {
1324
+ // Save current group state
1325
+ const previousGroup = this.currentGroup
1326
+
1327
+ // Create new group
1328
+ this.currentGroup = {
1329
+ prefix: options.prefix || '',
1330
+ middleware: [],
1331
+ }
1332
+
1333
+ // Resolve middleware if provided
1334
+ if (options.middleware) {
1335
+ for (const mw of options.middleware) {
1336
+ if (typeof mw === 'function') {
1337
+ this.currentGroup.middleware!.push(mw)
1338
+ }
1339
+ }
1340
+ }
1341
+
1342
+ // Execute callback
1343
+ const result = callback()
1344
+
1345
+ // Handle async callbacks
1346
+ if (result instanceof Promise) {
1347
+ result.then(() => {
1348
+ this.currentGroup = previousGroup
1349
+ })
1350
+ }
1351
+ else {
1352
+ // Restore previous group state
1353
+ this.currentGroup = previousGroup
1354
+ }
1355
+
1356
+ return this
1357
+ }
1358
+
1359
+ /**
1360
+ * Add route to the router
1361
+ */
1362
+ addRoute(route: Route): this {
1363
+ this.routes.push(route)
1364
+ if (route.name) {
1365
+ this.namedRoutes.set(route.name, route)
1366
+ registerNamedRoute(route.name, route.path)
1367
+ }
1368
+ return this
1369
+ }
1370
+
1371
+ /**
1372
+ * Register middleware dependency
1373
+ */
1374
+ registerMiddlewareDependency(dependency: MiddlewareDependency): this {
1375
+ if (this._middlewarePipeline) {
1376
+ this._middlewarePipeline.registerDependency(dependency)
1377
+ }
1378
+ return this
1379
+ }
1380
+
1381
+ /**
1382
+ * Register middleware skip conditions
1383
+ */
1384
+ registerMiddlewareSkipConditions(_middlewareName: string, _conditions: MiddlewareSkipCondition[]): this {
1385
+ // Implementation would register skip conditions with the pipeline
1386
+ return this
1387
+ }
1388
+
1389
+ /**
1390
+ * Get middleware statistics
1391
+ */
1392
+ getMiddlewareStats(): MiddlewarePipelineStats | Record<string, never> {
1393
+ if (this._middlewarePipeline) {
1394
+ return this._middlewarePipeline.getStats()
1395
+ }
1396
+ return {}
1397
+ }
1398
+
1399
+ /**
1400
+ * Get middleware cache info
1401
+ */
1402
+ getMiddlewareCacheInfo(): MiddlewareCacheInfo {
1403
+ // Return cache info based on compiled pipelines
1404
+ return {
1405
+ size: 0,
1406
+ routes: [],
1407
+ hitRate: 0,
1408
+ }
1409
+ }
1410
+
1411
+ /**
1412
+ * Clear middleware cache
1413
+ */
1414
+ clearMiddlewareCache(): this {
1415
+ if (this._middlewarePipeline) {
1416
+ this._middlewarePipeline.clear()
1417
+ }
1418
+ return this
1419
+ }
1420
+
1421
+ /**
1422
+ * Execute middleware pipeline
1423
+ */
1424
+ async executeMiddleware(middleware: MiddlewareHandler[], request: EnhancedRequest, handler: () => Promise<Response>): Promise<Response> {
1425
+ if (middleware.length === 0) {
1426
+ return handler()
1427
+ }
1428
+
1429
+ let currentIndex = 0
1430
+
1431
+ const next = async (): Promise<Response> => {
1432
+ if (currentIndex >= middleware.length) {
1433
+ return handler()
1434
+ }
1435
+
1436
+ const mw = middleware[currentIndex++]
1437
+ const result = await mw(request, next)
1438
+ return result || new Response('No response from middleware', { status: 500 })
1439
+ }
1440
+
1441
+ return next()
1442
+ }
1443
+ }
1444
+
1445
+ /**
1446
+ * Builder for conditional middleware
1447
+ */
1448
+ export class ConditionalBuilder {
1449
+ constructor(
1450
+ private router: Router,
1451
+ private condition: MiddlewareCondition,
1452
+ ) {}
1453
+
1454
+ middleware(middlewareName: string): this {
1455
+ const [name, params] = middlewareName.split(':')
1456
+ const middlewareFactory = (this.router as any).namedMiddleware.get(name)
1457
+
1458
+ if (!middlewareFactory) {
1459
+ throw new Error(`Unknown middleware: ${name}`)
1460
+ }
1461
+
1462
+ const middlewareHandler = middlewareFactory(params);
1463
+ (this.router as any).conditionalMiddleware.push({
1464
+ condition: this.condition,
1465
+ middleware: [middlewareHandler],
1466
+ })
1467
+
1468
+ return this
1469
+ }
1470
+ }
1471
+
1472
+ /**
1473
+ * Builder for middleware with parameters
1474
+ */
1475
+ export class MiddlewareBuilder {
1476
+ constructor(
1477
+ private router: Router,
1478
+ private middleware: MiddlewareHandler[],
1479
+ ) {}
1480
+
1481
+ get(_path: string, _handler: RouteHandler): this {
1482
+ // This would integrate with the router's route registration
1483
+ return this
1484
+ }
1485
+
1486
+ post(_path: string, _handler: RouteHandler): this {
1487
+ return this
1488
+ }
1489
+
1490
+ put(_path: string, _handler: RouteHandler): this {
1491
+ return this
1492
+ }
1493
+
1494
+ delete(_path: string, _handler: RouteHandler): this {
1495
+ return this
1496
+ }
1497
+ }
1498
+
1499
+ /**
1500
+ * Builder for route groups with middleware
1501
+ */
1502
+ export class RouteGroupBuilder {
1503
+ constructor(
1504
+ private router: Router,
1505
+ private middleware: MiddlewareHandler[],
1506
+ ) {}
1507
+
1508
+ get(_path: string, _handler: RouteHandler): this {
1509
+ // This would integrate with the router's route registration
1510
+ return this
1511
+ }
1512
+
1513
+ post(_path: string, _handler: RouteHandler): this {
1514
+ return this
1515
+ }
1516
+
1517
+ put(_path: string, _handler: RouteHandler): this {
1518
+ return this
1519
+ }
1520
+
1521
+ delete(_path: string, _handler: RouteHandler): this {
1522
+ return this
1523
+ }
1524
+ }