@wooksjs/event-http 0.6.0 → 0.6.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/index.cjs +447 -69
- package/dist/index.d.ts +68 -0
- package/dist/index.mjs +422 -43
- package/package.json +7 -8
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { attachHook, createAsyncEventContext, useAsyncEventContext, useEventId, useEventLogger, useRouteParams } from "@wooksjs/event-core";
|
|
1
|
+
import { attachHook, createAsyncEventContext, useAsyncEventContext, useEventId, useEventLogger, useEventLogger as useEventLogger$1, useRouteParams } from "@wooksjs/event-core";
|
|
2
2
|
import { Buffer as Buffer$1 } from "buffer";
|
|
3
3
|
import { Readable, pipeline } from "node:stream";
|
|
4
4
|
import { promisify } from "node:util";
|
|
@@ -18,6 +18,10 @@ function createHttpContext(data, options) {
|
|
|
18
18
|
options
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Wrapper on useEventContext with HTTP event types
|
|
23
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
24
|
+
*/
|
|
21
25
|
function useHttpContext() {
|
|
22
26
|
return useAsyncEventContext("HTTP");
|
|
23
27
|
}
|
|
@@ -52,12 +56,12 @@ function convertTime(time, unit = "ms") {
|
|
|
52
56
|
const units = {
|
|
53
57
|
ms: 1,
|
|
54
58
|
s: 1e3,
|
|
55
|
-
m:
|
|
56
|
-
h:
|
|
57
|
-
d:
|
|
58
|
-
w:
|
|
59
|
-
M:
|
|
60
|
-
Y:
|
|
59
|
+
m: 1e3 * 60,
|
|
60
|
+
h: 1e3 * 60 * 60,
|
|
61
|
+
d: 1e3 * 60 * 60 * 24,
|
|
62
|
+
w: 1e3 * 60 * 60 * 24 * 7,
|
|
63
|
+
M: 1e3 * 60 * 60 * 24 * 30,
|
|
64
|
+
Y: 1e3 * 60 * 60 * 24 * 365
|
|
61
65
|
};
|
|
62
66
|
|
|
63
67
|
//#endregion
|
|
@@ -167,7 +171,7 @@ var BaseHttpResponseRenderer = class {
|
|
|
167
171
|
if (!response.getContentType()) response.setContentType("text/plain");
|
|
168
172
|
return response.body.toString();
|
|
169
173
|
}
|
|
170
|
-
if (response.body ===
|
|
174
|
+
if (response.body === void 0) return "";
|
|
171
175
|
if (response.body instanceof Uint8Array) return response.body;
|
|
172
176
|
if (typeof response.body === "object") {
|
|
173
177
|
if (!response.getContentType()) response.setContentType("application/json");
|
|
@@ -244,7 +248,7 @@ const httpStatusCodes = {
|
|
|
244
248
|
510: "Not Extended",
|
|
245
249
|
511: "Network Authentication Required"
|
|
246
250
|
};
|
|
247
|
-
let EHttpStatusCode = function(EHttpStatusCode$1) {
|
|
251
|
+
let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
|
|
248
252
|
EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
|
|
249
253
|
EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
250
254
|
EHttpStatusCode$1[EHttpStatusCode$1["Processing"] = 102] = "Processing";
|
|
@@ -311,24 +315,393 @@ let EHttpStatusCode = function(EHttpStatusCode$1) {
|
|
|
311
315
|
return EHttpStatusCode$1;
|
|
312
316
|
}({});
|
|
313
317
|
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region packages/event-http/src/errors/403.tl.svg
|
|
320
|
+
function _403_tl_default(ctx) {
|
|
321
|
+
return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
|
|
322
|
+
<path d="M50 90.625C64.4042 87.1875 83.5751 69.8667 83.5751 48.6937V24.0854L50 9.375L16.425 24.0833V48.6937C16.425 69.8667 35.5959 87.1875 50 90.625Z" fill="#ff000050">
|
|
323
|
+
<animate attributeName="fill" dur="2s" repeatCount="indefinite"
|
|
324
|
+
values="#ff000000;#ff000050;#ff000000" />
|
|
325
|
+
</path>
|
|
326
|
+
|
|
327
|
+
<path d="M61.5395 46.0812H38.4604C37.1061 46.0812 36.0083 47.1791 36.0083 48.5333V65.075C36.0083 66.4292 37.1061 67.5271 38.4604 67.5271H61.5395C62.8938 67.5271 63.9916 66.4292 63.9916 65.075V48.5333C63.9916 47.1791 62.8938 46.0812 61.5395 46.0812Z" />
|
|
328
|
+
|
|
329
|
+
<path d="M41.7834 46.0834V39.6813C41.7834 37.5021 42.6491 35.4121 44.1901 33.8712C45.731 32.3303 47.8209 31.4646 50.0001 31.4646C52.1793 31.4646 54.2693 32.3303 55.8102 33.8712C57.3511 35.4121 58.2168 37.5021 58.2168 39.6813V46.0813" />
|
|
330
|
+
</svg>
|
|
331
|
+
`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
//#endregion
|
|
335
|
+
//#region packages/event-http/src/errors/404.tl.svg
|
|
336
|
+
function _404_tl_default(ctx) {
|
|
337
|
+
return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
338
|
+
<defs>
|
|
339
|
+
<path id="sheet" d="M86.5 36.5V47.5H97.5V92.5H52.5V36.5H86.5ZM63 68.5H87V67.5H63V68.5ZM63 63.5H87V62.5H63V63.5ZM63 58.5H87V57.5H63V58.5ZM96.793 46.5H87.5V37.207L96.793 46.5Z" fill="#88888833" stroke="#888888aa"/>
|
|
340
|
+
</defs>
|
|
341
|
+
|
|
342
|
+
<g id="queue" transform="translate(-5 -10)">
|
|
343
|
+
<use href="#sheet" opacity="0">
|
|
344
|
+
<animateTransform attributeName="transform" type="translate"
|
|
345
|
+
dur="3s" repeatCount="indefinite"
|
|
346
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
347
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
348
|
+
<animate attributeName="opacity"
|
|
349
|
+
dur="3s" repeatCount="indefinite"
|
|
350
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
351
|
+
values="0;1;1;0;0" />
|
|
352
|
+
</use>
|
|
353
|
+
|
|
354
|
+
<use href="#sheet" opacity="0">
|
|
355
|
+
<animateTransform attributeName="transform" type="translate"
|
|
356
|
+
dur="3s" begin="1s" repeatCount="indefinite"
|
|
357
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
358
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
359
|
+
<animate attributeName="opacity"
|
|
360
|
+
dur="3s" begin="1s" repeatCount="indefinite"
|
|
361
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
362
|
+
values="0;1;1;0;0" />
|
|
363
|
+
</use>
|
|
364
|
+
|
|
365
|
+
<use href="#sheet" opacity="0">
|
|
366
|
+
<animateTransform attributeName="transform" type="translate"
|
|
367
|
+
dur="3s" begin="2s" repeatCount="indefinite"
|
|
368
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
369
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
370
|
+
<animate attributeName="opacity"
|
|
371
|
+
dur="3s" begin="2s" repeatCount="indefinite"
|
|
372
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
373
|
+
values="0;1;1;0;0" />
|
|
374
|
+
</use>
|
|
375
|
+
</g>
|
|
376
|
+
|
|
377
|
+
<g>
|
|
378
|
+
<path d="M49.5 32.5C58.3366 32.5 65.5 39.6634 65.5 48.5C65.5 54.4781 62.222 59.6923 57.3584 62.4404C55.0386 63.7512 52.3591 64.5 49.5 64.5C40.6634 64.5 33.5 57.3366 33.5 48.5C33.5 39.6634 40.6634 32.5 49.5 32.5Z" fill="#ffffff50" stroke="#888888" stroke-width="3"/>
|
|
379
|
+
|
|
380
|
+
<path d="M62.7101 74.5691C63.117 75.2907 64.0318 75.5459 64.7534 75.139C65.4751 74.7321 65.7302 73.8173 65.3233 73.0957L62.7101 74.5691ZM58.05 63.25L56.7434 63.9867L62.7101 74.5691L64.0167 73.8324L65.3233 73.0957L59.3567 62.5133L58.05 63.25Z" fill="#888888"/>
|
|
381
|
+
</g>
|
|
382
|
+
</svg>
|
|
383
|
+
|
|
384
|
+
`;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
//#endregion
|
|
388
|
+
//#region packages/event-http/src/errors/500.tl.svg
|
|
389
|
+
function _500_tl_default(ctx) {
|
|
390
|
+
return `<svg height="64" viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
|
|
391
|
+
|
|
392
|
+
<g id="server">
|
|
393
|
+
|
|
394
|
+
<g fill="#88888888" stroke="#88888888" stroke-width="2" >
|
|
395
|
+
<path d="M18 90C13.5817 90 10 86.4182 10 82V38C10 33.5817 13.5817 30 18 30H50.5L58 43L52.5 53L56 68.5L49.0098 89.97L61.2141 71.4358L58.363 54.315L64.7769 43.5511L58.2763 30.2943L104.243 32.0434C108.658 32.2114 112.101 35.9267 111.933 40.3418L110.26 84.31C110.092 88.725 106.377 92.168 101.962 92L49 90H18Z" />
|
|
396
|
+
</g>
|
|
397
|
+
<circle cx="30" cy="60" r="6" fill="red">
|
|
398
|
+
<animate attributeName="fill" dur="0.8s"
|
|
399
|
+
values="red;#2d0000;red" repeatCount="indefinite"/>
|
|
400
|
+
</circle>
|
|
401
|
+
|
|
402
|
+
</g>
|
|
403
|
+
|
|
404
|
+
<g fill="lightgray" opacity="0.75">
|
|
405
|
+
<circle cx="50" cy="35" r="6">
|
|
406
|
+
<animate attributeName="cy" from="35" to="15" dur="2s"
|
|
407
|
+
repeatCount="indefinite"/>
|
|
408
|
+
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
409
|
+
repeatCount="indefinite"/>
|
|
410
|
+
</circle>
|
|
411
|
+
<circle cx="60" cy="40" r="4">
|
|
412
|
+
<animate attributeName="cy" from="40" to="20" dur="2s"
|
|
413
|
+
begin="0.4s" repeatCount="indefinite"/>
|
|
414
|
+
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
415
|
+
begin="0.4s" repeatCount="indefinite"/>
|
|
416
|
+
</circle>
|
|
417
|
+
</g>
|
|
418
|
+
|
|
419
|
+
</svg>
|
|
420
|
+
`;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
//#endregion
|
|
424
|
+
//#region packages/event-http/src/errors/error.tl.html
|
|
425
|
+
function error_tl_default(ctx) {
|
|
426
|
+
const { statusCode, statusMessage, icon, message, details, link, image, version } = ctx;
|
|
427
|
+
return `<!doctype html>
|
|
428
|
+
<html lang="en">
|
|
429
|
+
<head>
|
|
430
|
+
<meta charset="UTF-8" />
|
|
431
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
432
|
+
<title>${statusCode} ${statusMessage}</title>
|
|
433
|
+
<style>
|
|
434
|
+
body {
|
|
435
|
+
font-family: -apple-system, BlinkMacMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
|
|
436
|
+
Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
|
437
|
+
display: flex;
|
|
438
|
+
justify-content: center;
|
|
439
|
+
align-items: flex-start;
|
|
440
|
+
min-height: 100vh;
|
|
441
|
+
margin: 0;
|
|
442
|
+
padding: 0 20px;
|
|
443
|
+
box-sizing: border-box;
|
|
444
|
+
transition:
|
|
445
|
+
background-color 0.3s ease,
|
|
446
|
+
color 0.3s ease;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
.error-container {
|
|
450
|
+
padding: 48px;
|
|
451
|
+
padding-bottom: 12px !important;
|
|
452
|
+
background-color: #ffffff;
|
|
453
|
+
border-radius: 0 0 12px 12px;
|
|
454
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
455
|
+
text-align: center;
|
|
456
|
+
max-width: 650px;
|
|
457
|
+
width: 100%;
|
|
458
|
+
transition:
|
|
459
|
+
background-color 0.3s ease,
|
|
460
|
+
border-color 0.3s ease,
|
|
461
|
+
box-shadow 0.3s ease;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
.status-code {
|
|
465
|
+
font-size: 5rem;
|
|
466
|
+
font-weight: 900;
|
|
467
|
+
margin-bottom: 5px;
|
|
468
|
+
line-height: 1;
|
|
469
|
+
transition: color 0.3s ease;
|
|
470
|
+
display: flex;
|
|
471
|
+
align-items: center;
|
|
472
|
+
justify-content: center;
|
|
473
|
+
gap: 1rem;
|
|
474
|
+
position: relative;
|
|
475
|
+
margin-right: 24px;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
.status-text {
|
|
479
|
+
font-size: 2.25rem;
|
|
480
|
+
font-weight: 700;
|
|
481
|
+
margin-bottom: 25px;
|
|
482
|
+
transition: color 0.3s ease;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
.error-message {
|
|
486
|
+
font-size: 1.25rem;
|
|
487
|
+
margin-bottom: 40px;
|
|
488
|
+
line-height: 1.7;
|
|
489
|
+
transition: color 0.3s ease;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
.json-details-container {
|
|
493
|
+
padding: 20px;
|
|
494
|
+
border-radius: 8px;
|
|
495
|
+
text-align: left;
|
|
496
|
+
overflow-x: auto;
|
|
497
|
+
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
|
|
498
|
+
font-size: 0.9rem;
|
|
499
|
+
border: 1px solid;
|
|
500
|
+
transition:
|
|
501
|
+
background-color 0.3s ease,
|
|
502
|
+
color 0.3s ease,
|
|
503
|
+
border-color 0.3s ease;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
.json-details-container pre {
|
|
507
|
+
margin: 0;
|
|
508
|
+
white-space: pre-wrap;
|
|
509
|
+
word-break: break-all;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
.json-details-container code {
|
|
513
|
+
display: block;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
body {
|
|
517
|
+
background-color: #f8fafc;
|
|
518
|
+
color: #1f2937;
|
|
519
|
+
}
|
|
520
|
+
.error-container {
|
|
521
|
+
background-color: #ffffff;
|
|
522
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
523
|
+
}
|
|
524
|
+
.status-code {
|
|
525
|
+
color: #dc2626;
|
|
526
|
+
}
|
|
527
|
+
.status-text {
|
|
528
|
+
color: #1f2937;
|
|
529
|
+
}
|
|
530
|
+
.error-message {
|
|
531
|
+
color: #4b5563;
|
|
532
|
+
}
|
|
533
|
+
.json-details-container {
|
|
534
|
+
background-color: #f0f4f8;
|
|
535
|
+
color: #374151;
|
|
536
|
+
border-color: #d1d5db;
|
|
537
|
+
}
|
|
538
|
+
.json-details-container p {
|
|
539
|
+
color: #6b7280;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
@media (prefers-color-scheme: dark) {
|
|
543
|
+
body {
|
|
544
|
+
background-color: #2d3748;
|
|
545
|
+
color: #e2e8f0;
|
|
546
|
+
}
|
|
547
|
+
.error-container {
|
|
548
|
+
background-color: #1a202c;
|
|
549
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
|
|
550
|
+
}
|
|
551
|
+
.status-code {
|
|
552
|
+
color: #f56565;
|
|
553
|
+
}
|
|
554
|
+
.status-text {
|
|
555
|
+
color: #cbd5e1;
|
|
556
|
+
}
|
|
557
|
+
.error-message {
|
|
558
|
+
color: #a0aec0;
|
|
559
|
+
}
|
|
560
|
+
.json-details-container {
|
|
561
|
+
background-color: #2d3748;
|
|
562
|
+
color: #e2e8f0;
|
|
563
|
+
border-color: #4a5568;
|
|
564
|
+
}
|
|
565
|
+
.json-details-container p {
|
|
566
|
+
color: #a0aec0;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
.footer {
|
|
571
|
+
display: flex;
|
|
572
|
+
gap: 0.25rem;
|
|
573
|
+
font-size: 0.6em;
|
|
574
|
+
justify-content: flex-end;
|
|
575
|
+
align-items: center;
|
|
576
|
+
margin-top: 12px;
|
|
577
|
+
opacity: 0.5;
|
|
578
|
+
transition: 0.25s ease-in-out;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
.footer img {
|
|
582
|
+
filter: grayscale(0.5);
|
|
583
|
+
transition: 0.25s ease-in-out;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
.footer:hover {
|
|
587
|
+
opacity: 1;
|
|
588
|
+
}
|
|
589
|
+
.footer:hover img {
|
|
590
|
+
filter: grayscale(0);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
@media (max-width: 768px) {
|
|
594
|
+
body {
|
|
595
|
+
padding: 15px;
|
|
596
|
+
}
|
|
597
|
+
.error-container {
|
|
598
|
+
padding: 32px;
|
|
599
|
+
}
|
|
600
|
+
.status-code {
|
|
601
|
+
font-size: 4rem;
|
|
602
|
+
}
|
|
603
|
+
.status-text {
|
|
604
|
+
font-size: 1.8rem;
|
|
605
|
+
}
|
|
606
|
+
.error-message {
|
|
607
|
+
font-size: 1.1rem;
|
|
608
|
+
margin-bottom: 30px;
|
|
609
|
+
}
|
|
610
|
+
.json-details-container {
|
|
611
|
+
font-size: 0.85rem;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
@media (max-width: 480px) {
|
|
616
|
+
body {
|
|
617
|
+
padding: 10px;
|
|
618
|
+
}
|
|
619
|
+
.error-container {
|
|
620
|
+
padding: 24px;
|
|
621
|
+
border-radius: 8px;
|
|
622
|
+
}
|
|
623
|
+
.status-code {
|
|
624
|
+
font-size: 3rem;
|
|
625
|
+
}
|
|
626
|
+
.status-text {
|
|
627
|
+
font-size: 1.5rem;
|
|
628
|
+
margin-bottom: 20px;
|
|
629
|
+
}
|
|
630
|
+
.error-message {
|
|
631
|
+
font-size: 1rem;
|
|
632
|
+
margin-bottom: 25px;
|
|
633
|
+
}
|
|
634
|
+
.json-details-container {
|
|
635
|
+
padding: 15px;
|
|
636
|
+
font-size: 0.8rem;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
</style>
|
|
640
|
+
</head>
|
|
641
|
+
<body>
|
|
642
|
+
<div class="error-container">
|
|
643
|
+
<div id="statusCode" class="status-code">${icon} ${statusCode}</div>
|
|
644
|
+
|
|
645
|
+
<div id="statusText" class="status-text">${statusMessage}</div>
|
|
646
|
+
|
|
647
|
+
<p id="errorMessage" class="error-message">${message}</p>
|
|
648
|
+
|
|
649
|
+
<!-- prettier-ignore -->
|
|
650
|
+
<div class="json-details-container"style="display: ${details ? "block" : "none"};">
|
|
651
|
+
<p class="text-sm">Technical Details:</p>
|
|
652
|
+
<pre><code id="jsonDetails">${details}</code></pre>
|
|
653
|
+
</div>
|
|
654
|
+
<div class="footer">
|
|
655
|
+
Powered by
|
|
656
|
+
<a href="${link}" target="_blank">
|
|
657
|
+
<img height="20" alt="%{poweredBy}" src="${image}" />
|
|
658
|
+
</a>
|
|
659
|
+
v${version}
|
|
660
|
+
</div>
|
|
661
|
+
</div>
|
|
662
|
+
</body>
|
|
663
|
+
</html>
|
|
664
|
+
`;
|
|
665
|
+
}
|
|
666
|
+
|
|
314
667
|
//#endregion
|
|
315
668
|
//#region packages/event-http/src/errors/error-renderer.ts
|
|
316
|
-
|
|
669
|
+
let framework = {
|
|
670
|
+
version: "0.6.1",
|
|
671
|
+
poweredBy: `wooksjs`,
|
|
672
|
+
link: `https://wooks.moost.org/`,
|
|
673
|
+
image: `https://wooks.moost.org/wooks-full-logo.png`
|
|
674
|
+
};
|
|
317
675
|
var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
676
|
+
constructor(opts) {
|
|
677
|
+
super();
|
|
678
|
+
this.opts = opts;
|
|
679
|
+
}
|
|
680
|
+
icons = {
|
|
681
|
+
401: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
|
|
682
|
+
403: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
|
|
683
|
+
404: typeof _404_tl_default === "function" ? _404_tl_default({}) : "",
|
|
684
|
+
500: typeof _500_tl_default === "function" ? _500_tl_default({}) : ""
|
|
685
|
+
};
|
|
686
|
+
static registerFramework(opts) {
|
|
687
|
+
framework = opts;
|
|
688
|
+
}
|
|
318
689
|
renderHtml(response) {
|
|
319
690
|
const data = response.body || {};
|
|
320
691
|
response.setContentType("text/html");
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
692
|
+
const hasDetails = Object.keys(data).length > 3;
|
|
693
|
+
const icon = data.statusCode >= 500 ? this.icons[500] : this.icons[data.statusCode] || "";
|
|
694
|
+
return typeof error_tl_default === "function" ? error_tl_default({
|
|
695
|
+
icon,
|
|
696
|
+
statusCode: data.statusCode,
|
|
697
|
+
statusMessage: httpStatusCodes[data.statusCode],
|
|
698
|
+
message: data.message,
|
|
699
|
+
details: hasDetails ? JSON.stringify(data, null, " ") : "",
|
|
700
|
+
version: (this.opts || framework).version,
|
|
701
|
+
poweredBy: (this.opts || framework).poweredBy,
|
|
702
|
+
link: (this.opts || framework).link,
|
|
703
|
+
image: (this.opts || framework).image
|
|
704
|
+
}) : JSON.stringify(data, null, " ");
|
|
332
705
|
}
|
|
333
706
|
renderText(response) {
|
|
334
707
|
const data = response.body || {};
|
|
@@ -338,11 +711,11 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
338
711
|
"error",
|
|
339
712
|
"message"
|
|
340
713
|
].includes(key));
|
|
341
|
-
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}
|
|
714
|
+
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? `${JSON.stringify({
|
|
342
715
|
...data,
|
|
343
|
-
statusCode:
|
|
344
|
-
message:
|
|
345
|
-
error:
|
|
716
|
+
statusCode: void 0,
|
|
717
|
+
message: void 0,
|
|
718
|
+
error: void 0
|
|
346
719
|
}, null, " ")}` : ""}`;
|
|
347
720
|
}
|
|
348
721
|
renderJson(response) {
|
|
@@ -353,15 +726,15 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
353
726
|
"error",
|
|
354
727
|
"message"
|
|
355
728
|
].includes(key));
|
|
356
|
-
return `{"statusCode":${escapeQuotes(data.statusCode)}
|
|
729
|
+
return `{"statusCode":${escapeQuotes(data.statusCode)},"error":"${escapeQuotes(data.error)}","message":"${escapeQuotes(data.message)}"${keys.length > 0 ? `,${keys.map((k) => `"${escapeQuotes(k)}":${JSON.stringify(data[k])}`).join(",")}` : ""}}`;
|
|
357
730
|
}
|
|
358
731
|
render(response) {
|
|
359
732
|
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
360
733
|
response.status = response.body?.statusCode || 500;
|
|
361
734
|
if (acceptsJson()) return this.renderJson(response);
|
|
362
|
-
else if (acceptsHtml()) return this.renderHtml(response);
|
|
363
|
-
else if (acceptsText()) return this.renderText(response);
|
|
364
|
-
else return this.renderJson(response);
|
|
735
|
+
else if (acceptsHtml()) return this.renderHtml(response);
|
|
736
|
+
else if (acceptsText()) return this.renderText(response);
|
|
737
|
+
else return this.renderJson(response);
|
|
365
738
|
}
|
|
366
739
|
};
|
|
367
740
|
function escapeQuotes(s) {
|
|
@@ -402,8 +775,8 @@ var HttpError = class extends Error {
|
|
|
402
775
|
//#region packages/event-http/src/composables/request.ts
|
|
403
776
|
const xForwardedFor = "x-forwarded-for";
|
|
404
777
|
const DEFAULT_LIMITS = {
|
|
405
|
-
maxCompressed:
|
|
406
|
-
maxInflated:
|
|
778
|
+
maxCompressed: 1 * 1024 * 1024,
|
|
779
|
+
maxInflated: 10 * 1024 * 1024,
|
|
407
780
|
maxRatio: 100,
|
|
408
781
|
readTimeoutMs: 1e4
|
|
409
782
|
};
|
|
@@ -497,12 +870,12 @@ function useRequest() {
|
|
|
497
870
|
const reqId = useEventId().getId;
|
|
498
871
|
const forwardedIp = () => init("forwardedIp", () => {
|
|
499
872
|
if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
|
|
500
|
-
else return "";
|
|
873
|
+
else return "";
|
|
501
874
|
});
|
|
502
875
|
const remoteIp = () => init("remoteIp", () => req.socket.remoteAddress || req.connection.remoteAddress || "");
|
|
503
876
|
function getIp(options) {
|
|
504
877
|
if (options?.trustProxy) return forwardedIp() || getIp();
|
|
505
|
-
else return remoteIp();
|
|
878
|
+
else return remoteIp();
|
|
506
879
|
}
|
|
507
880
|
const getIpList = () => init("ipList", () => ({
|
|
508
881
|
remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
|
|
@@ -683,7 +1056,7 @@ function useAuthorization() {
|
|
|
683
1056
|
function renderCacheControl(data) {
|
|
684
1057
|
let attrs = "";
|
|
685
1058
|
for (const [a, v] of Object.entries(data)) {
|
|
686
|
-
if (v ===
|
|
1059
|
+
if (v === void 0) continue;
|
|
687
1060
|
const func = cacheControlFunc[a];
|
|
688
1061
|
if (typeof func === "function") {
|
|
689
1062
|
const val = func(v);
|
|
@@ -904,7 +1277,7 @@ var BaseHttpResponse = class {
|
|
|
904
1277
|
async respond() {
|
|
905
1278
|
const { rawResponse, hasResponded } = useResponse();
|
|
906
1279
|
const { method, rawRequest } = useRequest();
|
|
907
|
-
const logger = useEventLogger("http-response") || console;
|
|
1280
|
+
const logger = useEventLogger$1("http-response") || console;
|
|
908
1281
|
if (hasResponded()) this.panic("The response was already sent.", logger);
|
|
909
1282
|
this.mergeHeaders();
|
|
910
1283
|
const res = rawResponse();
|
|
@@ -926,14 +1299,14 @@ var BaseHttpResponse = class {
|
|
|
926
1299
|
});
|
|
927
1300
|
stream.on("close", () => {
|
|
928
1301
|
stream.destroy();
|
|
929
|
-
resolve(
|
|
1302
|
+
resolve(void 0);
|
|
930
1303
|
});
|
|
931
1304
|
stream.pipe(res);
|
|
932
1305
|
});
|
|
933
1306
|
} else if (globalThis.Response && this.body instanceof Response) {
|
|
934
1307
|
this.mergeFetchStatus(this.body.status);
|
|
935
1308
|
if (method === "HEAD") res.end();
|
|
936
|
-
else {
|
|
1309
|
+
else {
|
|
937
1310
|
const additionalHeaders = {};
|
|
938
1311
|
if (this.body.headers.get("content-length")) additionalHeaders["content-length"] = this.body.headers.get("content-length");
|
|
939
1312
|
if (this.body.headers.get("content-type")) additionalHeaders["content-type"] = this.body.headers.get("content-type");
|
|
@@ -970,11 +1343,11 @@ function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRe
|
|
|
970
1343
|
const r = new BaseHttpResponse(errorRenderer);
|
|
971
1344
|
let httpError;
|
|
972
1345
|
if (data instanceof HttpError) httpError = data;
|
|
973
|
-
else httpError = new HttpError(500, data.message);
|
|
1346
|
+
else httpError = new HttpError(500, data.message);
|
|
974
1347
|
r.setBody(httpError.body);
|
|
975
1348
|
return r;
|
|
976
1349
|
} else if (data instanceof BaseHttpResponse) return data;
|
|
977
|
-
else return new BaseHttpResponse(renderer).setBody(data);
|
|
1350
|
+
else return new BaseHttpResponse(renderer).setBody(data);
|
|
978
1351
|
}
|
|
979
1352
|
return {
|
|
980
1353
|
createResponse,
|
|
@@ -989,7 +1362,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
989
1362
|
constructor(opts, wooks) {
|
|
990
1363
|
super(wooks, opts?.logger, opts?.router);
|
|
991
1364
|
this.opts = opts;
|
|
992
|
-
this.logger = opts?.logger || this.getLogger(
|
|
1365
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-http]`);
|
|
993
1366
|
}
|
|
994
1367
|
all(path, handler) {
|
|
995
1368
|
return this.on("*", path, handler);
|
|
@@ -1027,7 +1400,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1027
1400
|
backlog,
|
|
1028
1401
|
listeningListener
|
|
1029
1402
|
];
|
|
1030
|
-
const ui = args.indexOf(
|
|
1403
|
+
const ui = args.indexOf(void 0);
|
|
1031
1404
|
if (ui >= 0) args = args.slice(0, ui);
|
|
1032
1405
|
server.listen(...args);
|
|
1033
1406
|
});
|
|
@@ -1069,7 +1442,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1069
1442
|
}
|
|
1070
1443
|
responder = createWooksResponder();
|
|
1071
1444
|
respond(data) {
|
|
1072
|
-
|
|
1445
|
+
this.responder.respond(data)?.catch((e) => {
|
|
1073
1446
|
this.logger.error("Uncaught response exception", e);
|
|
1074
1447
|
});
|
|
1075
1448
|
}
|
|
@@ -1100,7 +1473,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1100
1473
|
this.respond(error);
|
|
1101
1474
|
return error;
|
|
1102
1475
|
}
|
|
1103
|
-
else {
|
|
1476
|
+
else {
|
|
1104
1477
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
1105
1478
|
const error = new HttpError(404);
|
|
1106
1479
|
this.respond(error);
|
|
@@ -1128,6 +1501,12 @@ else {
|
|
|
1128
1501
|
}
|
|
1129
1502
|
}
|
|
1130
1503
|
};
|
|
1504
|
+
/**
|
|
1505
|
+
* Factory for WooksHttp App
|
|
1506
|
+
* @param opts TWooksHttpOptions
|
|
1507
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
1508
|
+
* @returns WooksHttp
|
|
1509
|
+
*/
|
|
1131
1510
|
function createHttpApp(opts, wooks) {
|
|
1132
1511
|
return new WooksHttp(opts, wooks);
|
|
1133
1512
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -40,16 +40,15 @@
|
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@prostojs/router": "^0.2.1",
|
|
43
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
"dependencies": {
|
|
47
|
-
"@prostojs/logger": "^0.4.3"
|
|
43
|
+
"@prostojs/logger": "^0.4.3",
|
|
44
|
+
"@wooksjs/event-core": "^0.6.2",
|
|
45
|
+
"wooks": "^0.6.2"
|
|
48
46
|
},
|
|
47
|
+
"dependencies": {},
|
|
49
48
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-http#readme",
|
|
50
49
|
"devDependencies": {
|
|
51
|
-
"typescript": "^5.
|
|
52
|
-
"vitest": "^2.
|
|
50
|
+
"typescript": "^5.8.3",
|
|
51
|
+
"vitest": "^3.2.4"
|
|
53
52
|
},
|
|
54
53
|
"scripts": {
|
|
55
54
|
"build": "rolldown -c ../../rolldown.config.mjs"
|