@wooksjs/event-http 0.1.0 → 0.2.2
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/README.md +17 -14
- package/dist/index.cjs +57 -68
- package/dist/index.d.ts +22 -23
- package/dist/index.mjs +58 -69
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
<img src="../../logo.png" width="128px"><br>
|
|
7
|
-
<a href="https://github.com/
|
|
7
|
+
<a href="https://github.com/wooksjs/wooksjs/blob/main/LICENSE">
|
|
8
8
|
<img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
|
|
9
9
|
</a>
|
|
10
10
|
</p>
|
|
@@ -24,6 +24,7 @@ As a part of `wooks` event processing framework, `@wooksjs/event-http` implement
|
|
|
24
24
|
|
|
25
25
|
### Official Wooks HTTP composables:
|
|
26
26
|
|
|
27
|
+
- [@wooksjs/http-body](https://github.com/wooksjs/wooksjs/tree/main/packages/http-body) - to parse body
|
|
27
28
|
- [@wooksjs/http-static](https://github.com/wooksjs/wooksjs/tree/main/packages/http-static) - to serve static files
|
|
28
29
|
- [@wooksjs/http-proxy](https://github.com/wooksjs/wooksjs/tree/main/packages/http-proxy) - to proxy requests
|
|
29
30
|
|
|
@@ -34,31 +35,33 @@ As a part of `wooks` event processing framework, `@wooksjs/event-http` implement
|
|
|
34
35
|
## Quick Start
|
|
35
36
|
|
|
36
37
|
```js
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
38
|
+
import { useRouteParams } from 'wooks'
|
|
39
|
+
import { createHttpApp } from '@wooksjs/event-http'
|
|
39
40
|
|
|
40
|
-
const app =
|
|
41
|
+
const app = createHttpApp()
|
|
41
42
|
|
|
42
43
|
app.on('GET', 'hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}))
|
|
47
|
-
```
|
|
45
|
+
// shortcuts for some methods are supported:
|
|
46
|
+
// app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
|
|
48
47
|
|
|
49
|
-
|
|
48
|
+
app.listen(3000, () => { console.log('Wooks Server is up on port 3000') })
|
|
49
|
+
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
### Get More Control on Server
|
|
52
|
+
You can create http(s) server manually and pass server callback from the wooks http app:
|
|
52
53
|
|
|
53
54
|
```js
|
|
54
|
-
import { useRouteParams
|
|
55
|
-
import {
|
|
55
|
+
import { useRouteParams } from 'wooks'
|
|
56
|
+
import { createHttpApp } from '@wooksjs/event-http'
|
|
57
|
+
import http from 'http' // or https
|
|
56
58
|
|
|
57
|
-
const app =
|
|
59
|
+
const app = createHttpApp()
|
|
58
60
|
|
|
59
61
|
app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
|
|
60
62
|
|
|
61
|
-
app.
|
|
63
|
+
const server = http.createServer(app.getServerCb())
|
|
64
|
+
server.listen(3000, () => { console.log('Wooks Server is up on port 3000') })
|
|
62
65
|
```
|
|
63
66
|
|
|
64
67
|
## Quick Navigation
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var crypto = require('crypto');
|
|
4
3
|
var eventCore = require('@wooksjs/event-core');
|
|
5
4
|
var url = require('url');
|
|
6
5
|
var stream = require('stream');
|
|
6
|
+
var wooks = require('wooks');
|
|
7
7
|
var http = require('http');
|
|
8
8
|
|
|
9
9
|
function createHttpContext(data) {
|
|
@@ -35,7 +35,7 @@ function useRequest() {
|
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
});
|
|
38
|
-
const reqId = ()
|
|
38
|
+
const reqId = eventCore.useEventId().getId;
|
|
39
39
|
const forwardedIp = () => init('forwardedIp', () => {
|
|
40
40
|
var _a;
|
|
41
41
|
if (typeof req.headers[xForwardedFor] === 'string' && req.headers[xForwardedFor]) {
|
|
@@ -812,7 +812,7 @@ class WooksErrorRenderer extends BaseWooksResponseRenderer {
|
|
|
812
812
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
813
813
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
814
814
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
815
|
-
`<center style="color: #666;"> Wooks v${"0.
|
|
815
|
+
`<center style="color: #666;"> Wooks v${"0.2.2"} </center>` +
|
|
816
816
|
`${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
|
|
817
817
|
'</body></html>';
|
|
818
818
|
}
|
|
@@ -909,33 +909,55 @@ errorRenderer = new WooksErrorRenderer()) {
|
|
|
909
909
|
};
|
|
910
910
|
}
|
|
911
911
|
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
912
|
+
class WooksHttp extends wooks.WooksAdapterBase {
|
|
913
|
+
constructor(opts, wooks) {
|
|
914
|
+
super(wooks);
|
|
915
|
+
this.opts = opts;
|
|
916
|
+
this.responder = createWooksResponder();
|
|
916
917
|
}
|
|
917
|
-
|
|
918
|
-
|
|
918
|
+
all(path, handler) {
|
|
919
|
+
return this.on('*', path, handler);
|
|
919
920
|
}
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
this.
|
|
937
|
-
|
|
938
|
-
|
|
921
|
+
get(path, handler) {
|
|
922
|
+
return this.on('GET', path, handler);
|
|
923
|
+
}
|
|
924
|
+
post(path, handler) {
|
|
925
|
+
return this.on('POST', path, handler);
|
|
926
|
+
}
|
|
927
|
+
put(path, handler) {
|
|
928
|
+
return this.on('PUT', path, handler);
|
|
929
|
+
}
|
|
930
|
+
patch(path, handler) {
|
|
931
|
+
return this.on('PATCH', path, handler);
|
|
932
|
+
}
|
|
933
|
+
delete(path, handler) {
|
|
934
|
+
return this.on('DELETE', path, handler);
|
|
935
|
+
}
|
|
936
|
+
head(path, handler) {
|
|
937
|
+
return this.on('HEAD', path, handler);
|
|
938
|
+
}
|
|
939
|
+
options(path, handler) {
|
|
940
|
+
return this.on('OPTIONS', path, handler);
|
|
941
|
+
}
|
|
942
|
+
listen(...args) {
|
|
943
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
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);
|
|
949
|
+
});
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
close(server) {
|
|
953
|
+
let srv = server || this.server;
|
|
954
|
+
return new Promise((resolve, reject) => {
|
|
955
|
+
srv === null || srv === void 0 ? void 0 : srv.close((err) => {
|
|
956
|
+
if (err)
|
|
957
|
+
return reject(err);
|
|
958
|
+
resolve(srv);
|
|
959
|
+
});
|
|
960
|
+
});
|
|
939
961
|
}
|
|
940
962
|
respond(data) {
|
|
941
963
|
var _a;
|
|
@@ -943,36 +965,10 @@ class WooksHttp {
|
|
|
943
965
|
traceError('Uncought response exception:\n', e);
|
|
944
966
|
}));
|
|
945
967
|
}
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
const hostname = this.hostname;
|
|
949
|
-
const cb = this.cb;
|
|
950
|
-
return new Promise((resolve, reject) => {
|
|
951
|
-
var _a;
|
|
952
|
-
const listenCb = () => {
|
|
953
|
-
var _a;
|
|
954
|
-
const fn = typeof hostname === 'function' ? hostname : cb;
|
|
955
|
-
if (fn) {
|
|
956
|
-
fn();
|
|
957
|
-
}
|
|
958
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.off('error', reject);
|
|
959
|
-
resolve();
|
|
960
|
-
};
|
|
961
|
-
try {
|
|
962
|
-
this.server = createServer({ port }, (req, res) => {
|
|
963
|
-
void this.processRequest(req, res, lookup);
|
|
964
|
-
}, typeof hostname === 'string' ? hostname : '', listenCb);
|
|
965
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.on('error', reject);
|
|
966
|
-
}
|
|
967
|
-
catch (e) {
|
|
968
|
-
reject(e);
|
|
969
|
-
}
|
|
970
|
-
});
|
|
971
|
-
}
|
|
972
|
-
processRequest(req, res, lookup) {
|
|
973
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
968
|
+
getServerCb() {
|
|
969
|
+
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
974
970
|
const { restoreCtx, clearCtx } = createHttpContext({ req, res });
|
|
975
|
-
const handlers = lookup(
|
|
971
|
+
const handlers = this.wooks.lookup(req.method, req.url);
|
|
976
972
|
if (handlers) {
|
|
977
973
|
try {
|
|
978
974
|
yield this.processHandlers(handlers);
|
|
@@ -1019,16 +1015,9 @@ class WooksHttp {
|
|
|
1019
1015
|
}
|
|
1020
1016
|
});
|
|
1021
1017
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close((err) => {
|
|
1026
|
-
if (err)
|
|
1027
|
-
return reject(err);
|
|
1028
|
-
resolve(this.server);
|
|
1029
|
-
});
|
|
1030
|
-
});
|
|
1031
|
-
}
|
|
1018
|
+
}
|
|
1019
|
+
function createHttpApp(opts, wooks) {
|
|
1020
|
+
return new WooksHttp(opts, wooks);
|
|
1032
1021
|
}
|
|
1033
1022
|
|
|
1034
1023
|
exports.BaseWooksResponse = BaseWooksResponse;
|
|
@@ -1037,9 +1026,9 @@ exports.WooksError = WooksError;
|
|
|
1037
1026
|
exports.WooksErrorRenderer = WooksErrorRenderer;
|
|
1038
1027
|
exports.WooksHttp = WooksHttp;
|
|
1039
1028
|
exports.WooksURLSearchParams = WooksURLSearchParams;
|
|
1029
|
+
exports.createHttpApp = createHttpApp;
|
|
1040
1030
|
exports.createHttpContext = createHttpContext;
|
|
1041
1031
|
exports.createWooksResponder = createWooksResponder;
|
|
1042
|
-
exports.httpShortcuts = httpShortcuts;
|
|
1043
1032
|
exports.renderCacheControl = renderCacheControl;
|
|
1044
1033
|
exports.useAccept = useAccept;
|
|
1045
1034
|
exports.useAuthorization = useAuthorization;
|
package/dist/index.d.ts
CHANGED
|
@@ -6,11 +6,11 @@ import { Server } from 'http';
|
|
|
6
6
|
import { ServerResponse } from 'http';
|
|
7
7
|
import { TGenericContextStore } from '@wooksjs/event-core';
|
|
8
8
|
import { TGenericEvent } from '@wooksjs/event-core';
|
|
9
|
+
import { TProstoRouterPathBuilder } from '@prostojs/router';
|
|
9
10
|
import { TWooksHandler } from 'wooks';
|
|
10
|
-
import { TWooksLookupArgs } from 'wooks';
|
|
11
|
-
import { TWooksLookupHandlers } from 'wooks';
|
|
12
|
-
import { TWooksSubscribeAdapter } from 'wooks';
|
|
13
11
|
import { URLSearchParams as URLSearchParams_2 } from 'url';
|
|
12
|
+
import { Wooks } from 'wooks';
|
|
13
|
+
import { WooksAdapterBase } from 'wooks';
|
|
14
14
|
|
|
15
15
|
export declare class BaseWooksResponse<BodyType = unknown> {
|
|
16
16
|
protected renderer: BaseWooksResponseRenderer;
|
|
@@ -42,6 +42,8 @@ export declare class BaseWooksResponseRenderer<T = unknown> implements TWooksRes
|
|
|
42
42
|
render(response: BaseWooksResponse<T>): string | Uint8Array;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
export declare function createHttpApp(opts?: TWooksHttpOptions, wooks?: Wooks | WooksAdapterBase): WooksHttp;
|
|
46
|
+
|
|
45
47
|
export declare function createHttpContext(data: THttpEventData): {
|
|
46
48
|
getCtx: () => THttpContextStore;
|
|
47
49
|
restoreCtx: () => THttpContextStore;
|
|
@@ -135,17 +137,6 @@ export declare enum EHttpStatusCode {
|
|
|
135
137
|
NetworkAuthenticationRequired = 511
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
export declare const httpShortcuts: {
|
|
139
|
-
all: string;
|
|
140
|
-
get: string;
|
|
141
|
-
post: string;
|
|
142
|
-
put: string;
|
|
143
|
-
patch: string;
|
|
144
|
-
delete: string;
|
|
145
|
-
head: string;
|
|
146
|
-
options: string;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
140
|
export declare function renderCacheControl(data: TCacheControl): string;
|
|
150
141
|
|
|
151
142
|
declare type TAuthCache = {
|
|
@@ -225,7 +216,6 @@ declare type THttpServerErrorCodes = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 5
|
|
|
225
216
|
declare type TRequestCache = {
|
|
226
217
|
rawBody: Promise<Buffer>;
|
|
227
218
|
parsed: unknown;
|
|
228
|
-
reqId?: string;
|
|
229
219
|
forwardedIp?: string;
|
|
230
220
|
remoteIp?: string;
|
|
231
221
|
ipList?: {
|
|
@@ -266,6 +256,9 @@ export declare interface TWooksErrorBodyExt extends TWooksErrorBody {
|
|
|
266
256
|
error: string;
|
|
267
257
|
}
|
|
268
258
|
|
|
259
|
+
export declare interface TWooksHttpOptions {
|
|
260
|
+
}
|
|
261
|
+
|
|
269
262
|
export declare interface TWooksResponseRenderer<T = unknown> {
|
|
270
263
|
render: (response: BaseWooksResponse<T>) => string | Uint8Array;
|
|
271
264
|
}
|
|
@@ -416,21 +409,27 @@ export declare class WooksErrorRenderer extends BaseWooksResponseRenderer<TWooks
|
|
|
416
409
|
render(response: BaseWooksResponse<TWooksErrorBodyExt>): string;
|
|
417
410
|
}
|
|
418
411
|
|
|
419
|
-
export declare class WooksHttp
|
|
420
|
-
protected
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
412
|
+
export declare class WooksHttp extends WooksAdapterBase {
|
|
413
|
+
protected opts?: TWooksHttpOptions | undefined;
|
|
414
|
+
constructor(opts?: TWooksHttpOptions | undefined, wooks?: Wooks | WooksAdapterBase);
|
|
415
|
+
all<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
416
|
+
get<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
417
|
+
post<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
418
|
+
put<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
419
|
+
patch<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
420
|
+
delete<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
421
|
+
head<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
422
|
+
options<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
|
|
424
423
|
protected server?: Server;
|
|
424
|
+
listen(...args: Parameters<Server['listen']>): Promise<unknown>;
|
|
425
|
+
close(server?: Server): Promise<unknown>;
|
|
425
426
|
protected responder: {
|
|
426
427
|
createResponse: <T = unknown>(data: T) => BaseWooksResponse<T | TWooksErrorBodyExt> | null;
|
|
427
428
|
respond: (data: unknown) => Promise<unknown> | undefined;
|
|
428
429
|
};
|
|
429
430
|
protected respond(data: unknown): void;
|
|
430
|
-
|
|
431
|
-
protected processRequest(req: IncomingMessage, res: ServerResponse, lookup: (route: TWooksLookupArgs) => TWooksLookupHandlers | null): Promise<void>;
|
|
431
|
+
getServerCb(): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
432
432
|
protected processHandlers(handlers: TWooksHandler<unknown>[]): Promise<void>;
|
|
433
|
-
close(): Promise<unknown>;
|
|
434
433
|
}
|
|
435
434
|
|
|
436
435
|
export declare class WooksURLSearchParams extends URLSearchParams_2 {
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createEventContext, useEventContext, attachHook } from '@wooksjs/event-core';
|
|
1
|
+
import { createEventContext, useEventContext, useEventId, attachHook } from '@wooksjs/event-core';
|
|
3
2
|
import { URLSearchParams } from 'url';
|
|
4
3
|
import { Readable } from 'stream';
|
|
4
|
+
import { WooksAdapterBase } from 'wooks';
|
|
5
5
|
import http from 'http';
|
|
6
6
|
|
|
7
7
|
function createHttpContext(data) {
|
|
@@ -33,7 +33,7 @@ function useRequest() {
|
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
|
-
const reqId = ()
|
|
36
|
+
const reqId = useEventId().getId;
|
|
37
37
|
const forwardedIp = () => init('forwardedIp', () => {
|
|
38
38
|
var _a;
|
|
39
39
|
if (typeof req.headers[xForwardedFor] === 'string' && req.headers[xForwardedFor]) {
|
|
@@ -810,7 +810,7 @@ class WooksErrorRenderer extends BaseWooksResponseRenderer {
|
|
|
810
810
|
`<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
|
|
811
811
|
`<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
|
|
812
812
|
`<center><h4>${data.message}</h1></center><hr color="#666">` +
|
|
813
|
-
`<center style="color: #666;"> Wooks v${"0.
|
|
813
|
+
`<center style="color: #666;"> Wooks v${"0.2.2"} </center>` +
|
|
814
814
|
`${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
|
|
815
815
|
'</body></html>';
|
|
816
816
|
}
|
|
@@ -907,33 +907,55 @@ errorRenderer = new WooksErrorRenderer()) {
|
|
|
907
907
|
};
|
|
908
908
|
}
|
|
909
909
|
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
910
|
+
class WooksHttp extends WooksAdapterBase {
|
|
911
|
+
constructor(opts, wooks) {
|
|
912
|
+
super(wooks);
|
|
913
|
+
this.opts = opts;
|
|
914
|
+
this.responder = createWooksResponder();
|
|
914
915
|
}
|
|
915
|
-
|
|
916
|
-
|
|
916
|
+
all(path, handler) {
|
|
917
|
+
return this.on('*', path, handler);
|
|
917
918
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
this.
|
|
935
|
-
|
|
936
|
-
|
|
919
|
+
get(path, handler) {
|
|
920
|
+
return this.on('GET', path, handler);
|
|
921
|
+
}
|
|
922
|
+
post(path, handler) {
|
|
923
|
+
return this.on('POST', path, handler);
|
|
924
|
+
}
|
|
925
|
+
put(path, handler) {
|
|
926
|
+
return this.on('PUT', path, handler);
|
|
927
|
+
}
|
|
928
|
+
patch(path, handler) {
|
|
929
|
+
return this.on('PATCH', path, handler);
|
|
930
|
+
}
|
|
931
|
+
delete(path, handler) {
|
|
932
|
+
return this.on('DELETE', path, handler);
|
|
933
|
+
}
|
|
934
|
+
head(path, handler) {
|
|
935
|
+
return this.on('HEAD', path, handler);
|
|
936
|
+
}
|
|
937
|
+
options(path, handler) {
|
|
938
|
+
return this.on('OPTIONS', path, handler);
|
|
939
|
+
}
|
|
940
|
+
listen(...args) {
|
|
941
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
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);
|
|
947
|
+
});
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
close(server) {
|
|
951
|
+
let srv = server || this.server;
|
|
952
|
+
return new Promise((resolve, reject) => {
|
|
953
|
+
srv === null || srv === void 0 ? void 0 : srv.close((err) => {
|
|
954
|
+
if (err)
|
|
955
|
+
return reject(err);
|
|
956
|
+
resolve(srv);
|
|
957
|
+
});
|
|
958
|
+
});
|
|
937
959
|
}
|
|
938
960
|
respond(data) {
|
|
939
961
|
var _a;
|
|
@@ -941,36 +963,10 @@ class WooksHttp {
|
|
|
941
963
|
traceError('Uncought response exception:\n', e);
|
|
942
964
|
}));
|
|
943
965
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
const hostname = this.hostname;
|
|
947
|
-
const cb = this.cb;
|
|
948
|
-
return new Promise((resolve, reject) => {
|
|
949
|
-
var _a;
|
|
950
|
-
const listenCb = () => {
|
|
951
|
-
var _a;
|
|
952
|
-
const fn = typeof hostname === 'function' ? hostname : cb;
|
|
953
|
-
if (fn) {
|
|
954
|
-
fn();
|
|
955
|
-
}
|
|
956
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.off('error', reject);
|
|
957
|
-
resolve();
|
|
958
|
-
};
|
|
959
|
-
try {
|
|
960
|
-
this.server = createServer({ port }, (req, res) => {
|
|
961
|
-
void this.processRequest(req, res, lookup);
|
|
962
|
-
}, typeof hostname === 'string' ? hostname : '', listenCb);
|
|
963
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.on('error', reject);
|
|
964
|
-
}
|
|
965
|
-
catch (e) {
|
|
966
|
-
reject(e);
|
|
967
|
-
}
|
|
968
|
-
});
|
|
969
|
-
}
|
|
970
|
-
processRequest(req, res, lookup) {
|
|
971
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
966
|
+
getServerCb() {
|
|
967
|
+
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
972
968
|
const { restoreCtx, clearCtx } = createHttpContext({ req, res });
|
|
973
|
-
const handlers = lookup(
|
|
969
|
+
const handlers = this.wooks.lookup(req.method, req.url);
|
|
974
970
|
if (handlers) {
|
|
975
971
|
try {
|
|
976
972
|
yield this.processHandlers(handlers);
|
|
@@ -1017,16 +1013,9 @@ class WooksHttp {
|
|
|
1017
1013
|
}
|
|
1018
1014
|
});
|
|
1019
1015
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close((err) => {
|
|
1024
|
-
if (err)
|
|
1025
|
-
return reject(err);
|
|
1026
|
-
resolve(this.server);
|
|
1027
|
-
});
|
|
1028
|
-
});
|
|
1029
|
-
}
|
|
1016
|
+
}
|
|
1017
|
+
function createHttpApp(opts, wooks) {
|
|
1018
|
+
return new WooksHttp(opts, wooks);
|
|
1030
1019
|
}
|
|
1031
1020
|
|
|
1032
|
-
export { BaseWooksResponse, BaseWooksResponseRenderer, EHttpStatusCode, WooksError, WooksErrorRenderer, WooksHttp, WooksURLSearchParams, createHttpContext, createWooksResponder,
|
|
1021
|
+
export { BaseWooksResponse, BaseWooksResponseRenderer, EHttpStatusCode, WooksError, WooksErrorRenderer, WooksHttp, WooksURLSearchParams, createHttpApp, createHttpContext, createWooksResponder, renderCacheControl, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-http#readme",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@wooksjs/event-core": "0.
|
|
36
|
-
"wooks": "0.
|
|
35
|
+
"@wooksjs/event-core": "0.2.2",
|
|
36
|
+
"wooks": "0.2.2"
|
|
37
37
|
}
|
|
38
38
|
}
|