@oxog/spark 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +632 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +703 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
package/src/router/router.js
CHANGED
|
@@ -1,758 +1,706 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* @fileoverview Router class for handling HTTP routes in Spark Framework
|
|
5
|
-
* @author Spark Framework Team
|
|
6
|
-
* @since 1.0.0
|
|
7
|
-
* @version 1.0.0
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
const Layer = require('./layer');
|
|
11
|
-
const Route = require('./route');
|
|
12
|
-
const { SafeRegexCache } = require('../utils/regex-validator');
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* router
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @param {
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
...options
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
* @
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*
|
|
102
|
-
* @
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* }
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* @
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
* @
|
|
206
|
-
*
|
|
207
|
-
* @
|
|
208
|
-
*
|
|
209
|
-
* @
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
* @
|
|
226
|
-
*
|
|
227
|
-
* @
|
|
228
|
-
*
|
|
229
|
-
* @
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* @param {
|
|
246
|
-
* @
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return this
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Register a
|
|
264
|
-
*
|
|
265
|
-
* @param {string} path - Route path pattern
|
|
266
|
-
* @param {...Function} handlers - Route handler functions
|
|
267
|
-
* @returns {
|
|
268
|
-
*
|
|
269
|
-
* @since 1.0.0
|
|
270
|
-
*
|
|
271
|
-
* @example
|
|
272
|
-
* router.
|
|
273
|
-
*
|
|
274
|
-
* ctx.
|
|
275
|
-
* });
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* @
|
|
287
|
-
*
|
|
288
|
-
* @
|
|
289
|
-
*
|
|
290
|
-
* @
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* @
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* @param {string
|
|
327
|
-
* @param {...Function} handlers - Route handler functions
|
|
328
|
-
* @returns {
|
|
329
|
-
*
|
|
330
|
-
* @
|
|
331
|
-
*
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
*
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
* @
|
|
490
|
-
* @
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
const
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
* @param {
|
|
550
|
-
* @
|
|
551
|
-
* @
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
* @
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
* router
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
*
|
|
707
|
-
* @example
|
|
708
|
-
* // Mount router on app
|
|
709
|
-
* const router = new Router();
|
|
710
|
-
* router.get('/users', handler);
|
|
711
|
-
* app.use('/api', router.routes());
|
|
712
|
-
*/
|
|
713
|
-
routes() {
|
|
714
|
-
const router = this;
|
|
715
|
-
return function routerMiddleware(ctx, next) {
|
|
716
|
-
return router.handle(ctx, next);
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
* Alias for routes() method
|
|
722
|
-
* @returns {Function} Middleware function
|
|
723
|
-
*/
|
|
724
|
-
middleware(prefix) {
|
|
725
|
-
const router = this;
|
|
726
|
-
return function routerMiddleware(ctx, next) {
|
|
727
|
-
// If prefix is provided, adjust the path
|
|
728
|
-
if (prefix && ctx.path.startsWith(prefix)) {
|
|
729
|
-
const originalPath = ctx.path;
|
|
730
|
-
ctx.path = ctx.path.slice(prefix.length) || '/';
|
|
731
|
-
|
|
732
|
-
return router.handle(ctx, next).then(() => {
|
|
733
|
-
ctx.path = originalPath;
|
|
734
|
-
}).catch(err => {
|
|
735
|
-
ctx.path = originalPath;
|
|
736
|
-
throw err;
|
|
737
|
-
});
|
|
738
|
-
}
|
|
739
|
-
return router.handle(ctx, next);
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* Get regular expression flags based on options
|
|
746
|
-
*
|
|
747
|
-
* @param {Object} options - Router options
|
|
748
|
-
* @param {boolean} options.sensitive - Case sensitivity flag
|
|
749
|
-
* @returns {string} Regular expression flags
|
|
750
|
-
*
|
|
751
|
-
* @private
|
|
752
|
-
* @since 1.0.0
|
|
753
|
-
*/
|
|
754
|
-
function getFlags(options) {
|
|
755
|
-
return options.sensitive ? '' : 'i';
|
|
756
|
-
}
|
|
757
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Router class for handling HTTP routes in Spark Framework
|
|
5
|
+
* @author Spark Framework Team
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
* @version 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const Layer = require('./layer');
|
|
11
|
+
const Route = require('./route');
|
|
12
|
+
const { SafeRegexCache } = require('../utils/regex-validator');
|
|
13
|
+
|
|
14
|
+
const { pathToRegExp, matchPath } = Layer;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Router class for managing HTTP routes and middleware
|
|
18
|
+
*
|
|
19
|
+
* The Router class provides route registration, path matching, and middleware execution.
|
|
20
|
+
* It supports dynamic route parameters, nested routers, route grouping, and various
|
|
21
|
+
* HTTP methods. Routes are matched using optimized regular expressions with caching.
|
|
22
|
+
*
|
|
23
|
+
* @class Router
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Basic usage
|
|
28
|
+
* const router = new Router();
|
|
29
|
+
*
|
|
30
|
+
* router.get('/users', (ctx) => {
|
|
31
|
+
* ctx.json({ users: [] });
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* router.post('/users', (ctx) => {
|
|
35
|
+
* const user = ctx.body;
|
|
36
|
+
* ctx.status(201).json({ id: 123, ...user });
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // With parameters and middleware
|
|
41
|
+
* router.get('/users/:id', authenticate, (ctx) => {
|
|
42
|
+
* const userId = ctx.params.id;
|
|
43
|
+
* ctx.json({ id: userId, name: 'User' });
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Route grouping
|
|
48
|
+
* router.group('/api/v1', (api) => {
|
|
49
|
+
* api.get('/users', getUsersHandler);
|
|
50
|
+
* api.post('/users', createUserHandler);
|
|
51
|
+
* });
|
|
52
|
+
*/
|
|
53
|
+
class Router {
|
|
54
|
+
/**
|
|
55
|
+
* Create a new Router instance
|
|
56
|
+
*
|
|
57
|
+
* @param {Object} [options={}] - Router configuration options
|
|
58
|
+
* @param {boolean} [options.caseSensitive=false] - Enable case-sensitive routing
|
|
59
|
+
* @param {boolean} [options.strict=false] - Enable strict routing (trailing slash matters)
|
|
60
|
+
*
|
|
61
|
+
* @since 1.0.0
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // Default router
|
|
65
|
+
* const router = new Router();
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // Case-sensitive router
|
|
69
|
+
* const router = new Router({ caseSensitive: true });
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Strict routing (trailing slash matters)
|
|
73
|
+
* const router = new Router({ strict: true });
|
|
74
|
+
*/
|
|
75
|
+
constructor(options = {}) {
|
|
76
|
+
/**
|
|
77
|
+
* Router configuration options
|
|
78
|
+
* @type {Object}
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
81
|
+
const config = {
|
|
82
|
+
...options,
|
|
83
|
+
caseSensitive: Boolean(options.caseSensitive !== undefined ? options.caseSensitive : options.sensitive),
|
|
84
|
+
strict: Boolean(options.strict)
|
|
85
|
+
};
|
|
86
|
+
// Also callable as router.options(path, ...handlers): a plain property
|
|
87
|
+
// would hide the options() route method.
|
|
88
|
+
this.options = Object.assign(
|
|
89
|
+
(path, ...handlers) => Router.prototype.options.call(this, path, ...handlers),
|
|
90
|
+
config
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Array of route layers (middleware and routes)
|
|
95
|
+
* @type {Array}
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
this.stack = [];
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Parameter handlers for route parameters
|
|
102
|
+
* @type {Object}
|
|
103
|
+
* @private
|
|
104
|
+
*/
|
|
105
|
+
this.params = {};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Cache for compiled regular expressions
|
|
109
|
+
* @type {SafeRegexCache}
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
this.regexCache = new SafeRegexCache();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Add middleware to the router
|
|
117
|
+
*
|
|
118
|
+
* Middleware functions are executed for routes that match the specified path.
|
|
119
|
+
* If no path is provided, middleware applies to all routes.
|
|
120
|
+
*
|
|
121
|
+
* @param {string|Function} pathOrMiddleware - Path pattern or middleware function
|
|
122
|
+
* @param {...Function} middlewares - Additional middleware functions
|
|
123
|
+
* @returns {Router} The router instance for method chaining
|
|
124
|
+
*
|
|
125
|
+
* @since 1.0.0
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* // Global middleware
|
|
129
|
+
* router.use((ctx, next) => {
|
|
130
|
+
* // Console.log removed for production
|
|
131
|
+
* return next();
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* // Path-specific middleware
|
|
136
|
+
* router.use('/api', authenticateMiddleware);
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* // Multiple middleware
|
|
140
|
+
* router.use('/admin', authenticate, authorize, logAccess);
|
|
141
|
+
*/
|
|
142
|
+
use(pathOrMiddleware, ...middlewares) {
|
|
143
|
+
let path = '/';
|
|
144
|
+
let handlers = [];
|
|
145
|
+
|
|
146
|
+
if (typeof pathOrMiddleware === 'string') {
|
|
147
|
+
path = pathOrMiddleware;
|
|
148
|
+
handlers = middlewares;
|
|
149
|
+
} else {
|
|
150
|
+
handlers = [pathOrMiddleware, ...middlewares];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const handler of handlers) {
|
|
154
|
+
// Accept mounted routers as well as middleware functions
|
|
155
|
+
const fn = handler && typeof handler === 'object' && typeof handler.handle === 'function'
|
|
156
|
+
? (ctx, next) => handler.handle(ctx, next)
|
|
157
|
+
: handler;
|
|
158
|
+
if (typeof fn !== 'function') {
|
|
159
|
+
throw new TypeError('router.use() requires middleware functions or routers');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const layer = new Layer(path, {
|
|
163
|
+
sensitive: this.options.caseSensitive,
|
|
164
|
+
strict: this.options.strict,
|
|
165
|
+
end: false
|
|
166
|
+
}, fn, this);
|
|
167
|
+
|
|
168
|
+
this.stack.push(layer);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Register a GET route
|
|
176
|
+
*
|
|
177
|
+
* @param {string} path - Route path pattern
|
|
178
|
+
* @param {...Function} handlers - Route handler functions
|
|
179
|
+
* @returns {Router} The router instance for method chaining
|
|
180
|
+
*
|
|
181
|
+
* @since 1.0.0
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* router.get('/', (ctx) => {
|
|
185
|
+
* ctx.json({ message: 'Hello World!' });
|
|
186
|
+
* });
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* // With parameters
|
|
190
|
+
* router.get('/users/:id', (ctx) => {
|
|
191
|
+
* const id = ctx.params.id;
|
|
192
|
+
* ctx.json({ id, name: 'User' });
|
|
193
|
+
* });
|
|
194
|
+
*/
|
|
195
|
+
get(path, ...handlers) {
|
|
196
|
+
this.route(path, 'GET', ...handlers);
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Register a POST route
|
|
202
|
+
*
|
|
203
|
+
* @param {string} path - Route path pattern
|
|
204
|
+
* @param {...Function} handlers - Route handler functions
|
|
205
|
+
* @returns {Router} The router instance for method chaining
|
|
206
|
+
*
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* router.post('/users', (ctx) => {
|
|
211
|
+
* const user = ctx.body;
|
|
212
|
+
* ctx.status(201).json({ id: 123, ...user });
|
|
213
|
+
* });
|
|
214
|
+
*/
|
|
215
|
+
post(path, ...handlers) {
|
|
216
|
+
this.route(path, 'POST', ...handlers);
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Register a PUT route
|
|
222
|
+
*
|
|
223
|
+
* @param {string} path - Route path pattern
|
|
224
|
+
* @param {...Function} handlers - Route handler functions
|
|
225
|
+
* @returns {Router} The router instance for method chaining
|
|
226
|
+
*
|
|
227
|
+
* @since 1.0.0
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* router.put('/users/:id', (ctx) => {
|
|
231
|
+
* const id = ctx.params.id;
|
|
232
|
+
* const updates = ctx.body;
|
|
233
|
+
* ctx.json({ id, ...updates });
|
|
234
|
+
* });
|
|
235
|
+
*/
|
|
236
|
+
put(path, ...handlers) {
|
|
237
|
+
this.route(path, 'PUT', ...handlers);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Register a DELETE route
|
|
243
|
+
*
|
|
244
|
+
* @param {string} path - Route path pattern
|
|
245
|
+
* @param {...Function} handlers - Route handler functions
|
|
246
|
+
* @returns {Router} The router instance for method chaining
|
|
247
|
+
*
|
|
248
|
+
* @since 1.0.0
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* router.delete('/users/:id', (ctx) => {
|
|
252
|
+
* const id = ctx.params.id;
|
|
253
|
+
* // Delete user logic
|
|
254
|
+
* ctx.status(204).end();
|
|
255
|
+
* });
|
|
256
|
+
*/
|
|
257
|
+
delete(path, ...handlers) {
|
|
258
|
+
this.route(path, 'DELETE', ...handlers);
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Register a PATCH route
|
|
264
|
+
*
|
|
265
|
+
* @param {string} path - Route path pattern
|
|
266
|
+
* @param {...Function} handlers - Route handler functions
|
|
267
|
+
* @returns {Router} The router instance for method chaining
|
|
268
|
+
*
|
|
269
|
+
* @since 1.0.0
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* router.patch('/users/:id', (ctx) => {
|
|
273
|
+
* const id = ctx.params.id;
|
|
274
|
+
* const updates = ctx.body;
|
|
275
|
+
* ctx.json({ id, ...updates });
|
|
276
|
+
* });
|
|
277
|
+
*/
|
|
278
|
+
patch(path, ...handlers) {
|
|
279
|
+
this.route(path, 'PATCH', ...handlers);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Register a HEAD route
|
|
285
|
+
*
|
|
286
|
+
* @param {string} path - Route path pattern
|
|
287
|
+
* @param {...Function} handlers - Route handler functions
|
|
288
|
+
* @returns {Router} The router instance for method chaining
|
|
289
|
+
*
|
|
290
|
+
* @since 1.0.0
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* router.head('/users/:id', (ctx) => {
|
|
294
|
+
* // Check if user exists
|
|
295
|
+
* ctx.status(200).end();
|
|
296
|
+
* });
|
|
297
|
+
*/
|
|
298
|
+
head(path, ...handlers) {
|
|
299
|
+
this.route(path, 'HEAD', ...handlers);
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Register an OPTIONS route
|
|
305
|
+
*
|
|
306
|
+
* @param {string} path - Route path pattern
|
|
307
|
+
* @param {...Function} handlers - Route handler functions
|
|
308
|
+
* @returns {Router} The router instance for method chaining
|
|
309
|
+
*
|
|
310
|
+
* @since 1.0.0
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* router.options('/api/*', (ctx) => {
|
|
314
|
+
* ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
|
|
315
|
+
* ctx.status(200).end();
|
|
316
|
+
* });
|
|
317
|
+
*/
|
|
318
|
+
options(path, ...handlers) {
|
|
319
|
+
this.route(path, 'OPTIONS', ...handlers);
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Register a route for all HTTP methods
|
|
325
|
+
*
|
|
326
|
+
* @param {string} path - Route path pattern
|
|
327
|
+
* @param {...Function} handlers - Route handler functions
|
|
328
|
+
* @returns {Router} The router instance for method chaining
|
|
329
|
+
*
|
|
330
|
+
* @since 1.0.0
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* router.all('/api/*', (ctx, next) => {
|
|
334
|
+
* ctx.set('X-API-Version', '1.0');
|
|
335
|
+
* return next();
|
|
336
|
+
* });
|
|
337
|
+
*/
|
|
338
|
+
all(path, ...handlers) {
|
|
339
|
+
this.route(path, null, ...handlers);
|
|
340
|
+
return this;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Create a new route for the specified path and method
|
|
345
|
+
*
|
|
346
|
+
* This is an internal method used by the HTTP method functions (get, post, etc.).
|
|
347
|
+
* It creates a new Route instance and wraps it in a Layer for the router stack.
|
|
348
|
+
*
|
|
349
|
+
* @param {string} path - Route path pattern
|
|
350
|
+
* @param {string|null} method - HTTP method or null for all methods
|
|
351
|
+
* @param {...Function} handlers - Route handler functions
|
|
352
|
+
* @returns {Router} The router instance for method chaining
|
|
353
|
+
*
|
|
354
|
+
* @private
|
|
355
|
+
* @since 1.0.0
|
|
356
|
+
*/
|
|
357
|
+
route(path, method, ...handlers) {
|
|
358
|
+
const route = new Route(path);
|
|
359
|
+
|
|
360
|
+
if (method) {
|
|
361
|
+
route[method.toLowerCase()](...handlers);
|
|
362
|
+
} else {
|
|
363
|
+
route.all(...handlers);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const layer = new Layer(path, {
|
|
367
|
+
sensitive: this.options.caseSensitive,
|
|
368
|
+
strict: this.options.strict,
|
|
369
|
+
end: true
|
|
370
|
+
}, (ctx, next) => {
|
|
371
|
+
return route.dispatch(ctx, next);
|
|
372
|
+
}, this);
|
|
373
|
+
|
|
374
|
+
layer.route = route;
|
|
375
|
+
this.stack.push(layer);
|
|
376
|
+
|
|
377
|
+
return route;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Register a parameter handler
|
|
382
|
+
*
|
|
383
|
+
* Parameter handlers are called when a route parameter is matched.
|
|
384
|
+
* They can be used for validation, transformation, or loading related data.
|
|
385
|
+
*
|
|
386
|
+
* @param {string} name - Parameter name
|
|
387
|
+
* @param {Function} handler - Handler function (ctx, next, value, name) => {}
|
|
388
|
+
* @returns {Router} The router instance for method chaining
|
|
389
|
+
*
|
|
390
|
+
* @since 1.0.0
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* // Load user for :userId parameter
|
|
394
|
+
* router.param('userId', async (ctx, next, id) => {
|
|
395
|
+
* ctx.user = await User.findById(id);
|
|
396
|
+
* if (!ctx.user) {
|
|
397
|
+
* ctx.status(404).json({ error: 'User not found' });
|
|
398
|
+
* return;
|
|
399
|
+
* }
|
|
400
|
+
* return next();
|
|
401
|
+
* });
|
|
402
|
+
*
|
|
403
|
+
* router.get('/users/:userId', (ctx) => {
|
|
404
|
+
* ctx.json(ctx.user); // User already loaded by param handler
|
|
405
|
+
* });
|
|
406
|
+
*/
|
|
407
|
+
param(name, handler) {
|
|
408
|
+
this.params[name] = handler;
|
|
409
|
+
return this;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Handle an incoming request through the router
|
|
414
|
+
*
|
|
415
|
+
* Processes the request through all layers (middleware and routes) in the router stack.
|
|
416
|
+
* Matches paths, extracts parameters, and executes appropriate handlers.
|
|
417
|
+
*
|
|
418
|
+
* @param {Context} ctx - The request context
|
|
419
|
+
* @param {Function} next - The next function to call if no routes match
|
|
420
|
+
* @returns {Promise<void>}
|
|
421
|
+
*
|
|
422
|
+
* @since 1.0.0
|
|
423
|
+
*/
|
|
424
|
+
async handle(ctx, done) {
|
|
425
|
+
const originalPath = ctx.path;
|
|
426
|
+
let index = 0;
|
|
427
|
+
|
|
428
|
+
const next = async (error) => {
|
|
429
|
+
// Undo prefix stripping done for a previous middleware layer
|
|
430
|
+
ctx.path = originalPath;
|
|
431
|
+
|
|
432
|
+
// Errors skip the remaining layers and propagate to the caller
|
|
433
|
+
if (error) {
|
|
434
|
+
return done(error);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
while (index < this.stack.length) {
|
|
438
|
+
const layer = this.stack[index++];
|
|
439
|
+
|
|
440
|
+
let match;
|
|
441
|
+
try {
|
|
442
|
+
match = layer.match(ctx.path);
|
|
443
|
+
} catch (err) {
|
|
444
|
+
return done(err);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (!match) {
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (layer.route && !layer.route.handles_method(ctx.method)) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
ctx.params = { ...ctx.params, ...match.params };
|
|
456
|
+
|
|
457
|
+
try {
|
|
458
|
+
if (await this.processParams(ctx, layer, match.params) === false) {
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (!layer.route && match.path) {
|
|
463
|
+
// Middleware mounted at a prefix sees the path below the prefix
|
|
464
|
+
ctx.path = originalPath.slice(match.path.length) || '/';
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
await layer.handle(ctx, next);
|
|
468
|
+
} catch (err) {
|
|
469
|
+
return next(err);
|
|
470
|
+
} finally {
|
|
471
|
+
ctx.path = originalPath;
|
|
472
|
+
}
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return done();
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
await next();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Process route parameters through registered parameter handlers
|
|
484
|
+
*
|
|
485
|
+
* Calls any registered parameter handlers for the matched route parameters.
|
|
486
|
+
* Parameter handlers can validate, transform, or load data based on parameter values.
|
|
487
|
+
*
|
|
488
|
+
* @param {Context} ctx - The request context
|
|
489
|
+
* @param {Layer} layer - The matched layer
|
|
490
|
+
* @param {Object} params - The extracted route parameters
|
|
491
|
+
* @returns {Promise<void>}
|
|
492
|
+
*
|
|
493
|
+
* @private
|
|
494
|
+
* @since 1.0.0
|
|
495
|
+
*/
|
|
496
|
+
async processParams(ctx, layer, params) {
|
|
497
|
+
if (!params || Object.keys(params).length === 0) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
for (const [key, value] of Object.entries(params)) {
|
|
502
|
+
if (this.params[key]) {
|
|
503
|
+
let continued = false;
|
|
504
|
+
await this.params[key](ctx, () => {
|
|
505
|
+
continued = true;
|
|
506
|
+
}, value, key);
|
|
507
|
+
// A param handler that responded without calling next() ends the request
|
|
508
|
+
if (!continued && ctx.responded) {
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Convert a path string to a regular expression
|
|
518
|
+
*
|
|
519
|
+
* Converts route path patterns to regular expressions for matching URLs.
|
|
520
|
+
* Supports parameter patterns (:param) and wildcard patterns (*wild).
|
|
521
|
+
*
|
|
522
|
+
* @param {string|RegExp|Array} path - Path pattern(s) to convert
|
|
523
|
+
* @param {Object} [options={}] - Conversion options
|
|
524
|
+
* @param {boolean} [options.sensitive=false] - Case-sensitive matching
|
|
525
|
+
* @param {boolean} [options.strict=false] - Strict mode (trailing slash matters)
|
|
526
|
+
* @param {boolean} [options.end=true] - Match to end of string
|
|
527
|
+
* @returns {Object} Object with regexp and keys properties
|
|
528
|
+
*
|
|
529
|
+
* @static
|
|
530
|
+
* @since 1.0.0
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* // Simple path
|
|
534
|
+
* Router.pathToRegExp('/users'); // { regexp: /^\/users\/?$/, keys: [] }
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* // With parameters
|
|
538
|
+
* Router.pathToRegExp('/users/:id'); // { regexp: /^\/users\/([^/]+)\/?$/, keys: [{ name: 'id', optional: false }] }
|
|
539
|
+
*/
|
|
540
|
+
static pathToRegExp(path, options = {}) {
|
|
541
|
+
return pathToRegExp(path, options);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Match a path against a compiled pattern
|
|
546
|
+
*
|
|
547
|
+
* @param {string} path - Path to match
|
|
548
|
+
* @param {RegExp} regexp - Compiled pattern
|
|
549
|
+
* @param {Array} keys - Parameter keys
|
|
550
|
+
* @returns {false|{path: string, params: Object}} Match result
|
|
551
|
+
* @throws {Error} 400 error for malformed percent-encoding in a parameter
|
|
552
|
+
*/
|
|
553
|
+
static match(path, regexp, keys) {
|
|
554
|
+
return matchPath(path, regexp, keys);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Create a route group with a common prefix
|
|
559
|
+
*
|
|
560
|
+
* Groups routes under a common path prefix. The callback function receives
|
|
561
|
+
* a new router instance to define routes within the group.
|
|
562
|
+
*
|
|
563
|
+
* @param {string} prefix - Common path prefix for the group
|
|
564
|
+
* @param {Function} callback - Function that receives the group router
|
|
565
|
+
* @returns {Router} The router instance for method chaining
|
|
566
|
+
*
|
|
567
|
+
* @since 1.0.0
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* router.group('/api', (api) => {
|
|
571
|
+
* api.get('/users', getUsersHandler);
|
|
572
|
+
* api.post('/users', createUserHandler);
|
|
573
|
+
* api.get('/users/:id', getUserHandler);
|
|
574
|
+
* });
|
|
575
|
+
* // Creates routes: GET /api/users, POST /api/users, GET /api/users/:id
|
|
576
|
+
*/
|
|
577
|
+
group(prefix, callback) {
|
|
578
|
+
const router = new Router(this.options);
|
|
579
|
+
callback(router);
|
|
580
|
+
this.use(prefix, router);
|
|
581
|
+
return this;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Create a versioned API group
|
|
586
|
+
*
|
|
587
|
+
* Convenience method for creating API version groups with /v{version} prefix.
|
|
588
|
+
*
|
|
589
|
+
* @param {string|number} version - API version number
|
|
590
|
+
* @param {Function} callback - Function that receives the version router
|
|
591
|
+
* @returns {Router} The router instance for method chaining
|
|
592
|
+
*
|
|
593
|
+
* @since 1.0.0
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* router.version('1', (v1) => {
|
|
597
|
+
* v1.get('/users', getUsersV1);
|
|
598
|
+
* v1.post('/users', createUserV1);
|
|
599
|
+
* });
|
|
600
|
+
*
|
|
601
|
+
* router.version('2', (v2) => {
|
|
602
|
+
* v2.get('/users', getUsersV2);
|
|
603
|
+
* v2.post('/users', createUserV2);
|
|
604
|
+
* });
|
|
605
|
+
* // Creates: GET /v1/users, POST /v1/users, GET /v2/users, POST /v2/users
|
|
606
|
+
*/
|
|
607
|
+
version(version, callback) {
|
|
608
|
+
const versionPrefix = `/v${version}`;
|
|
609
|
+
return this.group(versionPrefix, callback);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Mount another router at the specified path
|
|
614
|
+
*
|
|
615
|
+
* Mounts a sub-router at the given path, allowing for modular route organization.
|
|
616
|
+
*
|
|
617
|
+
* @param {string} path - Path prefix where the router should be mounted
|
|
618
|
+
* @param {Router} router - Router instance to mount
|
|
619
|
+
* @returns {Router} The router instance for method chaining
|
|
620
|
+
*
|
|
621
|
+
* @since 1.0.0
|
|
622
|
+
*
|
|
623
|
+
* @example
|
|
624
|
+
* const userRouter = new Router();
|
|
625
|
+
* userRouter.get('/', getUsersHandler);
|
|
626
|
+
* userRouter.get('/:id', getUserHandler);
|
|
627
|
+
*
|
|
628
|
+
* const apiRouter = new Router();
|
|
629
|
+
* apiRouter.mount('/users', userRouter);
|
|
630
|
+
* // Creates routes: GET /users/, GET /users/:id
|
|
631
|
+
*/
|
|
632
|
+
mount(path, router) {
|
|
633
|
+
this.use(path, router);
|
|
634
|
+
return this;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Returns router middleware function
|
|
640
|
+
*
|
|
641
|
+
* @returns {Function} Middleware function for this router
|
|
642
|
+
* @since 1.0.0
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* // Mount router on app
|
|
646
|
+
* const router = new Router();
|
|
647
|
+
* router.get('/users', handler);
|
|
648
|
+
* app.use('/api', router.routes());
|
|
649
|
+
*/
|
|
650
|
+
routes() {
|
|
651
|
+
const router = this;
|
|
652
|
+
return function routerMiddleware(ctx, next) {
|
|
653
|
+
return router.handle(ctx, next);
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Alias for routes() method
|
|
659
|
+
* @returns {Function} Middleware function
|
|
660
|
+
*/
|
|
661
|
+
middleware(prefix) {
|
|
662
|
+
const router = this;
|
|
663
|
+
let normalized = prefix ? (prefix.startsWith('/') ? prefix : `/${prefix}`) : '';
|
|
664
|
+
while (normalized.length > 1 && normalized.endsWith('/')) {
|
|
665
|
+
normalized = normalized.slice(0, -1);
|
|
666
|
+
}
|
|
667
|
+
if (normalized === '/') {
|
|
668
|
+
normalized = '';
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
return async function routerMiddleware(ctx, next) {
|
|
672
|
+
if (!normalized) {
|
|
673
|
+
return router.handle(ctx, next);
|
|
674
|
+
}
|
|
675
|
+
if (ctx.path !== normalized && !ctx.path.startsWith(`${normalized}/`)) {
|
|
676
|
+
return next();
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const originalPath = ctx.path;
|
|
680
|
+
ctx.path = originalPath.slice(normalized.length) || '/';
|
|
681
|
+
try {
|
|
682
|
+
await router.handle(ctx, (error) => {
|
|
683
|
+
ctx.path = originalPath;
|
|
684
|
+
return next(error);
|
|
685
|
+
});
|
|
686
|
+
} finally {
|
|
687
|
+
ctx.path = originalPath;
|
|
688
|
+
}
|
|
689
|
+
return undefined;
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Get regular expression flags based on options
|
|
697
|
+
*
|
|
698
|
+
* @param {Object} options - Router options
|
|
699
|
+
* @param {boolean} options.sensitive - Case sensitivity flag
|
|
700
|
+
* @returns {string} Regular expression flags
|
|
701
|
+
*
|
|
702
|
+
* @private
|
|
703
|
+
* @since 1.0.0
|
|
704
|
+
*/
|
|
705
|
+
|
|
758
706
|
module.exports = Router;
|