@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.mjs
CHANGED
|
@@ -13,10 +13,6 @@ function createHttpContext(data, options) {
|
|
|
13
13
|
options,
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Wrapper on useEventContext with HTTP event types
|
|
18
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
19
|
-
*/
|
|
20
16
|
function useHttpContext() {
|
|
21
17
|
return useEventContext('HTTP');
|
|
22
18
|
}
|
|
@@ -213,7 +209,6 @@ function renderCacheControl(data) {
|
|
|
213
209
|
}
|
|
214
210
|
return attrs;
|
|
215
211
|
}
|
|
216
|
-
// rfc7234#section-5.2.2
|
|
217
212
|
const cacheControlFunc = {
|
|
218
213
|
mustRevalidate: (v) => v ? 'must-revalidate' : '',
|
|
219
214
|
noCache: (v) => v ? (typeof v === 'string' ? `no-cache="${v}"` : 'no-cache') : '',
|
|
@@ -231,7 +226,6 @@ const renderExpires = (v) => typeof v === 'string' || typeof v === 'number'
|
|
|
231
226
|
? new Date(v).toUTCString()
|
|
232
227
|
: v.toUTCString();
|
|
233
228
|
const renderPragmaNoCache = (v) => (v ? 'no-cache' : '');
|
|
234
|
-
// rfc7234#section-5.2.2
|
|
235
229
|
function useSetCacheControl() {
|
|
236
230
|
const { setHeader } = useSetHeaders();
|
|
237
231
|
const setAge = (value) => {
|
|
@@ -688,7 +682,6 @@ class BaseHttpResponse {
|
|
|
688
682
|
this.mergeHeaders();
|
|
689
683
|
const res = rawResponse();
|
|
690
684
|
if (this.body instanceof Readable) {
|
|
691
|
-
// responding with readable stream
|
|
692
685
|
const stream = this.body;
|
|
693
686
|
this.mergeStatus('ok');
|
|
694
687
|
res.writeHead(this.status, {
|
|
@@ -717,7 +710,7 @@ class BaseHttpResponse {
|
|
|
717
710
|
}
|
|
718
711
|
}
|
|
719
712
|
else if (globalThis.Response &&
|
|
720
|
-
this.body instanceof Response
|
|
713
|
+
this.body instanceof Response) {
|
|
721
714
|
this.mergeFetchStatus(this.body.status);
|
|
722
715
|
if (method === 'HEAD') {
|
|
723
716
|
res.end();
|
|
@@ -755,7 +748,6 @@ async function respondWithFetch(fetchBody, res) {
|
|
|
755
748
|
}
|
|
756
749
|
}
|
|
757
750
|
catch (e) {
|
|
758
|
-
// ?
|
|
759
751
|
}
|
|
760
752
|
}
|
|
761
753
|
res.end();
|
|
@@ -778,7 +770,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
778
770
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
779
771
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
780
772
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
781
|
-
`<center style="color: #666;"> Wooks v${"0.4.
|
|
773
|
+
`<center style="color: #666;"> Wooks v${"0.4.13"} </center>` +
|
|
782
774
|
`${keys.length
|
|
783
775
|
? `<pre style="${preStyles}">${JSON.stringify({
|
|
784
776
|
...data,
|
|
@@ -868,11 +860,7 @@ class HttpError extends Error {
|
|
|
868
860
|
}
|
|
869
861
|
}
|
|
870
862
|
|
|
871
|
-
function createWooksResponder(
|
|
872
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
873
|
-
renderer = new BaseHttpResponseRenderer(),
|
|
874
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
875
|
-
errorRenderer = new HttpErrorRenderer()) {
|
|
863
|
+
function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRenderer = new HttpErrorRenderer()) {
|
|
876
864
|
function createResponse(data) {
|
|
877
865
|
const { hasResponded } = useResponse();
|
|
878
866
|
if (hasResponded())
|
|
@@ -933,11 +921,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
933
921
|
options(path, handler) {
|
|
934
922
|
return this.on('OPTIONS', path, handler);
|
|
935
923
|
}
|
|
936
|
-
/**
|
|
937
|
-
* Starts the http(s) server.
|
|
938
|
-
*
|
|
939
|
-
* Use this only if you rely on Wooks server.
|
|
940
|
-
*/
|
|
941
924
|
async listen(...args) {
|
|
942
925
|
const server = (this.server = http.createServer(this.getServerCb()));
|
|
943
926
|
return new Promise((resolve, reject) => {
|
|
@@ -946,10 +929,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
946
929
|
server.listen(...args);
|
|
947
930
|
});
|
|
948
931
|
}
|
|
949
|
-
/**
|
|
950
|
-
* Stops the server if it was attached or passed via argument
|
|
951
|
-
* @param server
|
|
952
|
-
*/
|
|
953
932
|
close(server) {
|
|
954
933
|
const srv = server || this.server;
|
|
955
934
|
return new Promise((resolve, reject) => {
|
|
@@ -960,22 +939,9 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
960
939
|
});
|
|
961
940
|
});
|
|
962
941
|
}
|
|
963
|
-
/**
|
|
964
|
-
* Returns http(s) server that was attached to Wooks
|
|
965
|
-
*
|
|
966
|
-
* See attachServer method docs
|
|
967
|
-
* @returns Server
|
|
968
|
-
*/
|
|
969
942
|
getServer() {
|
|
970
943
|
return this.server;
|
|
971
944
|
}
|
|
972
|
-
/**
|
|
973
|
-
* Attaches http(s) server instance
|
|
974
|
-
* to Wooks.
|
|
975
|
-
*
|
|
976
|
-
* Use it only if you want to `close` method to stop the server.
|
|
977
|
-
* @param server Server
|
|
978
|
-
*/
|
|
979
945
|
attachServer(server) {
|
|
980
946
|
this.server = server;
|
|
981
947
|
}
|
|
@@ -984,18 +950,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
984
950
|
this.logger.error('Uncought response exception', e);
|
|
985
951
|
});
|
|
986
952
|
}
|
|
987
|
-
/**
|
|
988
|
-
* Returns server callback function
|
|
989
|
-
* that can be passed to any node server:
|
|
990
|
-
* ```js
|
|
991
|
-
* import { createHttpApp } from '@wooksjs/event-http'
|
|
992
|
-
* import http from 'http'
|
|
993
|
-
*
|
|
994
|
-
* const app = createHttpApp()
|
|
995
|
-
* const server = http.createServer(app.getServerCb())
|
|
996
|
-
* server.listen(3000)
|
|
997
|
-
* ```
|
|
998
|
-
*/
|
|
999
953
|
getServerCb() {
|
|
1000
954
|
return async (req, res) => {
|
|
1001
955
|
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
@@ -1012,7 +966,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1012
966
|
}
|
|
1013
967
|
}
|
|
1014
968
|
else {
|
|
1015
|
-
// not found
|
|
1016
969
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
1017
970
|
this.respond(new HttpError(404));
|
|
1018
971
|
clearCtx();
|
|
@@ -1028,8 +981,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1028
981
|
const promise = handler();
|
|
1029
982
|
clearCtx();
|
|
1030
983
|
const result = await promise;
|
|
1031
|
-
// even if the returned value is an Error instance
|
|
1032
|
-
// we still want to process it as a response
|
|
1033
984
|
restoreCtx();
|
|
1034
985
|
this.respond(result);
|
|
1035
986
|
clearCtx();
|
|
@@ -1046,12 +997,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1046
997
|
}
|
|
1047
998
|
}
|
|
1048
999
|
}
|
|
1049
|
-
/**
|
|
1050
|
-
* Factory for WooksHttp App
|
|
1051
|
-
* @param opts TWooksHttpOptions
|
|
1052
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
1053
|
-
* @returns WooksHttp
|
|
1054
|
-
*/
|
|
1055
1000
|
function createHttpApp(opts, wooks) {
|
|
1056
1001
|
return new WooksHttp(opts, wooks);
|
|
1057
1002
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"exports": {
|
|
12
|
+
"./package.json": "./package.json",
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
11
19
|
"repository": {
|
|
12
20
|
"type": "git",
|
|
13
21
|
"url": "git+https://github.com/wooksjs/wooksjs.git",
|
|
@@ -31,12 +39,12 @@
|
|
|
31
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
40
|
},
|
|
33
41
|
"peerDependencies": {
|
|
34
|
-
"wooks": "0.4.
|
|
42
|
+
"wooks": "0.4.13",
|
|
35
43
|
"@prostojs/router": "^0.2.1",
|
|
36
|
-
"@wooksjs/event-core": "0.4.
|
|
44
|
+
"@wooksjs/event-core": "0.4.13"
|
|
37
45
|
},
|
|
38
46
|
"dependencies": {
|
|
39
|
-
"@prostojs/logger": "^0.
|
|
47
|
+
"@prostojs/logger": "^0.4.0"
|
|
40
48
|
},
|
|
41
49
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-http#readme"
|
|
42
50
|
}
|