@wooksjs/event-http 0.4.11 → 0.4.13
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 +3 -58
- package/dist/index.d.ts +519 -559
- package/dist/index.mjs +3 -58
- package/package.json +12 -4
package/dist/index.cjs
CHANGED
|
@@ -15,10 +15,6 @@ function createHttpContext(data, options) {
|
|
|
15
15
|
options,
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Wrapper on useEventContext with HTTP event types
|
|
20
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
21
|
-
*/
|
|
22
18
|
function useHttpContext() {
|
|
23
19
|
return eventCore.useEventContext('HTTP');
|
|
24
20
|
}
|
|
@@ -215,7 +211,6 @@ function renderCacheControl(data) {
|
|
|
215
211
|
}
|
|
216
212
|
return attrs;
|
|
217
213
|
}
|
|
218
|
-
// rfc7234#section-5.2.2
|
|
219
214
|
const cacheControlFunc = {
|
|
220
215
|
mustRevalidate: (v) => v ? 'must-revalidate' : '',
|
|
221
216
|
noCache: (v) => v ? (typeof v === 'string' ? `no-cache="${v}"` : 'no-cache') : '',
|
|
@@ -233,7 +228,6 @@ const renderExpires = (v) => typeof v === 'string' || typeof v === 'number'
|
|
|
233
228
|
? new Date(v).toUTCString()
|
|
234
229
|
: v.toUTCString();
|
|
235
230
|
const renderPragmaNoCache = (v) => (v ? 'no-cache' : '');
|
|
236
|
-
// rfc7234#section-5.2.2
|
|
237
231
|
function useSetCacheControl() {
|
|
238
232
|
const { setHeader } = useSetHeaders();
|
|
239
233
|
const setAge = (value) => {
|
|
@@ -690,7 +684,6 @@ class BaseHttpResponse {
|
|
|
690
684
|
this.mergeHeaders();
|
|
691
685
|
const res = rawResponse();
|
|
692
686
|
if (this.body instanceof stream.Readable) {
|
|
693
|
-
// responding with readable stream
|
|
694
687
|
const stream = this.body;
|
|
695
688
|
this.mergeStatus('ok');
|
|
696
689
|
res.writeHead(this.status, {
|
|
@@ -719,7 +712,7 @@ class BaseHttpResponse {
|
|
|
719
712
|
}
|
|
720
713
|
}
|
|
721
714
|
else if (globalThis.Response &&
|
|
722
|
-
this.body instanceof Response
|
|
715
|
+
this.body instanceof Response) {
|
|
723
716
|
this.mergeFetchStatus(this.body.status);
|
|
724
717
|
if (method === 'HEAD') {
|
|
725
718
|
res.end();
|
|
@@ -757,7 +750,6 @@ async function respondWithFetch(fetchBody, res) {
|
|
|
757
750
|
}
|
|
758
751
|
}
|
|
759
752
|
catch (e) {
|
|
760
|
-
// ?
|
|
761
753
|
}
|
|
762
754
|
}
|
|
763
755
|
res.end();
|
|
@@ -780,7 +772,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
780
772
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
781
773
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
782
774
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
783
|
-
`<center style="color: #666;"> Wooks v${"0.4.
|
|
775
|
+
`<center style="color: #666;"> Wooks v${"0.4.13"} </center>` +
|
|
784
776
|
`${keys.length
|
|
785
777
|
? `<pre style="${preStyles}">${JSON.stringify({
|
|
786
778
|
...data,
|
|
@@ -870,11 +862,7 @@ class HttpError extends Error {
|
|
|
870
862
|
}
|
|
871
863
|
}
|
|
872
864
|
|
|
873
|
-
function createWooksResponder(
|
|
874
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
875
|
-
renderer = new BaseHttpResponseRenderer(),
|
|
876
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
877
|
-
errorRenderer = new HttpErrorRenderer()) {
|
|
865
|
+
function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRenderer = new HttpErrorRenderer()) {
|
|
878
866
|
function createResponse(data) {
|
|
879
867
|
const { hasResponded } = useResponse();
|
|
880
868
|
if (hasResponded())
|
|
@@ -935,11 +923,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
935
923
|
options(path, handler) {
|
|
936
924
|
return this.on('OPTIONS', path, handler);
|
|
937
925
|
}
|
|
938
|
-
/**
|
|
939
|
-
* Starts the http(s) server.
|
|
940
|
-
*
|
|
941
|
-
* Use this only if you rely on Wooks server.
|
|
942
|
-
*/
|
|
943
926
|
async listen(...args) {
|
|
944
927
|
const server = (this.server = http.createServer(this.getServerCb()));
|
|
945
928
|
return new Promise((resolve, reject) => {
|
|
@@ -948,10 +931,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
948
931
|
server.listen(...args);
|
|
949
932
|
});
|
|
950
933
|
}
|
|
951
|
-
/**
|
|
952
|
-
* Stops the server if it was attached or passed via argument
|
|
953
|
-
* @param server
|
|
954
|
-
*/
|
|
955
934
|
close(server) {
|
|
956
935
|
const srv = server || this.server;
|
|
957
936
|
return new Promise((resolve, reject) => {
|
|
@@ -962,22 +941,9 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
962
941
|
});
|
|
963
942
|
});
|
|
964
943
|
}
|
|
965
|
-
/**
|
|
966
|
-
* Returns http(s) server that was attached to Wooks
|
|
967
|
-
*
|
|
968
|
-
* See attachServer method docs
|
|
969
|
-
* @returns Server
|
|
970
|
-
*/
|
|
971
944
|
getServer() {
|
|
972
945
|
return this.server;
|
|
973
946
|
}
|
|
974
|
-
/**
|
|
975
|
-
* Attaches http(s) server instance
|
|
976
|
-
* to Wooks.
|
|
977
|
-
*
|
|
978
|
-
* Use it only if you want to `close` method to stop the server.
|
|
979
|
-
* @param server Server
|
|
980
|
-
*/
|
|
981
947
|
attachServer(server) {
|
|
982
948
|
this.server = server;
|
|
983
949
|
}
|
|
@@ -986,18 +952,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
986
952
|
this.logger.error('Uncought response exception', e);
|
|
987
953
|
});
|
|
988
954
|
}
|
|
989
|
-
/**
|
|
990
|
-
* Returns server callback function
|
|
991
|
-
* that can be passed to any node server:
|
|
992
|
-
* ```js
|
|
993
|
-
* import { createHttpApp } from '@wooksjs/event-http'
|
|
994
|
-
* import http from 'http'
|
|
995
|
-
*
|
|
996
|
-
* const app = createHttpApp()
|
|
997
|
-
* const server = http.createServer(app.getServerCb())
|
|
998
|
-
* server.listen(3000)
|
|
999
|
-
* ```
|
|
1000
|
-
*/
|
|
1001
955
|
getServerCb() {
|
|
1002
956
|
return async (req, res) => {
|
|
1003
957
|
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
@@ -1014,7 +968,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1014
968
|
}
|
|
1015
969
|
}
|
|
1016
970
|
else {
|
|
1017
|
-
// not found
|
|
1018
971
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
1019
972
|
this.respond(new HttpError(404));
|
|
1020
973
|
clearCtx();
|
|
@@ -1030,8 +983,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1030
983
|
const promise = handler();
|
|
1031
984
|
clearCtx();
|
|
1032
985
|
const result = await promise;
|
|
1033
|
-
// even if the returned value is an Error instance
|
|
1034
|
-
// we still want to process it as a response
|
|
1035
986
|
restoreCtx();
|
|
1036
987
|
this.respond(result);
|
|
1037
988
|
clearCtx();
|
|
@@ -1048,12 +999,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1048
999
|
}
|
|
1049
1000
|
}
|
|
1050
1001
|
}
|
|
1051
|
-
/**
|
|
1052
|
-
* Factory for WooksHttp App
|
|
1053
|
-
* @param opts TWooksHttpOptions
|
|
1054
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
1055
|
-
* @returns WooksHttp
|
|
1056
|
-
*/
|
|
1057
1002
|
function createHttpApp(opts, wooks) {
|
|
1058
1003
|
return new WooksHttp(opts, wooks);
|
|
1059
1004
|
}
|