@stacksjs/bun-router 0.0.16 → 0.0.18

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 (176) hide show
  1. package/dist/auth.d.ts +22 -8
  2. package/dist/chunk-1ahs68ys.js +18 -0
  3. package/dist/{chunk-0145g1me.js → chunk-cgptvjdf.js} +147 -75
  4. package/dist/chunk-g3ybefhg.js +1923 -0
  5. package/dist/cli.js +3 -2
  6. package/dist/container/container.d.ts +1 -0
  7. package/dist/container/contextual-binding.d.ts +0 -1
  8. package/dist/container/decorators.d.ts +2 -5
  9. package/dist/container/index.d.ts +16 -0
  10. package/dist/container/index.js +322 -0
  11. package/dist/container/service-provider.d.ts +3 -3
  12. package/dist/development/performance-profiler.d.ts +3 -3
  13. package/dist/development/route-debugger.d.ts +1 -1
  14. package/dist/development/route-inspector.d.ts +2 -2
  15. package/dist/file-serving/static-files.d.ts +16 -0
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +1159 -1380
  18. package/dist/middleware/cors.d.ts +9 -0
  19. package/dist/middleware/csrf.d.ts +7 -0
  20. package/dist/middleware/ddos_protection.d.ts +16 -1
  21. package/dist/middleware/rate_limit.d.ts +7 -1
  22. package/dist/middleware/request_tracer.d.ts +15 -9
  23. package/dist/middleware/response_cache.d.ts +2 -1
  24. package/dist/observability/integration.d.ts +1 -1
  25. package/dist/request/context.d.ts +12 -1
  26. package/dist/request/macros.d.ts +40 -2
  27. package/dist/response/macros.d.ts +4 -4
  28. package/dist/router/fluent-routing.d.ts +50 -13
  29. package/dist/router/handler-resolver.d.ts +8 -0
  30. package/dist/router/index.d.ts +9 -7
  31. package/dist/router/route-compiler.d.ts +8 -10
  32. package/dist/router/route-trie.d.ts +7 -1
  33. package/dist/router/router.d.ts +13 -34
  34. package/dist/router/validation-integration.d.ts +10 -4
  35. package/dist/testing/performance-testing.d.ts +2 -2
  36. package/dist/testing/test-client.d.ts +1 -1
  37. package/dist/testing/websocket-testing.d.ts +1 -1
  38. package/dist/types/middleware-types.d.ts +1 -1
  39. package/dist/types/route-inference.d.ts +9 -2
  40. package/dist/types.d.ts +62 -9
  41. package/dist/utils.d.ts +1 -1
  42. package/package.json +12 -3
  43. package/dist/router/fluent-router.d.ts +0 -315
  44. package/src/auth.ts +0 -469
  45. package/src/cache/lru-cache.ts +0 -457
  46. package/src/cache/middleware-memoization.ts +0 -531
  47. package/src/cache/route-cache-warmer.ts +0 -486
  48. package/src/cache/sqlite-cache.ts +0 -783
  49. package/src/cache/streaming-cache.ts +0 -572
  50. package/src/cli/colors.ts +0 -29
  51. package/src/cli/index.ts +0 -287
  52. package/src/cli/middleware.ts +0 -291
  53. package/src/cli/openapi.ts +0 -407
  54. package/src/cli/router.ts +0 -188
  55. package/src/cli/routes.ts +0 -265
  56. package/src/cli/utils.ts +0 -531
  57. package/src/cli.ts +0 -5
  58. package/src/config.ts +0 -322
  59. package/src/container/container.ts +0 -740
  60. package/src/container/contextual-binding.ts +0 -603
  61. package/src/container/decorators.ts +0 -359
  62. package/src/container/service-provider.ts +0 -596
  63. package/src/development/hot-reload.ts +0 -673
  64. package/src/development/index.ts +0 -499
  65. package/src/development/performance-profiler.ts +0 -717
  66. package/src/development/route-debugger.ts +0 -527
  67. package/src/development/route-inspector.ts +0 -749
  68. package/src/development/typescript-utilities.ts +0 -682
  69. package/src/docs.ts +0 -397
  70. package/src/errors/circuit-breaker.ts +0 -732
  71. package/src/errors/error-handler.ts +0 -569
  72. package/src/errors/error-reporting.ts +0 -672
  73. package/src/errors/exceptions.ts +0 -536
  74. package/src/errors/graceful-degradation.ts +0 -621
  75. package/src/errors/index.ts +0 -21
  76. package/src/errors/router-errors.ts +0 -632
  77. package/src/file-serving/static-files.ts +0 -581
  78. package/src/index.ts +0 -14
  79. package/src/middleware/auth.ts +0 -220
  80. package/src/middleware/content_security_policy.ts +0 -215
  81. package/src/middleware/cors.ts +0 -74
  82. package/src/middleware/csrf.ts +0 -118
  83. package/src/middleware/ddos_protection.ts +0 -255
  84. package/src/middleware/file_security.ts +0 -191
  85. package/src/middleware/file_upload.ts +0 -275
  86. package/src/middleware/helmet.ts +0 -268
  87. package/src/middleware/index.ts +0 -117
  88. package/src/middleware/input_validation.ts +0 -449
  89. package/src/middleware/json_body.ts +0 -37
  90. package/src/middleware/performance_alerting.ts +0 -538
  91. package/src/middleware/performance_dashboard.ts +0 -661
  92. package/src/middleware/performance_monitor.ts +0 -943
  93. package/src/middleware/pipeline.ts +0 -489
  94. package/src/middleware/rate_limit.ts +0 -245
  95. package/src/middleware/request_id.ts +0 -36
  96. package/src/middleware/request_signing.ts +0 -636
  97. package/src/middleware/request_tracer.ts +0 -636
  98. package/src/middleware/response_cache.ts +0 -743
  99. package/src/middleware/security.ts +0 -482
  100. package/src/middleware/security_suite.ts +0 -257
  101. package/src/middleware/session.ts +0 -91
  102. package/src/model-binding/index.ts +0 -18
  103. package/src/model-binding/model-middleware.ts +0 -425
  104. package/src/model-binding/model-registry.ts +0 -550
  105. package/src/model-binding.ts +0 -370
  106. package/src/model-resolver-factory.ts +0 -106
  107. package/src/observability/correlation.ts +0 -691
  108. package/src/observability/health-checks.ts +0 -729
  109. package/src/observability/index.ts +0 -184
  110. package/src/observability/integration.ts +0 -548
  111. package/src/observability/metrics.ts +0 -753
  112. package/src/observability/tracing.ts +0 -638
  113. package/src/optimization/bun-utilities.ts +0 -778
  114. package/src/query-builder-integration.ts +0 -137
  115. package/src/request/context.ts +0 -62
  116. package/src/request/enhanced-request.ts +0 -857
  117. package/src/request/macros.ts +0 -688
  118. package/src/response/macros.ts +0 -665
  119. package/src/response/response-factory.ts +0 -596
  120. package/src/router/api-routes.ts +0 -243
  121. package/src/router/file-based-routing.ts +0 -690
  122. package/src/router/file-streaming.ts +0 -383
  123. package/src/router/fluent-router.ts +0 -927
  124. package/src/router/fluent-routing.ts +0 -797
  125. package/src/router/group-organization.ts +0 -213
  126. package/src/router/handler-resolver.ts +0 -291
  127. package/src/router/http-methods.ts +0 -377
  128. package/src/router/index.ts +0 -187
  129. package/src/router/middleware-groups.ts +0 -222
  130. package/src/router/middleware-integration.ts +0 -399
  131. package/src/router/middleware.ts +0 -231
  132. package/src/router/model-binding.ts +0 -215
  133. package/src/router/optimized-route-matching.ts +0 -253
  134. package/src/router/route-building.ts +0 -221
  135. package/src/router/route-compiler.ts +0 -702
  136. package/src/router/route-matching.ts +0 -349
  137. package/src/router/route-trie.ts +0 -464
  138. package/src/router/router.ts +0 -1604
  139. package/src/router/server.ts +0 -462
  140. package/src/router/validation-integration.ts +0 -443
  141. package/src/router/view-rendering.ts +0 -233
  142. package/src/router/websocket.ts +0 -100
  143. package/src/routing/route-caching.ts +0 -402
  144. package/src/routing/route-throttling.ts +0 -469
  145. package/src/routing/subdomain-routing.ts +0 -492
  146. package/src/session/database-store.ts +0 -109
  147. package/src/session/file-store.ts +0 -148
  148. package/src/session/index.ts +0 -244
  149. package/src/session/memory-store.ts +0 -88
  150. package/src/session/redis-store.ts +0 -93
  151. package/src/streaming/index.ts +0 -17
  152. package/src/streaming/sse-handler.ts +0 -482
  153. package/src/streaming/stream-handler.ts +0 -552
  154. package/src/testing/auth-testing.ts +0 -446
  155. package/src/testing/file-upload-testing.ts +0 -543
  156. package/src/testing/index.ts +0 -10
  157. package/src/testing/middleware-testing.ts +0 -322
  158. package/src/testing/model-binding-testing.ts +0 -645
  159. package/src/testing/performance-testing.ts +0 -738
  160. package/src/testing/test-client.ts +0 -310
  161. package/src/testing/test-request.ts +0 -315
  162. package/src/testing/test-response.ts +0 -332
  163. package/src/testing/types.ts +0 -224
  164. package/src/testing/websocket-testing.ts +0 -590
  165. package/src/types/controller-types.ts +0 -385
  166. package/src/types/core.ts +0 -683
  167. package/src/types/middleware-types.ts +0 -419
  168. package/src/types/request-response-augmentation.ts +0 -489
  169. package/src/types/route-inference.ts +0 -357
  170. package/src/types.ts +0 -2067
  171. package/src/url.ts +0 -126
  172. package/src/utils/index.ts +0 -1
  173. package/src/utils/query-preservation.ts +0 -201
  174. package/src/utils.ts +0 -392
  175. package/src/validation/validator.ts +0 -685
  176. package/src/websocket/clustering.ts +0 -762
