@wooksjs/event-http 0.4.37 → 0.5.1
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 +20 -23
- package/dist/index.d.ts +223 -27
- package/dist/index.mjs +21 -24
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -7,7 +7,7 @@ var wooks = require('wooks');
|
|
|
7
7
|
var stream = require('stream');
|
|
8
8
|
|
|
9
9
|
function createHttpContext(data, options) {
|
|
10
|
-
return eventCore.
|
|
10
|
+
return eventCore.createAsyncEventContext({
|
|
11
11
|
event: {
|
|
12
12
|
...data,
|
|
13
13
|
type: 'HTTP',
|
|
@@ -16,7 +16,7 @@ function createHttpContext(data, options) {
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
function useHttpContext() {
|
|
19
|
-
return eventCore.
|
|
19
|
+
return eventCore.useAsyncEventContext('HTTP');
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
function escapeRegex(s) {
|
|
@@ -579,7 +579,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
579
579
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
580
580
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
581
581
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
582
|
-
`<center style="color: #666;"> Wooks v${"0.
|
|
582
|
+
`<center style="color: #666;"> Wooks v${"0.5.1"} </center>` +
|
|
583
583
|
`${keys.length > 0
|
|
584
584
|
? `<pre style="${preStyles}">${JSON.stringify({
|
|
585
585
|
...data,
|
|
@@ -960,35 +960,33 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
960
960
|
.finally(() => endEvent());
|
|
961
961
|
}
|
|
962
962
|
getServerCb() {
|
|
963
|
-
return
|
|
964
|
-
const
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
963
|
+
return (req, res) => {
|
|
964
|
+
const runInContext = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
965
|
+
return runInContext(async () => {
|
|
966
|
+
const { handlers } = this.wooks.lookup(req.method, req.url);
|
|
967
|
+
if (handlers || this.opts?.onNotFound) {
|
|
968
|
+
try {
|
|
969
|
+
await this.processHandlers(handlers || [this.opts?.onNotFound]);
|
|
970
|
+
}
|
|
971
|
+
catch (error) {
|
|
972
|
+
this.logger.error('Internal error, please report', error);
|
|
973
|
+
this.respond(error);
|
|
974
|
+
}
|
|
969
975
|
}
|
|
970
|
-
|
|
971
|
-
this.logger.
|
|
972
|
-
|
|
973
|
-
this.respond(error);
|
|
976
|
+
else {
|
|
977
|
+
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
978
|
+
this.respond(new HttpError(404));
|
|
974
979
|
}
|
|
975
|
-
}
|
|
976
|
-
else {
|
|
977
|
-
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
978
|
-
this.respond(new HttpError(404));
|
|
979
|
-
}
|
|
980
|
+
});
|
|
980
981
|
};
|
|
981
982
|
}
|
|
982
983
|
async processHandlers(handlers) {
|
|
983
|
-
const {
|
|
984
|
+
const { store } = useHttpContext();
|
|
984
985
|
for (const [i, handler] of handlers.entries()) {
|
|
985
986
|
const isLastHandler = handlers.length === i + 1;
|
|
986
987
|
try {
|
|
987
|
-
restoreCtx();
|
|
988
988
|
const promise = handler();
|
|
989
|
-
clearCtx();
|
|
990
989
|
const result = await promise;
|
|
991
|
-
restoreCtx();
|
|
992
990
|
this.respond(result);
|
|
993
991
|
break;
|
|
994
992
|
}
|
|
@@ -1000,7 +998,6 @@ class WooksHttp extends wooks.WooksAdapterBase {
|
|
|
1000
998
|
this.logger.error(`Uncought route handler exception: ${store('event').get('req')?.url || ''}`, error);
|
|
1001
999
|
}
|
|
1002
1000
|
if (isLastHandler) {
|
|
1003
|
-
restoreCtx();
|
|
1004
1001
|
this.respond(error);
|
|
1005
1002
|
}
|
|
1006
1003
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -379,38 +379,13 @@ declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBody
|
|
|
379
379
|
render(response: BaseHttpResponse<TWooksErrorBodyExt>): string;
|
|
380
380
|
}
|
|
381
381
|
|
|
382
|
-
declare function createHttpContext(data: THttpEventData, options: TEventOptions):
|
|
383
|
-
getCtx: () => THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>;
|
|
384
|
-
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
385
|
-
clearCtx: () => null;
|
|
386
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
387
|
-
store: <K extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData>>(key: K) => {
|
|
388
|
-
value: (THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K];
|
|
389
|
-
hook: <K2 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2) => {
|
|
390
|
-
value: Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K][K2];
|
|
391
|
-
isDefined: boolean;
|
|
392
|
-
};
|
|
393
|
-
init: <K2_1 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2_1, getter: () => Required<Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>[K2_1]) => Required<Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>[K2_1];
|
|
394
|
-
set: <K2_2 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2_2, v: Required<(THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K]>[K2_2]) => Required<(THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K]>[K2_2];
|
|
395
|
-
get: <K2_3 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2_3) => Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K][K2_3] | undefined;
|
|
396
|
-
has: <K2_4 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2_4) => boolean;
|
|
397
|
-
del: <K2_5 extends keyof Required<THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>>[K]>(key2: K2_5) => void;
|
|
398
|
-
entries: () => [string, unknown][];
|
|
399
|
-
clear: () => void;
|
|
400
|
-
};
|
|
401
|
-
getStore: <K_1 extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData>>(key: K_1) => (THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K_1];
|
|
402
|
-
setStore: <K_2 extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData>>(key: K_2, v: (THttpContextStore & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K_2]) => void;
|
|
403
|
-
setParentCtx: (parentCtx: unknown) => void;
|
|
404
|
-
restoreParentCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty> | null;
|
|
405
|
-
};
|
|
382
|
+
declare function createHttpContext(data: THttpEventData, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
|
|
406
383
|
/**
|
|
407
384
|
* Wrapper on useEventContext with HTTP event types
|
|
408
385
|
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
409
386
|
*/
|
|
410
387
|
declare function useHttpContext<T extends TEmpty>(): {
|
|
411
388
|
getCtx: () => THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>;
|
|
412
|
-
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
413
|
-
clearCtx: () => null;
|
|
414
389
|
endEvent: (abortReason?: string | undefined) => void;
|
|
415
390
|
store: <K extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData> | keyof T>(key: K) => {
|
|
416
391
|
value: (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K];
|
|
@@ -429,7 +404,228 @@ declare function useHttpContext<T extends TEmpty>(): {
|
|
|
429
404
|
getStore: <K_1 extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData> | keyof T>(key: K_1) => (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K_1];
|
|
430
405
|
setStore: <K_2 extends keyof THttpContextStore | keyof _wooksjs_event_core.TGenericContextStore<THttpEventData> | keyof T>(key: K_2, v: (THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>)[K_2]) => void;
|
|
431
406
|
setParentCtx: (parentCtx: unknown) => void;
|
|
432
|
-
|
|
407
|
+
hasParentCtx: () => boolean;
|
|
408
|
+
getParentCtx: <T2 = THttpContextStore & T & _wooksjs_event_core.TGenericContextStore<THttpEventData>>() => {
|
|
409
|
+
getCtx: () => T2;
|
|
410
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
411
|
+
store: <K_3 extends keyof T2>(key: K_3) => {
|
|
412
|
+
value: T2[K_3];
|
|
413
|
+
hook: <K2_6 extends keyof Required<T2>[K_3]>(key2: K2_6) => {
|
|
414
|
+
value: Required<T2>[K_3][K2_6];
|
|
415
|
+
isDefined: boolean;
|
|
416
|
+
};
|
|
417
|
+
init: <K2_7 extends keyof Required<T2>[K_3]>(key2: K2_7, getter: () => Required<Required<T2>[K_3]>[K2_7]) => Required<Required<T2>[K_3]>[K2_7];
|
|
418
|
+
set: <K2_8 extends keyof Required<T2>[K_3]>(key2: K2_8, v: Required<T2[K_3]>[K2_8]) => Required<T2[K_3]>[K2_8];
|
|
419
|
+
get: <K2_9 extends keyof Required<T2>[K_3]>(key2: K2_9) => Required<T2>[K_3][K2_9] | undefined;
|
|
420
|
+
has: <K2_10 extends keyof Required<T2>[K_3]>(key2: K2_10) => boolean;
|
|
421
|
+
del: <K2_11 extends keyof Required<T2>[K_3]>(key2: K2_11) => void;
|
|
422
|
+
entries: () => [string, unknown][];
|
|
423
|
+
clear: () => void;
|
|
424
|
+
};
|
|
425
|
+
getStore: <K_4 extends keyof T2>(key: K_4) => T2[K_4];
|
|
426
|
+
setStore: <K_5 extends keyof T2>(key: K_5, v: T2[K_5]) => void;
|
|
427
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
428
|
+
hasParentCtx: () => boolean;
|
|
429
|
+
getParentCtx: <T2_1 = T2>() => {
|
|
430
|
+
getCtx: () => T2_1;
|
|
431
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
432
|
+
store: <K_6 extends keyof T2_1>(key: K_6) => {
|
|
433
|
+
value: T2_1[K_6];
|
|
434
|
+
hook: <K2_12 extends keyof Required<T2_1>[K_6]>(key2: K2_12) => {
|
|
435
|
+
value: Required<T2_1>[K_6][K2_12];
|
|
436
|
+
isDefined: boolean;
|
|
437
|
+
};
|
|
438
|
+
init: <K2_13 extends keyof Required<T2_1>[K_6]>(key2: K2_13, getter: () => Required<Required<T2_1>[K_6]>[K2_13]) => Required<Required<T2_1>[K_6]>[K2_13];
|
|
439
|
+
set: <K2_14 extends keyof Required<T2_1>[K_6]>(key2: K2_14, v: Required<T2_1[K_6]>[K2_14]) => Required<T2_1[K_6]>[K2_14];
|
|
440
|
+
get: <K2_15 extends keyof Required<T2_1>[K_6]>(key2: K2_15) => Required<T2_1>[K_6][K2_15] | undefined;
|
|
441
|
+
has: <K2_16 extends keyof Required<T2_1>[K_6]>(key2: K2_16) => boolean;
|
|
442
|
+
del: <K2_17 extends keyof Required<T2_1>[K_6]>(key2: K2_17) => void;
|
|
443
|
+
entries: () => [string, unknown][];
|
|
444
|
+
clear: () => void;
|
|
445
|
+
};
|
|
446
|
+
getStore: <K_7 extends keyof T2_1>(key: K_7) => T2_1[K_7];
|
|
447
|
+
setStore: <K_8 extends keyof T2_1>(key: K_8, v: T2_1[K_8]) => void;
|
|
448
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
449
|
+
hasParentCtx: () => boolean;
|
|
450
|
+
getParentCtx: <T2_2 = T2_1>() => {
|
|
451
|
+
getCtx: () => T2_2;
|
|
452
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
453
|
+
store: <K_9 extends keyof T2_2>(key: K_9) => {
|
|
454
|
+
value: T2_2[K_9];
|
|
455
|
+
hook: <K2_18 extends keyof Required<T2_2>[K_9]>(key2: K2_18) => {
|
|
456
|
+
value: Required<T2_2>[K_9][K2_18];
|
|
457
|
+
isDefined: boolean;
|
|
458
|
+
};
|
|
459
|
+
init: <K2_19 extends keyof Required<T2_2>[K_9]>(key2: K2_19, getter: () => Required<Required<T2_2>[K_9]>[K2_19]) => Required<Required<T2_2>[K_9]>[K2_19];
|
|
460
|
+
set: <K2_20 extends keyof Required<T2_2>[K_9]>(key2: K2_20, v: Required<T2_2[K_9]>[K2_20]) => Required<T2_2[K_9]>[K2_20];
|
|
461
|
+
get: <K2_21 extends keyof Required<T2_2>[K_9]>(key2: K2_21) => Required<T2_2>[K_9][K2_21] | undefined;
|
|
462
|
+
has: <K2_22 extends keyof Required<T2_2>[K_9]>(key2: K2_22) => boolean;
|
|
463
|
+
del: <K2_23 extends keyof Required<T2_2>[K_9]>(key2: K2_23) => void;
|
|
464
|
+
entries: () => [string, unknown][];
|
|
465
|
+
clear: () => void;
|
|
466
|
+
};
|
|
467
|
+
getStore: <K_10 extends keyof T2_2>(key: K_10) => T2_2[K_10];
|
|
468
|
+
setStore: <K_11 extends keyof T2_2>(key: K_11, v: T2_2[K_11]) => void;
|
|
469
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
470
|
+
hasParentCtx: () => boolean;
|
|
471
|
+
getParentCtx: <T2_3 = T2_2>() => {
|
|
472
|
+
getCtx: () => T2_3;
|
|
473
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
474
|
+
store: <K_12 extends keyof T2_3>(key: K_12) => {
|
|
475
|
+
value: T2_3[K_12];
|
|
476
|
+
hook: <K2_24 extends keyof Required<T2_3>[K_12]>(key2: K2_24) => {
|
|
477
|
+
value: Required<T2_3>[K_12][K2_24];
|
|
478
|
+
isDefined: boolean;
|
|
479
|
+
};
|
|
480
|
+
init: <K2_25 extends keyof Required<T2_3>[K_12]>(key2: K2_25, getter: () => Required<Required<T2_3>[K_12]>[K2_25]) => Required<Required<T2_3>[K_12]>[K2_25];
|
|
481
|
+
set: <K2_26 extends keyof Required<T2_3>[K_12]>(key2: K2_26, v: Required<T2_3[K_12]>[K2_26]) => Required<T2_3[K_12]>[K2_26];
|
|
482
|
+
get: <K2_27 extends keyof Required<T2_3>[K_12]>(key2: K2_27) => Required<T2_3>[K_12][K2_27] | undefined;
|
|
483
|
+
has: <K2_28 extends keyof Required<T2_3>[K_12]>(key2: K2_28) => boolean;
|
|
484
|
+
del: <K2_29 extends keyof Required<T2_3>[K_12]>(key2: K2_29) => void;
|
|
485
|
+
entries: () => [string, unknown][];
|
|
486
|
+
clear: () => void;
|
|
487
|
+
};
|
|
488
|
+
getStore: <K_13 extends keyof T2_3>(key: K_13) => T2_3[K_13];
|
|
489
|
+
setStore: <K_14 extends keyof T2_3>(key: K_14, v: T2_3[K_14]) => void;
|
|
490
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
491
|
+
hasParentCtx: () => boolean;
|
|
492
|
+
getParentCtx: <T2_4 = T2_3>() => {
|
|
493
|
+
getCtx: () => T2_4;
|
|
494
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
495
|
+
store: <K_15 extends keyof T2_4>(key: K_15) => {
|
|
496
|
+
value: T2_4[K_15];
|
|
497
|
+
hook: <K2_30 extends keyof Required<T2_4>[K_15]>(key2: K2_30) => {
|
|
498
|
+
value: Required<T2_4>[K_15][K2_30];
|
|
499
|
+
isDefined: boolean;
|
|
500
|
+
};
|
|
501
|
+
init: <K2_31 extends keyof Required<T2_4>[K_15]>(key2: K2_31, getter: () => Required<Required<T2_4>[K_15]>[K2_31]) => Required<Required<T2_4>[K_15]>[K2_31];
|
|
502
|
+
set: <K2_32 extends keyof Required<T2_4>[K_15]>(key2: K2_32, v: Required<T2_4[K_15]>[K2_32]) => Required<T2_4[K_15]>[K2_32];
|
|
503
|
+
get: <K2_33 extends keyof Required<T2_4>[K_15]>(key2: K2_33) => Required<T2_4>[K_15][K2_33] | undefined;
|
|
504
|
+
has: <K2_34 extends keyof Required<T2_4>[K_15]>(key2: K2_34) => boolean;
|
|
505
|
+
del: <K2_35 extends keyof Required<T2_4>[K_15]>(key2: K2_35) => void;
|
|
506
|
+
entries: () => [string, unknown][];
|
|
507
|
+
clear: () => void;
|
|
508
|
+
};
|
|
509
|
+
getStore: <K_16 extends keyof T2_4>(key: K_16) => T2_4[K_16];
|
|
510
|
+
setStore: <K_17 extends keyof T2_4>(key: K_17, v: T2_4[K_17]) => void;
|
|
511
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
512
|
+
hasParentCtx: () => boolean;
|
|
513
|
+
getParentCtx: <T2_5 = T2_4>() => {
|
|
514
|
+
getCtx: () => T2_5;
|
|
515
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
516
|
+
store: <K_18 extends keyof T2_5>(key: K_18) => {
|
|
517
|
+
value: T2_5[K_18];
|
|
518
|
+
hook: <K2_36 extends keyof Required<T2_5>[K_18]>(key2: K2_36) => {
|
|
519
|
+
value: Required<T2_5>[K_18][K2_36];
|
|
520
|
+
isDefined: boolean;
|
|
521
|
+
};
|
|
522
|
+
init: <K2_37 extends keyof Required<T2_5>[K_18]>(key2: K2_37, getter: () => Required<Required<T2_5>[K_18]>[K2_37]) => Required<Required<T2_5>[K_18]>[K2_37];
|
|
523
|
+
set: <K2_38 extends keyof Required<T2_5>[K_18]>(key2: K2_38, v: Required<T2_5[K_18]>[K2_38]) => Required<T2_5[K_18]>[K2_38];
|
|
524
|
+
get: <K2_39 extends keyof Required<T2_5>[K_18]>(key2: K2_39) => Required<T2_5>[K_18][K2_39] | undefined;
|
|
525
|
+
has: <K2_40 extends keyof Required<T2_5>[K_18]>(key2: K2_40) => boolean;
|
|
526
|
+
del: <K2_41 extends keyof Required<T2_5>[K_18]>(key2: K2_41) => void;
|
|
527
|
+
entries: () => [string, unknown][];
|
|
528
|
+
clear: () => void;
|
|
529
|
+
};
|
|
530
|
+
getStore: <K_19 extends keyof T2_5>(key: K_19) => T2_5[K_19];
|
|
531
|
+
setStore: <K_20 extends keyof T2_5>(key: K_20, v: T2_5[K_20]) => void;
|
|
532
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
533
|
+
hasParentCtx: () => boolean;
|
|
534
|
+
getParentCtx: <T2_6 = T2_5>() => {
|
|
535
|
+
getCtx: () => T2_6;
|
|
536
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
537
|
+
store: <K_21 extends keyof T2_6>(key: K_21) => {
|
|
538
|
+
value: T2_6[K_21];
|
|
539
|
+
hook: <K2_42 extends keyof Required<T2_6>[K_21]>(key2: K2_42) => {
|
|
540
|
+
value: Required<T2_6>[K_21][K2_42];
|
|
541
|
+
isDefined: boolean;
|
|
542
|
+
};
|
|
543
|
+
init: <K2_43 extends keyof Required<T2_6>[K_21]>(key2: K2_43, getter: () => Required<Required<T2_6>[K_21]>[K2_43]) => Required<Required<T2_6>[K_21]>[K2_43];
|
|
544
|
+
set: <K2_44 extends keyof Required<T2_6>[K_21]>(key2: K2_44, v: Required<T2_6[K_21]>[K2_44]) => Required<T2_6[K_21]>[K2_44];
|
|
545
|
+
get: <K2_45 extends keyof Required<T2_6>[K_21]>(key2: K2_45) => Required<T2_6>[K_21][K2_45] | undefined;
|
|
546
|
+
has: <K2_46 extends keyof Required<T2_6>[K_21]>(key2: K2_46) => boolean;
|
|
547
|
+
del: <K2_47 extends keyof Required<T2_6>[K_21]>(key2: K2_47) => void;
|
|
548
|
+
entries: () => [string, unknown][];
|
|
549
|
+
clear: () => void;
|
|
550
|
+
};
|
|
551
|
+
getStore: <K_22 extends keyof T2_6>(key: K_22) => T2_6[K_22];
|
|
552
|
+
setStore: <K_23 extends keyof T2_6>(key: K_23, v: T2_6[K_23]) => void;
|
|
553
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
554
|
+
hasParentCtx: () => boolean;
|
|
555
|
+
getParentCtx: <T2_7 = T2_6>() => {
|
|
556
|
+
getCtx: () => T2_7;
|
|
557
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
558
|
+
store: <K_24 extends keyof T2_7>(key: K_24) => {
|
|
559
|
+
value: T2_7[K_24];
|
|
560
|
+
hook: <K2_48 extends keyof Required<T2_7>[K_24]>(key2: K2_48) => {
|
|
561
|
+
value: Required<T2_7>[K_24][K2_48];
|
|
562
|
+
isDefined: boolean;
|
|
563
|
+
};
|
|
564
|
+
init: <K2_49 extends keyof Required<T2_7>[K_24]>(key2: K2_49, getter: () => Required<Required<T2_7>[K_24]>[K2_49]) => Required<Required<T2_7>[K_24]>[K2_49];
|
|
565
|
+
set: <K2_50 extends keyof Required<T2_7>[K_24]>(key2: K2_50, v: Required<T2_7[K_24]>[K2_50]) => Required<T2_7[K_24]>[K2_50];
|
|
566
|
+
get: <K2_51 extends keyof Required<T2_7>[K_24]>(key2: K2_51) => Required<T2_7>[K_24][K2_51] | undefined;
|
|
567
|
+
has: <K2_52 extends keyof Required<T2_7>[K_24]>(key2: K2_52) => boolean;
|
|
568
|
+
del: <K2_53 extends keyof Required<T2_7>[K_24]>(key2: K2_53) => void;
|
|
569
|
+
entries: () => [string, unknown][];
|
|
570
|
+
clear: () => void;
|
|
571
|
+
};
|
|
572
|
+
getStore: <K_25 extends keyof T2_7>(key: K_25) => T2_7[K_25];
|
|
573
|
+
setStore: <K_26 extends keyof T2_7>(key: K_26, v: T2_7[K_26]) => void;
|
|
574
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
575
|
+
hasParentCtx: () => boolean;
|
|
576
|
+
getParentCtx: <T2_8 = T2_7>() => {
|
|
577
|
+
getCtx: () => T2_8;
|
|
578
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
579
|
+
store: <K_27 extends keyof T2_8>(key: K_27) => {
|
|
580
|
+
value: T2_8[K_27];
|
|
581
|
+
hook: <K2_54 extends keyof Required<T2_8>[K_27]>(key2: K2_54) => {
|
|
582
|
+
value: Required<T2_8>[K_27][K2_54];
|
|
583
|
+
isDefined: boolean;
|
|
584
|
+
};
|
|
585
|
+
init: <K2_55 extends keyof Required<T2_8>[K_27]>(key2: K2_55, getter: () => Required<Required<T2_8>[K_27]>[K2_55]) => Required<Required<T2_8>[K_27]>[K2_55];
|
|
586
|
+
set: <K2_56 extends keyof Required<T2_8>[K_27]>(key2: K2_56, v: Required<T2_8[K_27]>[K2_56]) => Required<T2_8[K_27]>[K2_56];
|
|
587
|
+
get: <K2_57 extends keyof Required<T2_8>[K_27]>(key2: K2_57) => Required<T2_8>[K_27][K2_57] | undefined;
|
|
588
|
+
has: <K2_58 extends keyof Required<T2_8>[K_27]>(key2: K2_58) => boolean;
|
|
589
|
+
del: <K2_59 extends keyof Required<T2_8>[K_27]>(key2: K2_59) => void;
|
|
590
|
+
entries: () => [string, unknown][];
|
|
591
|
+
clear: () => void;
|
|
592
|
+
};
|
|
593
|
+
getStore: <K_28 extends keyof T2_8>(key: K_28) => T2_8[K_28];
|
|
594
|
+
setStore: <K_29 extends keyof T2_8>(key: K_29, v: T2_8[K_29]) => void;
|
|
595
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
596
|
+
hasParentCtx: () => boolean;
|
|
597
|
+
getParentCtx: <T2_9 = T2_8>() => {
|
|
598
|
+
getCtx: () => T2_9;
|
|
599
|
+
endEvent: (abortReason?: string | undefined) => void;
|
|
600
|
+
store: <K_30 extends keyof T2_9>(key: K_30) => {
|
|
601
|
+
value: T2_9[K_30];
|
|
602
|
+
hook: <K2_60 extends keyof Required<T2_9>[K_30]>(key2: K2_60) => {
|
|
603
|
+
value: Required<T2_9>[K_30][K2_60];
|
|
604
|
+
isDefined: boolean;
|
|
605
|
+
};
|
|
606
|
+
init: <K2_61 extends keyof Required<T2_9>[K_30]>(key2: K2_61, getter: () => Required<Required<T2_9>[K_30]>[K2_61]) => Required<Required<T2_9>[K_30]>[K2_61];
|
|
607
|
+
set: <K2_62 extends keyof Required<T2_9>[K_30]>(key2: K2_62, v: Required<T2_9[K_30]>[K2_62]) => Required<T2_9[K_30]>[K2_62];
|
|
608
|
+
get: <K2_63 extends keyof Required<T2_9>[K_30]>(key2: K2_63) => Required<T2_9>[K_30][K2_63] | undefined;
|
|
609
|
+
has: <K2_64 extends keyof Required<T2_9>[K_30]>(key2: K2_64) => boolean;
|
|
610
|
+
del: <K2_65 extends keyof Required<T2_9>[K_30]>(key2: K2_65) => void;
|
|
611
|
+
entries: () => [string, unknown][];
|
|
612
|
+
clear: () => void;
|
|
613
|
+
};
|
|
614
|
+
getStore: <K_31 extends keyof T2_9>(key: K_31) => T2_9[K_31];
|
|
615
|
+
setStore: <K_32 extends keyof T2_9>(key: K_32, v: T2_9[K_32]) => void;
|
|
616
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
617
|
+
hasParentCtx: () => boolean;
|
|
618
|
+
getParentCtx: <T2_10 = T2_9>() => any;
|
|
619
|
+
};
|
|
620
|
+
};
|
|
621
|
+
};
|
|
622
|
+
};
|
|
623
|
+
};
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
};
|
|
627
|
+
};
|
|
628
|
+
};
|
|
433
629
|
};
|
|
434
630
|
|
|
435
631
|
declare function createWooksResponder(renderer?: TWooksResponseRenderer<any>, errorRenderer?: TWooksResponseRenderer<any>): {
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createAsyncEventContext, useAsyncEventContext, useEventId, attachHook, useEventLogger } from '@wooksjs/event-core';
|
|
2
2
|
import { URLSearchParams } from 'url';
|
|
3
3
|
import http from 'http';
|
|
4
4
|
import { WooksAdapterBase } from 'wooks';
|
|
5
5
|
import { Readable } from 'stream';
|
|
6
6
|
|
|
7
7
|
function createHttpContext(data, options) {
|
|
8
|
-
return
|
|
8
|
+
return createAsyncEventContext({
|
|
9
9
|
event: {
|
|
10
10
|
...data,
|
|
11
11
|
type: 'HTTP',
|
|
@@ -14,7 +14,7 @@ function createHttpContext(data, options) {
|
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
function useHttpContext() {
|
|
17
|
-
return
|
|
17
|
+
return useAsyncEventContext('HTTP');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
function escapeRegex(s) {
|
|
@@ -577,7 +577,7 @@ class HttpErrorRenderer extends BaseHttpResponseRenderer {
|
|
|
577
577
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
578
578
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
579
579
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
580
|
-
`<center style="color: #666;"> Wooks v${"0.
|
|
580
|
+
`<center style="color: #666;"> Wooks v${"0.5.1"} </center>` +
|
|
581
581
|
`${keys.length > 0
|
|
582
582
|
? `<pre style="${preStyles}">${JSON.stringify({
|
|
583
583
|
...data,
|
|
@@ -958,35 +958,33 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
958
958
|
.finally(() => endEvent());
|
|
959
959
|
}
|
|
960
960
|
getServerCb() {
|
|
961
|
-
return
|
|
962
|
-
const
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
961
|
+
return (req, res) => {
|
|
962
|
+
const runInContext = createHttpContext({ req, res }, this.mergeEventOptions(this.opts?.eventOptions));
|
|
963
|
+
return runInContext(async () => {
|
|
964
|
+
const { handlers } = this.wooks.lookup(req.method, req.url);
|
|
965
|
+
if (handlers || this.opts?.onNotFound) {
|
|
966
|
+
try {
|
|
967
|
+
await this.processHandlers(handlers || [this.opts?.onNotFound]);
|
|
968
|
+
}
|
|
969
|
+
catch (error) {
|
|
970
|
+
this.logger.error('Internal error, please report', error);
|
|
971
|
+
this.respond(error);
|
|
972
|
+
}
|
|
967
973
|
}
|
|
968
|
-
|
|
969
|
-
this.logger.
|
|
970
|
-
|
|
971
|
-
this.respond(error);
|
|
974
|
+
else {
|
|
975
|
+
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
976
|
+
this.respond(new HttpError(404));
|
|
972
977
|
}
|
|
973
|
-
}
|
|
974
|
-
else {
|
|
975
|
-
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
976
|
-
this.respond(new HttpError(404));
|
|
977
|
-
}
|
|
978
|
+
});
|
|
978
979
|
};
|
|
979
980
|
}
|
|
980
981
|
async processHandlers(handlers) {
|
|
981
|
-
const {
|
|
982
|
+
const { store } = useHttpContext();
|
|
982
983
|
for (const [i, handler] of handlers.entries()) {
|
|
983
984
|
const isLastHandler = handlers.length === i + 1;
|
|
984
985
|
try {
|
|
985
|
-
restoreCtx();
|
|
986
986
|
const promise = handler();
|
|
987
|
-
clearCtx();
|
|
988
987
|
const result = await promise;
|
|
989
|
-
restoreCtx();
|
|
990
988
|
this.respond(result);
|
|
991
989
|
break;
|
|
992
990
|
}
|
|
@@ -998,7 +996,6 @@ class WooksHttp extends WooksAdapterBase {
|
|
|
998
996
|
this.logger.error(`Uncought route handler exception: ${store('event').get('req')?.url || ''}`, error);
|
|
999
997
|
}
|
|
1000
998
|
if (isLastHandler) {
|
|
1001
|
-
restoreCtx();
|
|
1002
999
|
this.respond(error);
|
|
1003
1000
|
}
|
|
1004
1001
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"wooks": "0.
|
|
42
|
+
"wooks": "0.5.1",
|
|
43
43
|
"@prostojs/router": "^0.2.1",
|
|
44
|
-
"@wooksjs/event-core": "0.
|
|
44
|
+
"@wooksjs/event-core": "0.5.1"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@prostojs/logger": "^0.4.0"
|