@wooksjs/event-http 0.4.10 → 0.4.11
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 +151 -199
- package/dist/index.mjs +151 -199
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -8,7 +8,10 @@ var http = require('http');
|
|
|
8
8
|
|
|
9
9
|
function createHttpContext(data, options) {
|
|
10
10
|
return eventCore.createEventContext({
|
|
11
|
-
event:
|
|
11
|
+
event: {
|
|
12
|
+
...data,
|
|
13
|
+
type: 'HTTP',
|
|
14
|
+
},
|
|
12
15
|
options,
|
|
13
16
|
});
|
|
14
17
|
}
|
|
@@ -42,20 +45,20 @@ function useRequest() {
|
|
|
42
45
|
});
|
|
43
46
|
const reqId = eventCore.useEventId().getId;
|
|
44
47
|
const forwardedIp = () => init('forwardedIp', () => {
|
|
45
|
-
var _a;
|
|
46
48
|
if (typeof req.headers[xForwardedFor] === 'string' &&
|
|
47
49
|
req.headers[xForwardedFor]) {
|
|
48
|
-
return
|
|
50
|
+
return req.headers[xForwardedFor]
|
|
49
51
|
.split(',')
|
|
50
|
-
.shift()
|
|
52
|
+
.shift()
|
|
53
|
+
?.trim();
|
|
51
54
|
}
|
|
52
55
|
else {
|
|
53
56
|
return '';
|
|
54
57
|
}
|
|
55
58
|
});
|
|
56
|
-
const remoteIp = () => init('remoteIp', () =>
|
|
59
|
+
const remoteIp = () => init('remoteIp', () => req.socket?.remoteAddress || req.connection?.remoteAddress || '');
|
|
57
60
|
function getIp(options) {
|
|
58
|
-
if (options
|
|
61
|
+
if (options?.trustProxy) {
|
|
59
62
|
return forwardedIp() || getIp();
|
|
60
63
|
}
|
|
61
64
|
else {
|
|
@@ -63,10 +66,9 @@ function useRequest() {
|
|
|
63
66
|
}
|
|
64
67
|
}
|
|
65
68
|
const getIpList = () => init('ipList', () => {
|
|
66
|
-
var _a, _b;
|
|
67
69
|
return {
|
|
68
|
-
remoteIp:
|
|
69
|
-
|
|
70
|
+
remoteIp: req.socket?.remoteAddress ||
|
|
71
|
+
req.connection?.remoteAddress ||
|
|
70
72
|
'',
|
|
71
73
|
forwarded: (req.headers[xForwardedFor] || '')
|
|
72
74
|
.split(',')
|
|
@@ -157,12 +159,12 @@ function useAuthorization() {
|
|
|
157
159
|
authorization,
|
|
158
160
|
authType,
|
|
159
161
|
authRawCredentials,
|
|
160
|
-
isBasic: () =>
|
|
161
|
-
isBearer: () =>
|
|
162
|
+
isBasic: () => authType()?.toLocaleLowerCase() === 'basic',
|
|
163
|
+
isBearer: () => authType()?.toLocaleLowerCase() === 'bearer',
|
|
162
164
|
basicCredentials: () => init('basicCredentials', () => {
|
|
163
165
|
if (authorization) {
|
|
164
166
|
const type = authType();
|
|
165
|
-
if (
|
|
167
|
+
if (type?.toLocaleLowerCase() === 'basic') {
|
|
166
168
|
const creds = Buffer.from(authRawCredentials() || '', 'base64').toString('ascii');
|
|
167
169
|
const [username, password] = creds.split(':');
|
|
168
170
|
return { username, password };
|
|
@@ -375,12 +377,12 @@ function useSetCookie(name) {
|
|
|
375
377
|
name,
|
|
376
378
|
type: 'cookie',
|
|
377
379
|
}, {
|
|
378
|
-
get: () =>
|
|
379
|
-
set: (value) =>
|
|
380
|
+
get: () => getCookie(name)?.value,
|
|
381
|
+
set: (value) => setCookie(name, value, getCookie(name)?.attrs),
|
|
380
382
|
});
|
|
381
383
|
return eventCore.attachHook(valueHook, {
|
|
382
|
-
get: () =>
|
|
383
|
-
set: (attrs) =>
|
|
384
|
+
get: () => getCookie(name)?.attrs,
|
|
385
|
+
set: (attrs) => setCookie(name, getCookie(name)?.value || '', attrs),
|
|
384
386
|
}, 'attrs');
|
|
385
387
|
}
|
|
386
388
|
|
|
@@ -419,58 +421,6 @@ function useSearchParams() {
|
|
|
419
421
|
};
|
|
420
422
|
}
|
|
421
423
|
|
|
422
|
-
/******************************************************************************
|
|
423
|
-
Copyright (c) Microsoft Corporation.
|
|
424
|
-
|
|
425
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
426
|
-
purpose with or without fee is hereby granted.
|
|
427
|
-
|
|
428
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
429
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
430
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
431
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
432
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
433
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
434
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
435
|
-
***************************************************************************** */
|
|
436
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
440
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
441
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
442
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
443
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
444
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
445
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
function __values(o) {
|
|
450
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
451
|
-
if (m) return m.call(o);
|
|
452
|
-
if (o && typeof o.length === "number") return {
|
|
453
|
-
next: function () {
|
|
454
|
-
if (o && i >= o.length) o = void 0;
|
|
455
|
-
return { value: o && o[i++], done: !o };
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
function __asyncValues(o) {
|
|
462
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
463
|
-
var m = o[Symbol.asyncIterator], i;
|
|
464
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
465
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
466
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
470
|
-
var e = new Error(message);
|
|
471
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
472
|
-
};
|
|
473
|
-
|
|
474
424
|
class BaseHttpResponseRenderer {
|
|
475
425
|
render(response) {
|
|
476
426
|
if (typeof response.body === 'string' ||
|
|
@@ -702,7 +652,10 @@ class BaseHttpResponse {
|
|
|
702
652
|
for (const cookie of newCookies) {
|
|
703
653
|
removeCookie(cookie.slice(0, cookie.indexOf('=')));
|
|
704
654
|
}
|
|
705
|
-
this._headers =
|
|
655
|
+
this._headers = {
|
|
656
|
+
...headers(),
|
|
657
|
+
...this._headers,
|
|
658
|
+
};
|
|
706
659
|
const setCookie = [...newCookies, ...cookies()];
|
|
707
660
|
if (setCookie && setCookie.length) {
|
|
708
661
|
this._headers['set-cookie'] = setCookie;
|
|
@@ -727,96 +680,87 @@ class BaseHttpResponse {
|
|
|
727
680
|
logger.error(error);
|
|
728
681
|
throw error;
|
|
729
682
|
}
|
|
730
|
-
respond() {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
683
|
+
async respond() {
|
|
684
|
+
const { rawResponse, hasResponded } = useResponse();
|
|
685
|
+
const { method, rawRequest } = useRequest();
|
|
686
|
+
const logger = eventCore.useEventLogger('http-response');
|
|
687
|
+
if (hasResponded()) {
|
|
688
|
+
this.panic('The response was already sent.', logger);
|
|
689
|
+
}
|
|
690
|
+
this.mergeHeaders();
|
|
691
|
+
const res = rawResponse();
|
|
692
|
+
if (this.body instanceof stream.Readable) {
|
|
693
|
+
// responding with readable stream
|
|
694
|
+
const stream = this.body;
|
|
695
|
+
this.mergeStatus('ok');
|
|
696
|
+
res.writeHead(this.status, {
|
|
697
|
+
...this._headers,
|
|
698
|
+
});
|
|
699
|
+
rawRequest.once('close', () => {
|
|
700
|
+
stream.destroy();
|
|
701
|
+
});
|
|
702
|
+
if (method === 'HEAD') {
|
|
703
|
+
stream.destroy();
|
|
704
|
+
res.end();
|
|
737
705
|
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
res.writeHead(this.status, Object.assign({}, this._headers));
|
|
745
|
-
rawRequest.once('close', () => {
|
|
746
|
-
stream.destroy();
|
|
747
|
-
});
|
|
748
|
-
if (method === 'HEAD') {
|
|
749
|
-
stream.destroy();
|
|
750
|
-
res.end();
|
|
751
|
-
}
|
|
752
|
-
else {
|
|
753
|
-
return new Promise((resolve, reject) => {
|
|
754
|
-
stream.on('error', (e) => {
|
|
755
|
-
stream.destroy();
|
|
756
|
-
res.end();
|
|
757
|
-
reject(e);
|
|
758
|
-
});
|
|
759
|
-
stream.on('close', () => {
|
|
760
|
-
stream.destroy();
|
|
761
|
-
resolve(undefined);
|
|
762
|
-
});
|
|
763
|
-
stream.pipe(res);
|
|
706
|
+
else {
|
|
707
|
+
return new Promise((resolve, reject) => {
|
|
708
|
+
stream.on('error', (e) => {
|
|
709
|
+
stream.destroy();
|
|
710
|
+
res.end();
|
|
711
|
+
reject(e);
|
|
764
712
|
});
|
|
765
|
-
|
|
713
|
+
stream.on('close', () => {
|
|
714
|
+
stream.destroy();
|
|
715
|
+
resolve(undefined);
|
|
716
|
+
});
|
|
717
|
+
stream.pipe(res);
|
|
718
|
+
});
|
|
766
719
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
else {
|
|
774
|
-
const additionalHeaders = {};
|
|
775
|
-
if (this.body.headers.get('content-length')) {
|
|
776
|
-
additionalHeaders['content-length'] = this.body.headers.get('content-length');
|
|
777
|
-
}
|
|
778
|
-
if (this.body.headers.get('content-type')) {
|
|
779
|
-
additionalHeaders['content-type'] = this.body.headers.get('content-type');
|
|
780
|
-
}
|
|
781
|
-
res.writeHead(this.status, Object.assign(Object.assign({}, additionalHeaders), this._headers));
|
|
782
|
-
yield respondWithFetch(this.body.body, res);
|
|
783
|
-
}
|
|
720
|
+
}
|
|
721
|
+
else if (globalThis.Response &&
|
|
722
|
+
this.body instanceof Response /* Fetch Response */) {
|
|
723
|
+
this.mergeFetchStatus(this.body.status);
|
|
724
|
+
if (method === 'HEAD') {
|
|
725
|
+
res.end();
|
|
784
726
|
}
|
|
785
727
|
else {
|
|
786
|
-
const
|
|
787
|
-
this.
|
|
788
|
-
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
function respondWithFetch(fetchBody, res) {
|
|
794
|
-
var _a, e_1, _b, _c;
|
|
795
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
796
|
-
if (fetchBody) {
|
|
797
|
-
try {
|
|
798
|
-
try {
|
|
799
|
-
for (var _d = true, _e = __asyncValues(fetchBody), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
800
|
-
_c = _f.value;
|
|
801
|
-
_d = false;
|
|
802
|
-
const chunk = _c;
|
|
803
|
-
res.write(chunk);
|
|
804
|
-
}
|
|
728
|
+
const additionalHeaders = {};
|
|
729
|
+
if (this.body.headers.get('content-length')) {
|
|
730
|
+
additionalHeaders['content-length'] = this.body.headers.get('content-length');
|
|
805
731
|
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
try {
|
|
809
|
-
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
810
|
-
}
|
|
811
|
-
finally { if (e_1) throw e_1.error; }
|
|
732
|
+
if (this.body.headers.get('content-type')) {
|
|
733
|
+
additionalHeaders['content-type'] = this.body.headers.get('content-type');
|
|
812
734
|
}
|
|
735
|
+
res.writeHead(this.status, {
|
|
736
|
+
...additionalHeaders,
|
|
737
|
+
...this._headers,
|
|
738
|
+
});
|
|
739
|
+
await respondWithFetch(this.body.body, res);
|
|
813
740
|
}
|
|
814
|
-
|
|
815
|
-
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
const renderedBody = this.renderer.render(this);
|
|
744
|
+
this.mergeStatus(renderedBody);
|
|
745
|
+
res.writeHead(this.status, {
|
|
746
|
+
'content-length': Buffer.byteLength(renderedBody),
|
|
747
|
+
...this._headers,
|
|
748
|
+
}).end(method !== 'HEAD' ? renderedBody : '');
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
async function respondWithFetch(fetchBody, res) {
|
|
753
|
+
if (fetchBody) {
|
|
754
|
+
try {
|
|
755
|
+
for await (const chunk of fetchBody) {
|
|
756
|
+
res.write(chunk);
|
|
816
757
|
}
|
|
817
758
|
}
|
|
818
|
-
|
|
819
|
-
|
|
759
|
+
catch (e) {
|
|
760
|
+
// ?
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
res.end();
|
|
820
764
|
}
|
|
821
765
|
|
|
822
766
|
const preStyles = 'font-family: monospace;' +
|
|
@@ -836,9 +780,14 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
836
780
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
837
781
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
838
782
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
839
|
-
`<center style="color: #666;"> Wooks v${"0.4.
|
|
783
|
+
`<center style="color: #666;"> Wooks v${"0.4.11"} </center>` +
|
|
840
784
|
`${keys.length
|
|
841
|
-
? `<pre style="${preStyles}">${JSON.stringify(
|
|
785
|
+
? `<pre style="${preStyles}">${JSON.stringify({
|
|
786
|
+
...data,
|
|
787
|
+
statusCode: undefined,
|
|
788
|
+
message: undefined,
|
|
789
|
+
error: undefined,
|
|
790
|
+
}, null, ' ')}</pre>`
|
|
842
791
|
: ''}` +
|
|
843
792
|
'</body></html>');
|
|
844
793
|
}
|
|
@@ -848,7 +797,12 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
848
797
|
const keys = Object.keys(data).filter((key) => !['statusCode', 'error', 'message'].includes(key));
|
|
849
798
|
return (`${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}` +
|
|
850
799
|
`\n\n${keys.length
|
|
851
|
-
? `${JSON.stringify(
|
|
800
|
+
? `${JSON.stringify({
|
|
801
|
+
...data,
|
|
802
|
+
statusCode: undefined,
|
|
803
|
+
message: undefined,
|
|
804
|
+
error: undefined,
|
|
805
|
+
}, null, ' ')}`
|
|
852
806
|
: ''}`);
|
|
853
807
|
}
|
|
854
808
|
renderJson(response) {
|
|
@@ -866,9 +820,8 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
866
820
|
: ''}}`);
|
|
867
821
|
}
|
|
868
822
|
render(response) {
|
|
869
|
-
var _a;
|
|
870
823
|
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
871
|
-
response.status =
|
|
824
|
+
response.status = response.body?.statusCode || 500;
|
|
872
825
|
if (acceptsJson()) {
|
|
873
826
|
return this.renderJson(response);
|
|
874
827
|
}
|
|
@@ -902,7 +855,12 @@ class HttpError extends Error {
|
|
|
902
855
|
message: this.message,
|
|
903
856
|
error: httpStatusCodes[this.code],
|
|
904
857
|
}
|
|
905
|
-
:
|
|
858
|
+
: {
|
|
859
|
+
...this._body,
|
|
860
|
+
statusCode: this.code,
|
|
861
|
+
message: this.message,
|
|
862
|
+
error: httpStatusCodes[this.code],
|
|
863
|
+
};
|
|
906
864
|
}
|
|
907
865
|
attachRenderer(renderer) {
|
|
908
866
|
this.renderer = renderer;
|
|
@@ -942,16 +900,16 @@ errorRenderer = new HttpErrorRenderer()) {
|
|
|
942
900
|
}
|
|
943
901
|
return {
|
|
944
902
|
createResponse,
|
|
945
|
-
respond: (data) =>
|
|
903
|
+
respond: (data) => createResponse(data)?.respond(),
|
|
946
904
|
};
|
|
947
905
|
}
|
|
948
906
|
|
|
949
907
|
class WooksHttp extends wooks.WooksAdapterBase {
|
|
950
908
|
constructor(opts, wooks) {
|
|
951
|
-
super(wooks, opts
|
|
909
|
+
super(wooks, opts?.logger, opts?.router);
|
|
952
910
|
this.opts = opts;
|
|
953
911
|
this.responder = createWooksResponder();
|
|
954
|
-
this.logger =
|
|
912
|
+
this.logger = opts?.logger || this.getLogger('wooks-http');
|
|
955
913
|
}
|
|
956
914
|
all(path, handler) {
|
|
957
915
|
return this.on('*', path, handler);
|
|
@@ -982,14 +940,12 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
982
940
|
*
|
|
983
941
|
* Use this only if you rely on Wooks server.
|
|
984
942
|
*/
|
|
985
|
-
listen(...args) {
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
server.listen(...args);
|
|
992
|
-
});
|
|
943
|
+
async listen(...args) {
|
|
944
|
+
const server = (this.server = http.createServer(this.getServerCb()));
|
|
945
|
+
return new Promise((resolve, reject) => {
|
|
946
|
+
server.once('listening', resolve);
|
|
947
|
+
server.once('error', reject);
|
|
948
|
+
server.listen(...args);
|
|
993
949
|
});
|
|
994
950
|
}
|
|
995
951
|
/**
|
|
@@ -999,7 +955,7 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
999
955
|
close(server) {
|
|
1000
956
|
const srv = server || this.server;
|
|
1001
957
|
return new Promise((resolve, reject) => {
|
|
1002
|
-
srv
|
|
958
|
+
srv?.close((err) => {
|
|
1003
959
|
if (err)
|
|
1004
960
|
return reject(err);
|
|
1005
961
|
resolve(srv);
|
|
@@ -1026,10 +982,9 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1026
982
|
this.server = server;
|
|
1027
983
|
}
|
|
1028
984
|
respond(data) {
|
|
1029
|
-
|
|
1030
|
-
void ((_a = this.responder.respond(data)) === null || _a === void 0 ? void 0 : _a.catch((e) => {
|
|
985
|
+
void this.responder.respond(data)?.catch((e) => {
|
|
1031
986
|
this.logger.error('Uncought response exception', e);
|
|
1032
|
-
})
|
|
987
|
+
});
|
|
1033
988
|
}
|
|
1034
989
|
/**
|
|
1035
990
|
* Returns server callback function
|
|
@@ -1044,13 +999,12 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1044
999
|
* ```
|
|
1045
1000
|
*/
|
|
1046
1001
|
getServerCb() {
|
|
1047
|
-
return (req, res) =>
|
|
1048
|
-
|
|
1049
|
-
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
1002
|
+
return async (req, res) => {
|
|
1003
|
+
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
1050
1004
|
const { handlers } = this.wooks.lookup(req.method, req.url);
|
|
1051
|
-
if (handlers ||
|
|
1005
|
+
if (handlers || this.opts?.onNotFound) {
|
|
1052
1006
|
try {
|
|
1053
|
-
|
|
1007
|
+
await this.processHandlers(handlers || [this.opts?.onNotFound]);
|
|
1054
1008
|
}
|
|
1055
1009
|
catch (e) {
|
|
1056
1010
|
this.logger.error('Internal error, please report', e);
|
|
@@ -1065,35 +1019,33 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1065
1019
|
this.respond(new HttpError(404));
|
|
1066
1020
|
clearCtx();
|
|
1067
1021
|
}
|
|
1068
|
-
}
|
|
1022
|
+
};
|
|
1069
1023
|
}
|
|
1070
|
-
processHandlers(handlers) {
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1024
|
+
async processHandlers(handlers) {
|
|
1025
|
+
const { restoreCtx, clearCtx, store } = useHttpContext();
|
|
1026
|
+
for (const [i, handler] of handlers.entries()) {
|
|
1027
|
+
const isLastHandler = handlers.length === i + 1;
|
|
1028
|
+
try {
|
|
1029
|
+
restoreCtx();
|
|
1030
|
+
const promise = handler();
|
|
1031
|
+
clearCtx();
|
|
1032
|
+
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
|
+
restoreCtx();
|
|
1036
|
+
this.respond(result);
|
|
1037
|
+
clearCtx();
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
catch (e) {
|
|
1041
|
+
this.logger.error(`Uncought route handler exception: ${store('event').get('req').url || ''}`, e);
|
|
1042
|
+
if (isLastHandler) {
|
|
1082
1043
|
restoreCtx();
|
|
1083
|
-
this.respond(
|
|
1044
|
+
this.respond(e);
|
|
1084
1045
|
clearCtx();
|
|
1085
|
-
break;
|
|
1086
|
-
}
|
|
1087
|
-
catch (e) {
|
|
1088
|
-
this.logger.error(`Uncought route handler exception: ${store('event').get('req').url || ''}`, e);
|
|
1089
|
-
if (isLastHandler) {
|
|
1090
|
-
restoreCtx();
|
|
1091
|
-
this.respond(e);
|
|
1092
|
-
clearCtx();
|
|
1093
|
-
}
|
|
1094
1046
|
}
|
|
1095
1047
|
}
|
|
1096
|
-
}
|
|
1048
|
+
}
|
|
1097
1049
|
}
|
|
1098
1050
|
}
|
|
1099
1051
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,10 @@ import http from 'http';
|
|
|
6
6
|
|
|
7
7
|
function createHttpContext(data, options) {
|
|
8
8
|
return createEventContext({
|
|
9
|
-
event:
|
|
9
|
+
event: {
|
|
10
|
+
...data,
|
|
11
|
+
type: 'HTTP',
|
|
12
|
+
},
|
|
10
13
|
options,
|
|
11
14
|
});
|
|
12
15
|
}
|
|
@@ -40,20 +43,20 @@ function useRequest() {
|
|
|
40
43
|
});
|
|
41
44
|
const reqId = useEventId().getId;
|
|
42
45
|
const forwardedIp = () => init('forwardedIp', () => {
|
|
43
|
-
var _a;
|
|
44
46
|
if (typeof req.headers[xForwardedFor] === 'string' &&
|
|
45
47
|
req.headers[xForwardedFor]) {
|
|
46
|
-
return
|
|
48
|
+
return req.headers[xForwardedFor]
|
|
47
49
|
.split(',')
|
|
48
|
-
.shift()
|
|
50
|
+
.shift()
|
|
51
|
+
?.trim();
|
|
49
52
|
}
|
|
50
53
|
else {
|
|
51
54
|
return '';
|
|
52
55
|
}
|
|
53
56
|
});
|
|
54
|
-
const remoteIp = () => init('remoteIp', () =>
|
|
57
|
+
const remoteIp = () => init('remoteIp', () => req.socket?.remoteAddress || req.connection?.remoteAddress || '');
|
|
55
58
|
function getIp(options) {
|
|
56
|
-
if (options
|
|
59
|
+
if (options?.trustProxy) {
|
|
57
60
|
return forwardedIp() || getIp();
|
|
58
61
|
}
|
|
59
62
|
else {
|
|
@@ -61,10 +64,9 @@ function useRequest() {
|
|
|
61
64
|
}
|
|
62
65
|
}
|
|
63
66
|
const getIpList = () => init('ipList', () => {
|
|
64
|
-
var _a, _b;
|
|
65
67
|
return {
|
|
66
|
-
remoteIp:
|
|
67
|
-
|
|
68
|
+
remoteIp: req.socket?.remoteAddress ||
|
|
69
|
+
req.connection?.remoteAddress ||
|
|
68
70
|
'',
|
|
69
71
|
forwarded: (req.headers[xForwardedFor] || '')
|
|
70
72
|
.split(',')
|
|
@@ -155,12 +157,12 @@ function useAuthorization() {
|
|
|
155
157
|
authorization,
|
|
156
158
|
authType,
|
|
157
159
|
authRawCredentials,
|
|
158
|
-
isBasic: () =>
|
|
159
|
-
isBearer: () =>
|
|
160
|
+
isBasic: () => authType()?.toLocaleLowerCase() === 'basic',
|
|
161
|
+
isBearer: () => authType()?.toLocaleLowerCase() === 'bearer',
|
|
160
162
|
basicCredentials: () => init('basicCredentials', () => {
|
|
161
163
|
if (authorization) {
|
|
162
164
|
const type = authType();
|
|
163
|
-
if (
|
|
165
|
+
if (type?.toLocaleLowerCase() === 'basic') {
|
|
164
166
|
const creds = Buffer.from(authRawCredentials() || '', 'base64').toString('ascii');
|
|
165
167
|
const [username, password] = creds.split(':');
|
|
166
168
|
return { username, password };
|
|
@@ -373,12 +375,12 @@ function useSetCookie(name) {
|
|
|
373
375
|
name,
|
|
374
376
|
type: 'cookie',
|
|
375
377
|
}, {
|
|
376
|
-
get: () =>
|
|
377
|
-
set: (value) =>
|
|
378
|
+
get: () => getCookie(name)?.value,
|
|
379
|
+
set: (value) => setCookie(name, value, getCookie(name)?.attrs),
|
|
378
380
|
});
|
|
379
381
|
return attachHook(valueHook, {
|
|
380
|
-
get: () =>
|
|
381
|
-
set: (attrs) =>
|
|
382
|
+
get: () => getCookie(name)?.attrs,
|
|
383
|
+
set: (attrs) => setCookie(name, getCookie(name)?.value || '', attrs),
|
|
382
384
|
}, 'attrs');
|
|
383
385
|
}
|
|
384
386
|
|
|
@@ -417,58 +419,6 @@ function useSearchParams() {
|
|
|
417
419
|
};
|
|
418
420
|
}
|
|
419
421
|
|
|
420
|
-
/******************************************************************************
|
|
421
|
-
Copyright (c) Microsoft Corporation.
|
|
422
|
-
|
|
423
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
424
|
-
purpose with or without fee is hereby granted.
|
|
425
|
-
|
|
426
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
427
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
428
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
429
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
430
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
431
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
432
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
433
|
-
***************************************************************************** */
|
|
434
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
438
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
439
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
440
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
441
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
442
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
443
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function __values(o) {
|
|
448
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
449
|
-
if (m) return m.call(o);
|
|
450
|
-
if (o && typeof o.length === "number") return {
|
|
451
|
-
next: function () {
|
|
452
|
-
if (o && i >= o.length) o = void 0;
|
|
453
|
-
return { value: o && o[i++], done: !o };
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function __asyncValues(o) {
|
|
460
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
461
|
-
var m = o[Symbol.asyncIterator], i;
|
|
462
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
463
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
464
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
468
|
-
var e = new Error(message);
|
|
469
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
470
|
-
};
|
|
471
|
-
|
|
472
422
|
class BaseHttpResponseRenderer {
|
|
473
423
|
render(response) {
|
|
474
424
|
if (typeof response.body === 'string' ||
|
|
@@ -700,7 +650,10 @@ class BaseHttpResponse {
|
|
|
700
650
|
for (const cookie of newCookies) {
|
|
701
651
|
removeCookie(cookie.slice(0, cookie.indexOf('=')));
|
|
702
652
|
}
|
|
703
|
-
this._headers =
|
|
653
|
+
this._headers = {
|
|
654
|
+
...headers(),
|
|
655
|
+
...this._headers,
|
|
656
|
+
};
|
|
704
657
|
const setCookie = [...newCookies, ...cookies()];
|
|
705
658
|
if (setCookie && setCookie.length) {
|
|
706
659
|
this._headers['set-cookie'] = setCookie;
|
|
@@ -725,96 +678,87 @@ class BaseHttpResponse {
|
|
|
725
678
|
logger.error(error);
|
|
726
679
|
throw error;
|
|
727
680
|
}
|
|
728
|
-
respond() {
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
681
|
+
async respond() {
|
|
682
|
+
const { rawResponse, hasResponded } = useResponse();
|
|
683
|
+
const { method, rawRequest } = useRequest();
|
|
684
|
+
const logger = useEventLogger('http-response');
|
|
685
|
+
if (hasResponded()) {
|
|
686
|
+
this.panic('The response was already sent.', logger);
|
|
687
|
+
}
|
|
688
|
+
this.mergeHeaders();
|
|
689
|
+
const res = rawResponse();
|
|
690
|
+
if (this.body instanceof Readable) {
|
|
691
|
+
// responding with readable stream
|
|
692
|
+
const stream = this.body;
|
|
693
|
+
this.mergeStatus('ok');
|
|
694
|
+
res.writeHead(this.status, {
|
|
695
|
+
...this._headers,
|
|
696
|
+
});
|
|
697
|
+
rawRequest.once('close', () => {
|
|
698
|
+
stream.destroy();
|
|
699
|
+
});
|
|
700
|
+
if (method === 'HEAD') {
|
|
701
|
+
stream.destroy();
|
|
702
|
+
res.end();
|
|
735
703
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
res.writeHead(this.status, Object.assign({}, this._headers));
|
|
743
|
-
rawRequest.once('close', () => {
|
|
744
|
-
stream.destroy();
|
|
745
|
-
});
|
|
746
|
-
if (method === 'HEAD') {
|
|
747
|
-
stream.destroy();
|
|
748
|
-
res.end();
|
|
749
|
-
}
|
|
750
|
-
else {
|
|
751
|
-
return new Promise((resolve, reject) => {
|
|
752
|
-
stream.on('error', (e) => {
|
|
753
|
-
stream.destroy();
|
|
754
|
-
res.end();
|
|
755
|
-
reject(e);
|
|
756
|
-
});
|
|
757
|
-
stream.on('close', () => {
|
|
758
|
-
stream.destroy();
|
|
759
|
-
resolve(undefined);
|
|
760
|
-
});
|
|
761
|
-
stream.pipe(res);
|
|
704
|
+
else {
|
|
705
|
+
return new Promise((resolve, reject) => {
|
|
706
|
+
stream.on('error', (e) => {
|
|
707
|
+
stream.destroy();
|
|
708
|
+
res.end();
|
|
709
|
+
reject(e);
|
|
762
710
|
});
|
|
763
|
-
|
|
711
|
+
stream.on('close', () => {
|
|
712
|
+
stream.destroy();
|
|
713
|
+
resolve(undefined);
|
|
714
|
+
});
|
|
715
|
+
stream.pipe(res);
|
|
716
|
+
});
|
|
764
717
|
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
else {
|
|
772
|
-
const additionalHeaders = {};
|
|
773
|
-
if (this.body.headers.get('content-length')) {
|
|
774
|
-
additionalHeaders['content-length'] = this.body.headers.get('content-length');
|
|
775
|
-
}
|
|
776
|
-
if (this.body.headers.get('content-type')) {
|
|
777
|
-
additionalHeaders['content-type'] = this.body.headers.get('content-type');
|
|
778
|
-
}
|
|
779
|
-
res.writeHead(this.status, Object.assign(Object.assign({}, additionalHeaders), this._headers));
|
|
780
|
-
yield respondWithFetch(this.body.body, res);
|
|
781
|
-
}
|
|
718
|
+
}
|
|
719
|
+
else if (globalThis.Response &&
|
|
720
|
+
this.body instanceof Response /* Fetch Response */) {
|
|
721
|
+
this.mergeFetchStatus(this.body.status);
|
|
722
|
+
if (method === 'HEAD') {
|
|
723
|
+
res.end();
|
|
782
724
|
}
|
|
783
725
|
else {
|
|
784
|
-
const
|
|
785
|
-
this.
|
|
786
|
-
|
|
787
|
-
}
|
|
788
|
-
});
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
function respondWithFetch(fetchBody, res) {
|
|
792
|
-
var _a, e_1, _b, _c;
|
|
793
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
794
|
-
if (fetchBody) {
|
|
795
|
-
try {
|
|
796
|
-
try {
|
|
797
|
-
for (var _d = true, _e = __asyncValues(fetchBody), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
798
|
-
_c = _f.value;
|
|
799
|
-
_d = false;
|
|
800
|
-
const chunk = _c;
|
|
801
|
-
res.write(chunk);
|
|
802
|
-
}
|
|
726
|
+
const additionalHeaders = {};
|
|
727
|
+
if (this.body.headers.get('content-length')) {
|
|
728
|
+
additionalHeaders['content-length'] = this.body.headers.get('content-length');
|
|
803
729
|
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
try {
|
|
807
|
-
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
808
|
-
}
|
|
809
|
-
finally { if (e_1) throw e_1.error; }
|
|
730
|
+
if (this.body.headers.get('content-type')) {
|
|
731
|
+
additionalHeaders['content-type'] = this.body.headers.get('content-type');
|
|
810
732
|
}
|
|
733
|
+
res.writeHead(this.status, {
|
|
734
|
+
...additionalHeaders,
|
|
735
|
+
...this._headers,
|
|
736
|
+
});
|
|
737
|
+
await respondWithFetch(this.body.body, res);
|
|
811
738
|
}
|
|
812
|
-
|
|
813
|
-
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
const renderedBody = this.renderer.render(this);
|
|
742
|
+
this.mergeStatus(renderedBody);
|
|
743
|
+
res.writeHead(this.status, {
|
|
744
|
+
'content-length': Buffer.byteLength(renderedBody),
|
|
745
|
+
...this._headers,
|
|
746
|
+
}).end(method !== 'HEAD' ? renderedBody : '');
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
async function respondWithFetch(fetchBody, res) {
|
|
751
|
+
if (fetchBody) {
|
|
752
|
+
try {
|
|
753
|
+
for await (const chunk of fetchBody) {
|
|
754
|
+
res.write(chunk);
|
|
814
755
|
}
|
|
815
756
|
}
|
|
816
|
-
|
|
817
|
-
|
|
757
|
+
catch (e) {
|
|
758
|
+
// ?
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
res.end();
|
|
818
762
|
}
|
|
819
763
|
|
|
820
764
|
const preStyles = 'font-family: monospace;' +
|
|
@@ -834,9 +778,14 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
834
778
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
835
779
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
836
780
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
837
|
-
`<center style="color: #666;"> Wooks v${"0.4.
|
|
781
|
+
`<center style="color: #666;"> Wooks v${"0.4.11"} </center>` +
|
|
838
782
|
`${keys.length
|
|
839
|
-
? `<pre style="${preStyles}">${JSON.stringify(
|
|
783
|
+
? `<pre style="${preStyles}">${JSON.stringify({
|
|
784
|
+
...data,
|
|
785
|
+
statusCode: undefined,
|
|
786
|
+
message: undefined,
|
|
787
|
+
error: undefined,
|
|
788
|
+
}, null, ' ')}</pre>`
|
|
840
789
|
: ''}` +
|
|
841
790
|
'</body></html>');
|
|
842
791
|
}
|
|
@@ -846,7 +795,12 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
846
795
|
const keys = Object.keys(data).filter((key) => !['statusCode', 'error', 'message'].includes(key));
|
|
847
796
|
return (`${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}` +
|
|
848
797
|
`\n\n${keys.length
|
|
849
|
-
? `${JSON.stringify(
|
|
798
|
+
? `${JSON.stringify({
|
|
799
|
+
...data,
|
|
800
|
+
statusCode: undefined,
|
|
801
|
+
message: undefined,
|
|
802
|
+
error: undefined,
|
|
803
|
+
}, null, ' ')}`
|
|
850
804
|
: ''}`);
|
|
851
805
|
}
|
|
852
806
|
renderJson(response) {
|
|
@@ -864,9 +818,8 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
864
818
|
: ''}}`);
|
|
865
819
|
}
|
|
866
820
|
render(response) {
|
|
867
|
-
var _a;
|
|
868
821
|
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
869
|
-
response.status =
|
|
822
|
+
response.status = response.body?.statusCode || 500;
|
|
870
823
|
if (acceptsJson()) {
|
|
871
824
|
return this.renderJson(response);
|
|
872
825
|
}
|
|
@@ -900,7 +853,12 @@ class HttpError extends Error {
|
|
|
900
853
|
message: this.message,
|
|
901
854
|
error: httpStatusCodes[this.code],
|
|
902
855
|
}
|
|
903
|
-
:
|
|
856
|
+
: {
|
|
857
|
+
...this._body,
|
|
858
|
+
statusCode: this.code,
|
|
859
|
+
message: this.message,
|
|
860
|
+
error: httpStatusCodes[this.code],
|
|
861
|
+
};
|
|
904
862
|
}
|
|
905
863
|
attachRenderer(renderer) {
|
|
906
864
|
this.renderer = renderer;
|
|
@@ -940,16 +898,16 @@ errorRenderer = new HttpErrorRenderer()) {
|
|
|
940
898
|
}
|
|
941
899
|
return {
|
|
942
900
|
createResponse,
|
|
943
|
-
respond: (data) =>
|
|
901
|
+
respond: (data) => createResponse(data)?.respond(),
|
|
944
902
|
};
|
|
945
903
|
}
|
|
946
904
|
|
|
947
905
|
class WooksHttp extends WooksAdapterBase {
|
|
948
906
|
constructor(opts, wooks) {
|
|
949
|
-
super(wooks, opts
|
|
907
|
+
super(wooks, opts?.logger, opts?.router);
|
|
950
908
|
this.opts = opts;
|
|
951
909
|
this.responder = createWooksResponder();
|
|
952
|
-
this.logger =
|
|
910
|
+
this.logger = opts?.logger || this.getLogger('wooks-http');
|
|
953
911
|
}
|
|
954
912
|
all(path, handler) {
|
|
955
913
|
return this.on('*', path, handler);
|
|
@@ -980,14 +938,12 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
980
938
|
*
|
|
981
939
|
* Use this only if you rely on Wooks server.
|
|
982
940
|
*/
|
|
983
|
-
listen(...args) {
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
server.listen(...args);
|
|
990
|
-
});
|
|
941
|
+
async listen(...args) {
|
|
942
|
+
const server = (this.server = http.createServer(this.getServerCb()));
|
|
943
|
+
return new Promise((resolve, reject) => {
|
|
944
|
+
server.once('listening', resolve);
|
|
945
|
+
server.once('error', reject);
|
|
946
|
+
server.listen(...args);
|
|
991
947
|
});
|
|
992
948
|
}
|
|
993
949
|
/**
|
|
@@ -997,7 +953,7 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
997
953
|
close(server) {
|
|
998
954
|
const srv = server || this.server;
|
|
999
955
|
return new Promise((resolve, reject) => {
|
|
1000
|
-
srv
|
|
956
|
+
srv?.close((err) => {
|
|
1001
957
|
if (err)
|
|
1002
958
|
return reject(err);
|
|
1003
959
|
resolve(srv);
|
|
@@ -1024,10 +980,9 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1024
980
|
this.server = server;
|
|
1025
981
|
}
|
|
1026
982
|
respond(data) {
|
|
1027
|
-
|
|
1028
|
-
void ((_a = this.responder.respond(data)) === null || _a === void 0 ? void 0 : _a.catch((e) => {
|
|
983
|
+
void this.responder.respond(data)?.catch((e) => {
|
|
1029
984
|
this.logger.error('Uncought response exception', e);
|
|
1030
|
-
})
|
|
985
|
+
});
|
|
1031
986
|
}
|
|
1032
987
|
/**
|
|
1033
988
|
* Returns server callback function
|
|
@@ -1042,13 +997,12 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1042
997
|
* ```
|
|
1043
998
|
*/
|
|
1044
999
|
getServerCb() {
|
|
1045
|
-
return (req, res) =>
|
|
1046
|
-
|
|
1047
|
-
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
1000
|
+
return async (req, res) => {
|
|
1001
|
+
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
1048
1002
|
const { handlers } = this.wooks.lookup(req.method, req.url);
|
|
1049
|
-
if (handlers ||
|
|
1003
|
+
if (handlers || this.opts?.onNotFound) {
|
|
1050
1004
|
try {
|
|
1051
|
-
|
|
1005
|
+
await this.processHandlers(handlers || [this.opts?.onNotFound]);
|
|
1052
1006
|
}
|
|
1053
1007
|
catch (e) {
|
|
1054
1008
|
this.logger.error('Internal error, please report', e);
|
|
@@ -1063,35 +1017,33 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1063
1017
|
this.respond(new HttpError(404));
|
|
1064
1018
|
clearCtx();
|
|
1065
1019
|
}
|
|
1066
|
-
}
|
|
1020
|
+
};
|
|
1067
1021
|
}
|
|
1068
|
-
processHandlers(handlers) {
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1022
|
+
async processHandlers(handlers) {
|
|
1023
|
+
const { restoreCtx, clearCtx, store } = useHttpContext();
|
|
1024
|
+
for (const [i, handler] of handlers.entries()) {
|
|
1025
|
+
const isLastHandler = handlers.length === i + 1;
|
|
1026
|
+
try {
|
|
1027
|
+
restoreCtx();
|
|
1028
|
+
const promise = handler();
|
|
1029
|
+
clearCtx();
|
|
1030
|
+
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
|
+
restoreCtx();
|
|
1034
|
+
this.respond(result);
|
|
1035
|
+
clearCtx();
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
catch (e) {
|
|
1039
|
+
this.logger.error(`Uncought route handler exception: ${store('event').get('req').url || ''}`, e);
|
|
1040
|
+
if (isLastHandler) {
|
|
1080
1041
|
restoreCtx();
|
|
1081
|
-
this.respond(
|
|
1042
|
+
this.respond(e);
|
|
1082
1043
|
clearCtx();
|
|
1083
|
-
break;
|
|
1084
|
-
}
|
|
1085
|
-
catch (e) {
|
|
1086
|
-
this.logger.error(`Uncought route handler exception: ${store('event').get('req').url || ''}`, e);
|
|
1087
|
-
if (isLastHandler) {
|
|
1088
|
-
restoreCtx();
|
|
1089
|
-
this.respond(e);
|
|
1090
|
-
clearCtx();
|
|
1091
|
-
}
|
|
1092
1044
|
}
|
|
1093
1045
|
}
|
|
1094
|
-
}
|
|
1046
|
+
}
|
|
1095
1047
|
}
|
|
1096
1048
|
}
|
|
1097
1049
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"wooks": "0.4.
|
|
34
|
+
"wooks": "0.4.11",
|
|
35
35
|
"@prostojs/router": "^0.2.1",
|
|
36
|
-
"@wooksjs/event-core": "0.4.
|
|
36
|
+
"@wooksjs/event-core": "0.4.11"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@prostojs/logger": "^0.3.7"
|