@@ -1,688 +0,0 @@
1
- /**
2
- * Request/Response Enhancements - Request Macros
3
- *
4
- * Laravel-style request macros for enhanced request functionality
5
- */
6
-
7
- import type { EnhancedRequest } from '../types'
8
-
9
- export interface RequestMacro {
10
- name: string
11
- handler: (this: EnhancedRequest, ...args: any[]) => any
12
- }
13
-
14
- /**
15
- * Request macro registry
16
- */
17
- class RequestMacroRegistry {
18
- private macros: Map<string, RequestMacro> = new Map()
19
-
20
- /**
21
- * Register a request macro
22
- */
23
- register(name: string, handler: (this: EnhancedRequest, ...args: any[]) => any): void {
24
- this.macros.set(name, { name, handler })
25
- }
26
-
27
- /**
28
- * Get a registered macro
29
- */
30
- get(name: string): RequestMacro | undefined {
31
- return this.macros.get(name)
32
- }
33
-
34
- /**
35
- * Check if macro exists
36
- */
37
- has(name: string): boolean {
38
- return this.macros.has(name)
39
- }
40
-
41
- /**
42
- * Get all registered macros
43
- */
44
- all(): RequestMacro[] {
45
- return Array.from(this.macros.values())
46
- }
47
-
48
- /**
49
- * Remove a macro
50
- */
51
- remove(name: string): boolean {
52
- return this.macros.delete(name)
53
- }
54
-
55
- /**
56
- * Clear all macros
57
- */
58
- clear(): void {
59
- this.macros.clear()
60
- }
61
- }
62
-
63
- /**
64
- * Global request macro registry
65
- */
66
- export const requestMacroRegistry: RequestMacroRegistry = new RequestMacroRegistry()
67
-
68
- /**
69
- * Request class with macro support
70
- */
71
- export class RequestWithMacros {
72
- /**
73
- * Register a request macro
74
- */
75
- static macro(name: string, handler: (this: EnhancedRequest, ...args: any[]) => any): void {
76
- requestMacroRegistry.register(name, handler)
77
- }
78
-
79
- /**
80
- * Apply macros to a request object
81
- */
82
- static applyMacros(request: EnhancedRequest): EnhancedRequest {
83
- const macros = requestMacroRegistry.all()
84
-
85
- macros.forEach((macro) => {
86
- ;(request as any)[macro.name] = macro.handler.bind(request)
87
- })
88
-
89
- return request
90
- }
91
-
92
- /**
93
- * Check if macro exists
94
- */
95
- static hasMacro(name: string): boolean {
96
- return requestMacroRegistry.has(name)
97
- }
98
- }
99
-
100
- /**
101
- * Built-in request macros
102
- */
103
- export const BuiltInRequestMacros = {
104
- /**
105
- * Check if request wants JSON response
106
- */
107
- wantsJson(this: EnhancedRequest): boolean {
108
- const accept = this.headers.get('accept') || ''
109
- return accept.includes('application/json') || accept.includes('json')
110
- },
111
-
112
- /**
113
- * Check if request wants HTML response
114
- */
115
- wantsHtml(this: EnhancedRequest): boolean {
116
- const accept = this.headers.get('accept') || ''
117
- return accept.includes('text/html') || accept.includes('html')
118
- },
119
-
120
- /**
121
- * Check if request wants XML response
122
- */
123
- wantsXml(this: EnhancedRequest): boolean {
124
- const accept = this.headers.get('accept') || ''
125
- return accept.includes('application/xml') || accept.includes('xml')
126
- },
127
-
128
- /**
129
- * Check if request expects JSON response
130
- */
131
- expectsJson(this: EnhancedRequest): boolean {
132
- return this.wantsJson() || this.isAjax() || this.isPjax()
133
- },
134
-
135
- /**
136
- * Check if request is AJAX
137
- */
138
- isAjax(this: EnhancedRequest): boolean {
139
- return this.headers.get('x-requested-with') === 'XMLHttpRequest'
140
- },
141
-
142
- /**
143
- * Check if request is PJAX
144
- */
145
- isPjax(this: EnhancedRequest): boolean {
146
- return this.headers.get('x-pjax') === 'true'
147
- },
148
-
149
- /**
150
- * Check if request is from mobile device
151
- */
152
- isMobile(this: EnhancedRequest): boolean {
153
- const userAgent = this.headers.get('user-agent') || ''
154
- const mobileRegex = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
155
- return mobileRegex.test(userAgent)
156
- },
157
-
158
- /**
159
- * Check if request is from bot/crawler
160
- */
161
- isBot(this: EnhancedRequest): boolean {
162
- const userAgent = this.headers.get('user-agent') || ''
163
- const botRegex = /bot|crawler|spider|crawling/i
164
- return botRegex.test(userAgent)
165
- },
166
-
167
- /**
168
- * Check if request is secure (HTTPS)
169
- */
170
- isSecure(this: EnhancedRequest): boolean {
171
- return this.url.startsWith('https://')
172
- || this.headers.get('x-forwarded-proto') === 'https'
173
- || this.headers.get('x-forwarded-ssl') === 'on'
174
- },
175
-
176
- /**
177
- * Get client IP address
178
- */
179
- ip(this: EnhancedRequest): string {
180
- return this.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
181
- || this.headers.get('x-real-ip')
182
- || this.headers.get('cf-connecting-ip')
183
- || this.headers.get('x-client-ip')
184
- || 'unknown'
185
- },
186
-
187
- /**
188
- * Get all client IPs (including proxies)
189
- */
190
- ips(this: EnhancedRequest): string[] {
191
- const forwardedFor = this.headers.get('x-forwarded-for')
192
- if (forwardedFor) {
193
- return forwardedFor.split(',').map(ip => ip.trim())
194
- }
195
- return [BuiltInRequestMacros.ip.call(this)]
196
- },
197
-
198
- /**
199
- * Get user agent
200
- */
201
- userAgent(this: EnhancedRequest): string {
202
- return this.headers.get('user-agent') || ''
203
- },
204
-
205
- /**
206
- * Get referer URL
207
- */
208
- referer(this: EnhancedRequest): string | null {
209
- return this.headers.get('referer') || this.headers.get('referrer') || null
210
- },
211
-
212
- /**
213
- * Get authorization header
214
- */
215
- bearerToken(this: EnhancedRequest): string | null {
216
- const auth = this.headers.get('authorization')
217
- if (auth && auth.startsWith('Bearer ')) {
218
- return auth.substring(7)
219
- }
220
- return null
221
- },
222
-
223
- /**
224
- * Get basic auth credentials
225
- */
226
- basicAuth(this: EnhancedRequest): { username: string, password: string } | null {
227
- const auth = this.headers.get('authorization')
228
- if (auth && auth.startsWith('Basic ')) {
229
- try {
230
- const decoded = atob(auth.substring(6))
231
- const [username, password] = decoded.split(':')
232
- return { username, password }
233
- }
234
- catch {
235
- return null
236
- }
237
- }
238
- return null
239
- },
240
-
241
- /**
242
- * Check if request has specific header
243
- */
244
- hasHeader(this: EnhancedRequest, name: string): boolean {
245
- return this.headers.has(name.toLowerCase())
246
- },
247
-
248
- /**
249
- * Get header value with default
250
- */
251
- header(this: EnhancedRequest, name: string, defaultValue?: string): string | null {
252
- return this.headers.get(name.toLowerCase()) || defaultValue || null
253
- },
254
-
255
- /**
256
- * Get all headers as object
257
- */
258
- allHeaders(this: EnhancedRequest): Record<string, string> {
259
- const headers: Record<string, string> = {}
260
- this.headers.forEach((value, key) => {
261
- headers[key] = value
262
- })
263
- return headers
264
- },
265
-
266
- /**
267
- * Get input value from query, body, or params
268
- */
269
- input(this: EnhancedRequest, key: string, defaultValue?: any): any {
270
- // Check validated data first
271
- if (this.validated && key in this.validated) {
272
- return this.validated[key]
273
- }
274
-
275
- // Check query parameters
276
- if (this.query && key in this.query) {
277
- return this.query[key]
278
- }
279
-
280
- // Check route parameters
281
- if (this.params && key in this.params) {
282
- return this.params[key]
283
- }
284
-
285
- // Check JSON body
286
- if (this.jsonBody && typeof this.jsonBody === 'object' && key in this.jsonBody) {
287
- return (this.jsonBody as Record<string, unknown>)[key]
288
- }
289
-
290
- // Check form body
291
- if (this.formBody && typeof this.formBody === 'object' && key in this.formBody) {
292
- return (this.formBody as Record<string, unknown>)[key]
293
- }
294
-
295
- return defaultValue
296
- },
297
-
298
- /**
299
- * Get all input data
300
- */
301
- all(this: EnhancedRequest): Record<string, unknown> {
302
- return {
303
- ...this.params,
304
- ...this.query,
305
- ...(typeof this.jsonBody === 'object' && this.jsonBody !== null ? this.jsonBody : {}),
306
- ...(typeof this.formBody === 'object' && this.formBody !== null ? this.formBody : {}),
307
- ...(typeof this.validated === 'object' && this.validated !== null ? this.validated : {}),
308
- }
309
- },
310
-
311
- /**
312
- * Get only specified input keys
313
- */
314
- only(this: EnhancedRequest, keys: string[]): Record<string, any> {
315
- const all = this.all()
316
- const result: Record<string, any> = {}
317
-
318
- keys.forEach((key) => {
319
- if (key in all) {
320
- result[key] = all[key]
321
- }
322
- })
323
-
324
- return result
325
- },
326
-
327
- /**
328
- * Get all input except specified keys
329
- */
330
- except(this: EnhancedRequest, keys: string[]): Record<string, any> {
331
- const all = this.all()
332
- const result: Record<string, any> = {}
333
-
334
- Object.keys(all).forEach((key) => {
335
- if (!keys.includes(key)) {
336
- result[key] = all[key]
337
- }
338
- })
339
-
340
- return result
341
- },
342
-
343
- /**
344
- * Check if input has specific key
345
- */
346
- has(this: EnhancedRequest, key: string): boolean {
347
- const all = this.all()
348
- return key in all && all[key] !== undefined && all[key] !== null && all[key] !== ''
349
- },
350
-
351
- /**
352
- * Check if input has any of the specified keys
353
- */
354
- hasAny(this: EnhancedRequest, keys: string[]): boolean {
355
- return keys.some(key => this.has(key))
356
- },
357
-
358
- /**
359
- * Check if input is missing specific key
360
- */
361
- missing(this: EnhancedRequest, key: string): boolean {
362
- return !this.has(key)
363
- },
364
-
365
- /**
366
- * Check if input is filled (not empty)
367
- */
368
- filled(this: EnhancedRequest, key: string): boolean {
369
- const value = this.input(key)
370
- if (value === null || value === undefined)
371
- return false
372
- if (typeof value === 'string')
373
- return value.trim().length > 0
374
- if (Array.isArray(value))
375
- return value.length > 0
376
- if (typeof value === 'object')
377
- return Object.keys(value).length > 0
378
- return true
379
- },
380
-
381
- /**
382
- * Get query parameter
383
- */
384
- getQuery(this: EnhancedRequest, key?: string, defaultValue?: any): any {
385
- if (!key)
386
- return this.query || {}
387
- return this.query?.[key] || defaultValue
388
- },
389
-
390
- /**
391
- * Get route parameter
392
- */
393
- param(this: EnhancedRequest, key: string, defaultValue?: any): any {
394
- return this.params?.[key] || defaultValue
395
- },
396
-
397
- /**
398
- * Get cookie value
399
- */
400
- cookie(this: EnhancedRequest, name: string, defaultValue?: string): string | null {
401
- const cookies = this.headers.get('cookie')
402
- if (!cookies)
403
- return defaultValue || null
404
-
405
- const cookieArray = cookies.split(';')
406
- for (const cookie of cookieArray) {
407
- const [key, value] = cookie.trim().split('=')
408
- if (key === name) {
409
- return decodeURIComponent(value)
410
- }
411
- }
412
-
413
- return defaultValue || null
414
- },
415
-
416
- /**
417
- * Get all cookies
418
- */
419
- cookies(this: EnhancedRequest): Record<string, string> {
420
- const cookies: Record<string, string> = {}
421
- const cookieHeader = this.headers.get('cookie')
422
-
423
- if (cookieHeader) {
424
- cookieHeader.split(';').forEach((cookie) => {
425
- const [key, value] = cookie.trim().split('=')
426
- if (key && value) {
427
- cookies[key] = decodeURIComponent(value)
428
- }
429
- })
430
- }
431
-
432
- return cookies
433
- },
434
-
435
- /**
436
- * Get file from upload
437
- */
438
- file(this: EnhancedRequest, name: string): any {
439
- return this.files?.find(f => f.fieldName === name)
440
- },
441
-
442
- /**
443
- * Check if request has file upload
444
- */
445
- hasFile(this: EnhancedRequest, name: string): boolean {
446
- return !!this.files?.find(f => f.fieldName === name)
447
- },
448
-
449
- /**
450
- * Get request path without query string
451
- */
452
- path(this: EnhancedRequest): string {
453
- return new URL(this.url).pathname
454
- },
455
-
456
- /**
457
- * Get request URL
458
- */
459
- fullUrl(this: EnhancedRequest): string {
460
- return this.url
461
- },
462
-
463
- /**
464
- * Get request root URL
465
- */
466
- root(this: EnhancedRequest): string {
467
- const url = new URL(this.url)
468
- return `${url.protocol}//${url.host}`
469
- },
470
-
471
- /**
472
- * Check if path matches pattern
473
- */
474
- is(this: EnhancedRequest, pattern: string): boolean {
475
- const path = this.path()
476
-
477
- // Convert pattern to regex
478
- const regexPattern = pattern
479
- .replace(/\*/g, '.*')
480
- .replace(/\?/g, '.')
481
-
482
- const regex = new RegExp(`^${regexPattern}$`)
483
- return regex.test(path)
484
- },
485
-
486
- /**
487
- * Get route name (if available)
488
- */
489
- route(this: EnhancedRequest): string | null {
490
- return (this as any).routeName || null
491
- },
492
-
493
- /**
494
- * Get request fingerprint for caching
495
- */
496
- fingerprint(this: EnhancedRequest): string {
497
- const url = new URL(this.url)
498
- const data = {
499
- method: this.method,
500
- path: url.pathname,
501
- query: url.search,
502
- ip: BuiltInRequestMacros.ip.call(this),
503
- }
504
-
505
- return btoa(JSON.stringify(data))
506
- },
507
-
508
- /**
509
- * Get request signature for security
510
- */
511
- signature(this: EnhancedRequest, secret: string): string {
512
- const data = `${this.method}${this.url}${BuiltInRequestMacros.ip.call(this)}${secret}`
513
-
514
- // Simple hash function (in production, use crypto)
515
- let hash = 0
516
- for (let i = 0; i < data.length; i++) {
517
- const char = data.charCodeAt(i)
518
- hash = ((hash << 5) - hash) + char
519
- hash = hash & hash // Convert to 32-bit integer
520
- }
521
-
522
- return Math.abs(hash).toString(36)
523
- },
524
-
525
- /**
526
- * Merge additional data into request
527
- */
528
- merge(this: EnhancedRequest, data: Record<string, any>): void {
529
- if (!this.validated) {
530
- this.validated = {}
531
- }
532
- Object.assign(this.validated, data)
533
- },
534
-
535
- /**
536
- * Replace request data
537
- */
538
- replace(this: EnhancedRequest, data: Record<string, any>): void {
539
- this.validated = { ...data }
540
- },
541
-
542
- /**
543
- * Get request age in milliseconds
544
- */
545
- age(this: EnhancedRequest): number {
546
- const startTime = (this as any).startTime || Date.now()
547
- return Date.now() - startTime
548
- },
549
-
550
- /**
551
- * Check if request is from trusted proxy
552
- */
553
- isFromTrustedProxy(this: EnhancedRequest, trustedProxies: string[] = []): boolean {
554
- const ip = BuiltInRequestMacros.ip.call(this)
555
- return trustedProxies.includes(ip)
556
- },
557
-
558
- /**
559
- * Get request content length
560
- */
561
- contentLength(this: EnhancedRequest): number {
562
- const length = this.headers.get('content-length')
563
- return length ? Number.parseInt(length, 10) : 0
564
- },
565
-
566
- /**
567
- * Get request content type
568
- */
569
- contentType(this: EnhancedRequest): string | null {
570
- return this.headers.get('content-type')
571
- },
572
-
573
- /**
574
- * Check if request content type matches
575
- */
576
- isContentType(this: EnhancedRequest, type: string): boolean {
577
- const contentType = this.contentType()
578
- return contentType ? contentType.includes(type) : false
579
- },
580
- }
581
-
582
- /**
583
- * Register all built-in macros
584
- */
585
- export function registerBuiltInRequestMacros(): void {
586
- Object.entries(BuiltInRequestMacros).forEach(([name, handler]) => {
587
- RequestWithMacros.macro(name, handler)
588
- })
589
- }
590
-
591
- /**
592
- * Request macro factory functions
593
- */
594
- export const RequestMacroFactory = {
595
- /**
596
- * Create input validation macro
597
- */
598
- validateInput: (_rules: Record<string, string>) => {
599
- // eslint-disable-next-line pickier/no-unused-vars
600
- return function (this: EnhancedRequest): boolean {
601
- // This would integrate with the validation system
602
- // For now, return true as placeholder
603
- return true
604
- }
605
- },
606
-
607
- /**
608
- * Create custom header checker macro
609
- */
610
- hasCustomHeader: (headerName: string) => {
611
- return function (this: EnhancedRequest): boolean {
612
- return this.hasHeader(headerName)
613
- }
614
- },
615
-
616
- /**
617
- * Create role checker macro
618
- */
619
- hasRole: (role: string) => {
620
- return function (this: EnhancedRequest): boolean {
621
- const user = (this as any).user
622
- return user?.roles?.includes(role) || false
623
- }
624
- },
625
-
626
- /**
627
- * Create permission checker macro
628
- */
629
- can: (permission: string) => {
630
- return function (this: EnhancedRequest): boolean {
631
- const user = (this as any).user
632
- return user?.permissions?.includes(permission) || false
633
- }
634
- },
635
- }
636
-
637
- /**
638
- * Request helper utilities
639
- */
640
- export const RequestHelpers = {
641
- /**
642
- * Apply macros to request
643
- */
644
- withMacros: (request: EnhancedRequest): EnhancedRequest => {
645
- return RequestWithMacros.applyMacros(request)
646
- },
647
-
648
- /**
649
- * Create request from URL and options
650
- */
651
- create: (url: string, options: RequestInit = {}): EnhancedRequest => {
652
- const request = new Request(url, options) as EnhancedRequest
653
- return RequestWithMacros.applyMacros(request)
654
- },
655
-
656
- /**
657
- * Parse query string to object
658
- */
659
- parseQuery: (queryString: string): Record<string, string> => {
660
- const params = new URLSearchParams(queryString)
661
- const result: Record<string, string> = {}
662
-
663
- params.forEach((value, key) => {
664
- result[key] = value
665
- })
666
-
667
- return result
668
- },
669
-
670
- /**
671
- * Parse cookies from header
672
- */
673
- parseCookies: (cookieHeader: string): Record<string, string> => {
674
- const cookies: Record<string, string> = {}
675
-
676
- cookieHeader.split(';').forEach((cookie) => {
677
- const [key, value] = cookie.trim().split('=')
678
- if (key && value) {
679
- cookies[key] = decodeURIComponent(value)
680
- }
681
- })
682
-
683
- return cookies
684
- },
685
- }
686
-
687
- // Auto-register built-in macros
688
- registerBuiltInRequestMacros()