azurajs 3.0.1 → 3.0.2
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/dist/config/index.js +128 -6
- package/dist/config/index.js.map +1 -1
- package/dist/config/index.mjs +130 -1
- package/dist/config/index.mjs.map +1 -1
- package/dist/core/index.js +1100 -11
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +1102 -3
- package/dist/core/index.mjs.map +1 -1
- package/dist/decorators/index.js +117 -87
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/index.mjs +98 -1
- package/dist/decorators/index.mjs.map +1 -1
- package/dist/index.js +2592 -236
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2537 -9
- package/dist/index.mjs.map +1 -1
- package/dist/middleware/index.js +16 -7
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/index.mjs +17 -1
- package/dist/middleware/index.mjs.map +1 -1
- package/dist/plugins/index.js +1056 -73
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/index.mjs +1042 -1
- package/dist/plugins/index.mjs.map +1 -1
- package/dist/types/index.js +49 -12
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +49 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/index.js +551 -50
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +541 -3
- package/dist/utils/index.mjs.map +1 -1
- package/package.json +35 -17
- package/{dist/chunk-DR254CWJ.mjs → src/config/ConfigModule.ts} +169 -132
- package/src/config/index.ts +1 -0
- package/src/core/index.ts +2 -0
- package/src/core/router.ts +284 -0
- package/{dist/chunk-EYAHUNC7.mjs → src/core/server.ts} +590 -699
- package/src/decorators/Route.ts +110 -0
- package/src/decorators/index.ts +23 -0
- package/src/index.ts +12 -0
- package/src/middleware/LoggingMiddleware.ts +20 -0
- package/src/middleware/index.ts +1 -0
- package/src/plugins/CORSPlugin.ts +56 -0
- package/src/plugins/CircuitBreakerPlugin.ts +84 -0
- package/src/plugins/CompressionPlugin.ts +80 -0
- package/src/plugins/ETagPlugin.ts +31 -0
- package/src/plugins/HealthCheckPlugin.ts +57 -0
- package/src/plugins/HelmetPlugin.ts +89 -0
- package/src/plugins/JWTPlugin.ts +132 -0
- package/src/plugins/MultipartPlugin.ts +168 -0
- package/src/plugins/ProxyPlugin.ts +89 -0
- package/src/plugins/RateLimitPlugin.ts +96 -0
- package/src/plugins/RequestIdPlugin.ts +21 -0
- package/src/plugins/SSEPlugin.ts +114 -0
- package/src/plugins/SessionPlugin.ts +98 -0
- package/src/plugins/StaticPlugin.ts +152 -0
- package/src/plugins/TimeoutPlugin.ts +33 -0
- package/src/plugins/index.ts +18 -0
- package/src/types/common.type.ts +82 -0
- package/src/types/config.type.ts +57 -0
- package/{dist/chunk-OWUGAI5V.mjs → src/types/http/status.ts} +49 -51
- package/src/types/index.ts +55 -0
- package/src/types/plugins/plugin.type.ts +170 -0
- package/src/types/reflect.d.ts +14 -0
- package/src/types/routes.type.ts +70 -0
- package/src/utils/HttpError.ts +62 -0
- package/src/utils/IpResolver.ts +30 -0
- package/src/utils/Logger.ts +144 -0
- package/src/utils/Parser.ts +182 -0
- package/src/utils/cookies/CookieManager.ts +48 -0
- package/src/utils/index.ts +9 -0
- package/{dist/chunk-UWIFSGSQ.mjs → src/utils/validators/DTOValidator.ts} +145 -141
- package/src/utils/validators/SchemaValidator.ts +45 -0
- package/dist/chunk-3UFAWS2V.js +0 -392
- package/dist/chunk-3UFAWS2V.js.map +0 -1
- package/dist/chunk-4LSFAAZW.js +0 -4
- package/dist/chunk-4LSFAAZW.js.map +0 -1
- package/dist/chunk-7NSRIVZM.js +0 -54
- package/dist/chunk-7NSRIVZM.js.map +0 -1
- package/dist/chunk-AOG6NYAM.js +0 -144
- package/dist/chunk-AOG6NYAM.js.map +0 -1
- package/dist/chunk-DR254CWJ.mjs.map +0 -1
- package/dist/chunk-EYAHUNC7.mjs.map +0 -1
- package/dist/chunk-HHDQPIJN.mjs +0 -19
- package/dist/chunk-HHDQPIJN.mjs.map +0 -1
- package/dist/chunk-HHZNAGGI.js +0 -702
- package/dist/chunk-HHZNAGGI.js.map +0 -1
- package/dist/chunk-KJM5XCAY.js +0 -21
- package/dist/chunk-KJM5XCAY.js.map +0 -1
- package/dist/chunk-NLSZKAPA.mjs +0 -1044
- package/dist/chunk-NLSZKAPA.mjs.map +0 -1
- package/dist/chunk-OWUGAI5V.mjs.map +0 -1
- package/dist/chunk-POPNQEOK.js +0 -1063
- package/dist/chunk-POPNQEOK.js.map +0 -1
- package/dist/chunk-QPRW4YU4.js +0 -134
- package/dist/chunk-QPRW4YU4.js.map +0 -1
- package/dist/chunk-REJDZUZ5.mjs +0 -382
- package/dist/chunk-REJDZUZ5.mjs.map +0 -1
- package/dist/chunk-TC6N6TJZ.mjs +0 -100
- package/dist/chunk-TC6N6TJZ.mjs.map +0 -1
- package/dist/chunk-TEUXKMXP.js +0 -122
- package/dist/chunk-TEUXKMXP.js.map +0 -1
- package/dist/chunk-UWIFSGSQ.mjs.map +0 -1
- package/dist/chunk-YPBKY4KY.mjs +0 -3
- package/dist/chunk-YPBKY4KY.mjs.map +0 -1
|
@@ -1,699 +1,590 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createServer as
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this
|
|
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
|
-
const
|
|
141
|
-
const
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
this.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
)
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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
|
-
const azRes = res;
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
this.
|
|
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
|
-
if (
|
|
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
|
-
this.
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
const
|
|
500
|
-
const
|
|
501
|
-
|
|
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
|
-
|
|
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
|
-
} finally {
|
|
592
|
-
if (this.config.logging?.requestLogging) {
|
|
593
|
-
const duration = process.hrtime.bigint() - azReq.startTime;
|
|
594
|
-
this.logger.request(
|
|
595
|
-
req.method ?? "GET",
|
|
596
|
-
pathname,
|
|
597
|
-
res.statusCode,
|
|
598
|
-
duration
|
|
599
|
-
);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
handleError(err, req, res) {
|
|
604
|
-
if (res.sent) return;
|
|
605
|
-
if (this.errorHandler) {
|
|
606
|
-
try {
|
|
607
|
-
this.errorHandler(err, req, res, () => {
|
|
608
|
-
});
|
|
609
|
-
return;
|
|
610
|
-
} catch (handlerErr) {
|
|
611
|
-
this.logger.error("Error handler threw:", handlerErr);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
if (err instanceof HttpError) {
|
|
615
|
-
res.status(err.statusCode).json(err.toJSON());
|
|
616
|
-
return;
|
|
617
|
-
}
|
|
618
|
-
const statusCode = err?.statusCode ?? err?.status ?? 500;
|
|
619
|
-
const message = this.config.environment === "production" ? "Internal Server Error" : err?.message ?? "Internal Server Error";
|
|
620
|
-
this.logger.error(`Unhandled error: ${err?.message ?? err}`);
|
|
621
|
-
if (this.config.debug && err?.stack) {
|
|
622
|
-
this.logger.error(err.stack);
|
|
623
|
-
}
|
|
624
|
-
res.status(statusCode).json({
|
|
625
|
-
error: {
|
|
626
|
-
statusCode,
|
|
627
|
-
message,
|
|
628
|
-
...this.config.debug ? { stack: err?.stack } : {}
|
|
629
|
-
}
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
async listen(port, callback) {
|
|
633
|
-
const serverPort = port ?? this.config.server?.port ?? 3e3;
|
|
634
|
-
const host = this.config.server?.host ?? "0.0.0.0";
|
|
635
|
-
const handler = (req, res) => {
|
|
636
|
-
this.handleRequest(req, res).catch((err) => {
|
|
637
|
-
this.logger.error("Fatal request error:", err);
|
|
638
|
-
if (!res.headersSent) {
|
|
639
|
-
res.statusCode = 500;
|
|
640
|
-
res.end('{"error":{"statusCode":500,"message":"Internal Server Error"}}');
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
};
|
|
644
|
-
if (this.config.server?.https) {
|
|
645
|
-
const httpsConfig = this.config.server.https;
|
|
646
|
-
this.server = createServer(
|
|
647
|
-
{
|
|
648
|
-
key: readFileSync(httpsConfig.key),
|
|
649
|
-
cert: readFileSync(httpsConfig.cert),
|
|
650
|
-
...httpsConfig.ca ? { ca: readFileSync(httpsConfig.ca) } : {}
|
|
651
|
-
},
|
|
652
|
-
handler
|
|
653
|
-
);
|
|
654
|
-
} else {
|
|
655
|
-
this.server = createServer$1(handler);
|
|
656
|
-
}
|
|
657
|
-
if (this.config.server?.keepAliveTimeout) {
|
|
658
|
-
this.server.keepAliveTimeout = this.config.server.keepAliveTimeout;
|
|
659
|
-
}
|
|
660
|
-
if (this.config.server?.headersTimeout) {
|
|
661
|
-
this.server.headersTimeout = this.config.server.headersTimeout;
|
|
662
|
-
}
|
|
663
|
-
return new Promise((resolve) => {
|
|
664
|
-
this.server.listen(serverPort, host, () => {
|
|
665
|
-
this.logger.banner(serverPort, host);
|
|
666
|
-
callback?.();
|
|
667
|
-
resolve(this.server);
|
|
668
|
-
});
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
async close() {
|
|
672
|
-
return new Promise((resolve, reject) => {
|
|
673
|
-
if (!this.server) {
|
|
674
|
-
resolve();
|
|
675
|
-
return;
|
|
676
|
-
}
|
|
677
|
-
this.server.close((err) => {
|
|
678
|
-
if (err) reject(err);
|
|
679
|
-
else resolve();
|
|
680
|
-
});
|
|
681
|
-
});
|
|
682
|
-
}
|
|
683
|
-
getRouter() {
|
|
684
|
-
return this.router;
|
|
685
|
-
}
|
|
686
|
-
getLogger() {
|
|
687
|
-
return this.logger;
|
|
688
|
-
}
|
|
689
|
-
getConfig() {
|
|
690
|
-
return this.config;
|
|
691
|
-
}
|
|
692
|
-
getRoutes() {
|
|
693
|
-
return this.router.getAllRoutes();
|
|
694
|
-
}
|
|
695
|
-
};
|
|
696
|
-
|
|
697
|
-
export { AzuraServer, Router };
|
|
698
|
-
//# sourceMappingURL=chunk-EYAHUNC7.mjs.map
|
|
699
|
-
//# sourceMappingURL=chunk-EYAHUNC7.mjs.map
|
|
1
|
+
import { createServer, type Server, type IncomingMessage, type ServerResponse } from "node:http";
|
|
2
|
+
import { createServer as createHttpsServer } from "node:https";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { Router } from "./router.js";
|
|
5
|
+
import { Logger } from "../utils/Logger.js";
|
|
6
|
+
import { HttpError } from "../utils/HttpError.js";
|
|
7
|
+
import { parseQueryString, parseCookies, parseBody, parseUrl } from "../utils/Parser.js";
|
|
8
|
+
import { resolveIp } from "../utils/IpResolver.js";
|
|
9
|
+
import { serializeCookie, clearCookieHeader } from "../utils/cookies/CookieManager.js";
|
|
10
|
+
import type {
|
|
11
|
+
AzuraRequest,
|
|
12
|
+
AzuraResponse,
|
|
13
|
+
NextFunction,
|
|
14
|
+
MiddlewareHandler,
|
|
15
|
+
ErrorHandler,
|
|
16
|
+
HttpMethod,
|
|
17
|
+
CookieOptions,
|
|
18
|
+
} from "../types/common.type.js";
|
|
19
|
+
import type { AzuraConfig } from "../types/config.type.js";
|
|
20
|
+
import type { PluginHandler } from "../types/plugins/plugin.type.js";
|
|
21
|
+
import type { ControllerMetadata, RouteMetadata } from "../types/routes.type.js";
|
|
22
|
+
|
|
23
|
+
const CONTROLLER_META_KEY = "azura:controller";
|
|
24
|
+
const ROUTES_META_KEY = "azura:routes";
|
|
25
|
+
const PARAMS_META_KEY = "azura:params";
|
|
26
|
+
|
|
27
|
+
export class AzuraServer {
|
|
28
|
+
private server: Server | null = null;
|
|
29
|
+
private router: Router;
|
|
30
|
+
private logger: Logger;
|
|
31
|
+
private middlewares: MiddlewareHandler[] = [];
|
|
32
|
+
private plugins: PluginHandler[] = [];
|
|
33
|
+
private errorHandler: ErrorHandler | null = null;
|
|
34
|
+
private config: AzuraConfig;
|
|
35
|
+
private compiledChain: ((req: AzuraRequest, res: AzuraResponse) => Promise<void>) | null = null;
|
|
36
|
+
|
|
37
|
+
constructor(config: AzuraConfig = {}) {
|
|
38
|
+
this.config = {
|
|
39
|
+
environment: config.environment ?? (process.env.NODE_ENV as any) ?? "development",
|
|
40
|
+
server: {
|
|
41
|
+
port: config.server?.port ?? 3000,
|
|
42
|
+
host: config.server?.host ?? "0.0.0.0",
|
|
43
|
+
keepAliveTimeout: config.server?.keepAliveTimeout ?? 72000,
|
|
44
|
+
headersTimeout: config.server?.headersTimeout ?? 75000,
|
|
45
|
+
requestTimeout: config.server?.requestTimeout ?? 30000,
|
|
46
|
+
...config.server,
|
|
47
|
+
},
|
|
48
|
+
logging: {
|
|
49
|
+
enabled: config.logging?.enabled ?? true,
|
|
50
|
+
level: config.logging?.level ?? "info",
|
|
51
|
+
timestamp: config.logging?.timestamp ?? true,
|
|
52
|
+
colors: config.logging?.colors ?? true,
|
|
53
|
+
requestLogging: config.logging?.requestLogging ?? true,
|
|
54
|
+
...config.logging,
|
|
55
|
+
},
|
|
56
|
+
plugins: config.plugins ?? {},
|
|
57
|
+
debug: config.debug ?? false,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
this.router = new Router();
|
|
61
|
+
this.logger = new Logger({
|
|
62
|
+
level: this.config.logging?.enabled === false ? "silent" : this.config.logging?.level,
|
|
63
|
+
colors: this.config.logging?.colors,
|
|
64
|
+
timestamp: this.config.logging?.timestamp,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
use(handler: MiddlewareHandler | PluginHandler): this {
|
|
69
|
+
this.compiledChain = null;
|
|
70
|
+
if (handler.length <= 2) {
|
|
71
|
+
this.plugins.push(handler as PluginHandler);
|
|
72
|
+
} else {
|
|
73
|
+
this.middlewares.push(handler as MiddlewareHandler);
|
|
74
|
+
}
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
plugin(handler: PluginHandler): this {
|
|
79
|
+
this.compiledChain = null;
|
|
80
|
+
this.plugins.push(handler);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
onError(handler: ErrorHandler): this {
|
|
85
|
+
this.errorHandler = handler;
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
90
|
+
return this.route("GET", path, handlers);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
post(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
94
|
+
return this.route("POST", path, handlers);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
put(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
98
|
+
return this.route("PUT", path, handlers);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
delete(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
102
|
+
return this.route("DELETE", path, handlers);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
patch(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
106
|
+
return this.route("PATCH", path, handlers);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
head(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
110
|
+
return this.route("HEAD", path, handlers);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
options(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
114
|
+
return this.route("OPTIONS", path, handlers);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
all(path: string, ...handlers: (MiddlewareHandler | Function)[]): this {
|
|
118
|
+
const methods: HttpMethod[] = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
|
|
119
|
+
for (const method of methods) {
|
|
120
|
+
this.route(method, path, handlers);
|
|
121
|
+
}
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private route(method: HttpMethod, path: string, handlers: (MiddlewareHandler | Function)[]): this {
|
|
126
|
+
const routeHandler = handlers[handlers.length - 1] as any;
|
|
127
|
+
const middlewares = handlers.slice(0, -1) as MiddlewareHandler[];
|
|
128
|
+
this.router.add(method, path, routeHandler, middlewares);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
register(...controllers: (new (...args: any[]) => any)[]): this {
|
|
133
|
+
for (const Controller of controllers) {
|
|
134
|
+
this.registerController(Controller);
|
|
135
|
+
}
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private registerController(Controller: new (...args: any[]) => any): void {
|
|
140
|
+
const instance = new Controller();
|
|
141
|
+
const prefix: string = Reflect.getMetadata?.(CONTROLLER_META_KEY, Controller) ?? "";
|
|
142
|
+
const controllerMiddlewares: MiddlewareHandler[] =
|
|
143
|
+
Reflect.getMetadata?.("azura:middlewares", Controller) ?? [];
|
|
144
|
+
|
|
145
|
+
const prototype = Controller.prototype;
|
|
146
|
+
const propertyNames = Object.getOwnPropertyNames(prototype).filter(
|
|
147
|
+
(p) => p !== "constructor",
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
for (const propertyKey of propertyNames) {
|
|
151
|
+
const routeMeta: RouteMetadata | undefined = Reflect.getMetadata?.(
|
|
152
|
+
ROUTES_META_KEY,
|
|
153
|
+
prototype,
|
|
154
|
+
propertyKey,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
if (!routeMeta) continue;
|
|
158
|
+
|
|
159
|
+
const fullPath = this.joinPaths(prefix, routeMeta.path);
|
|
160
|
+
const paramsMeta = Reflect.getMetadata?.(PARAMS_META_KEY, prototype, propertyKey) ?? [];
|
|
161
|
+
const routeMiddlewares: MiddlewareHandler[] =
|
|
162
|
+
Reflect.getMetadata?.("azura:middlewares", prototype, propertyKey) ?? [];
|
|
163
|
+
|
|
164
|
+
const allMiddlewares = [...controllerMiddlewares, ...routeMiddlewares];
|
|
165
|
+
|
|
166
|
+
const handler = async (req: AzuraRequest, res: AzuraResponse) => {
|
|
167
|
+
const args = this.resolveParams(paramsMeta, req, res);
|
|
168
|
+
const result = await instance[propertyKey](...args);
|
|
169
|
+
if (result !== undefined && !res.sent) {
|
|
170
|
+
if (typeof result === "object") {
|
|
171
|
+
res.json(result);
|
|
172
|
+
} else {
|
|
173
|
+
res.send(String(result));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
this.router.add(routeMeta.method, fullPath, handler, allMiddlewares, routeMeta.meta);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private resolveParams(paramsMeta: any[], req: AzuraRequest, res: AzuraResponse): any[] {
|
|
183
|
+
if (!paramsMeta || paramsMeta.length === 0) return [req, res];
|
|
184
|
+
|
|
185
|
+
const sorted = [...paramsMeta].sort((a, b) => a.index - b.index);
|
|
186
|
+
const args: any[] = new Array(sorted.length);
|
|
187
|
+
|
|
188
|
+
for (const meta of sorted) {
|
|
189
|
+
let value: any;
|
|
190
|
+
|
|
191
|
+
switch (meta.type) {
|
|
192
|
+
case "body":
|
|
193
|
+
value = meta.name ? req.body?.[meta.name] : req.body;
|
|
194
|
+
break;
|
|
195
|
+
case "query":
|
|
196
|
+
value = meta.name ? req.query[meta.name] : req.query;
|
|
197
|
+
break;
|
|
198
|
+
case "param":
|
|
199
|
+
value = meta.name ? req.params[meta.name] : req.params;
|
|
200
|
+
break;
|
|
201
|
+
case "header":
|
|
202
|
+
value = meta.name ? req.headers[meta.name.toLowerCase()] : req.headers;
|
|
203
|
+
break;
|
|
204
|
+
case "cookie":
|
|
205
|
+
value = meta.name ? req.cookies[meta.name] : req.cookies;
|
|
206
|
+
break;
|
|
207
|
+
case "req":
|
|
208
|
+
value = req;
|
|
209
|
+
break;
|
|
210
|
+
case "res":
|
|
211
|
+
value = res;
|
|
212
|
+
break;
|
|
213
|
+
case "next":
|
|
214
|
+
value = () => {};
|
|
215
|
+
break;
|
|
216
|
+
case "ip":
|
|
217
|
+
value = req.ip;
|
|
218
|
+
break;
|
|
219
|
+
case "session":
|
|
220
|
+
value = req.session;
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
value = undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (meta.pipes) {
|
|
227
|
+
for (const pipe of meta.pipes) {
|
|
228
|
+
value = pipe(value);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
args[meta.index] = value;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return args;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private joinPaths(prefix: string, path: string): string {
|
|
239
|
+
const p = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
240
|
+
const s = path.startsWith("/") ? path : `/${path}`;
|
|
241
|
+
return `${p}${s}`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private extendRequest(req: IncomingMessage, pathname: string, query: Record<string, string | string[]>): AzuraRequest {
|
|
245
|
+
const azReq = req as AzuraRequest;
|
|
246
|
+
azReq.pathname = pathname;
|
|
247
|
+
azReq.query = query;
|
|
248
|
+
azReq.params = {};
|
|
249
|
+
azReq.cookies = parseCookies(req.headers.cookie);
|
|
250
|
+
azReq.ip = resolveIp(req);
|
|
251
|
+
azReq.protocol = (req.socket as any).encrypted ? "https" : "http";
|
|
252
|
+
azReq.secure = azReq.protocol === "https";
|
|
253
|
+
azReq.hostname = (req.headers.host ?? "localhost").split(":")[0];
|
|
254
|
+
azReq.startTime = process.hrtime.bigint();
|
|
255
|
+
azReq.raw = req;
|
|
256
|
+
return azReq;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private extendResponse(res: ServerResponse): AzuraResponse {
|
|
260
|
+
const azRes = res as AzuraResponse;
|
|
261
|
+
azRes.locals = {};
|
|
262
|
+
azRes.sent = false;
|
|
263
|
+
|
|
264
|
+
azRes.status = function (code: number) {
|
|
265
|
+
this.statusCode = code;
|
|
266
|
+
return this;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
azRes.json = function (data: unknown) {
|
|
270
|
+
if (this.sent) return;
|
|
271
|
+
this.sent = true;
|
|
272
|
+
const body = JSON.stringify(data);
|
|
273
|
+
this.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
274
|
+
this.setHeader("Content-Length", Buffer.byteLength(body));
|
|
275
|
+
this.end(body);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
azRes.send = function (body: string | Buffer) {
|
|
279
|
+
if (this.sent) return;
|
|
280
|
+
this.sent = true;
|
|
281
|
+
if (!this.getHeader("Content-Type")) {
|
|
282
|
+
this.setHeader("Content-Type", typeof body === "string" ? "text/plain; charset=utf-8" : "application/octet-stream");
|
|
283
|
+
}
|
|
284
|
+
const buf = typeof body === "string" ? Buffer.from(body) : body;
|
|
285
|
+
this.setHeader("Content-Length", buf.length);
|
|
286
|
+
this.end(buf);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
azRes.html = function (body: string) {
|
|
290
|
+
if (this.sent) return;
|
|
291
|
+
this.sent = true;
|
|
292
|
+
this.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
293
|
+
this.setHeader("Content-Length", Buffer.byteLength(body));
|
|
294
|
+
this.end(body);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
azRes.redirect = function (url: string, code = 302) {
|
|
298
|
+
if (this.sent) return;
|
|
299
|
+
this.sent = true;
|
|
300
|
+
this.statusCode = code;
|
|
301
|
+
this.setHeader("Location", url);
|
|
302
|
+
this.end();
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
azRes.cookie = function (name: string, value: string, options?: CookieOptions) {
|
|
306
|
+
const header = serializeCookie(name, value, options);
|
|
307
|
+
const existing = this.getHeader("Set-Cookie");
|
|
308
|
+
if (existing) {
|
|
309
|
+
const arr = Array.isArray(existing) ? existing : [String(existing)];
|
|
310
|
+
arr.push(header);
|
|
311
|
+
this.setHeader("Set-Cookie", arr);
|
|
312
|
+
} else {
|
|
313
|
+
this.setHeader("Set-Cookie", header);
|
|
314
|
+
}
|
|
315
|
+
return this;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
azRes.clearCookie = function (name: string, options?: CookieOptions) {
|
|
319
|
+
const header = clearCookieHeader(name, options);
|
|
320
|
+
const existing = this.getHeader("Set-Cookie");
|
|
321
|
+
if (existing) {
|
|
322
|
+
const arr = Array.isArray(existing) ? existing : [String(existing)];
|
|
323
|
+
arr.push(header);
|
|
324
|
+
this.setHeader("Set-Cookie", arr);
|
|
325
|
+
} else {
|
|
326
|
+
this.setHeader("Set-Cookie", header);
|
|
327
|
+
}
|
|
328
|
+
return this;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
azRes.header = function (name: string, value: string | number | string[]) {
|
|
332
|
+
this.setHeader(name, value as any);
|
|
333
|
+
return this;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
azRes.type = function (contentType: string) {
|
|
337
|
+
this.setHeader("Content-Type", contentType);
|
|
338
|
+
return this;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
azRes.attachment = function (filename?: string) {
|
|
342
|
+
if (filename) {
|
|
343
|
+
this.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
|
|
344
|
+
} else {
|
|
345
|
+
this.setHeader("Content-Disposition", "attachment");
|
|
346
|
+
}
|
|
347
|
+
return this;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
azRes.download = function (_path: string, _filename?: string) {
|
|
351
|
+
// TODO: implement file streaming
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
return azRes;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private compileMiddlewareChain(): (req: AzuraRequest, res: AzuraResponse) => Promise<void> {
|
|
358
|
+
if (this.compiledChain) return this.compiledChain;
|
|
359
|
+
|
|
360
|
+
const self = this;
|
|
361
|
+
const globalMiddlewares = [...this.middlewares];
|
|
362
|
+
|
|
363
|
+
this.compiledChain = async (req: AzuraRequest, res: AzuraResponse) => {
|
|
364
|
+
for (const plugin of self.plugins) {
|
|
365
|
+
if (res.sent) return;
|
|
366
|
+
await new Promise<void>((resolve, reject) => {
|
|
367
|
+
try {
|
|
368
|
+
const result = plugin({ req, res }, (err) => {
|
|
369
|
+
if (err) reject(err);
|
|
370
|
+
else resolve();
|
|
371
|
+
});
|
|
372
|
+
if (result && typeof (result as any).then === "function") {
|
|
373
|
+
(result as Promise<void>).then(resolve, reject);
|
|
374
|
+
}
|
|
375
|
+
} catch (e) {
|
|
376
|
+
reject(e);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (res.sent) return;
|
|
382
|
+
|
|
383
|
+
let idx = 0;
|
|
384
|
+
const runMiddleware = async (): Promise<void> => {
|
|
385
|
+
if (idx >= globalMiddlewares.length || res.sent) return;
|
|
386
|
+
const mw = globalMiddlewares[idx++];
|
|
387
|
+
await new Promise<void>((resolve, reject) => {
|
|
388
|
+
try {
|
|
389
|
+
const result = mw(req, res, (err) => {
|
|
390
|
+
if (err) reject(err);
|
|
391
|
+
else resolve();
|
|
392
|
+
});
|
|
393
|
+
if (result && typeof (result as any).then === "function") {
|
|
394
|
+
(result as Promise<void>).then(resolve, reject);
|
|
395
|
+
}
|
|
396
|
+
} catch (e) {
|
|
397
|
+
reject(e);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
await runMiddleware();
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
await runMiddleware();
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
return this.compiledChain;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
410
|
+
const { pathname, search } = parseUrl(req.url ?? "/");
|
|
411
|
+
const query = parseQueryString(search);
|
|
412
|
+
|
|
413
|
+
const azReq = this.extendRequest(req, pathname, query);
|
|
414
|
+
const azRes = this.extendResponse(res);
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
const chain = this.compileMiddlewareChain();
|
|
418
|
+
await chain(azReq, azRes);
|
|
419
|
+
|
|
420
|
+
if (azRes.sent) return;
|
|
421
|
+
|
|
422
|
+
const method = (req.method ?? "GET").toUpperCase() as HttpMethod;
|
|
423
|
+
const match = this.router.find(method, pathname);
|
|
424
|
+
|
|
425
|
+
if (!match) {
|
|
426
|
+
if (pathname === "/favicon.ico") {
|
|
427
|
+
azRes.status(204).send("");
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
throw HttpError.notFound(`Route ${method} ${pathname} not found`);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
azReq.params = match.params;
|
|
434
|
+
|
|
435
|
+
if (method !== "GET" && method !== "HEAD" && method !== "OPTIONS") {
|
|
436
|
+
azReq.body = await parseBody(req);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
for (const mw of match.middlewares) {
|
|
440
|
+
if (azRes.sent) return;
|
|
441
|
+
await new Promise<void>((resolve, reject) => {
|
|
442
|
+
try {
|
|
443
|
+
const result = mw(azReq, azRes, (err) => {
|
|
444
|
+
if (err) reject(err);
|
|
445
|
+
else resolve();
|
|
446
|
+
});
|
|
447
|
+
if (result && typeof (result as any).then === "function") {
|
|
448
|
+
(result as Promise<void>).then(resolve, reject);
|
|
449
|
+
}
|
|
450
|
+
} catch (e) {
|
|
451
|
+
reject(e);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (azRes.sent) return;
|
|
457
|
+
|
|
458
|
+
const result = await match.handler(azReq, azRes);
|
|
459
|
+
|
|
460
|
+
if (result !== undefined && !azRes.sent) {
|
|
461
|
+
if (typeof result === "object" && result !== null) {
|
|
462
|
+
azRes.json(result);
|
|
463
|
+
} else {
|
|
464
|
+
azRes.send(String(result));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
} catch (err) {
|
|
468
|
+
this.handleError(err, azReq, azRes);
|
|
469
|
+
} finally {
|
|
470
|
+
if (this.config.logging?.requestLogging) {
|
|
471
|
+
const duration = process.hrtime.bigint() - azReq.startTime;
|
|
472
|
+
this.logger.request(
|
|
473
|
+
req.method ?? "GET",
|
|
474
|
+
pathname,
|
|
475
|
+
res.statusCode,
|
|
476
|
+
duration,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
private handleError(err: any, req: AzuraRequest, res: AzuraResponse): void {
|
|
483
|
+
if (res.sent) return;
|
|
484
|
+
|
|
485
|
+
if (this.errorHandler) {
|
|
486
|
+
try {
|
|
487
|
+
this.errorHandler(err, req, res, () => {});
|
|
488
|
+
return;
|
|
489
|
+
} catch (handlerErr) {
|
|
490
|
+
this.logger.error("Error handler threw:", handlerErr);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (err instanceof HttpError) {
|
|
495
|
+
res.status(err.statusCode).json(err.toJSON());
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const statusCode = err?.statusCode ?? err?.status ?? 500;
|
|
500
|
+
const message = this.config.environment === "production"
|
|
501
|
+
? "Internal Server Error"
|
|
502
|
+
: err?.message ?? "Internal Server Error";
|
|
503
|
+
|
|
504
|
+
this.logger.error(`Unhandled error: ${err?.message ?? err}`);
|
|
505
|
+
if (this.config.debug && err?.stack) {
|
|
506
|
+
this.logger.error(err.stack);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
res.status(statusCode).json({
|
|
510
|
+
error: {
|
|
511
|
+
statusCode,
|
|
512
|
+
message,
|
|
513
|
+
...(this.config.debug ? { stack: err?.stack } : {}),
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async listen(port?: number, callback?: () => void): Promise<Server> {
|
|
519
|
+
const serverPort = port ?? this.config.server?.port ?? 3000;
|
|
520
|
+
const host = this.config.server?.host ?? "0.0.0.0";
|
|
521
|
+
|
|
522
|
+
const handler = (req: IncomingMessage, res: ServerResponse) => {
|
|
523
|
+
this.handleRequest(req, res).catch((err) => {
|
|
524
|
+
this.logger.error("Fatal request error:", err);
|
|
525
|
+
if (!res.headersSent) {
|
|
526
|
+
res.statusCode = 500;
|
|
527
|
+
res.end('{"error":{"statusCode":500,"message":"Internal Server Error"}}');
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
if (this.config.server?.https) {
|
|
533
|
+
const httpsConfig = this.config.server.https;
|
|
534
|
+
this.server = createHttpsServer(
|
|
535
|
+
{
|
|
536
|
+
key: readFileSync(httpsConfig.key),
|
|
537
|
+
cert: readFileSync(httpsConfig.cert),
|
|
538
|
+
...(httpsConfig.ca ? { ca: readFileSync(httpsConfig.ca) } : {}),
|
|
539
|
+
},
|
|
540
|
+
handler,
|
|
541
|
+
);
|
|
542
|
+
} else {
|
|
543
|
+
this.server = createServer(handler);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (this.config.server?.keepAliveTimeout) {
|
|
547
|
+
this.server.keepAliveTimeout = this.config.server.keepAliveTimeout;
|
|
548
|
+
}
|
|
549
|
+
if (this.config.server?.headersTimeout) {
|
|
550
|
+
this.server.headersTimeout = this.config.server.headersTimeout;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return new Promise((resolve) => {
|
|
554
|
+
this.server!.listen(serverPort, host, () => {
|
|
555
|
+
this.logger.banner(serverPort, host);
|
|
556
|
+
callback?.();
|
|
557
|
+
resolve(this.server!);
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async close(): Promise<void> {
|
|
563
|
+
return new Promise((resolve, reject) => {
|
|
564
|
+
if (!this.server) {
|
|
565
|
+
resolve();
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
this.server.close((err) => {
|
|
569
|
+
if (err) reject(err);
|
|
570
|
+
else resolve();
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
getRouter(): Router {
|
|
576
|
+
return this.router;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
getLogger(): Logger {
|
|
580
|
+
return this.logger;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
getConfig(): AzuraConfig {
|
|
584
|
+
return this.config;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
getRoutes(): Array<{ method: HttpMethod; path: string }> {
|
|
588
|
+
return this.router.getAllRoutes();
|
|
589
|
+
}
|
|
590
|
+
}
|