@stacksjs/bun-router 0.0.17 → 0.0.19

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 (153) hide show
  1. package/dist/chunk-2mx7fq49.js +4 -0
  2. package/dist/chunk-a8p83068.js +103 -0
  3. package/dist/{chunk-cgptvjdf.js → chunk-y3xq6wq6.js} +1 -1
  4. package/dist/cli.js +4 -3
  5. package/dist/container/index.js +1149 -211
  6. package/dist/container/service-provider.d.ts +3 -3
  7. package/dist/development/performance-profiler.d.ts +3 -3
  8. package/dist/development/route-debugger.d.ts +1 -1
  9. package/dist/development/route-inspector.d.ts +2 -2
  10. package/dist/index.js +369 -2209
  11. package/dist/observability/integration.d.ts +1 -1
  12. package/dist/response/macros.d.ts +1 -1
  13. package/dist/router/index.d.ts +7 -7
  14. package/dist/router/router.d.ts +0 -32
  15. package/dist/testing/performance-testing.d.ts +2 -2
  16. package/dist/testing/test-client.d.ts +1 -1
  17. package/dist/testing/websocket-testing.d.ts +1 -1
  18. package/package.json +7 -7
  19. package/dist/chunk-1ahs68ys.js +0 -18
  20. package/dist/chunk-g3ybefhg.js +0 -1923
  21. package/src/auth.ts +0 -489
  22. package/src/cache/lru-cache.ts +0 -457
  23. package/src/cache/middleware-memoization.ts +0 -531
  24. package/src/cache/route-cache-warmer.ts +0 -486
  25. package/src/cache/sqlite-cache.ts +0 -783
  26. package/src/cache/streaming-cache.ts +0 -572
  27. package/src/cli/colors.ts +0 -29
  28. package/src/cli/index.ts +0 -287
  29. package/src/cli/middleware.ts +0 -291
  30. package/src/cli/openapi.ts +0 -407
  31. package/src/cli/router.ts +0 -188
  32. package/src/cli/routes.ts +0 -265
  33. package/src/cli/utils.ts +0 -531
  34. package/src/cli.ts +0 -5
  35. package/src/config.ts +0 -322
  36. package/src/container/container.ts +0 -786
  37. package/src/container/contextual-binding.ts +0 -606
  38. package/src/container/decorators.ts +0 -368
  39. package/src/container/index.ts +0 -49
  40. package/src/container/service-provider.ts +0 -596
  41. package/src/development/hot-reload.ts +0 -673
  42. package/src/development/index.ts +0 -499
  43. package/src/development/performance-profiler.ts +0 -717
  44. package/src/development/route-debugger.ts +0 -527
  45. package/src/development/route-inspector.ts +0 -749
  46. package/src/development/typescript-utilities.ts +0 -682
  47. package/src/docs.ts +0 -397
  48. package/src/errors/circuit-breaker.ts +0 -733
  49. package/src/errors/error-handler.ts +0 -569
  50. package/src/errors/error-reporting.ts +0 -672
  51. package/src/errors/exceptions.ts +0 -536
  52. package/src/errors/graceful-degradation.ts +0 -623
  53. package/src/errors/index.ts +0 -21
  54. package/src/errors/router-errors.ts +0 -632
  55. package/src/file-serving/static-files.ts +0 -718
  56. package/src/index.ts +0 -66
  57. package/src/middleware/auth.ts +0 -220
  58. package/src/middleware/content_security_policy.ts +0 -215
  59. package/src/middleware/cors.ts +0 -142
  60. package/src/middleware/csrf.ts +0 -166
  61. package/src/middleware/ddos_protection.ts +0 -279
  62. package/src/middleware/file_security.ts +0 -191
  63. package/src/middleware/file_upload.ts +0 -275
  64. package/src/middleware/helmet.ts +0 -268
  65. package/src/middleware/index.ts +0 -117
  66. package/src/middleware/input_validation.ts +0 -453
  67. package/src/middleware/json_body.ts +0 -37
  68. package/src/middleware/performance_alerting.ts +0 -540
  69. package/src/middleware/performance_dashboard.ts +0 -661
  70. package/src/middleware/performance_monitor.ts +0 -945
  71. package/src/middleware/pipeline.ts +0 -489
  72. package/src/middleware/rate_limit.ts +0 -251
  73. package/src/middleware/request_id.ts +0 -36
  74. package/src/middleware/request_signing.ts +0 -638
  75. package/src/middleware/request_tracer.ts +0 -653
  76. package/src/middleware/response_cache.ts +0 -778
  77. package/src/middleware/security.ts +0 -482
  78. package/src/middleware/security_suite.ts +0 -257
  79. package/src/middleware/session.ts +0 -91
  80. package/src/model-binding/index.ts +0 -18
  81. package/src/model-binding/model-middleware.ts +0 -425
  82. package/src/model-binding/model-registry.ts +0 -550
  83. package/src/model-binding.ts +0 -370
  84. package/src/model-resolver-factory.ts +0 -106
  85. package/src/observability/correlation.ts +0 -691
  86. package/src/observability/health-checks.ts +0 -729
  87. package/src/observability/index.ts +0 -184
  88. package/src/observability/integration.ts +0 -548
  89. package/src/observability/metrics.ts +0 -753
  90. package/src/observability/tracing.ts +0 -638
  91. package/src/optimization/bun-utilities.ts +0 -778
  92. package/src/query-builder-integration.ts +0 -137
  93. package/src/request/context.ts +0 -109
  94. package/src/request/enhanced-request.ts +0 -876
  95. package/src/request/macros.ts +0 -822
  96. package/src/response/macros.ts +0 -665
  97. package/src/response/response-factory.ts +0 -596
  98. package/src/router/api-routes.ts +0 -243
  99. package/src/router/file-based-routing.ts +0 -697
  100. package/src/router/file-streaming.ts +0 -383
  101. package/src/router/fluent-routing.ts +0 -866
  102. package/src/router/group-organization.ts +0 -229
  103. package/src/router/handler-resolver.ts +0 -327
  104. package/src/router/http-methods.ts +0 -407
  105. package/src/router/index.ts +0 -193
  106. package/src/router/middleware-groups.ts +0 -222
  107. package/src/router/middleware-integration.ts +0 -399
  108. package/src/router/middleware.ts +0 -234
  109. package/src/router/model-binding.ts +0 -215
  110. package/src/router/optimized-route-matching.ts +0 -302
  111. package/src/router/route-building.ts +0 -221
  112. package/src/router/route-compiler.ts +0 -691
  113. package/src/router/route-matching.ts +0 -368
  114. package/src/router/route-trie.ts +0 -450
  115. package/src/router/router.ts +0 -1616
  116. package/src/router/server.ts +0 -684
  117. package/src/router/validation-integration.ts +0 -449
  118. package/src/router/view-rendering.ts +0 -233
  119. package/src/router/websocket.ts +0 -100
  120. package/src/routing/route-caching.ts +0 -402
  121. package/src/routing/route-throttling.ts +0 -469
  122. package/src/routing/subdomain-routing.ts +0 -492
  123. package/src/session/database-store.ts +0 -109
  124. package/src/session/file-store.ts +0 -148
  125. package/src/session/index.ts +0 -244
  126. package/src/session/memory-store.ts +0 -88
  127. package/src/session/redis-store.ts +0 -93
  128. package/src/streaming/index.ts +0 -17
  129. package/src/streaming/sse-handler.ts +0 -482
  130. package/src/streaming/stream-handler.ts +0 -552
  131. package/src/testing/auth-testing.ts +0 -448
  132. package/src/testing/file-upload-testing.ts +0 -543
  133. package/src/testing/index.ts +0 -10
  134. package/src/testing/middleware-testing.ts +0 -322
  135. package/src/testing/model-binding-testing.ts +0 -645
  136. package/src/testing/performance-testing.ts +0 -738
  137. package/src/testing/test-client.ts +0 -310
  138. package/src/testing/test-request.ts +0 -315
  139. package/src/testing/test-response.ts +0 -332
  140. package/src/testing/types.ts +0 -224
  141. package/src/testing/websocket-testing.ts +0 -590
  142. package/src/types/controller-types.ts +0 -385
  143. package/src/types/core.ts +0 -683
  144. package/src/types/middleware-types.ts +0 -422
  145. package/src/types/request-response-augmentation.ts +0 -489
  146. package/src/types/route-inference.ts +0 -372
  147. package/src/types.ts +0 -2123
  148. package/src/url.ts +0 -131
  149. package/src/utils/index.ts +0 -1
  150. package/src/utils/query-preservation.ts +0 -201
  151. package/src/utils.ts +0 -395
  152. package/src/validation/validator.ts +0 -688
  153. package/src/websocket/clustering.ts +0 -769
