@wooksjs/event-http 0.7.3 → 0.7.5
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 +608 -420
- package/dist/index.d.ts +58 -18
- package/dist/index.mjs +609 -422
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -28,9 +28,9 @@ const node_util = __toESM(require("node:util"));
|
|
|
28
28
|
const node_zlib = __toESM(require("node:zlib"));
|
|
29
29
|
const url = __toESM(require("url"));
|
|
30
30
|
const http = __toESM(require("http"));
|
|
31
|
+
const net = __toESM(require("net"));
|
|
31
32
|
const wooks = __toESM(require("wooks"));
|
|
32
33
|
const stream = __toESM(require("stream"));
|
|
33
|
-
const net = __toESM(require("net"));
|
|
34
34
|
|
|
35
35
|
//#region packages/event-http/src/http-kind.ts
|
|
36
36
|
/** Event kind definition for HTTP requests. Provides typed context slots for `req`, `response`, and `requestLimits`. */
|
|
@@ -669,430 +669,80 @@ function useHttpContext(ctx) {
|
|
|
669
669
|
}
|
|
670
670
|
|
|
671
671
|
//#endregion
|
|
672
|
-
//#region packages/event-http/src/
|
|
673
|
-
function
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
<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" />
|
|
681
|
-
|
|
682
|
-
<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" />
|
|
683
|
-
</svg>
|
|
684
|
-
`;
|
|
672
|
+
//#region packages/event-http/src/utils/time.ts
|
|
673
|
+
function convertTime(time, unit = "ms") {
|
|
674
|
+
if (typeof time === "number") return time / units[unit];
|
|
675
|
+
const rg = /(\d+)(\w+)/gu;
|
|
676
|
+
let t = 0;
|
|
677
|
+
let r;
|
|
678
|
+
while (r = rg.exec(time)) t += Number(r[1]) * (units[r[2]] || 0);
|
|
679
|
+
return t / units[unit];
|
|
685
680
|
}
|
|
681
|
+
const units = {
|
|
682
|
+
ms: 1,
|
|
683
|
+
s: 1e3,
|
|
684
|
+
m: 1e3 * 60,
|
|
685
|
+
h: 1e3 * 60 * 60,
|
|
686
|
+
d: 1e3 * 60 * 60 * 24,
|
|
687
|
+
w: 1e3 * 60 * 60 * 24 * 7,
|
|
688
|
+
M: 1e3 * 60 * 60 * 24 * 30,
|
|
689
|
+
Y: 1e3 * 60 * 60 * 24 * 365
|
|
690
|
+
};
|
|
686
691
|
|
|
687
692
|
//#endregion
|
|
688
|
-
//#region packages/event-http/src/
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
<animate attributeName="opacity"
|
|
702
|
-
dur="3s" repeatCount="indefinite"
|
|
703
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
704
|
-
values="0;1;1;0;0" />
|
|
705
|
-
</use>
|
|
706
|
-
|
|
707
|
-
<use href="#sheet" opacity="0">
|
|
708
|
-
<animateTransform attributeName="transform" type="translate"
|
|
709
|
-
dur="3s" begin="1s" repeatCount="indefinite"
|
|
710
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
711
|
-
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
712
|
-
<animate attributeName="opacity"
|
|
713
|
-
dur="3s" begin="1s" repeatCount="indefinite"
|
|
714
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
715
|
-
values="0;1;1;0;0" />
|
|
716
|
-
</use>
|
|
717
|
-
|
|
718
|
-
<use href="#sheet" opacity="0">
|
|
719
|
-
<animateTransform attributeName="transform" type="translate"
|
|
720
|
-
dur="3s" begin="2s" repeatCount="indefinite"
|
|
721
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
722
|
-
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
723
|
-
<animate attributeName="opacity"
|
|
724
|
-
dur="3s" begin="2s" repeatCount="indefinite"
|
|
725
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
726
|
-
values="0;1;1;0;0" />
|
|
727
|
-
</use>
|
|
728
|
-
</g>
|
|
729
|
-
|
|
730
|
-
<g>
|
|
731
|
-
<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"/>
|
|
732
|
-
|
|
733
|
-
<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"/>
|
|
734
|
-
</g>
|
|
735
|
-
</svg>
|
|
736
|
-
|
|
737
|
-
`;
|
|
693
|
+
//#region packages/event-http/src/utils/cache-control.ts
|
|
694
|
+
/** Renders a `TCacheControl` object into a `Cache-Control` header string. */
|
|
695
|
+
function renderCacheControl(data) {
|
|
696
|
+
let attrs = "";
|
|
697
|
+
for (const [a, v] of Object.entries(data)) {
|
|
698
|
+
if (v === void 0) continue;
|
|
699
|
+
const func = cacheControlFunc[a];
|
|
700
|
+
if (typeof func === "function") {
|
|
701
|
+
const val = func(v);
|
|
702
|
+
if (val) attrs += attrs ? `, ${val}` : val;
|
|
703
|
+
} else throw new TypeError(`Unknown Cache-Control attribute ${a}`);
|
|
704
|
+
}
|
|
705
|
+
return attrs;
|
|
738
706
|
}
|
|
707
|
+
const cacheControlFunc = {
|
|
708
|
+
mustRevalidate: (v) => v ? "must-revalidate" : "",
|
|
709
|
+
noCache: (v) => v ? typeof v === "string" ? `no-cache="${v}"` : "no-cache" : "",
|
|
710
|
+
noStore: (v) => v ? "no-store" : "",
|
|
711
|
+
noTransform: (v) => v ? "no-transform" : "",
|
|
712
|
+
public: (v) => v ? "public" : "",
|
|
713
|
+
private: (v) => v ? typeof v === "string" ? `private="${v}"` : "private" : "",
|
|
714
|
+
proxyRevalidate: (v) => v ? "proxy-revalidate" : "",
|
|
715
|
+
maxAge: (v) => `max-age=${convertTime(v, "s").toString()}`,
|
|
716
|
+
sMaxage: (v) => `s-maxage=${convertTime(v, "s").toString()}`
|
|
717
|
+
};
|
|
739
718
|
|
|
740
719
|
//#endregion
|
|
741
|
-
//#region packages/event-http/src/
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
<g id="server">
|
|
746
|
-
|
|
747
|
-
<g fill="#88888888" stroke="#88888888" stroke-width="2" >
|
|
748
|
-
<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" />
|
|
749
|
-
</g>
|
|
750
|
-
<circle cx="30" cy="60" r="6" fill="red">
|
|
751
|
-
<animate attributeName="fill" dur="0.8s"
|
|
752
|
-
values="red;#2d0000;red" repeatCount="indefinite"/>
|
|
753
|
-
</circle>
|
|
754
|
-
|
|
755
|
-
</g>
|
|
756
|
-
|
|
757
|
-
<g fill="lightgray" opacity="0.75">
|
|
758
|
-
<circle cx="50" cy="35" r="6">
|
|
759
|
-
<animate attributeName="cy" from="35" to="15" dur="2s"
|
|
760
|
-
repeatCount="indefinite"/>
|
|
761
|
-
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
762
|
-
repeatCount="indefinite"/>
|
|
763
|
-
</circle>
|
|
764
|
-
<circle cx="60" cy="40" r="4">
|
|
765
|
-
<animate attributeName="cy" from="40" to="20" dur="2s"
|
|
766
|
-
begin="0.4s" repeatCount="indefinite"/>
|
|
767
|
-
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
768
|
-
begin="0.4s" repeatCount="indefinite"/>
|
|
769
|
-
</circle>
|
|
770
|
-
</g>
|
|
771
|
-
|
|
772
|
-
</svg>
|
|
773
|
-
`;
|
|
720
|
+
//#region packages/event-http/src/utils/set-cookie.ts
|
|
721
|
+
const COOKIE_NAME_RE = /^[\w!#$%&'*+\-.^`|~]+$/;
|
|
722
|
+
function sanitizeCookieAttrValue(v) {
|
|
723
|
+
return v.replace(/[;\r\n]/g, "");
|
|
774
724
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
padding: 0 20px;
|
|
797
|
-
box-sizing: border-box;
|
|
798
|
-
transition:
|
|
799
|
-
background-color 0.3s ease,
|
|
800
|
-
color 0.3s ease;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
.error-container {
|
|
804
|
-
padding: 48px;
|
|
805
|
-
padding-bottom: 12px !important;
|
|
806
|
-
background-color: #ffffff;
|
|
807
|
-
border-radius: 0 0 12px 12px;
|
|
808
|
-
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
809
|
-
text-align: center;
|
|
810
|
-
max-width: 650px;
|
|
811
|
-
width: 100%;
|
|
812
|
-
transition:
|
|
813
|
-
background-color 0.3s ease,
|
|
814
|
-
border-color 0.3s ease,
|
|
815
|
-
box-shadow 0.3s ease;
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
.status-code {
|
|
819
|
-
font-size: 5rem;
|
|
820
|
-
font-weight: 900;
|
|
821
|
-
margin-bottom: 5px;
|
|
822
|
-
line-height: 1;
|
|
823
|
-
transition: color 0.3s ease;
|
|
824
|
-
display: flex;
|
|
825
|
-
align-items: center;
|
|
826
|
-
justify-content: center;
|
|
827
|
-
gap: 1rem;
|
|
828
|
-
position: relative;
|
|
829
|
-
margin-right: 24px;
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
.status-text {
|
|
833
|
-
font-size: 2.25rem;
|
|
834
|
-
font-weight: 700;
|
|
835
|
-
margin-bottom: 25px;
|
|
836
|
-
transition: color 0.3s ease;
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
.error-message {
|
|
840
|
-
font-size: 1.25rem;
|
|
841
|
-
margin-bottom: 40px;
|
|
842
|
-
line-height: 1.7;
|
|
843
|
-
transition: color 0.3s ease;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
.json-details-container {
|
|
847
|
-
padding: 20px;
|
|
848
|
-
border-radius: 8px;
|
|
849
|
-
text-align: left;
|
|
850
|
-
overflow-x: auto;
|
|
851
|
-
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
|
|
852
|
-
font-size: 0.9rem;
|
|
853
|
-
border: 1px solid;
|
|
854
|
-
transition:
|
|
855
|
-
background-color 0.3s ease,
|
|
856
|
-
color 0.3s ease,
|
|
857
|
-
border-color 0.3s ease;
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
.json-details-container pre {
|
|
861
|
-
margin: 0;
|
|
862
|
-
white-space: pre-wrap;
|
|
863
|
-
word-break: break-all;
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
.json-details-container code {
|
|
867
|
-
display: block;
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
body {
|
|
871
|
-
background-color: #f8fafc;
|
|
872
|
-
color: #1f2937;
|
|
873
|
-
}
|
|
874
|
-
.error-container {
|
|
875
|
-
background-color: #ffffff;
|
|
876
|
-
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
877
|
-
}
|
|
878
|
-
.status-code {
|
|
879
|
-
color: #dc2626;
|
|
880
|
-
}
|
|
881
|
-
.status-text {
|
|
882
|
-
color: #1f2937;
|
|
883
|
-
}
|
|
884
|
-
.error-message {
|
|
885
|
-
color: #4b5563;
|
|
886
|
-
}
|
|
887
|
-
.json-details-container {
|
|
888
|
-
background-color: #f0f4f8;
|
|
889
|
-
color: #374151;
|
|
890
|
-
border-color: #d1d5db;
|
|
891
|
-
}
|
|
892
|
-
.json-details-container p {
|
|
893
|
-
color: #6b7280;
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
@media (prefers-color-scheme: dark) {
|
|
897
|
-
body {
|
|
898
|
-
background-color: #2d3748;
|
|
899
|
-
color: #e2e8f0;
|
|
900
|
-
}
|
|
901
|
-
.error-container {
|
|
902
|
-
background-color: #1a202c;
|
|
903
|
-
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
|
|
904
|
-
}
|
|
905
|
-
.status-code {
|
|
906
|
-
color: #f56565;
|
|
907
|
-
}
|
|
908
|
-
.status-text {
|
|
909
|
-
color: #cbd5e1;
|
|
910
|
-
}
|
|
911
|
-
.error-message {
|
|
912
|
-
color: #a0aec0;
|
|
913
|
-
}
|
|
914
|
-
.json-details-container {
|
|
915
|
-
background-color: #2d3748;
|
|
916
|
-
color: #e2e8f0;
|
|
917
|
-
border-color: #4a5568;
|
|
918
|
-
}
|
|
919
|
-
.json-details-container p {
|
|
920
|
-
color: #a0aec0;
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
.footer {
|
|
925
|
-
display: flex;
|
|
926
|
-
gap: 0.25rem;
|
|
927
|
-
font-size: 0.6em;
|
|
928
|
-
justify-content: flex-end;
|
|
929
|
-
align-items: center;
|
|
930
|
-
margin-top: 12px;
|
|
931
|
-
opacity: 0.5;
|
|
932
|
-
transition: 0.25s ease-in-out;
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
.footer img {
|
|
936
|
-
filter: grayscale(0.5);
|
|
937
|
-
transition: 0.25s ease-in-out;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
.footer:hover {
|
|
941
|
-
opacity: 1;
|
|
942
|
-
}
|
|
943
|
-
.footer:hover img {
|
|
944
|
-
filter: grayscale(0);
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
@media (max-width: 768px) {
|
|
948
|
-
body {
|
|
949
|
-
padding: 15px;
|
|
950
|
-
}
|
|
951
|
-
.error-container {
|
|
952
|
-
padding: 32px;
|
|
953
|
-
}
|
|
954
|
-
.status-code {
|
|
955
|
-
font-size: 4rem;
|
|
956
|
-
}
|
|
957
|
-
.status-text {
|
|
958
|
-
font-size: 1.8rem;
|
|
959
|
-
}
|
|
960
|
-
.error-message {
|
|
961
|
-
font-size: 1.1rem;
|
|
962
|
-
margin-bottom: 30px;
|
|
963
|
-
}
|
|
964
|
-
.json-details-container {
|
|
965
|
-
font-size: 0.85rem;
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
@media (max-width: 480px) {
|
|
970
|
-
body {
|
|
971
|
-
padding: 10px;
|
|
972
|
-
}
|
|
973
|
-
.error-container {
|
|
974
|
-
padding: 24px;
|
|
975
|
-
border-radius: 8px;
|
|
976
|
-
}
|
|
977
|
-
.status-code {
|
|
978
|
-
font-size: 3rem;
|
|
979
|
-
}
|
|
980
|
-
.status-text {
|
|
981
|
-
font-size: 1.5rem;
|
|
982
|
-
margin-bottom: 20px;
|
|
983
|
-
}
|
|
984
|
-
.error-message {
|
|
985
|
-
font-size: 1rem;
|
|
986
|
-
margin-bottom: 25px;
|
|
987
|
-
}
|
|
988
|
-
.json-details-container {
|
|
989
|
-
padding: 15px;
|
|
990
|
-
font-size: 0.8rem;
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
</style>
|
|
994
|
-
</head>
|
|
995
|
-
<body>
|
|
996
|
-
<div class="error-container">
|
|
997
|
-
<div id="statusCode" class="status-code">${icon} ${statusCode}</div>
|
|
998
|
-
|
|
999
|
-
<div id="statusText" class="status-text">${statusMessage}</div>
|
|
1000
|
-
|
|
1001
|
-
<p id="errorMessage" class="error-message">${message}</p>
|
|
1002
|
-
|
|
1003
|
-
<!-- prettier-ignore -->
|
|
1004
|
-
<div class="json-details-container"style="display: ${details ? "block" : "none"};">
|
|
1005
|
-
<p class="text-sm">Technical Details:</p>
|
|
1006
|
-
<pre><code id="jsonDetails">${details}</code></pre>
|
|
1007
|
-
</div>
|
|
1008
|
-
<div class="footer">
|
|
1009
|
-
Powered by
|
|
1010
|
-
<a href="${link}" target="_blank">
|
|
1011
|
-
<img height="20" alt="%{poweredBy}" src="${image}" />
|
|
1012
|
-
</a>
|
|
1013
|
-
v${version}
|
|
1014
|
-
</div>
|
|
1015
|
-
</div>
|
|
1016
|
-
</body>
|
|
1017
|
-
</html>
|
|
1018
|
-
`;
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
//#endregion
|
|
1022
|
-
//#region packages/event-http/src/utils/time.ts
|
|
1023
|
-
function convertTime(time, unit = "ms") {
|
|
1024
|
-
if (typeof time === "number") return time / units[unit];
|
|
1025
|
-
const rg = /(\d+)(\w+)/gu;
|
|
1026
|
-
let t = 0;
|
|
1027
|
-
let r;
|
|
1028
|
-
while (r = rg.exec(time)) t += Number(r[1]) * (units[r[2]] || 0);
|
|
1029
|
-
return t / units[unit];
|
|
1030
|
-
}
|
|
1031
|
-
const units = {
|
|
1032
|
-
ms: 1,
|
|
1033
|
-
s: 1e3,
|
|
1034
|
-
m: 1e3 * 60,
|
|
1035
|
-
h: 1e3 * 60 * 60,
|
|
1036
|
-
d: 1e3 * 60 * 60 * 24,
|
|
1037
|
-
w: 1e3 * 60 * 60 * 24 * 7,
|
|
1038
|
-
M: 1e3 * 60 * 60 * 24 * 30,
|
|
1039
|
-
Y: 1e3 * 60 * 60 * 24 * 365
|
|
1040
|
-
};
|
|
1041
|
-
|
|
1042
|
-
//#endregion
|
|
1043
|
-
//#region packages/event-http/src/utils/cache-control.ts
|
|
1044
|
-
/** Renders a `TCacheControl` object into a `Cache-Control` header string. */
|
|
1045
|
-
function renderCacheControl(data) {
|
|
1046
|
-
let attrs = "";
|
|
1047
|
-
for (const [a, v] of Object.entries(data)) {
|
|
1048
|
-
if (v === void 0) continue;
|
|
1049
|
-
const func = cacheControlFunc[a];
|
|
1050
|
-
if (typeof func === "function") {
|
|
1051
|
-
const val = func(v);
|
|
1052
|
-
if (val) attrs += attrs ? `, ${val}` : val;
|
|
1053
|
-
} else throw new TypeError(`Unknown Cache-Control attribute ${a}`);
|
|
1054
|
-
}
|
|
1055
|
-
return attrs;
|
|
1056
|
-
}
|
|
1057
|
-
const cacheControlFunc = {
|
|
1058
|
-
mustRevalidate: (v) => v ? "must-revalidate" : "",
|
|
1059
|
-
noCache: (v) => v ? typeof v === "string" ? `no-cache="${v}"` : "no-cache" : "",
|
|
1060
|
-
noStore: (v) => v ? "no-store" : "",
|
|
1061
|
-
noTransform: (v) => v ? "no-transform" : "",
|
|
1062
|
-
public: (v) => v ? "public" : "",
|
|
1063
|
-
private: (v) => v ? typeof v === "string" ? `private="${v}"` : "private" : "",
|
|
1064
|
-
proxyRevalidate: (v) => v ? "proxy-revalidate" : "",
|
|
1065
|
-
maxAge: (v) => `max-age=${convertTime(v, "s").toString()}`,
|
|
1066
|
-
sMaxage: (v) => `s-maxage=${convertTime(v, "s").toString()}`
|
|
1067
|
-
};
|
|
1068
|
-
|
|
1069
|
-
//#endregion
|
|
1070
|
-
//#region packages/event-http/src/utils/set-cookie.ts
|
|
1071
|
-
const COOKIE_NAME_RE = /^[\w!#$%&'*+\-.^`|~]+$/;
|
|
1072
|
-
function sanitizeCookieAttrValue(v) {
|
|
1073
|
-
return v.replace(/[;\r\n]/g, "");
|
|
1074
|
-
}
|
|
1075
|
-
function renderCookie(key, data) {
|
|
1076
|
-
if (!COOKIE_NAME_RE.test(key)) throw new TypeError(`Invalid cookie name "${key}"`);
|
|
1077
|
-
let attrs = "";
|
|
1078
|
-
for (const [a, v] of Object.entries(data.attrs)) {
|
|
1079
|
-
const func = cookieAttrFunc[a];
|
|
1080
|
-
if (typeof func === "function") {
|
|
1081
|
-
const val = func(v);
|
|
1082
|
-
attrs += val ? `; ${val}` : "";
|
|
1083
|
-
} else throw new TypeError(`Unknown Set-Cookie attribute ${a}`);
|
|
1084
|
-
}
|
|
1085
|
-
return `${key}=${encodeURIComponent(data.value)}${attrs}`;
|
|
1086
|
-
}
|
|
1087
|
-
const cookieAttrFunc = {
|
|
1088
|
-
expires: (v) => `Expires=${typeof v === "string" || typeof v === "number" ? new Date(v).toUTCString() : v.toUTCString()}`,
|
|
1089
|
-
maxAge: (v) => `Max-Age=${convertTime(v, "s").toString()}`,
|
|
1090
|
-
domain: (v) => `Domain=${sanitizeCookieAttrValue(String(v))}`,
|
|
1091
|
-
path: (v) => `Path=${sanitizeCookieAttrValue(String(v))}`,
|
|
1092
|
-
secure: (v) => v ? "Secure" : "",
|
|
1093
|
-
httpOnly: (v) => v ? "HttpOnly" : "",
|
|
1094
|
-
sameSite: (v) => v ? `SameSite=${typeof v === "string" ? v : "Strict"}` : ""
|
|
1095
|
-
};
|
|
725
|
+
function renderCookie(key, data) {
|
|
726
|
+
if (!COOKIE_NAME_RE.test(key)) throw new TypeError(`Invalid cookie name "${key}"`);
|
|
727
|
+
let attrs = "";
|
|
728
|
+
for (const [a, v] of Object.entries(data.attrs)) {
|
|
729
|
+
const func = cookieAttrFunc[a];
|
|
730
|
+
if (typeof func === "function") {
|
|
731
|
+
const val = func(v);
|
|
732
|
+
attrs += val ? `; ${val}` : "";
|
|
733
|
+
} else throw new TypeError(`Unknown Set-Cookie attribute ${a}`);
|
|
734
|
+
}
|
|
735
|
+
return `${key}=${encodeURIComponent(data.value)}${attrs}`;
|
|
736
|
+
}
|
|
737
|
+
const cookieAttrFunc = {
|
|
738
|
+
expires: (v) => `Expires=${typeof v === "string" || typeof v === "number" ? new Date(v).toUTCString() : v.toUTCString()}`,
|
|
739
|
+
maxAge: (v) => `Max-Age=${convertTime(v, "s").toString()}`,
|
|
740
|
+
domain: (v) => `Domain=${sanitizeCookieAttrValue(String(v))}`,
|
|
741
|
+
path: (v) => `Path=${sanitizeCookieAttrValue(String(v))}`,
|
|
742
|
+
secure: (v) => v ? "Secure" : "",
|
|
743
|
+
httpOnly: (v) => v ? "HttpOnly" : "",
|
|
744
|
+
sameSite: (v) => v ? `SameSite=${typeof v === "string" ? v : "Strict"}` : ""
|
|
745
|
+
};
|
|
1096
746
|
|
|
1097
747
|
//#endregion
|
|
1098
748
|
//#region packages/event-http/src/response/http-response.ts
|
|
@@ -1124,10 +774,11 @@ var HttpResponse = class {
|
|
|
1124
774
|
* @param _logger - Logger instance for error reporting.
|
|
1125
775
|
* @param defaultHeaders - Optional headers to pre-populate on this response (e.g. from `securityHeaders()`).
|
|
1126
776
|
*/
|
|
1127
|
-
constructor(_res, _req, _logger, defaultHeaders) {
|
|
777
|
+
constructor(_res, _req, _logger, defaultHeaders, _captureMode = false) {
|
|
1128
778
|
this._res = _res;
|
|
1129
779
|
this._req = _req;
|
|
1130
780
|
this._logger = _logger;
|
|
781
|
+
this._captureMode = _captureMode;
|
|
1131
782
|
if (defaultHeaders) for (const key in defaultHeaders) this._headers[key] = defaultHeaders[key];
|
|
1132
783
|
}
|
|
1133
784
|
_status = 0;
|
|
@@ -1261,6 +912,53 @@ var HttpResponse = class {
|
|
|
1261
912
|
get responded() {
|
|
1262
913
|
return this._responded || !this._res.writable || this._res.writableEnded;
|
|
1263
914
|
}
|
|
915
|
+
/**
|
|
916
|
+
* Builds a Web Standard `Response` from the accumulated response state
|
|
917
|
+
* (status, headers, cookies, body) without writing to the underlying `ServerResponse`.
|
|
918
|
+
*
|
|
919
|
+
* Used by `WooksHttp.fetch()` for programmatic invocation.
|
|
920
|
+
*/
|
|
921
|
+
toWebResponse() {
|
|
922
|
+
this.finalizeCookies();
|
|
923
|
+
const body = this._body;
|
|
924
|
+
const method = this._req.method;
|
|
925
|
+
if (body instanceof stream.Readable) {
|
|
926
|
+
this.autoStatus(true);
|
|
927
|
+
return new globalThis.Response(method === "HEAD" ? null : stream.Readable.toWeb(body), {
|
|
928
|
+
status: this._status,
|
|
929
|
+
headers: this._buildWebHeaders()
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
if (hasFetchResponse && body instanceof globalThis.Response) {
|
|
933
|
+
this._status = this._status || body.status;
|
|
934
|
+
body.headers.forEach((v, k) => {
|
|
935
|
+
if (!this._headers[k]) this._headers[k] = v;
|
|
936
|
+
});
|
|
937
|
+
return new globalThis.Response(method === "HEAD" ? null : body.body, {
|
|
938
|
+
status: this._status,
|
|
939
|
+
headers: this._buildWebHeaders()
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
const rendered = this.renderBody();
|
|
943
|
+
this.autoStatus(!!rendered);
|
|
944
|
+
if (rendered) {
|
|
945
|
+
const contentLength = typeof rendered === "string" ? Buffer.byteLength(rendered) : rendered.byteLength;
|
|
946
|
+
this._headers["content-length"] = contentLength.toString();
|
|
947
|
+
}
|
|
948
|
+
const webResponse = new globalThis.Response(method === "HEAD" ? null : rendered || null, {
|
|
949
|
+
status: this._status,
|
|
950
|
+
headers: this._buildWebHeaders()
|
|
951
|
+
});
|
|
952
|
+
if (typeof rendered === "string" && rendered) webResponse.text = () => Promise.resolve(rendered);
|
|
953
|
+
if (typeof body === "object" && body !== null && !(body instanceof Uint8Array) && !(body instanceof stream.Readable) && !(hasFetchResponse && body instanceof globalThis.Response)) {
|
|
954
|
+
const original = body;
|
|
955
|
+
webResponse.json = () => Promise.resolve(original);
|
|
956
|
+
}
|
|
957
|
+
return webResponse;
|
|
958
|
+
}
|
|
959
|
+
_buildWebHeaders() {
|
|
960
|
+
return recordToWebHeaders(this._headers);
|
|
961
|
+
}
|
|
1264
962
|
renderBody() {
|
|
1265
963
|
const body = this._body;
|
|
1266
964
|
if (body === void 0 || body === null) return "";
|
|
@@ -1305,6 +1003,10 @@ var HttpResponse = class {
|
|
|
1305
1003
|
throw err;
|
|
1306
1004
|
}
|
|
1307
1005
|
this._responded = true;
|
|
1006
|
+
if (this._captureMode) {
|
|
1007
|
+
this.finalizeCookies();
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1308
1010
|
this.finalizeCookies();
|
|
1309
1011
|
const body = this._body;
|
|
1310
1012
|
const method = this._req.method;
|
|
@@ -1323,6 +1025,7 @@ var HttpResponse = class {
|
|
|
1323
1025
|
if (existing) this._headers["set-cookie"] = [...Array.isArray(existing) ? existing : [existing], ...rendered];
|
|
1324
1026
|
else this._headers["set-cookie"] = rendered;
|
|
1325
1027
|
}
|
|
1028
|
+
this._hasCookies = false;
|
|
1326
1029
|
}
|
|
1327
1030
|
autoStatus(hasBody) {
|
|
1328
1031
|
if (this._status) return;
|
|
@@ -1384,11 +1087,368 @@ var HttpResponse = class {
|
|
|
1384
1087
|
this._res.writeHead(this._status, this._headers).end(method === "HEAD" ? "" : renderedBody);
|
|
1385
1088
|
}
|
|
1386
1089
|
};
|
|
1090
|
+
/** Converts a Record of headers to a Web Standard `Headers` object. */
|
|
1091
|
+
function recordToWebHeaders(record) {
|
|
1092
|
+
const headers = new Headers();
|
|
1093
|
+
for (const [key, value] of Object.entries(record)) if (Array.isArray(value)) for (const v of value) headers.append(key, v);
|
|
1094
|
+
else if (value) headers.set(key, value);
|
|
1095
|
+
return headers;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
//#endregion
|
|
1099
|
+
//#region packages/event-http/src/errors/403.tl.svg
|
|
1100
|
+
function _403_tl_default(ctx) {
|
|
1101
|
+
return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
|
|
1102
|
+
<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">
|
|
1103
|
+
<animate attributeName="fill" dur="2s" repeatCount="indefinite"
|
|
1104
|
+
values="#ff000000;#ff000050;#ff000000" />
|
|
1105
|
+
</path>
|
|
1106
|
+
|
|
1107
|
+
<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" />
|
|
1108
|
+
|
|
1109
|
+
<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" />
|
|
1110
|
+
</svg>
|
|
1111
|
+
`;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
//#endregion
|
|
1115
|
+
//#region packages/event-http/src/errors/404.tl.svg
|
|
1116
|
+
function _404_tl_default(ctx) {
|
|
1117
|
+
return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
1118
|
+
<defs>
|
|
1119
|
+
<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"/>
|
|
1120
|
+
</defs>
|
|
1121
|
+
|
|
1122
|
+
<g id="queue" transform="translate(-5 -10)">
|
|
1123
|
+
<use href="#sheet" opacity="0">
|
|
1124
|
+
<animateTransform attributeName="transform" type="translate"
|
|
1125
|
+
dur="3s" repeatCount="indefinite"
|
|
1126
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1127
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1128
|
+
<animate attributeName="opacity"
|
|
1129
|
+
dur="3s" repeatCount="indefinite"
|
|
1130
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1131
|
+
values="0;1;1;0;0" />
|
|
1132
|
+
</use>
|
|
1133
|
+
|
|
1134
|
+
<use href="#sheet" opacity="0">
|
|
1135
|
+
<animateTransform attributeName="transform" type="translate"
|
|
1136
|
+
dur="3s" begin="1s" repeatCount="indefinite"
|
|
1137
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1138
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1139
|
+
<animate attributeName="opacity"
|
|
1140
|
+
dur="3s" begin="1s" repeatCount="indefinite"
|
|
1141
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1142
|
+
values="0;1;1;0;0" />
|
|
1143
|
+
</use>
|
|
1144
|
+
|
|
1145
|
+
<use href="#sheet" opacity="0">
|
|
1146
|
+
<animateTransform attributeName="transform" type="translate"
|
|
1147
|
+
dur="3s" begin="2s" repeatCount="indefinite"
|
|
1148
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1149
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1150
|
+
<animate attributeName="opacity"
|
|
1151
|
+
dur="3s" begin="2s" repeatCount="indefinite"
|
|
1152
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
1153
|
+
values="0;1;1;0;0" />
|
|
1154
|
+
</use>
|
|
1155
|
+
</g>
|
|
1156
|
+
|
|
1157
|
+
<g>
|
|
1158
|
+
<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"/>
|
|
1159
|
+
|
|
1160
|
+
<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"/>
|
|
1161
|
+
</g>
|
|
1162
|
+
</svg>
|
|
1163
|
+
|
|
1164
|
+
`;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
//#endregion
|
|
1168
|
+
//#region packages/event-http/src/errors/500.tl.svg
|
|
1169
|
+
function _500_tl_default(ctx) {
|
|
1170
|
+
return `<svg height="64" viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
|
|
1171
|
+
|
|
1172
|
+
<g id="server">
|
|
1173
|
+
|
|
1174
|
+
<g fill="#88888888" stroke="#88888888" stroke-width="2" >
|
|
1175
|
+
<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" />
|
|
1176
|
+
</g>
|
|
1177
|
+
<circle cx="30" cy="60" r="6" fill="red">
|
|
1178
|
+
<animate attributeName="fill" dur="0.8s"
|
|
1179
|
+
values="red;#2d0000;red" repeatCount="indefinite"/>
|
|
1180
|
+
</circle>
|
|
1181
|
+
|
|
1182
|
+
</g>
|
|
1183
|
+
|
|
1184
|
+
<g fill="lightgray" opacity="0.75">
|
|
1185
|
+
<circle cx="50" cy="35" r="6">
|
|
1186
|
+
<animate attributeName="cy" from="35" to="15" dur="2s"
|
|
1187
|
+
repeatCount="indefinite"/>
|
|
1188
|
+
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
1189
|
+
repeatCount="indefinite"/>
|
|
1190
|
+
</circle>
|
|
1191
|
+
<circle cx="60" cy="40" r="4">
|
|
1192
|
+
<animate attributeName="cy" from="40" to="20" dur="2s"
|
|
1193
|
+
begin="0.4s" repeatCount="indefinite"/>
|
|
1194
|
+
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
1195
|
+
begin="0.4s" repeatCount="indefinite"/>
|
|
1196
|
+
</circle>
|
|
1197
|
+
</g>
|
|
1198
|
+
|
|
1199
|
+
</svg>
|
|
1200
|
+
`;
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
//#endregion
|
|
1204
|
+
//#region packages/event-http/src/errors/error.tl.html
|
|
1205
|
+
function error_tl_default(ctx) {
|
|
1206
|
+
const { statusCode, statusMessage, icon, message, details, link, image, version } = ctx;
|
|
1207
|
+
return `<!doctype html>
|
|
1208
|
+
<html lang="en">
|
|
1209
|
+
<head>
|
|
1210
|
+
<meta charset="UTF-8" />
|
|
1211
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
1212
|
+
<title>${statusCode} ${statusMessage}</title>
|
|
1213
|
+
<style>
|
|
1214
|
+
body {
|
|
1215
|
+
font-family:
|
|
1216
|
+
-apple-system, BlinkMacMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
|
|
1217
|
+
'Open Sans', 'Helvetica Neue', sans-serif;
|
|
1218
|
+
display: flex;
|
|
1219
|
+
justify-content: center;
|
|
1220
|
+
align-items: flex-start;
|
|
1221
|
+
min-height: 100vh;
|
|
1222
|
+
margin: 0;
|
|
1223
|
+
padding: 0 20px;
|
|
1224
|
+
box-sizing: border-box;
|
|
1225
|
+
transition:
|
|
1226
|
+
background-color 0.3s ease,
|
|
1227
|
+
color 0.3s ease;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
.error-container {
|
|
1231
|
+
padding: 48px;
|
|
1232
|
+
padding-bottom: 12px !important;
|
|
1233
|
+
background-color: #ffffff;
|
|
1234
|
+
border-radius: 0 0 12px 12px;
|
|
1235
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
1236
|
+
text-align: center;
|
|
1237
|
+
max-width: 650px;
|
|
1238
|
+
width: 100%;
|
|
1239
|
+
transition:
|
|
1240
|
+
background-color 0.3s ease,
|
|
1241
|
+
border-color 0.3s ease,
|
|
1242
|
+
box-shadow 0.3s ease;
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
.status-code {
|
|
1246
|
+
font-size: 5rem;
|
|
1247
|
+
font-weight: 900;
|
|
1248
|
+
margin-bottom: 5px;
|
|
1249
|
+
line-height: 1;
|
|
1250
|
+
transition: color 0.3s ease;
|
|
1251
|
+
display: flex;
|
|
1252
|
+
align-items: center;
|
|
1253
|
+
justify-content: center;
|
|
1254
|
+
gap: 1rem;
|
|
1255
|
+
position: relative;
|
|
1256
|
+
margin-right: 24px;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
.status-text {
|
|
1260
|
+
font-size: 2.25rem;
|
|
1261
|
+
font-weight: 700;
|
|
1262
|
+
margin-bottom: 25px;
|
|
1263
|
+
transition: color 0.3s ease;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
.error-message {
|
|
1267
|
+
font-size: 1.25rem;
|
|
1268
|
+
margin-bottom: 40px;
|
|
1269
|
+
line-height: 1.7;
|
|
1270
|
+
transition: color 0.3s ease;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
.json-details-container {
|
|
1274
|
+
padding: 20px;
|
|
1275
|
+
border-radius: 8px;
|
|
1276
|
+
text-align: left;
|
|
1277
|
+
overflow-x: auto;
|
|
1278
|
+
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
|
|
1279
|
+
font-size: 0.9rem;
|
|
1280
|
+
border: 1px solid;
|
|
1281
|
+
transition:
|
|
1282
|
+
background-color 0.3s ease,
|
|
1283
|
+
color 0.3s ease,
|
|
1284
|
+
border-color 0.3s ease;
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
.json-details-container pre {
|
|
1288
|
+
margin: 0;
|
|
1289
|
+
white-space: pre-wrap;
|
|
1290
|
+
word-break: break-all;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
.json-details-container code {
|
|
1294
|
+
display: block;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
body {
|
|
1298
|
+
background-color: #f8fafc;
|
|
1299
|
+
color: #1f2937;
|
|
1300
|
+
}
|
|
1301
|
+
.error-container {
|
|
1302
|
+
background-color: #ffffff;
|
|
1303
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
|
|
1304
|
+
}
|
|
1305
|
+
.status-code {
|
|
1306
|
+
color: #dc2626;
|
|
1307
|
+
}
|
|
1308
|
+
.status-text {
|
|
1309
|
+
color: #1f2937;
|
|
1310
|
+
}
|
|
1311
|
+
.error-message {
|
|
1312
|
+
color: #4b5563;
|
|
1313
|
+
}
|
|
1314
|
+
.json-details-container {
|
|
1315
|
+
background-color: #f0f4f8;
|
|
1316
|
+
color: #374151;
|
|
1317
|
+
border-color: #d1d5db;
|
|
1318
|
+
}
|
|
1319
|
+
.json-details-container p {
|
|
1320
|
+
color: #6b7280;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
@media (prefers-color-scheme: dark) {
|
|
1324
|
+
body {
|
|
1325
|
+
background-color: #2d3748;
|
|
1326
|
+
color: #e2e8f0;
|
|
1327
|
+
}
|
|
1328
|
+
.error-container {
|
|
1329
|
+
background-color: #1a202c;
|
|
1330
|
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
|
|
1331
|
+
}
|
|
1332
|
+
.status-code {
|
|
1333
|
+
color: #f56565;
|
|
1334
|
+
}
|
|
1335
|
+
.status-text {
|
|
1336
|
+
color: #cbd5e1;
|
|
1337
|
+
}
|
|
1338
|
+
.error-message {
|
|
1339
|
+
color: #a0aec0;
|
|
1340
|
+
}
|
|
1341
|
+
.json-details-container {
|
|
1342
|
+
background-color: #2d3748;
|
|
1343
|
+
color: #e2e8f0;
|
|
1344
|
+
border-color: #4a5568;
|
|
1345
|
+
}
|
|
1346
|
+
.json-details-container p {
|
|
1347
|
+
color: #a0aec0;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
.footer {
|
|
1352
|
+
display: flex;
|
|
1353
|
+
gap: 0.25rem;
|
|
1354
|
+
font-size: 0.6em;
|
|
1355
|
+
justify-content: flex-end;
|
|
1356
|
+
align-items: center;
|
|
1357
|
+
margin-top: 12px;
|
|
1358
|
+
opacity: 0.5;
|
|
1359
|
+
transition: 0.25s ease-in-out;
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
.footer img {
|
|
1363
|
+
filter: grayscale(0.5);
|
|
1364
|
+
transition: 0.25s ease-in-out;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
.footer:hover {
|
|
1368
|
+
opacity: 1;
|
|
1369
|
+
}
|
|
1370
|
+
.footer:hover img {
|
|
1371
|
+
filter: grayscale(0);
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
@media (max-width: 768px) {
|
|
1375
|
+
body {
|
|
1376
|
+
padding: 15px;
|
|
1377
|
+
}
|
|
1378
|
+
.error-container {
|
|
1379
|
+
padding: 32px;
|
|
1380
|
+
}
|
|
1381
|
+
.status-code {
|
|
1382
|
+
font-size: 4rem;
|
|
1383
|
+
}
|
|
1384
|
+
.status-text {
|
|
1385
|
+
font-size: 1.8rem;
|
|
1386
|
+
}
|
|
1387
|
+
.error-message {
|
|
1388
|
+
font-size: 1.1rem;
|
|
1389
|
+
margin-bottom: 30px;
|
|
1390
|
+
}
|
|
1391
|
+
.json-details-container {
|
|
1392
|
+
font-size: 0.85rem;
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
@media (max-width: 480px) {
|
|
1397
|
+
body {
|
|
1398
|
+
padding: 10px;
|
|
1399
|
+
}
|
|
1400
|
+
.error-container {
|
|
1401
|
+
padding: 24px;
|
|
1402
|
+
border-radius: 8px;
|
|
1403
|
+
}
|
|
1404
|
+
.status-code {
|
|
1405
|
+
font-size: 3rem;
|
|
1406
|
+
}
|
|
1407
|
+
.status-text {
|
|
1408
|
+
font-size: 1.5rem;
|
|
1409
|
+
margin-bottom: 20px;
|
|
1410
|
+
}
|
|
1411
|
+
.error-message {
|
|
1412
|
+
font-size: 1rem;
|
|
1413
|
+
margin-bottom: 25px;
|
|
1414
|
+
}
|
|
1415
|
+
.json-details-container {
|
|
1416
|
+
padding: 15px;
|
|
1417
|
+
font-size: 0.8rem;
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
</style>
|
|
1421
|
+
</head>
|
|
1422
|
+
<body>
|
|
1423
|
+
<div class="error-container">
|
|
1424
|
+
<div id="statusCode" class="status-code">${icon} ${statusCode}</div>
|
|
1425
|
+
|
|
1426
|
+
<div id="statusText" class="status-text">${statusMessage}</div>
|
|
1427
|
+
|
|
1428
|
+
<p id="errorMessage" class="error-message">${message}</p>
|
|
1429
|
+
|
|
1430
|
+
<!-- prettier-ignore -->
|
|
1431
|
+
<div class="json-details-container"style="display: ${details ? "block" : "none"};">
|
|
1432
|
+
<p class="text-sm">Technical Details:</p>
|
|
1433
|
+
<pre><code id="jsonDetails">${details}</code></pre>
|
|
1434
|
+
</div>
|
|
1435
|
+
<div class="footer">
|
|
1436
|
+
Powered by
|
|
1437
|
+
<a href="${link}" target="_blank">
|
|
1438
|
+
<img height="20" alt="%{poweredBy}" src="${image}" />
|
|
1439
|
+
</a>
|
|
1440
|
+
v${version}
|
|
1441
|
+
</div>
|
|
1442
|
+
</div>
|
|
1443
|
+
</body>
|
|
1444
|
+
</html>
|
|
1445
|
+
`;
|
|
1446
|
+
}
|
|
1387
1447
|
|
|
1388
1448
|
//#endregion
|
|
1389
1449
|
//#region packages/event-http/src/response/wooks-http-response.ts
|
|
1390
1450
|
let framework = {
|
|
1391
|
-
version: "0.7.
|
|
1451
|
+
version: "0.7.4",
|
|
1392
1452
|
poweredBy: "wooksjs",
|
|
1393
1453
|
link: "https://wooks.moost.org/",
|
|
1394
1454
|
image: "https://wooks.moost.org/wooks-full-logo.svg"
|
|
@@ -1459,6 +1519,13 @@ function renderErrorText(data) {
|
|
|
1459
1519
|
|
|
1460
1520
|
//#endregion
|
|
1461
1521
|
//#region packages/event-http/src/http-adapter.ts
|
|
1522
|
+
const DEFAULT_FORWARD_HEADERS = [
|
|
1523
|
+
"authorization",
|
|
1524
|
+
"cookie",
|
|
1525
|
+
"accept-language",
|
|
1526
|
+
"x-forwarded-for",
|
|
1527
|
+
"x-request-id"
|
|
1528
|
+
];
|
|
1462
1529
|
/** HTTP adapter for Wooks that provides route registration, server lifecycle, and request handling. */
|
|
1463
1530
|
var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
1464
1531
|
logger;
|
|
@@ -1720,7 +1787,127 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1720
1787
|
}
|
|
1721
1788
|
}
|
|
1722
1789
|
}
|
|
1790
|
+
/**
|
|
1791
|
+
* Programmatic route invocation using the Web Standard fetch API.
|
|
1792
|
+
* Goes through the full dispatch pipeline: context creation, route matching,
|
|
1793
|
+
* handler execution, response finalization.
|
|
1794
|
+
*
|
|
1795
|
+
* When called from within an existing HTTP context (e.g. during SSR),
|
|
1796
|
+
* identity headers (authorization, cookie) are automatically forwarded
|
|
1797
|
+
* from the calling request unless already present on the given Request.
|
|
1798
|
+
*
|
|
1799
|
+
* @param request - A Web Standard Request object.
|
|
1800
|
+
* @returns A Web Standard Response.
|
|
1801
|
+
*/
|
|
1802
|
+
async fetch(request) {
|
|
1803
|
+
const url$1 = new URL(request.url);
|
|
1804
|
+
const method = request.method;
|
|
1805
|
+
const pathname = url$1.pathname + url$1.search;
|
|
1806
|
+
const callerCtx = (0, __wooksjs_event_core.tryGetCurrent)();
|
|
1807
|
+
let callerReq;
|
|
1808
|
+
if (callerCtx) try {
|
|
1809
|
+
callerReq = callerCtx.get(httpKind.keys.req);
|
|
1810
|
+
} catch {}
|
|
1811
|
+
const fakeReq = createFakeIncomingMessage(request, pathname, callerReq, this.opts?.forwardHeaders);
|
|
1812
|
+
const fakeRes = new http.ServerResponse(fakeReq);
|
|
1813
|
+
const rawChunks = [];
|
|
1814
|
+
const rawHeaders = {};
|
|
1815
|
+
let rawStatusCode = 0;
|
|
1816
|
+
fakeRes.writeHead = (...args) => {
|
|
1817
|
+
rawStatusCode = args[0];
|
|
1818
|
+
for (const arg of args) if (typeof arg === "object" && arg !== null) for (const [k, v] of Object.entries(arg)) rawHeaders[k] = v;
|
|
1819
|
+
return fakeRes;
|
|
1820
|
+
};
|
|
1821
|
+
fakeRes.write = (chunk, ...args) => {
|
|
1822
|
+
if (chunk !== null && chunk !== void 0) rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
|
|
1823
|
+
const cb = args.find((a) => typeof a === "function");
|
|
1824
|
+
if (cb) cb();
|
|
1825
|
+
return true;
|
|
1826
|
+
};
|
|
1827
|
+
fakeRes.end = (chunk, ...args) => {
|
|
1828
|
+
if (chunk !== null && chunk !== void 0 && typeof chunk !== "function") rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
|
|
1829
|
+
const cb = args.find((a) => typeof a === "function");
|
|
1830
|
+
if (cb) cb();
|
|
1831
|
+
return fakeRes;
|
|
1832
|
+
};
|
|
1833
|
+
const response = new this.ResponseClass(fakeRes, fakeReq, this.logger, this.opts?.defaultHeaders, true);
|
|
1834
|
+
let bodyBuffer;
|
|
1835
|
+
if (request.body) bodyBuffer = buffer.Buffer.from(await request.bytes());
|
|
1836
|
+
const ctxOptions = this.eventContextOptions;
|
|
1837
|
+
const requestLimits = this.opts?.requestLimits;
|
|
1838
|
+
const notFoundHandler = this.opts?.onNotFound;
|
|
1839
|
+
return createHttpContext(ctxOptions, {
|
|
1840
|
+
req: fakeReq,
|
|
1841
|
+
response,
|
|
1842
|
+
requestLimits
|
|
1843
|
+
}, async () => {
|
|
1844
|
+
const ctx = (0, __wooksjs_event_core.current)();
|
|
1845
|
+
if (bodyBuffer) ctx.set(rawBodySlot, Promise.resolve(bodyBuffer));
|
|
1846
|
+
try {
|
|
1847
|
+
const handlers = this.wooks.lookupHandlers(method, pathname, ctx);
|
|
1848
|
+
if (handlers || notFoundHandler) {
|
|
1849
|
+
const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
|
|
1850
|
+
if (result !== null && result !== void 0 && typeof result.then === "function") await result.catch((error) => {
|
|
1851
|
+
if (!response.responded) this.respond(error, response, ctx);
|
|
1852
|
+
});
|
|
1853
|
+
} else {
|
|
1854
|
+
const error = new HttpError(404);
|
|
1855
|
+
this.respond(error, response, ctx);
|
|
1856
|
+
}
|
|
1857
|
+
} finally {
|
|
1858
|
+
fakeReq.emit("end");
|
|
1859
|
+
fakeReq.emit("close");
|
|
1860
|
+
}
|
|
1861
|
+
let webResponse;
|
|
1862
|
+
if (rawChunks.length > 0 || rawStatusCode > 0) {
|
|
1863
|
+
const body = buffer.Buffer.concat(rawChunks);
|
|
1864
|
+
webResponse = new Response(body.length > 0 ? body : null, {
|
|
1865
|
+
status: rawStatusCode || 200,
|
|
1866
|
+
headers: recordToWebHeaders(rawHeaders)
|
|
1867
|
+
});
|
|
1868
|
+
} else webResponse = response.toWebResponse();
|
|
1869
|
+
if (callerReq) try {
|
|
1870
|
+
const parentResponse = callerCtx?.get(httpKind.keys.response);
|
|
1871
|
+
if (parentResponse) for (const cookie of webResponse.headers.getSetCookie()) parentResponse.setCookieRaw(cookie);
|
|
1872
|
+
} catch {}
|
|
1873
|
+
fakeReq.destroy();
|
|
1874
|
+
fakeRes.destroy();
|
|
1875
|
+
return webResponse;
|
|
1876
|
+
});
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Convenience wrapper for programmatic route invocation.
|
|
1880
|
+
* Accepts a URL string (relative paths auto-prefixed with `http://localhost`),
|
|
1881
|
+
* URL object, or Request, plus optional `RequestInit`.
|
|
1882
|
+
*
|
|
1883
|
+
* @param input - URL string, URL object, or Request.
|
|
1884
|
+
* @param init - Optional RequestInit (method, headers, body, etc.).
|
|
1885
|
+
* @returns A Web Standard Response.
|
|
1886
|
+
*/
|
|
1887
|
+
request(input, init) {
|
|
1888
|
+
if (typeof input === "string" && !input.startsWith("http://") && !input.startsWith("https://")) input = `http://localhost${input.startsWith("/") ? "" : "/"}${input}`;
|
|
1889
|
+
const req = input instanceof Request ? input : new Request(input, init);
|
|
1890
|
+
return this.fetch(req);
|
|
1891
|
+
}
|
|
1723
1892
|
};
|
|
1893
|
+
function createFakeIncomingMessage(request, pathname, forwardFrom, forwardHeaders) {
|
|
1894
|
+
const req = new http.IncomingMessage(new net.Socket({}));
|
|
1895
|
+
req.method = request.method;
|
|
1896
|
+
req.url = pathname;
|
|
1897
|
+
const headers = {};
|
|
1898
|
+
if (forwardFrom && forwardHeaders !== false) {
|
|
1899
|
+
const headerList = Array.isArray(forwardHeaders) ? forwardHeaders : DEFAULT_FORWARD_HEADERS;
|
|
1900
|
+
for (const h of headerList) {
|
|
1901
|
+
const val = forwardFrom.headers[h];
|
|
1902
|
+
if (typeof val === "string" && val) headers[h] = val;
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
request.headers.forEach((value, key) => {
|
|
1906
|
+
headers[key] = value;
|
|
1907
|
+
});
|
|
1908
|
+
req.headers = headers;
|
|
1909
|
+
return req;
|
|
1910
|
+
}
|
|
1724
1911
|
/**
|
|
1725
1912
|
* Creates a new WooksHttp application instance.
|
|
1726
1913
|
* @example
|
|
@@ -1844,6 +2031,7 @@ exports.httpKind = httpKind;
|
|
|
1844
2031
|
exports.httpStatusCodes = httpStatusCodes;
|
|
1845
2032
|
exports.prepareTestHttpContext = prepareTestHttpContext;
|
|
1846
2033
|
exports.rawBodySlot = rawBodySlot;
|
|
2034
|
+
exports.recordToWebHeaders = recordToWebHeaders;
|
|
1847
2035
|
exports.renderCacheControl = renderCacheControl;
|
|
1848
2036
|
exports.securityHeaders = securityHeaders;
|
|
1849
2037
|
exports.useAccept = useAccept;
|