@wooksjs/event-http 0.2.13 → 0.2.14
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 +19 -32
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +20 -33
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -6,9 +6,10 @@ var stream = require('stream');
|
|
|
6
6
|
var wooks = require('wooks');
|
|
7
7
|
var http = require('http');
|
|
8
8
|
|
|
9
|
-
function createHttpContext(data) {
|
|
9
|
+
function createHttpContext(data, options) {
|
|
10
10
|
return eventCore.createEventContext({
|
|
11
11
|
event: Object.assign(Object.assign({}, data), { type: 'HTTP' }),
|
|
12
|
+
options,
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
15
|
function useHttpContext() {
|
|
@@ -183,28 +184,6 @@ const units = {
|
|
|
183
184
|
Y: 1000 * 60 * 60 * 24 * 365,
|
|
184
185
|
};
|
|
185
186
|
|
|
186
|
-
const banner = () => `[${"@wooksjs/event-http"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
187
|
-
|
|
188
|
-
/* istanbul ignore file */
|
|
189
|
-
function warn(text) {
|
|
190
|
-
console.warn('[33m' + banner() + text + '[0m');
|
|
191
|
-
}
|
|
192
|
-
function logError(error) {
|
|
193
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
194
|
-
}
|
|
195
|
-
function traceError(expl, e) {
|
|
196
|
-
logError(expl);
|
|
197
|
-
logError(e.message);
|
|
198
|
-
if (e.stack) {
|
|
199
|
-
warn(e.stack);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function panic(error) {
|
|
204
|
-
logError(error);
|
|
205
|
-
return new Error(error);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
187
|
function renderCacheControl(data) {
|
|
209
188
|
let attrs = '';
|
|
210
189
|
for (const [a, v] of Object.entries(data)) {
|
|
@@ -218,7 +197,7 @@ function renderCacheControl(data) {
|
|
|
218
197
|
}
|
|
219
198
|
}
|
|
220
199
|
else {
|
|
221
|
-
|
|
200
|
+
throw new Error('Unknown Cache-Control attribute ' + a);
|
|
222
201
|
}
|
|
223
202
|
}
|
|
224
203
|
return attrs;
|
|
@@ -299,7 +278,7 @@ function renderCookie(key, data) {
|
|
|
299
278
|
attrs += val ? '; ' + val : '';
|
|
300
279
|
}
|
|
301
280
|
else {
|
|
302
|
-
|
|
281
|
+
throw new Error('Unknown Set-Cookie attribute ' + a);
|
|
303
282
|
}
|
|
304
283
|
}
|
|
305
284
|
return `${key}=${encodeURIComponent(data.value)}${attrs}`;
|
|
@@ -482,7 +461,7 @@ class BaseHttpResponseRenderer {
|
|
|
482
461
|
response.setContentType('application/json');
|
|
483
462
|
return JSON.stringify(response.body);
|
|
484
463
|
}
|
|
485
|
-
throw
|
|
464
|
+
throw new Error('Unsupported body format "' + typeof response.body + '"');
|
|
486
465
|
}
|
|
487
466
|
}
|
|
488
467
|
|
|
@@ -711,12 +690,18 @@ class BaseHttpResponse {
|
|
|
711
690
|
mergeFetchStatus(fetchStatus) {
|
|
712
691
|
this.status = this.status || useResponse().status() || fetchStatus;
|
|
713
692
|
}
|
|
693
|
+
panic(text, logger) {
|
|
694
|
+
const error = new Error(text);
|
|
695
|
+
logger.error(error);
|
|
696
|
+
throw error;
|
|
697
|
+
}
|
|
714
698
|
respond() {
|
|
715
699
|
return __awaiter(this, void 0, void 0, function* () {
|
|
716
700
|
const { rawResponse, hasResponded } = useResponse();
|
|
717
701
|
const { method, rawRequest } = useRequest();
|
|
702
|
+
const logger = eventCore.useEventLogger('http-response');
|
|
718
703
|
if (hasResponded()) {
|
|
719
|
-
|
|
704
|
+
this.panic('The response was already sent.', logger);
|
|
720
705
|
}
|
|
721
706
|
this.mergeHeaders();
|
|
722
707
|
const res = rawResponse();
|
|
@@ -823,7 +808,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
823
808
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
824
809
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
825
810
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
826
|
-
`<center style="color: #666;"> Wooks v${"0.2.
|
|
811
|
+
`<center style="color: #666;"> Wooks v${"0.2.14"} </center>` +
|
|
827
812
|
`${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
|
|
828
813
|
'</body></html>';
|
|
829
814
|
}
|
|
@@ -925,6 +910,7 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
925
910
|
super(wooks);
|
|
926
911
|
this.opts = opts;
|
|
927
912
|
this.responder = createWooksResponder();
|
|
913
|
+
this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-http');
|
|
928
914
|
}
|
|
929
915
|
all(path, handler) {
|
|
930
916
|
return this.on('*', path, handler);
|
|
@@ -979,19 +965,20 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
979
965
|
respond(data) {
|
|
980
966
|
var _a;
|
|
981
967
|
void ((_a = this.responder.respond(data)) === null || _a === void 0 ? void 0 : _a.catch((e) => {
|
|
982
|
-
|
|
968
|
+
this.logger.error('Uncought response exception', e);
|
|
983
969
|
}));
|
|
984
970
|
}
|
|
985
971
|
getServerCb() {
|
|
986
972
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
987
|
-
|
|
973
|
+
var _a;
|
|
974
|
+
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions) || {});
|
|
988
975
|
const handlers = this.wooks.lookup(req.method, req.url);
|
|
989
976
|
if (handlers) {
|
|
990
977
|
try {
|
|
991
978
|
yield this.processHandlers(handlers);
|
|
992
979
|
}
|
|
993
980
|
catch (e) {
|
|
994
|
-
|
|
981
|
+
this.logger.error('Internal error, please report', e);
|
|
995
982
|
restoreCtx();
|
|
996
983
|
this.respond(e);
|
|
997
984
|
clearCtx();
|
|
@@ -1022,7 +1009,7 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1022
1009
|
break;
|
|
1023
1010
|
}
|
|
1024
1011
|
catch (e) {
|
|
1025
|
-
|
|
1012
|
+
this.logger.error(`Uncought route handler exception: ${(store('event').get('req').url || '')}`, e);
|
|
1026
1013
|
if (isLastHandler) {
|
|
1027
1014
|
restoreCtx();
|
|
1028
1015
|
this.respond(e);
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ import { IncomingHttpHeaders } from 'http';
|
|
|
5
5
|
import { IncomingMessage } from 'http';
|
|
6
6
|
import { Server } from 'http';
|
|
7
7
|
import { ServerResponse } from 'http';
|
|
8
|
+
import { TConsoleBase } from '@prostojs/logger';
|
|
9
|
+
import { TEventOptions } from '@wooksjs/event-core';
|
|
8
10
|
import { TGenericContextStore } from '@wooksjs/event-core';
|
|
9
11
|
import { TGenericEvent } from '@wooksjs/event-core';
|
|
10
12
|
import { TProstoRouterPathBuilder } from '@prostojs/router';
|
|
@@ -37,6 +39,7 @@ export declare class BaseHttpResponse<BodyType = unknown> {
|
|
|
37
39
|
protected mergeHeaders(): this;
|
|
38
40
|
protected mergeStatus(renderedBody: string | Uint8Array): this;
|
|
39
41
|
protected mergeFetchStatus(fetchStatus: number): void;
|
|
42
|
+
protected panic(text: string, logger: TConsoleBase): void;
|
|
40
43
|
respond(): Promise<unknown>;
|
|
41
44
|
}
|
|
42
45
|
|
|
@@ -46,7 +49,7 @@ export declare class BaseHttpResponseRenderer<T = unknown> implements TWooksResp
|
|
|
46
49
|
|
|
47
50
|
export declare function createHttpApp(opts?: TWooksHttpOptions, wooks?: Wooks | WooksAdapterBase): WooksHttp;
|
|
48
51
|
|
|
49
|
-
export declare function createHttpContext(data: THttpEventData): {
|
|
52
|
+
export declare function createHttpContext(data: THttpEventData, options: TEventOptions): {
|
|
50
53
|
getCtx: () => THttpContextStore;
|
|
51
54
|
restoreCtx: () => THttpContextStore;
|
|
52
55
|
clearCtx: () => null;
|
|
@@ -344,6 +347,8 @@ export declare interface TWooksErrorBodyExt extends TWooksErrorBody {
|
|
|
344
347
|
}
|
|
345
348
|
|
|
346
349
|
export declare interface TWooksHttpOptions {
|
|
350
|
+
logger?: TConsoleBase;
|
|
351
|
+
eventOptions?: TEventOptions;
|
|
347
352
|
}
|
|
348
353
|
|
|
349
354
|
export declare interface TWooksResponseRenderer<T = unknown> {
|
|
@@ -481,6 +486,7 @@ export declare function useStatus(): {
|
|
|
481
486
|
|
|
482
487
|
export declare class WooksHttp extends WooksAdapterBase {
|
|
483
488
|
protected opts?: TWooksHttpOptions | undefined;
|
|
489
|
+
protected logger: TConsoleBase;
|
|
484
490
|
constructor(opts?: TWooksHttpOptions | undefined, wooks?: Wooks | WooksAdapterBase);
|
|
485
491
|
all<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
486
492
|
get<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { createEventContext, useEventContext, useEventId, attachHook } from '@wooksjs/event-core';
|
|
1
|
+
import { createEventContext, useEventContext, useEventId, attachHook, useEventLogger } from '@wooksjs/event-core';
|
|
2
2
|
import { URLSearchParams } from 'url';
|
|
3
3
|
import { Readable } from 'stream';
|
|
4
4
|
import { WooksAdapterBase } from 'wooks';
|
|
5
5
|
import http from 'http';
|
|
6
6
|
|
|
7
|
-
function createHttpContext(data) {
|
|
7
|
+
function createHttpContext(data, options) {
|
|
8
8
|
return createEventContext({
|
|
9
9
|
event: Object.assign(Object.assign({}, data), { type: 'HTTP' }),
|
|
10
|
+
options,
|
|
10
11
|
});
|
|
11
12
|
}
|
|
12
13
|
function useHttpContext() {
|
|
@@ -181,28 +182,6 @@ const units = {
|
|
|
181
182
|
Y: 1000 * 60 * 60 * 24 * 365,
|
|
182
183
|
};
|
|
183
184
|
|
|
184
|
-
const banner = () => `[${"@wooksjs/event-http"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
185
|
-
|
|
186
|
-
/* istanbul ignore file */
|
|
187
|
-
function warn(text) {
|
|
188
|
-
console.warn('[33m' + banner() + text + '[0m');
|
|
189
|
-
}
|
|
190
|
-
function logError(error) {
|
|
191
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
192
|
-
}
|
|
193
|
-
function traceError(expl, e) {
|
|
194
|
-
logError(expl);
|
|
195
|
-
logError(e.message);
|
|
196
|
-
if (e.stack) {
|
|
197
|
-
warn(e.stack);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function panic(error) {
|
|
202
|
-
logError(error);
|
|
203
|
-
return new Error(error);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
185
|
function renderCacheControl(data) {
|
|
207
186
|
let attrs = '';
|
|
208
187
|
for (const [a, v] of Object.entries(data)) {
|
|
@@ -216,7 +195,7 @@ function renderCacheControl(data) {
|
|
|
216
195
|
}
|
|
217
196
|
}
|
|
218
197
|
else {
|
|
219
|
-
|
|
198
|
+
throw new Error('Unknown Cache-Control attribute ' + a);
|
|
220
199
|
}
|
|
221
200
|
}
|
|
222
201
|
return attrs;
|
|
@@ -297,7 +276,7 @@ function renderCookie(key, data) {
|
|
|
297
276
|
attrs += val ? '; ' + val : '';
|
|
298
277
|
}
|
|
299
278
|
else {
|
|
300
|
-
|
|
279
|
+
throw new Error('Unknown Set-Cookie attribute ' + a);
|
|
301
280
|
}
|
|
302
281
|
}
|
|
303
282
|
return `${key}=${encodeURIComponent(data.value)}${attrs}`;
|
|
@@ -480,7 +459,7 @@ class BaseHttpResponseRenderer {
|
|
|
480
459
|
response.setContentType('application/json');
|
|
481
460
|
return JSON.stringify(response.body);
|
|
482
461
|
}
|
|
483
|
-
throw
|
|
462
|
+
throw new Error('Unsupported body format "' + typeof response.body + '"');
|
|
484
463
|
}
|
|
485
464
|
}
|
|
486
465
|
|
|
@@ -709,12 +688,18 @@ class BaseHttpResponse {
|
|
|
709
688
|
mergeFetchStatus(fetchStatus) {
|
|
710
689
|
this.status = this.status || useResponse().status() || fetchStatus;
|
|
711
690
|
}
|
|
691
|
+
panic(text, logger) {
|
|
692
|
+
const error = new Error(text);
|
|
693
|
+
logger.error(error);
|
|
694
|
+
throw error;
|
|
695
|
+
}
|
|
712
696
|
respond() {
|
|
713
697
|
return __awaiter(this, void 0, void 0, function* () {
|
|
714
698
|
const { rawResponse, hasResponded } = useResponse();
|
|
715
699
|
const { method, rawRequest } = useRequest();
|
|
700
|
+
const logger = useEventLogger('http-response');
|
|
716
701
|
if (hasResponded()) {
|
|
717
|
-
|
|
702
|
+
this.panic('The response was already sent.', logger);
|
|
718
703
|
}
|
|
719
704
|
this.mergeHeaders();
|
|
720
705
|
const res = rawResponse();
|
|
@@ -821,7 +806,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
821
806
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
822
807
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
823
808
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
824
|
-
`<center style="color: #666;"> Wooks v${"0.2.
|
|
809
|
+
`<center style="color: #666;"> Wooks v${"0.2.14"} </center>` +
|
|
825
810
|
`${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
|
|
826
811
|
'</body></html>';
|
|
827
812
|
}
|
|
@@ -923,6 +908,7 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
923
908
|
super(wooks);
|
|
924
909
|
this.opts = opts;
|
|
925
910
|
this.responder = createWooksResponder();
|
|
911
|
+
this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-http');
|
|
926
912
|
}
|
|
927
913
|
all(path, handler) {
|
|
928
914
|
return this.on('*', path, handler);
|
|
@@ -977,19 +963,20 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
977
963
|
respond(data) {
|
|
978
964
|
var _a;
|
|
979
965
|
void ((_a = this.responder.respond(data)) === null || _a === void 0 ? void 0 : _a.catch((e) => {
|
|
980
|
-
|
|
966
|
+
this.logger.error('Uncought response exception', e);
|
|
981
967
|
}));
|
|
982
968
|
}
|
|
983
969
|
getServerCb() {
|
|
984
970
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
985
|
-
|
|
971
|
+
var _a;
|
|
972
|
+
const { restoreCtx, clearCtx } = createHttpContext({ req, res }, ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions) || {});
|
|
986
973
|
const handlers = this.wooks.lookup(req.method, req.url);
|
|
987
974
|
if (handlers) {
|
|
988
975
|
try {
|
|
989
976
|
yield this.processHandlers(handlers);
|
|
990
977
|
}
|
|
991
978
|
catch (e) {
|
|
992
|
-
|
|
979
|
+
this.logger.error('Internal error, please report', e);
|
|
993
980
|
restoreCtx();
|
|
994
981
|
this.respond(e);
|
|
995
982
|
clearCtx();
|
|
@@ -1020,7 +1007,7 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
1020
1007
|
break;
|
|
1021
1008
|
}
|
|
1022
1009
|
catch (e) {
|
|
1023
|
-
|
|
1010
|
+
this.logger.error(`Uncought route handler exception: ${(store('event').get('req').url || '')}`, e);
|
|
1024
1011
|
if (isLastHandler) {
|
|
1025
1012
|
restoreCtx();
|
|
1026
1013
|
this.respond(e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,9 +31,12 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"wooks": "0.2.
|
|
34
|
+
"wooks": "0.2.14",
|
|
35
35
|
"@prostojs/router": "^0.0.16",
|
|
36
|
-
"@wooksjs/event-core": "0.2.
|
|
36
|
+
"@wooksjs/event-core": "0.2.14"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@prostojs/logger": "^0.3.2"
|
|
37
40
|
},
|
|
38
41
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-http#readme"
|
|
39
42
|
}
|