@wooksjs/event-http 0.7.2 → 0.7.4
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 +481 -484
- package/dist/index.d.ts +23 -23
- package/dist/index.mjs +481 -484
- package/package.json +6 -6
- package/skills/wooksjs-event-http/request.md +7 -7
package/dist/index.cjs
CHANGED
|
@@ -27,9 +27,9 @@ const node_stream = __toESM(require("node:stream"));
|
|
|
27
27
|
const node_util = __toESM(require("node:util"));
|
|
28
28
|
const node_zlib = __toESM(require("node:zlib"));
|
|
29
29
|
const url = __toESM(require("url"));
|
|
30
|
-
const stream = __toESM(require("stream"));
|
|
31
30
|
const http = __toESM(require("http"));
|
|
32
31
|
const wooks = __toESM(require("wooks"));
|
|
32
|
+
const stream = __toESM(require("stream"));
|
|
33
33
|
const net = __toESM(require("net"));
|
|
34
34
|
|
|
35
35
|
//#region packages/event-http/src/http-kind.ts
|
|
@@ -658,491 +658,119 @@ const useUrlParams = (0, __wooksjs_event_core.defineWook)((ctx) => ({
|
|
|
658
658
|
}));
|
|
659
659
|
|
|
660
660
|
//#endregion
|
|
661
|
-
//#region packages/event-http/src/
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
return
|
|
661
|
+
//#region packages/event-http/src/event-http.ts
|
|
662
|
+
/** Creates an HTTP event context and runs `fn` inside it. */
|
|
663
|
+
function createHttpContext(options, seeds, fn) {
|
|
664
|
+
return (0, __wooksjs_event_core.createEventContext)(options, httpKind, seeds, fn);
|
|
665
|
+
}
|
|
666
|
+
/** Returns the current HTTP event context. */
|
|
667
|
+
function useHttpContext(ctx) {
|
|
668
|
+
return ctx ?? (0, __wooksjs_event_core.current)();
|
|
669
669
|
}
|
|
670
|
-
const units = {
|
|
671
|
-
ms: 1,
|
|
672
|
-
s: 1e3,
|
|
673
|
-
m: 1e3 * 60,
|
|
674
|
-
h: 1e3 * 60 * 60,
|
|
675
|
-
d: 1e3 * 60 * 60 * 24,
|
|
676
|
-
w: 1e3 * 60 * 60 * 24 * 7,
|
|
677
|
-
M: 1e3 * 60 * 60 * 24 * 30,
|
|
678
|
-
Y: 1e3 * 60 * 60 * 24 * 365
|
|
679
|
-
};
|
|
680
670
|
|
|
681
671
|
//#endregion
|
|
682
|
-
//#region packages/event-http/src/
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
672
|
+
//#region packages/event-http/src/errors/403.tl.svg
|
|
673
|
+
function _403_tl_default(ctx) {
|
|
674
|
+
return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
|
|
675
|
+
<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">
|
|
676
|
+
<animate attributeName="fill" dur="2s" repeatCount="indefinite"
|
|
677
|
+
values="#ff000000;#ff000050;#ff000000" />
|
|
678
|
+
</path>
|
|
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
|
+
`;
|
|
695
685
|
}
|
|
696
|
-
const cacheControlFunc = {
|
|
697
|
-
mustRevalidate: (v) => v ? "must-revalidate" : "",
|
|
698
|
-
noCache: (v) => v ? typeof v === "string" ? `no-cache="${v}"` : "no-cache" : "",
|
|
699
|
-
noStore: (v) => v ? "no-store" : "",
|
|
700
|
-
noTransform: (v) => v ? "no-transform" : "",
|
|
701
|
-
public: (v) => v ? "public" : "",
|
|
702
|
-
private: (v) => v ? typeof v === "string" ? `private="${v}"` : "private" : "",
|
|
703
|
-
proxyRevalidate: (v) => v ? "proxy-revalidate" : "",
|
|
704
|
-
maxAge: (v) => `max-age=${convertTime(v, "s").toString()}`,
|
|
705
|
-
sMaxage: (v) => `s-maxage=${convertTime(v, "s").toString()}`
|
|
706
|
-
};
|
|
707
686
|
|
|
708
687
|
//#endregion
|
|
709
|
-
//#region packages/event-http/src/
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
688
|
+
//#region packages/event-http/src/errors/404.tl.svg
|
|
689
|
+
function _404_tl_default(ctx) {
|
|
690
|
+
return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
691
|
+
<defs>
|
|
692
|
+
<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"/>
|
|
693
|
+
</defs>
|
|
694
|
+
|
|
695
|
+
<g id="queue" transform="translate(-5 -10)">
|
|
696
|
+
<use href="#sheet" opacity="0">
|
|
697
|
+
<animateTransform attributeName="transform" type="translate"
|
|
698
|
+
dur="3s" repeatCount="indefinite"
|
|
699
|
+
keyTimes="0;0.1;0.32;0.42;1"
|
|
700
|
+
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
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
|
+
`;
|
|
713
738
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
* All header mutations are accumulated in memory and flushed in a single `writeHead()` call
|
|
750
|
-
* when `send()` is invoked. Setter methods are chainable.
|
|
751
|
-
*
|
|
752
|
-
* @example
|
|
753
|
-
* ```ts
|
|
754
|
-
* const response = useResponse()
|
|
755
|
-
* response.setStatus(200).setHeader('x-custom', 'value')
|
|
756
|
-
* response.setCookie('session', 'abc', { httpOnly: true })
|
|
757
|
-
* ```
|
|
758
|
-
*/
|
|
759
|
-
var HttpResponse = class {
|
|
760
|
-
/**
|
|
761
|
-
* @param _res - The underlying Node.js `ServerResponse`.
|
|
762
|
-
* @param _req - The underlying Node.js `IncomingMessage`.
|
|
763
|
-
* @param _logger - Logger instance for error reporting.
|
|
764
|
-
* @param defaultHeaders - Optional headers to pre-populate on this response (e.g. from `securityHeaders()`).
|
|
765
|
-
*/
|
|
766
|
-
constructor(_res, _req, _logger, defaultHeaders) {
|
|
767
|
-
this._res = _res;
|
|
768
|
-
this._req = _req;
|
|
769
|
-
this._logger = _logger;
|
|
770
|
-
if (defaultHeaders) for (const key in defaultHeaders) this._headers[key] = defaultHeaders[key];
|
|
771
|
-
}
|
|
772
|
-
_status = 0;
|
|
773
|
-
_body = void 0;
|
|
774
|
-
_headers = {};
|
|
775
|
-
_cookies = {};
|
|
776
|
-
_rawCookies = [];
|
|
777
|
-
_hasCookies = false;
|
|
778
|
-
_responded = false;
|
|
779
|
-
/** The HTTP status code. If not set, it is inferred automatically when `send()` is called. */
|
|
780
|
-
get status() {
|
|
781
|
-
return this._status;
|
|
782
|
-
}
|
|
783
|
-
set status(value) {
|
|
784
|
-
this._status = value;
|
|
785
|
-
}
|
|
786
|
-
/** Sets the HTTP status code (chainable). */
|
|
787
|
-
setStatus(value) {
|
|
788
|
-
this._status = value;
|
|
789
|
-
return this;
|
|
790
|
-
}
|
|
791
|
-
/** The response body. Automatically serialized by `send()` (objects → JSON, strings → text). */
|
|
792
|
-
get body() {
|
|
793
|
-
return this._body;
|
|
794
|
-
}
|
|
795
|
-
set body(value) {
|
|
796
|
-
this._body = value;
|
|
797
|
-
}
|
|
798
|
-
/** Sets the response body (chainable). */
|
|
799
|
-
setBody(value) {
|
|
800
|
-
this._body = value;
|
|
801
|
-
return this;
|
|
802
|
-
}
|
|
803
|
-
/** Sets a single response header (chainable). Arrays produce multi-value headers. */
|
|
804
|
-
setHeader(name, value) {
|
|
805
|
-
this._headers[name] = Array.isArray(value) ? value : value.toString();
|
|
806
|
-
return this;
|
|
807
|
-
}
|
|
808
|
-
/** Batch-sets multiple response headers from a record (chainable). Existing keys are overwritten. */
|
|
809
|
-
setHeaders(headers) {
|
|
810
|
-
for (const key in headers) this._headers[key] = headers[key];
|
|
811
|
-
return this;
|
|
812
|
-
}
|
|
813
|
-
/** Returns the value of a response header, or `undefined` if not set. */
|
|
814
|
-
getHeader(name) {
|
|
815
|
-
return this._headers[name];
|
|
816
|
-
}
|
|
817
|
-
/** Removes a response header (chainable). */
|
|
818
|
-
removeHeader(name) {
|
|
819
|
-
delete this._headers[name];
|
|
820
|
-
return this;
|
|
821
|
-
}
|
|
822
|
-
/** Returns a read-only snapshot of all response headers. */
|
|
823
|
-
headers() {
|
|
824
|
-
return this._headers;
|
|
825
|
-
}
|
|
826
|
-
/** Sets the `Content-Type` response header (chainable). */
|
|
827
|
-
setContentType(value) {
|
|
828
|
-
this._headers["content-type"] = value;
|
|
829
|
-
return this;
|
|
830
|
-
}
|
|
831
|
-
/** Returns the current `Content-Type` header value. */
|
|
832
|
-
getContentType() {
|
|
833
|
-
return this._headers["content-type"];
|
|
834
|
-
}
|
|
835
|
-
/** Sets the `Access-Control-Allow-Origin` header (chainable). Defaults to `'*'`. */
|
|
836
|
-
enableCors(origin = "*") {
|
|
837
|
-
this._headers["access-control-allow-origin"] = origin;
|
|
838
|
-
return this;
|
|
839
|
-
}
|
|
840
|
-
/** Sets an outgoing `Set-Cookie` header with optional attributes (chainable). */
|
|
841
|
-
setCookie(name, value, attrs) {
|
|
842
|
-
this._cookies[name] = {
|
|
843
|
-
value,
|
|
844
|
-
attrs: attrs || {}
|
|
845
|
-
};
|
|
846
|
-
this._hasCookies = true;
|
|
847
|
-
return this;
|
|
848
|
-
}
|
|
849
|
-
/** Returns a previously set cookie's data, or `undefined` if not set. */
|
|
850
|
-
getCookie(name) {
|
|
851
|
-
return this._cookies[name];
|
|
852
|
-
}
|
|
853
|
-
/** Removes a cookie from the outgoing set list (chainable). */
|
|
854
|
-
removeCookie(name) {
|
|
855
|
-
delete this._cookies[name];
|
|
856
|
-
return this;
|
|
857
|
-
}
|
|
858
|
-
/** Removes all outgoing cookies (chainable). */
|
|
859
|
-
clearCookies() {
|
|
860
|
-
this._cookies = {};
|
|
861
|
-
this._rawCookies = [];
|
|
862
|
-
this._hasCookies = false;
|
|
863
|
-
return this;
|
|
864
|
-
}
|
|
865
|
-
/** Appends a raw `Set-Cookie` header string (chainable). Use when you need full control over the cookie format. */
|
|
866
|
-
setCookieRaw(rawValue) {
|
|
867
|
-
this._rawCookies.push(rawValue);
|
|
868
|
-
this._hasCookies = true;
|
|
869
|
-
return this;
|
|
870
|
-
}
|
|
871
|
-
/** Sets the `Cache-Control` header from a directive object (chainable). */
|
|
872
|
-
setCacheControl(data) {
|
|
873
|
-
this._headers["cache-control"] = renderCacheControl(data);
|
|
874
|
-
return this;
|
|
875
|
-
}
|
|
876
|
-
/** Sets the `Age` header in seconds (chainable). Accepts a number or time string (e.g. `'2h 15m'`). */
|
|
877
|
-
setAge(value) {
|
|
878
|
-
this._headers.age = convertTime(value, "s").toString();
|
|
879
|
-
return this;
|
|
880
|
-
}
|
|
881
|
-
/** Sets the `Expires` header (chainable). Accepts a `Date`, date string, or timestamp. */
|
|
882
|
-
setExpires(value) {
|
|
883
|
-
this._headers.expires = typeof value === "string" || typeof value === "number" ? new Date(value).toUTCString() : value.toUTCString();
|
|
884
|
-
return this;
|
|
885
|
-
}
|
|
886
|
-
/** Sets or clears the `Pragma: no-cache` header (chainable). */
|
|
887
|
-
setPragmaNoCache(value = true) {
|
|
888
|
-
this._headers.pragma = value ? "no-cache" : "";
|
|
889
|
-
return this;
|
|
890
|
-
}
|
|
891
|
-
/**
|
|
892
|
-
* Returns the underlying Node.js `ServerResponse`.
|
|
893
|
-
* @param passthrough - If `true`, the framework still manages the response lifecycle. If `false` (default), the response is marked as "responded" and the framework will not touch it.
|
|
894
|
-
*/
|
|
895
|
-
getRawRes(passthrough) {
|
|
896
|
-
if (!passthrough) this._responded = true;
|
|
897
|
-
return this._res;
|
|
898
|
-
}
|
|
899
|
-
/** Whether the response has already been sent (or the underlying stream is no longer writable). */
|
|
900
|
-
get responded() {
|
|
901
|
-
return this._responded || !this._res.writable || this._res.writableEnded;
|
|
902
|
-
}
|
|
903
|
-
renderBody() {
|
|
904
|
-
const body = this._body;
|
|
905
|
-
if (body === void 0 || body === null) return "";
|
|
906
|
-
if (typeof body === "string") {
|
|
907
|
-
if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
|
|
908
|
-
return body;
|
|
909
|
-
}
|
|
910
|
-
if (typeof body === "boolean" || typeof body === "number") {
|
|
911
|
-
if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
|
|
912
|
-
return body.toString();
|
|
913
|
-
}
|
|
914
|
-
if (body instanceof Uint8Array) return body;
|
|
915
|
-
if (typeof body === "object") {
|
|
916
|
-
if (!this._headers["content-type"]) this._headers["content-type"] = "application/json";
|
|
917
|
-
return JSON.stringify(body);
|
|
918
|
-
}
|
|
919
|
-
throw new Error(`Unsupported body format "${typeof body}"`);
|
|
920
|
-
}
|
|
921
|
-
renderError(data, _ctx) {
|
|
922
|
-
this._status = data.statusCode || 500;
|
|
923
|
-
this._headers["content-type"] = "application/json";
|
|
924
|
-
this._body = JSON.stringify(data);
|
|
925
|
-
}
|
|
926
|
-
/** Renders and sends an HTTP error response. Called automatically by the framework when a handler throws an `HttpError`. */
|
|
927
|
-
sendError(error, ctx) {
|
|
928
|
-
const data = error.body;
|
|
929
|
-
this.renderError(data, ctx);
|
|
930
|
-
return this.send();
|
|
931
|
-
}
|
|
932
|
-
/**
|
|
933
|
-
* Finalizes and sends the response.
|
|
934
|
-
*
|
|
935
|
-
* Flushes all accumulated headers (including cookies) in a single `writeHead()` call,
|
|
936
|
-
* then writes the body. Supports `Readable` streams, `fetch` `Response` objects, and regular values.
|
|
937
|
-
*
|
|
938
|
-
* @throws Error if the response was already sent.
|
|
939
|
-
*/
|
|
940
|
-
send() {
|
|
941
|
-
if (this._responded) {
|
|
942
|
-
const err = /* @__PURE__ */ new Error("The response was already sent.");
|
|
943
|
-
this._logger.error(err.message, err);
|
|
944
|
-
throw err;
|
|
945
|
-
}
|
|
946
|
-
this._responded = true;
|
|
947
|
-
this.finalizeCookies();
|
|
948
|
-
const body = this._body;
|
|
949
|
-
const method = this._req.method;
|
|
950
|
-
if (body instanceof stream.Readable) return this.sendStream(body, method);
|
|
951
|
-
if (hasFetchResponse && body instanceof Response) return this.sendFetchResponse(body, method);
|
|
952
|
-
this.sendRegular(method);
|
|
953
|
-
}
|
|
954
|
-
finalizeCookies() {
|
|
955
|
-
if (!this._hasCookies) return;
|
|
956
|
-
const entries = Object.entries(this._cookies);
|
|
957
|
-
const rendered = [];
|
|
958
|
-
for (const [name, data] of entries) if (data) rendered.push(renderCookie(name, data));
|
|
959
|
-
if (this._rawCookies.length > 0) rendered.push(...this._rawCookies);
|
|
960
|
-
if (rendered.length > 0) {
|
|
961
|
-
const existing = this._headers["set-cookie"];
|
|
962
|
-
if (existing) this._headers["set-cookie"] = [...Array.isArray(existing) ? existing : [existing], ...rendered];
|
|
963
|
-
else this._headers["set-cookie"] = rendered;
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
autoStatus(hasBody) {
|
|
967
|
-
if (this._status) return;
|
|
968
|
-
if (!hasBody) {
|
|
969
|
-
this._status = EHttpStatusCode.NoContent;
|
|
970
|
-
return;
|
|
971
|
-
}
|
|
972
|
-
this._status = defaultStatus[this._req.method] || EHttpStatusCode.OK;
|
|
973
|
-
}
|
|
974
|
-
sendStream(stream$1, method) {
|
|
975
|
-
this.autoStatus(true);
|
|
976
|
-
this._res.writeHead(this._status, this._headers);
|
|
977
|
-
this._req.once("close", () => {
|
|
978
|
-
stream$1.destroy();
|
|
979
|
-
});
|
|
980
|
-
if (method === "HEAD") {
|
|
981
|
-
stream$1.destroy();
|
|
982
|
-
this._res.end();
|
|
983
|
-
return Promise.resolve();
|
|
984
|
-
}
|
|
985
|
-
return new Promise((resolve, reject) => {
|
|
986
|
-
stream$1.on("error", (e) => {
|
|
987
|
-
this._logger.error("Stream error", e);
|
|
988
|
-
stream$1.destroy();
|
|
989
|
-
this._res.end();
|
|
990
|
-
reject(e);
|
|
991
|
-
});
|
|
992
|
-
stream$1.on("close", () => {
|
|
993
|
-
stream$1.destroy();
|
|
994
|
-
resolve();
|
|
995
|
-
});
|
|
996
|
-
stream$1.pipe(this._res);
|
|
997
|
-
});
|
|
998
|
-
}
|
|
999
|
-
async sendFetchResponse(fetchResponse, method) {
|
|
1000
|
-
this._status = this._status || fetchResponse.status;
|
|
1001
|
-
const fetchContentLength = fetchResponse.headers.get("content-length");
|
|
1002
|
-
if (fetchContentLength) this._headers["content-length"] = fetchContentLength;
|
|
1003
|
-
const fetchContentType = fetchResponse.headers.get("content-type");
|
|
1004
|
-
if (fetchContentType) this._headers["content-type"] = fetchContentType;
|
|
1005
|
-
this._res.writeHead(this._status, this._headers);
|
|
1006
|
-
if (method === "HEAD") {
|
|
1007
|
-
this._res.end();
|
|
1008
|
-
return;
|
|
1009
|
-
}
|
|
1010
|
-
const fetchBody = fetchResponse.body;
|
|
1011
|
-
if (fetchBody) try {
|
|
1012
|
-
for await (const chunk of fetchBody) this._res.write(chunk);
|
|
1013
|
-
} catch (error) {
|
|
1014
|
-
this._logger.error("Error streaming fetch response body", error);
|
|
1015
|
-
}
|
|
1016
|
-
if (!this._res.writableEnded) this._res.end();
|
|
1017
|
-
}
|
|
1018
|
-
sendRegular(method) {
|
|
1019
|
-
const renderedBody = this.renderBody();
|
|
1020
|
-
this.autoStatus(!!renderedBody);
|
|
1021
|
-
const contentLength = typeof renderedBody === "string" ? Buffer.byteLength(renderedBody) : renderedBody.byteLength;
|
|
1022
|
-
this._headers["content-length"] = contentLength.toString();
|
|
1023
|
-
this._res.writeHead(this._status, this._headers).end(method === "HEAD" ? "" : renderedBody);
|
|
1024
|
-
}
|
|
1025
|
-
};
|
|
1026
|
-
|
|
1027
|
-
//#endregion
|
|
1028
|
-
//#region packages/event-http/src/event-http.ts
|
|
1029
|
-
/** Creates an async event context for an incoming HTTP request/response pair. */
|
|
1030
|
-
function createHttpContext(data, options, ResponseClass = HttpResponse) {
|
|
1031
|
-
const response = new ResponseClass(data.res, data.req, options.logger);
|
|
1032
|
-
return (fn) => (0, __wooksjs_event_core.createEventContext)(options, httpKind, {
|
|
1033
|
-
req: data.req,
|
|
1034
|
-
response,
|
|
1035
|
-
requestLimits: data.requestLimits
|
|
1036
|
-
}, fn);
|
|
1037
|
-
}
|
|
1038
|
-
/** Returns the current HTTP event context. */
|
|
1039
|
-
function useHttpContext(ctx) {
|
|
1040
|
-
return ctx ?? (0, __wooksjs_event_core.current)();
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
//#endregion
|
|
1044
|
-
//#region packages/event-http/src/errors/403.tl.svg
|
|
1045
|
-
function _403_tl_default(ctx) {
|
|
1046
|
-
return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
|
|
1047
|
-
<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">
|
|
1048
|
-
<animate attributeName="fill" dur="2s" repeatCount="indefinite"
|
|
1049
|
-
values="#ff000000;#ff000050;#ff000000" />
|
|
1050
|
-
</path>
|
|
1051
|
-
|
|
1052
|
-
<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" />
|
|
1053
|
-
|
|
1054
|
-
<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" />
|
|
1055
|
-
</svg>
|
|
1056
|
-
`;
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
//#endregion
|
|
1060
|
-
//#region packages/event-http/src/errors/404.tl.svg
|
|
1061
|
-
function _404_tl_default(ctx) {
|
|
1062
|
-
return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
1063
|
-
<defs>
|
|
1064
|
-
<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"/>
|
|
1065
|
-
</defs>
|
|
1066
|
-
|
|
1067
|
-
<g id="queue" transform="translate(-5 -10)">
|
|
1068
|
-
<use href="#sheet" opacity="0">
|
|
1069
|
-
<animateTransform attributeName="transform" type="translate"
|
|
1070
|
-
dur="3s" repeatCount="indefinite"
|
|
1071
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1072
|
-
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1073
|
-
<animate attributeName="opacity"
|
|
1074
|
-
dur="3s" repeatCount="indefinite"
|
|
1075
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1076
|
-
values="0;1;1;0;0" />
|
|
1077
|
-
</use>
|
|
1078
|
-
|
|
1079
|
-
<use href="#sheet" opacity="0">
|
|
1080
|
-
<animateTransform attributeName="transform" type="translate"
|
|
1081
|
-
dur="3s" begin="1s" repeatCount="indefinite"
|
|
1082
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1083
|
-
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1084
|
-
<animate attributeName="opacity"
|
|
1085
|
-
dur="3s" begin="1s" repeatCount="indefinite"
|
|
1086
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1087
|
-
values="0;1;1;0;0" />
|
|
1088
|
-
</use>
|
|
1089
|
-
|
|
1090
|
-
<use href="#sheet" opacity="0">
|
|
1091
|
-
<animateTransform attributeName="transform" type="translate"
|
|
1092
|
-
dur="3s" begin="2s" repeatCount="indefinite"
|
|
1093
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1094
|
-
values="30 0; -20 0; -20 0; -70 0; -70 0" />
|
|
1095
|
-
<animate attributeName="opacity"
|
|
1096
|
-
dur="3s" begin="2s" repeatCount="indefinite"
|
|
1097
|
-
keyTimes="0;0.1;0.32;0.42;1"
|
|
1098
|
-
values="0;1;1;0;0" />
|
|
1099
|
-
</use>
|
|
1100
|
-
</g>
|
|
1101
|
-
|
|
1102
|
-
<g>
|
|
1103
|
-
<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"/>
|
|
1104
|
-
|
|
1105
|
-
<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"/>
|
|
1106
|
-
</g>
|
|
1107
|
-
</svg>
|
|
1108
|
-
|
|
1109
|
-
`;
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
//#endregion
|
|
1113
|
-
//#region packages/event-http/src/errors/500.tl.svg
|
|
1114
|
-
function _500_tl_default(ctx) {
|
|
1115
|
-
return `<svg height="64" viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
|
|
1116
|
-
|
|
1117
|
-
<g id="server">
|
|
1118
|
-
|
|
1119
|
-
<g fill="#88888888" stroke="#88888888" stroke-width="2" >
|
|
1120
|
-
<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" />
|
|
1121
|
-
</g>
|
|
1122
|
-
<circle cx="30" cy="60" r="6" fill="red">
|
|
1123
|
-
<animate attributeName="fill" dur="0.8s"
|
|
1124
|
-
values="red;#2d0000;red" repeatCount="indefinite"/>
|
|
1125
|
-
</circle>
|
|
1126
|
-
|
|
1127
|
-
</g>
|
|
1128
|
-
|
|
1129
|
-
<g fill="lightgray" opacity="0.75">
|
|
1130
|
-
<circle cx="50" cy="35" r="6">
|
|
1131
|
-
<animate attributeName="cy" from="35" to="15" dur="2s"
|
|
1132
|
-
repeatCount="indefinite"/>
|
|
1133
|
-
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
1134
|
-
repeatCount="indefinite"/>
|
|
1135
|
-
</circle>
|
|
1136
|
-
<circle cx="60" cy="40" r="4">
|
|
1137
|
-
<animate attributeName="cy" from="40" to="20" dur="2s"
|
|
1138
|
-
begin="0.4s" repeatCount="indefinite"/>
|
|
1139
|
-
<animate attributeName="opacity" values="0.75;0" dur="2s"
|
|
1140
|
-
begin="0.4s" repeatCount="indefinite"/>
|
|
1141
|
-
</circle>
|
|
1142
|
-
</g>
|
|
1143
|
-
|
|
1144
|
-
</svg>
|
|
1145
|
-
`;
|
|
739
|
+
|
|
740
|
+
//#endregion
|
|
741
|
+
//#region packages/event-http/src/errors/500.tl.svg
|
|
742
|
+
function _500_tl_default(ctx) {
|
|
743
|
+
return `<svg height="64" viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
|
|
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
|
+
`;
|
|
1146
774
|
}
|
|
1147
775
|
|
|
1148
776
|
//#endregion
|
|
@@ -1390,10 +1018,377 @@ function error_tl_default(ctx) {
|
|
|
1390
1018
|
`;
|
|
1391
1019
|
}
|
|
1392
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
|
+
};
|
|
1096
|
+
|
|
1097
|
+
//#endregion
|
|
1098
|
+
//#region packages/event-http/src/response/http-response.ts
|
|
1099
|
+
const hasFetchResponse = typeof globalThis.Response === "function";
|
|
1100
|
+
const defaultStatus = {
|
|
1101
|
+
GET: EHttpStatusCode.OK,
|
|
1102
|
+
POST: EHttpStatusCode.Created,
|
|
1103
|
+
PUT: EHttpStatusCode.Created,
|
|
1104
|
+
PATCH: EHttpStatusCode.Accepted,
|
|
1105
|
+
DELETE: EHttpStatusCode.Accepted
|
|
1106
|
+
};
|
|
1107
|
+
/**
|
|
1108
|
+
* Manages response status, headers, cookies, cache control, and body for an HTTP request.
|
|
1109
|
+
*
|
|
1110
|
+
* All header mutations are accumulated in memory and flushed in a single `writeHead()` call
|
|
1111
|
+
* when `send()` is invoked. Setter methods are chainable.
|
|
1112
|
+
*
|
|
1113
|
+
* @example
|
|
1114
|
+
* ```ts
|
|
1115
|
+
* const response = useResponse()
|
|
1116
|
+
* response.setStatus(200).setHeader('x-custom', 'value')
|
|
1117
|
+
* response.setCookie('session', 'abc', { httpOnly: true })
|
|
1118
|
+
* ```
|
|
1119
|
+
*/
|
|
1120
|
+
var HttpResponse = class {
|
|
1121
|
+
/**
|
|
1122
|
+
* @param _res - The underlying Node.js `ServerResponse`.
|
|
1123
|
+
* @param _req - The underlying Node.js `IncomingMessage`.
|
|
1124
|
+
* @param _logger - Logger instance for error reporting.
|
|
1125
|
+
* @param defaultHeaders - Optional headers to pre-populate on this response (e.g. from `securityHeaders()`).
|
|
1126
|
+
*/
|
|
1127
|
+
constructor(_res, _req, _logger, defaultHeaders) {
|
|
1128
|
+
this._res = _res;
|
|
1129
|
+
this._req = _req;
|
|
1130
|
+
this._logger = _logger;
|
|
1131
|
+
if (defaultHeaders) for (const key in defaultHeaders) this._headers[key] = defaultHeaders[key];
|
|
1132
|
+
}
|
|
1133
|
+
_status = 0;
|
|
1134
|
+
_body = void 0;
|
|
1135
|
+
_headers = {};
|
|
1136
|
+
_cookies = {};
|
|
1137
|
+
_rawCookies = [];
|
|
1138
|
+
_hasCookies = false;
|
|
1139
|
+
_responded = false;
|
|
1140
|
+
/** The HTTP status code. If not set, it is inferred automatically when `send()` is called. */
|
|
1141
|
+
get status() {
|
|
1142
|
+
return this._status;
|
|
1143
|
+
}
|
|
1144
|
+
set status(value) {
|
|
1145
|
+
this._status = value;
|
|
1146
|
+
}
|
|
1147
|
+
/** Sets the HTTP status code (chainable). */
|
|
1148
|
+
setStatus(value) {
|
|
1149
|
+
this._status = value;
|
|
1150
|
+
return this;
|
|
1151
|
+
}
|
|
1152
|
+
/** The response body. Automatically serialized by `send()` (objects → JSON, strings → text). */
|
|
1153
|
+
get body() {
|
|
1154
|
+
return this._body;
|
|
1155
|
+
}
|
|
1156
|
+
set body(value) {
|
|
1157
|
+
this._body = value;
|
|
1158
|
+
}
|
|
1159
|
+
/** Sets the response body (chainable). */
|
|
1160
|
+
setBody(value) {
|
|
1161
|
+
this._body = value;
|
|
1162
|
+
return this;
|
|
1163
|
+
}
|
|
1164
|
+
/** Sets a single response header (chainable). Arrays produce multi-value headers. */
|
|
1165
|
+
setHeader(name, value) {
|
|
1166
|
+
this._headers[name] = Array.isArray(value) ? value : value.toString();
|
|
1167
|
+
return this;
|
|
1168
|
+
}
|
|
1169
|
+
/** Batch-sets multiple response headers from a record (chainable). Existing keys are overwritten. */
|
|
1170
|
+
setHeaders(headers) {
|
|
1171
|
+
for (const key in headers) this._headers[key] = headers[key];
|
|
1172
|
+
return this;
|
|
1173
|
+
}
|
|
1174
|
+
/** Returns the value of a response header, or `undefined` if not set. */
|
|
1175
|
+
getHeader(name) {
|
|
1176
|
+
return this._headers[name];
|
|
1177
|
+
}
|
|
1178
|
+
/** Removes a response header (chainable). */
|
|
1179
|
+
removeHeader(name) {
|
|
1180
|
+
delete this._headers[name];
|
|
1181
|
+
return this;
|
|
1182
|
+
}
|
|
1183
|
+
/** Returns a read-only snapshot of all response headers. */
|
|
1184
|
+
headers() {
|
|
1185
|
+
return this._headers;
|
|
1186
|
+
}
|
|
1187
|
+
/** Sets the `Content-Type` response header (chainable). */
|
|
1188
|
+
setContentType(value) {
|
|
1189
|
+
this._headers["content-type"] = value;
|
|
1190
|
+
return this;
|
|
1191
|
+
}
|
|
1192
|
+
/** Returns the current `Content-Type` header value. */
|
|
1193
|
+
getContentType() {
|
|
1194
|
+
return this._headers["content-type"];
|
|
1195
|
+
}
|
|
1196
|
+
/** Sets the `Access-Control-Allow-Origin` header (chainable). Defaults to `'*'`. */
|
|
1197
|
+
enableCors(origin = "*") {
|
|
1198
|
+
this._headers["access-control-allow-origin"] = origin;
|
|
1199
|
+
return this;
|
|
1200
|
+
}
|
|
1201
|
+
/** Sets an outgoing `Set-Cookie` header with optional attributes (chainable). */
|
|
1202
|
+
setCookie(name, value, attrs) {
|
|
1203
|
+
this._cookies[name] = {
|
|
1204
|
+
value,
|
|
1205
|
+
attrs: attrs || {}
|
|
1206
|
+
};
|
|
1207
|
+
this._hasCookies = true;
|
|
1208
|
+
return this;
|
|
1209
|
+
}
|
|
1210
|
+
/** Returns a previously set cookie's data, or `undefined` if not set. */
|
|
1211
|
+
getCookie(name) {
|
|
1212
|
+
return this._cookies[name];
|
|
1213
|
+
}
|
|
1214
|
+
/** Removes a cookie from the outgoing set list (chainable). */
|
|
1215
|
+
removeCookie(name) {
|
|
1216
|
+
delete this._cookies[name];
|
|
1217
|
+
return this;
|
|
1218
|
+
}
|
|
1219
|
+
/** Removes all outgoing cookies (chainable). */
|
|
1220
|
+
clearCookies() {
|
|
1221
|
+
this._cookies = {};
|
|
1222
|
+
this._rawCookies = [];
|
|
1223
|
+
this._hasCookies = false;
|
|
1224
|
+
return this;
|
|
1225
|
+
}
|
|
1226
|
+
/** Appends a raw `Set-Cookie` header string (chainable). Use when you need full control over the cookie format. */
|
|
1227
|
+
setCookieRaw(rawValue) {
|
|
1228
|
+
this._rawCookies.push(rawValue);
|
|
1229
|
+
this._hasCookies = true;
|
|
1230
|
+
return this;
|
|
1231
|
+
}
|
|
1232
|
+
/** Sets the `Cache-Control` header from a directive object (chainable). */
|
|
1233
|
+
setCacheControl(data) {
|
|
1234
|
+
this._headers["cache-control"] = renderCacheControl(data);
|
|
1235
|
+
return this;
|
|
1236
|
+
}
|
|
1237
|
+
/** Sets the `Age` header in seconds (chainable). Accepts a number or time string (e.g. `'2h 15m'`). */
|
|
1238
|
+
setAge(value) {
|
|
1239
|
+
this._headers.age = convertTime(value, "s").toString();
|
|
1240
|
+
return this;
|
|
1241
|
+
}
|
|
1242
|
+
/** Sets the `Expires` header (chainable). Accepts a `Date`, date string, or timestamp. */
|
|
1243
|
+
setExpires(value) {
|
|
1244
|
+
this._headers.expires = typeof value === "string" || typeof value === "number" ? new Date(value).toUTCString() : value.toUTCString();
|
|
1245
|
+
return this;
|
|
1246
|
+
}
|
|
1247
|
+
/** Sets or clears the `Pragma: no-cache` header (chainable). */
|
|
1248
|
+
setPragmaNoCache(value = true) {
|
|
1249
|
+
this._headers.pragma = value ? "no-cache" : "";
|
|
1250
|
+
return this;
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Returns the underlying Node.js `ServerResponse`.
|
|
1254
|
+
* @param passthrough - If `true`, the framework still manages the response lifecycle. If `false` (default), the response is marked as "responded" and the framework will not touch it.
|
|
1255
|
+
*/
|
|
1256
|
+
getRawRes(passthrough) {
|
|
1257
|
+
if (!passthrough) this._responded = true;
|
|
1258
|
+
return this._res;
|
|
1259
|
+
}
|
|
1260
|
+
/** Whether the response has already been sent (or the underlying stream is no longer writable). */
|
|
1261
|
+
get responded() {
|
|
1262
|
+
return this._responded || !this._res.writable || this._res.writableEnded;
|
|
1263
|
+
}
|
|
1264
|
+
renderBody() {
|
|
1265
|
+
const body = this._body;
|
|
1266
|
+
if (body === void 0 || body === null) return "";
|
|
1267
|
+
if (typeof body === "string") {
|
|
1268
|
+
if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
|
|
1269
|
+
return body;
|
|
1270
|
+
}
|
|
1271
|
+
if (typeof body === "boolean" || typeof body === "number") {
|
|
1272
|
+
if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
|
|
1273
|
+
return body.toString();
|
|
1274
|
+
}
|
|
1275
|
+
if (body instanceof Uint8Array) return body;
|
|
1276
|
+
if (typeof body === "object") {
|
|
1277
|
+
if (!this._headers["content-type"]) this._headers["content-type"] = "application/json";
|
|
1278
|
+
return JSON.stringify(body);
|
|
1279
|
+
}
|
|
1280
|
+
throw new Error(`Unsupported body format "${typeof body}"`);
|
|
1281
|
+
}
|
|
1282
|
+
renderError(data, _ctx) {
|
|
1283
|
+
this._status = data.statusCode || 500;
|
|
1284
|
+
this._headers["content-type"] = "application/json";
|
|
1285
|
+
this._body = JSON.stringify(data);
|
|
1286
|
+
}
|
|
1287
|
+
/** Renders and sends an HTTP error response. Called automatically by the framework when a handler throws an `HttpError`. */
|
|
1288
|
+
sendError(error, ctx) {
|
|
1289
|
+
const data = error.body;
|
|
1290
|
+
this.renderError(data, ctx);
|
|
1291
|
+
return this.send();
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* Finalizes and sends the response.
|
|
1295
|
+
*
|
|
1296
|
+
* Flushes all accumulated headers (including cookies) in a single `writeHead()` call,
|
|
1297
|
+
* then writes the body. Supports `Readable` streams, `fetch` `Response` objects, and regular values.
|
|
1298
|
+
*
|
|
1299
|
+
* @throws Error if the response was already sent.
|
|
1300
|
+
*/
|
|
1301
|
+
send() {
|
|
1302
|
+
if (this._responded) {
|
|
1303
|
+
const err = /* @__PURE__ */ new Error("The response was already sent.");
|
|
1304
|
+
this._logger.error(err.message, err);
|
|
1305
|
+
throw err;
|
|
1306
|
+
}
|
|
1307
|
+
this._responded = true;
|
|
1308
|
+
this.finalizeCookies();
|
|
1309
|
+
const body = this._body;
|
|
1310
|
+
const method = this._req.method;
|
|
1311
|
+
if (body instanceof stream.Readable) return this.sendStream(body, method);
|
|
1312
|
+
if (hasFetchResponse && body instanceof Response) return this.sendFetchResponse(body, method);
|
|
1313
|
+
this.sendRegular(method);
|
|
1314
|
+
}
|
|
1315
|
+
finalizeCookies() {
|
|
1316
|
+
if (!this._hasCookies) return;
|
|
1317
|
+
const entries = Object.entries(this._cookies);
|
|
1318
|
+
const rendered = [];
|
|
1319
|
+
for (const [name, data] of entries) if (data) rendered.push(renderCookie(name, data));
|
|
1320
|
+
if (this._rawCookies.length > 0) rendered.push(...this._rawCookies);
|
|
1321
|
+
if (rendered.length > 0) {
|
|
1322
|
+
const existing = this._headers["set-cookie"];
|
|
1323
|
+
if (existing) this._headers["set-cookie"] = [...Array.isArray(existing) ? existing : [existing], ...rendered];
|
|
1324
|
+
else this._headers["set-cookie"] = rendered;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
autoStatus(hasBody) {
|
|
1328
|
+
if (this._status) return;
|
|
1329
|
+
if (!hasBody) {
|
|
1330
|
+
this._status = EHttpStatusCode.NoContent;
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
this._status = defaultStatus[this._req.method] || EHttpStatusCode.OK;
|
|
1334
|
+
}
|
|
1335
|
+
sendStream(stream$1, method) {
|
|
1336
|
+
this.autoStatus(true);
|
|
1337
|
+
this._res.writeHead(this._status, this._headers);
|
|
1338
|
+
this._req.once("close", () => {
|
|
1339
|
+
stream$1.destroy();
|
|
1340
|
+
});
|
|
1341
|
+
if (method === "HEAD") {
|
|
1342
|
+
stream$1.destroy();
|
|
1343
|
+
this._res.end();
|
|
1344
|
+
return Promise.resolve();
|
|
1345
|
+
}
|
|
1346
|
+
return new Promise((resolve, reject) => {
|
|
1347
|
+
stream$1.on("error", (e) => {
|
|
1348
|
+
this._logger.error("Stream error", e);
|
|
1349
|
+
stream$1.destroy();
|
|
1350
|
+
this._res.end();
|
|
1351
|
+
reject(e);
|
|
1352
|
+
});
|
|
1353
|
+
stream$1.on("close", () => {
|
|
1354
|
+
stream$1.destroy();
|
|
1355
|
+
resolve();
|
|
1356
|
+
});
|
|
1357
|
+
stream$1.pipe(this._res);
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
async sendFetchResponse(fetchResponse, method) {
|
|
1361
|
+
this._status = this._status || fetchResponse.status;
|
|
1362
|
+
const fetchContentLength = fetchResponse.headers.get("content-length");
|
|
1363
|
+
if (fetchContentLength) this._headers["content-length"] = fetchContentLength;
|
|
1364
|
+
const fetchContentType = fetchResponse.headers.get("content-type");
|
|
1365
|
+
if (fetchContentType) this._headers["content-type"] = fetchContentType;
|
|
1366
|
+
this._res.writeHead(this._status, this._headers);
|
|
1367
|
+
if (method === "HEAD") {
|
|
1368
|
+
this._res.end();
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
const fetchBody = fetchResponse.body;
|
|
1372
|
+
if (fetchBody) try {
|
|
1373
|
+
for await (const chunk of fetchBody) this._res.write(chunk);
|
|
1374
|
+
} catch (error) {
|
|
1375
|
+
this._logger.error("Error streaming fetch response body", error);
|
|
1376
|
+
}
|
|
1377
|
+
if (!this._res.writableEnded) this._res.end();
|
|
1378
|
+
}
|
|
1379
|
+
sendRegular(method) {
|
|
1380
|
+
const renderedBody = this.renderBody();
|
|
1381
|
+
this.autoStatus(!!renderedBody);
|
|
1382
|
+
const contentLength = typeof renderedBody === "string" ? Buffer.byteLength(renderedBody) : renderedBody.byteLength;
|
|
1383
|
+
this._headers["content-length"] = contentLength.toString();
|
|
1384
|
+
this._res.writeHead(this._status, this._headers).end(method === "HEAD" ? "" : renderedBody);
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1393
1388
|
//#endregion
|
|
1394
1389
|
//#region packages/event-http/src/response/wooks-http-response.ts
|
|
1395
1390
|
let framework = {
|
|
1396
|
-
version: "0.7.
|
|
1391
|
+
version: "0.7.3",
|
|
1397
1392
|
poweredBy: "wooksjs",
|
|
1398
1393
|
link: "https://wooks.moost.org/",
|
|
1399
1394
|
image: "https://wooks.moost.org/wooks-full-logo.svg"
|
|
@@ -1603,7 +1598,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1603
1598
|
const response = new this.ResponseClass(res, req, ctxOptions.logger, defaultHeaders);
|
|
1604
1599
|
const method = req.method || "";
|
|
1605
1600
|
const url$1 = req.url || "";
|
|
1606
|
-
(
|
|
1601
|
+
createHttpContext(ctxOptions, {
|
|
1607
1602
|
req,
|
|
1608
1603
|
response,
|
|
1609
1604
|
requestLimits: RequestLimits
|
|
@@ -1616,10 +1611,12 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1616
1611
|
this.logger.error("Internal error, please report", error);
|
|
1617
1612
|
this.respond(error, response, ctx);
|
|
1618
1613
|
});
|
|
1614
|
+
return result;
|
|
1619
1615
|
} else {
|
|
1620
1616
|
this.logger.debug(`404 Not found (${method})${url$1}`);
|
|
1621
1617
|
const error = new HttpError(404);
|
|
1622
1618
|
this.respond(error, response, ctx);
|
|
1619
|
+
return error;
|
|
1623
1620
|
}
|
|
1624
1621
|
});
|
|
1625
1622
|
};
|
|
@@ -1638,7 +1635,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1638
1635
|
return;
|
|
1639
1636
|
}
|
|
1640
1637
|
const url$1 = req.url || "";
|
|
1641
|
-
(
|
|
1638
|
+
createHttpContext(ctxOptions, {
|
|
1642
1639
|
req,
|
|
1643
1640
|
response: void 0,
|
|
1644
1641
|
requestLimits
|
|
@@ -1648,8 +1645,8 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1648
1645
|
ctx.set(wsHandler.socketKey, socket);
|
|
1649
1646
|
ctx.set(wsHandler.headKey, head);
|
|
1650
1647
|
const handlers = this.wooks.lookupHandlers("UPGRADE", url$1, ctx);
|
|
1651
|
-
if (handlers) this.processUpgradeHandlers(handlers, ctx, socket);
|
|
1652
|
-
else wsHandler.handleUpgrade(req, socket, head);
|
|
1648
|
+
if (handlers) return this.processUpgradeHandlers(handlers, ctx, socket);
|
|
1649
|
+
else return wsHandler.handleUpgrade(req, socket, head);
|
|
1653
1650
|
});
|
|
1654
1651
|
};
|
|
1655
1652
|
}
|