@@ -1,552 +0,0 @@
1
- import type { BunFile } from 'bun'
2
- import type {
3
- BufferedStreamConfig,
4
- ContentType,
5
- DirectStreamConfig,
6
- EnhancedRequest,
7
- ResponseStatus,
8
- StreamingCompression,
9
- StreamingFormat,
10
- } from '../types'
11
-
12
- /**
13
- * Base streaming configuration
14
- */
15
- export interface BaseStreamConfig {
16
- contentType?: ContentType
17
- headers?: Record<string, string>
18
- status?: ResponseStatus
19
- bufferSize?: number
20
- enableCompression?: boolean
21
- compressionType?: StreamingCompression
22
- }
23
-
24
- /**
25
- * Stream response wrapper
26
- */
27
- export class StreamResponse {
28
- private stream: ReadableStream
29
- private headers: Headers
30
- private status: ResponseStatus
31
-
32
- constructor(
33
- stream: ReadableStream,
34
- options: {
35
- headers?: Headers | Record<string, string>
36
- status?: ResponseStatus
37
- contentType?: ContentType
38
- } = {},
39
- ) {
40
- this.stream = stream
41
- this.status = options.status || 200
42
- this.headers = new Headers(options.headers)
43
-
44
- if (options.contentType) {
45
- this.headers.set('Content-Type', options.contentType)
46
- }
47
- }
48
-
49
- toResponse(): Response {
50
- return new Response(this.stream, {
51
- status: this.status,
52
- headers: this.headers,
53
- })
54
- }
55
-
56
- addHeader(name: string, value: string): this {
57
- this.headers.set(name, value)
58
- return this
59
- }
60
-
61
- setContentType(contentType: ContentType): this {
62
- this.headers.set('Content-Type', contentType)
63
- return this
64
- }
65
-
66
- setStatus(status: ResponseStatus): this {
67
- this.status = status
68
- return this
69
- }
70
- }
71
-
72
- /**
73
- * Generic streaming handler
74
- */
75
- export class StreamHandler {
76
- /**
77
- * Create a direct stream response
78
- */
79
- static direct<T>(
80
- data: T[] | AsyncIterable<T>,
81
- config: DirectStreamConfig = {},
82
- ): StreamResponse {
83
- const {
84
- format = 'json',
85
- delimiter = '\n',
86
- contentType,
87
- headers = {},
88
- status = 200,
89
- enableCompression: _enableCompression = false,
90
- } = config
91
-
92
- const stream = new ReadableStream({
93
- async start(controller) {
94
- try {
95
- const encoder = new TextEncoder()
96
- let isFirst = true
97
-
98
- for await (const item of Array.isArray(data) ? data : data) {
99
- if (!isFirst && delimiter) {
100
- controller.enqueue(encoder.encode(delimiter))
101
- }
102
-
103
- const formatted = StreamHandler.formatData(item, format)
104
- controller.enqueue(encoder.encode(formatted))
105
- isFirst = false
106
- }
107
-
108
- controller.close()
109
- }
110
- catch (error) {
111
- controller.error(error)
112
- }
113
- },
114
- })
115
-
116
- const responseHeaders = new Headers(headers)
117
- if (contentType) {
118
- responseHeaders.set('Content-Type', contentType)
119
- }
120
- else {
121
- responseHeaders.set('Content-Type', StreamHandler.getContentTypeForFormat(format))
122
- }
123
-
124
- return new StreamResponse(stream, {
125
- headers: responseHeaders,
126
- status,
127
- })
128
- }
129
-
130
- /**
131
- * Create a buffered stream response
132
- */
133
- static buffered<T>(
134
- data: T[] | AsyncIterable<T>,
135
- config: BufferedStreamConfig = {},
136
- ): StreamResponse {
137
- const {
138
- format = 'json',
139
- bufferSize = 1024,
140
- flushInterval = 100,
141
- contentType,
142
- headers = {},
143
- status = 200,
144
- } = config
145
-
146
- const stream = new ReadableStream({
147
- async start(controller) {
148
- try {
149
- const encoder = new TextEncoder()
150
- let buffer: string[] = []
151
- let bufferLength = 0
152
-
153
- const flush = () => {
154
- if (buffer.length > 0) {
155
- const chunk = buffer.join('')
156
- controller.enqueue(encoder.encode(chunk))
157
- buffer = []
158
- bufferLength = 0
159
- }
160
- }
161
-
162
- // Set up flush interval
163
- const flushTimer = setInterval(flush, flushInterval)
164
-
165
- try {
166
- for await (const item of Array.isArray(data) ? data : data) {
167
- const formatted = StreamHandler.formatData(item, format)
168
- buffer.push(formatted)
169
- bufferLength += formatted.length
170
-
171
- if (bufferLength >= bufferSize) {
172
- flush()
173
- }
174
- }
175
-
176
- // Final flush
177
- flush()
178
- }
179
- finally {
180
- clearInterval(flushTimer)
181
- }
182
-
183
- controller.close()
184
- }
185
- catch (error) {
186
- controller.error(error)
187
- }
188
- },
189
- })
190
-
191
- const responseHeaders = new Headers(headers)
192
- if (contentType) {
193
- responseHeaders.set('Content-Type', contentType)
194
- }
195
- else {
196
- responseHeaders.set('Content-Type', StreamHandler.getContentTypeForFormat(format))
197
- }
198
-
199
- return new StreamResponse(stream, {
200
- headers: responseHeaders,
201
- status,
202
- })
203
- }
204
-
205
- /**
206
- * Create a JSON stream response
207
- */
208
- static json<T>(
209
- data: T[] | AsyncIterable<T>,
210
- config: BaseStreamConfig = {},
211
- ): StreamResponse {
212
- return StreamHandler.direct(data, {
213
- ...config,
214
- format: 'json',
215
- contentType: config.contentType || 'application/json',
216
- })
217
- }
218
-
219
- /**
220
- * Create a text stream response
221
- */
222
- static text(
223
- data: string[] | AsyncIterable<string>,
224
- config: BaseStreamConfig = {},
225
- ): StreamResponse {
226
- return StreamHandler.direct(data, {
227
- ...config,
228
- format: 'text',
229
- contentType: config.contentType || 'text/plain',
230
- })
231
- }
232
-
233
- /**
234
- * Create a CSV stream response
235
- */
236
- static csv<T extends Record<string, any>>(
237
- data: T[] | AsyncIterable<T>,
238
- config: BaseStreamConfig & { csvHeaders?: string[] } = {},
239
- ): StreamResponse {
240
- const { csvHeaders, ...streamConfig } = config
241
-
242
- const stream = new ReadableStream({
243
- async start(controller) {
244
- try {
245
- const encoder = new TextEncoder()
246
- const _isFirst = true
247
-
248
- // Write CSV headers if provided
249
- if (csvHeaders && csvHeaders.length > 0) {
250
- controller.enqueue(encoder.encode(`${csvHeaders.join(',')}\n`))
251
- }
252
-
253
- for await (const item of Array.isArray(data) ? data : data) {
254
- const csvRow = StreamHandler.objectToCsvRow(item, csvHeaders)
255
- controller.enqueue(encoder.encode(`${csvRow}\n`))
256
- }
257
-
258
- controller.close()
259
- }
260
- catch (error) {
261
- controller.error(error)
262
- }
263
- },
264
- })
265
-
266
- return new StreamResponse(stream, {
267
- headers: streamConfig.headers,
268
- status: streamConfig.status,
269
- contentType: streamConfig.contentType || 'text/csv',
270
- })
271
- }
272
-
273
- /**
274
- * Format data according to the specified format
275
- */
276
- private static formatData<T>(data: T, format: StreamingFormat): string {
277
- switch (format) {
278
- case 'json':
279
- return JSON.stringify(data)
280
- case 'text':
281
- return String(data)
282
- case 'csv':
283
- return typeof data === 'object' ? StreamHandler.objectToCsvRow(data as Record<string, any>) : String(data)
284
- case 'ndjson':
285
- return JSON.stringify(data)
286
- default:
287
- return JSON.stringify(data)
288
- }
289
- }
290
-
291
- /**
292
- * Convert object to CSV row
293
- */
294
- private static objectToCsvRow(obj: Record<string, any>, headers?: string[]): string {
295
- const keys = headers || Object.keys(obj)
296
- // eslint-disable-next-line pickier/no-unused-vars
297
- return keys.map((key) => {
298
- const value = obj[key]
299
- const stringValue = value === null || value === undefined ? '' : String(value)
300
- // Escape quotes and wrap in quotes if contains comma, quote, or newline
301
- if (stringValue.includes(',') || stringValue.includes('"') || stringValue.includes('\n')) {
302
- return `"${stringValue.replace(/"/g, '""')}"`
303
- }
304
- return stringValue
305
- }).join(',')
306
- }
307
-
308
- /**
309
- * Get appropriate content type for format
310
- */
311
- private static getContentTypeForFormat(format: StreamingFormat): ContentType {
312
- switch (format) {
313
- case 'json':
314
- case 'ndjson':
315
- return 'application/json'
316
- case 'text':
317
- return 'text/plain'
318
- case 'csv':
319
- return 'text/csv'
320
- default:
321
- return 'application/json'
322
- }
323
- }
324
- }
325
-
326
- /**
327
- * Stream utilities
328
- */
329
- export const StreamUtils = {
330
- /**
331
- * Create a transform stream
332
- */
333
- transform<T, U>(
334
- transformFn: (chunk: T) => U | Promise<U>,
335
- ): TransformStream<T, U> {
336
- return new TransformStream({
337
- async transform(chunk, controller) {
338
- try {
339
- const transformed = await transformFn(chunk)
340
- controller.enqueue(transformed)
341
- }
342
- catch (error) {
343
- controller.error(error)
344
- }
345
- },
346
- })
347
- },
348
-
349
- /**
350
- * Create a filter stream
351
- */
352
- filter<T>(
353
- filterFn: (chunk: T) => boolean | Promise<boolean>,
354
- ): TransformStream<T, T> {
355
- return new TransformStream({
356
- async transform(chunk, controller) {
357
- try {
358
- const shouldInclude = await filterFn(chunk)
359
- if (shouldInclude) {
360
- controller.enqueue(chunk)
361
- }
362
- }
363
- catch (error) {
364
- controller.error(error)
365
- }
366
- },
367
- })
368
- },
369
-
370
- /**
371
- * Create a batch stream
372
- */
373
- batch<T>(batchSize: number): TransformStream<T, T[]> {
374
- let batch: T[] = []
375
-
376
- return new TransformStream({
377
- transform(chunk, controller) {
378
- batch.push(chunk)
379
- if (batch.length >= batchSize) {
380
- controller.enqueue([...batch])
381
- batch = []
382
- }
383
- },
384
- flush(controller) {
385
- if (batch.length > 0) {
386
- controller.enqueue(batch)
387
- }
388
- },
389
- })
390
- },
391
-
392
- /**
393
- * Create a delay stream
394
- */
395
- delay<T>(ms: number): TransformStream<T, T> {
396
- return new TransformStream({
397
- async transform(chunk, controller) {
398
- await new Promise(resolve => setTimeout(resolve, ms))
399
- controller.enqueue(chunk)
400
- },
401
- })
402
- },
403
-
404
- /**
405
- * Create a rate limit stream
406
- */
407
- rateLimit<T>(itemsPerSecond: number): TransformStream<T, T> {
408
- const interval = 1000 / itemsPerSecond
409
- let lastEmit = 0
410
-
411
- return new TransformStream({
412
- async transform(chunk, controller) {
413
- const now = Date.now()
414
- const timeSinceLastEmit = now - lastEmit
415
-
416
- if (timeSinceLastEmit < interval) {
417
- await new Promise(resolve => setTimeout(resolve, interval - timeSinceLastEmit))
418
- }
419
-
420
- lastEmit = Date.now()
421
- controller.enqueue(chunk)
422
- },
423
- })
424
- },
425
- }
426
-
427
- /**
428
- * File streaming utilities
429
- */
430
- export class FileStreamHandler {
431
- /**
432
- * Stream a file with range support
433
- */
434
- static async streamFile(
435
- filePath: string,
436
- request: EnhancedRequest,
437
- options: {
438
- contentType?: ContentType
439
- enableRanges?: boolean
440
- chunkSize?: number
441
- } = {},
442
- ): Promise<Response> {
443
- const { contentType, enableRanges = true, chunkSize: _chunkSize = 64 * 1024 } = options
444
-
445
- try {
446
- const file = Bun.file(filePath)
447
- const fileSize = file.size
448
- const range = enableRanges ? request.headers.get('range') : null
449
-
450
- if (range && enableRanges) {
451
- return FileStreamHandler.handleRangeRequest(file, range, contentType)
452
- }
453
-
454
- const headers = new Headers({
455
- 'Content-Length': fileSize.toString(),
456
- 'Accept-Ranges': enableRanges ? 'bytes' : 'none',
457
- })
458
-
459
- if (contentType) {
460
- headers.set('Content-Type', contentType)
461
- }
462
-
463
- return new Response(file.stream(), {
464
- status: 200,
465
- headers,
466
- })
467
- }
468
- catch (error) {
469
- console.error('Error streaming file:', error)
470
- return new Response('File not found', { status: 404 })
471
- }
472
- }
473
-
474
- /**
475
- * Handle range requests for file streaming
476
- */
477
- private static async handleRangeRequest(
478
- file: BunFile,
479
- rangeHeader: string,
480
- contentType?: ContentType,
481
- ): Promise<Response> {
482
- const ranges = FileStreamHandler.parseRangeHeader(rangeHeader, file.size)
483
-
484
- if (!ranges || ranges.length === 0) {
485
- return new Response('Invalid range', { status: 416 })
486
- }
487
-
488
- const [start, end] = ranges[0]
489
- const contentLength = end - start + 1
490
-
491
- const headers = new Headers({
492
- 'Content-Range': `bytes ${start}-${end}/${file.size}`,
493
- 'Content-Length': contentLength.toString(),
494
- 'Accept-Ranges': 'bytes',
495
- })
496
-
497
- if (contentType) {
498
- headers.set('Content-Type', contentType)
499
- }
500
-
501
- const slice = file.slice(start, end + 1)
502
-
503
- return new Response(slice.stream(), {
504
- status: 206,
505
- headers,
506
- })
507
- }
508
-
509
- /**
510
- * Parse Range header
511
- */
512
- private static parseRangeHeader(rangeHeader: string, fileSize: number): [number, number][] | null {
513
- const matches = rangeHeader.match(/^bytes=(.+)$/)
514
- if (!matches)
515
- return null
516
-
517
- const ranges: [number, number][] = []
518
- const rangeSpecs = matches[1].split(',')
519
-
520
- for (const rangeSpec of rangeSpecs) {
521
- const [startStr, endStr] = rangeSpec.trim().split('-')
522
-
523
- let start: number
524
- let end: number
525
-
526
- if (startStr === '') {
527
- // Suffix range: -500 (last 500 bytes)
528
- start = Math.max(0, fileSize - Number.parseInt(endStr, 10))
529
- end = fileSize - 1
530
- }
531
- else if (endStr === '') {
532
- // Prefix range: 500- (from byte 500 to end)
533
- start = Number.parseInt(startStr, 10)
534
- end = fileSize - 1
535
- }
536
- else {
537
- // Full range: 500-1000
538
- start = Number.parseInt(startStr, 10)
539
- end = Number.parseInt(endStr, 10)
540
- }
541
-
542
- // Validate range
543
- if (start < 0 || end >= fileSize || start > end) {
544
- return null
545
- }
546
-
547
- ranges.push([start, end])
548
- }
549
-
550
- return ranges
551
- }
552
- }