@wooksjs/event-http 0.1.0 → 0.2.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/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/prostojs/wooks/blob/main/LICENSE">
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 { Wooks, useRouteParams } from 'wooks'
38
- import { WooksHttp } from '@wooksjs/event-http'
38
+ import { useRouteParams } from 'wooks'
39
+ import { createHttpApp } from '@wooksjs/event-http'
39
40
 
40
- const app = new Wooks()
41
+ const app = createHttpApp()
41
42
 
42
43
  app.on('GET', 'hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
43
44
 
44
- app.subscribe(new WooksHttp(3000, () => {
45
- console.log('Wooks Server is up on port 3000')
46
- }))
47
- ```
45
+ // shortcuts for some methods are supported:
46
+ // app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
48
47
 
49
- ## Shortcuts
48
+ app.listen(3000, () => { console.log('Wooks Server is up on port 3000') })
49
+ ```
50
50
 
51
- You can use `get`, `post`, ... methods directly with `shortcuts` feature:
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, Wooks } from 'wooks'
55
- import { httpShortcuts, WooksHttp } from '@wooksjs/event-http'
55
+ import { useRouteParams } from 'wooks'
56
+ import { createHttpApp } from '@wooksjs/event-http'
57
+ import http from 'http' // or https
56
58
 
57
- const app = new Wooks().shortcuts(httpShortcuts)
59
+ const app = createHttpApp()
58
60
 
59
61
  app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
60
62
 
61
- app.subscribe(new WooksHttp(3000))
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
@@ -4,6 +4,7 @@ var crypto = require('crypto');
4
4
  var eventCore = require('@wooksjs/event-core');
5
5
  var url = require('url');
6
6
  var stream = require('stream');
7
+ var wooks = require('wooks');
7
8
  var http = require('http');
8
9
 
9
10
  function createHttpContext(data) {
@@ -812,7 +813,7 @@ class WooksErrorRenderer extends BaseWooksResponseRenderer {
812
813
  `<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
813
814
  `<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
814
815
  `<center><h4>${data.message}</h1></center><hr color="#666">` +
815
- `<center style="color: #666;"> Wooks v${"0.1.0"} </center>` +
816
+ `<center style="color: #666;"> Wooks v${"0.2.1"} </center>` +
816
817
  `${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
817
818
  '</body></html>';
818
819
  }
@@ -909,33 +910,55 @@ errorRenderer = new WooksErrorRenderer()) {
909
910
  };
910
911
  }
911
912
 
912
- function createServer(opts, cb, hostname, onListen) {
913
- const server = http.createServer(cb);
914
- if (hostname) {
915
- server.listen(opts.port, hostname, onListen);
913
+ class WooksHttp extends wooks.WooksAdapterBase {
914
+ constructor(opts, wooks) {
915
+ super(wooks);
916
+ this.opts = opts;
917
+ this.responder = createWooksResponder();
916
918
  }
917
- else {
918
- server.listen(opts.port, onListen);
919
+ all(path, handler) {
920
+ return this.on('*', path, handler);
919
921
  }
920
- return server;
921
- }
922
-
923
- const httpShortcuts = {
924
- all: '*',
925
- get: 'GET',
926
- post: 'POST',
927
- put: 'PUT',
928
- patch: 'PATCH',
929
- delete: 'DELETE',
930
- head: 'HEAD',
931
- options: 'OPTIONS',
932
- };
933
- class WooksHttp {
934
- constructor(port, hostname, cb) {
935
- this.port = port;
936
- this.hostname = hostname;
937
- this.cb = cb;
938
- this.responder = createWooksResponder();
922
+ get(path, handler) {
923
+ return this.on('GET', path, handler);
924
+ }
925
+ post(path, handler) {
926
+ return this.on('POST', path, handler);
927
+ }
928
+ put(path, handler) {
929
+ return this.on('PUT', path, handler);
930
+ }
931
+ patch(path, handler) {
932
+ return this.on('PATCH', path, handler);
933
+ }
934
+ delete(path, handler) {
935
+ return this.on('DELETE', path, handler);
936
+ }
937
+ head(path, handler) {
938
+ return this.on('HEAD', path, handler);
939
+ }
940
+ options(path, handler) {
941
+ return this.on('OPTIONS', path, handler);
942
+ }
943
+ listen(...args) {
944
+ return __awaiter(this, void 0, void 0, function* () {
945
+ const server = this.server = http.createServer(this.getServerCb());
946
+ return new Promise((resolve, reject) => {
947
+ server.once('listening', resolve);
948
+ server.once('error', reject);
949
+ server.listen(...args);
950
+ });
951
+ });
952
+ }
953
+ close(server) {
954
+ let srv = server || this.server;
955
+ return new Promise((resolve, reject) => {
956
+ srv === null || srv === void 0 ? void 0 : srv.close((err) => {
957
+ if (err)
958
+ return reject(err);
959
+ resolve(srv);
960
+ });
961
+ });
939
962
  }
940
963
  respond(data) {
941
964
  var _a;
@@ -943,36 +966,10 @@ class WooksHttp {
943
966
  traceError('Uncought response exception:\n', e);
944
967
  }));
945
968
  }
946
- subscribe(lookup) {
947
- const port = this.port;
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* () {
969
+ getServerCb() {
970
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
974
971
  const { restoreCtx, clearCtx } = createHttpContext({ req, res });
975
- const handlers = lookup({ method: req.method, url: req.url });
972
+ const handlers = this.wooks.lookup(req.method, req.url);
976
973
  if (handlers) {
977
974
  try {
978
975
  yield this.processHandlers(handlers);
@@ -1019,16 +1016,9 @@ class WooksHttp {
1019
1016
  }
1020
1017
  });
1021
1018
  }
1022
- close() {
1023
- return new Promise((resolve, reject) => {
1024
- var _a;
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
- }
1019
+ }
1020
+ function createHttpApp(opts, wooks) {
1021
+ return new WooksHttp(opts, wooks);
1032
1022
  }
1033
1023
 
1034
1024
  exports.BaseWooksResponse = BaseWooksResponse;
@@ -1037,9 +1027,9 @@ exports.WooksError = WooksError;
1037
1027
  exports.WooksErrorRenderer = WooksErrorRenderer;
1038
1028
  exports.WooksHttp = WooksHttp;
1039
1029
  exports.WooksURLSearchParams = WooksURLSearchParams;
1030
+ exports.createHttpApp = createHttpApp;
1040
1031
  exports.createHttpContext = createHttpContext;
1041
1032
  exports.createWooksResponder = createWooksResponder;
1042
- exports.httpShortcuts = httpShortcuts;
1043
1033
  exports.renderCacheControl = renderCacheControl;
1044
1034
  exports.useAccept = useAccept;
1045
1035
  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 = {
@@ -266,6 +257,9 @@ export declare interface TWooksErrorBodyExt extends TWooksErrorBody {
266
257
  error: string;
267
258
  }
268
259
 
260
+ export declare interface TWooksHttpOptions {
261
+ }
262
+
269
263
  export declare interface TWooksResponseRenderer<T = unknown> {
270
264
  render: (response: BaseWooksResponse<T>) => string | Uint8Array;
271
265
  }
@@ -416,21 +410,27 @@ export declare class WooksErrorRenderer extends BaseWooksResponseRenderer<TWooks
416
410
  render(response: BaseWooksResponse<TWooksErrorBodyExt>): string;
417
411
  }
418
412
 
419
- export declare class WooksHttp implements TWooksSubscribeAdapter {
420
- protected port: number;
421
- protected hostname?: string | (() => void) | undefined;
422
- protected cb?: (() => void) | undefined;
423
- constructor(port: number, hostname?: string | (() => void) | undefined, cb?: (() => void) | undefined);
413
+ export declare class WooksHttp extends WooksAdapterBase {
414
+ protected opts?: TWooksHttpOptions | undefined;
415
+ constructor(opts?: TWooksHttpOptions | undefined, wooks?: Wooks | WooksAdapterBase);
416
+ all<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
417
+ get<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
418
+ post<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
419
+ put<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
420
+ patch<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
421
+ delete<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
422
+ head<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
423
+ options<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
424
424
  protected server?: Server;
425
+ listen(...args: Parameters<Server['listen']>): Promise<unknown>;
426
+ close(server?: Server): Promise<unknown>;
425
427
  protected responder: {
426
428
  createResponse: <T = unknown>(data: T) => BaseWooksResponse<T | TWooksErrorBodyExt> | null;
427
429
  respond: (data: unknown) => Promise<unknown> | undefined;
428
430
  };
429
431
  protected respond(data: unknown): void;
430
- subscribe(lookup: (route: TWooksLookupArgs) => TWooksLookupHandlers | null): void | Promise<void>;
431
- protected processRequest(req: IncomingMessage, res: ServerResponse, lookup: (route: TWooksLookupArgs) => TWooksLookupHandlers | null): Promise<void>;
432
+ getServerCb(): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
432
433
  protected processHandlers(handlers: TWooksHandler<unknown>[]): Promise<void>;
433
- close(): Promise<unknown>;
434
434
  }
435
435
 
436
436
  export declare class WooksURLSearchParams extends URLSearchParams_2 {
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@ import { randomUUID } from 'crypto';
2
2
  import { createEventContext, useEventContext, attachHook } from '@wooksjs/event-core';
3
3
  import { URLSearchParams } from 'url';
4
4
  import { Readable } from 'stream';
5
+ import { WooksAdapterBase } from 'wooks';
5
6
  import http from 'http';
6
7
 
7
8
  function createHttpContext(data) {
@@ -810,7 +811,7 @@ class WooksErrorRenderer extends BaseWooksResponseRenderer {
810
811
  `<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` +
811
812
  `<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` +
812
813
  `<center><h4>${data.message}</h1></center><hr color="#666">` +
813
- `<center style="color: #666;"> Wooks v${"0.1.0"} </center>` +
814
+ `<center style="color: #666;"> Wooks v${"0.2.1"} </center>` +
814
815
  `${keys.length ? `<pre style="${preStyles}">${JSON.stringify(Object.assign(Object.assign({}, data), { statusCode: undefined, message: undefined, error: undefined }), null, ' ')}</pre>` : ''}` +
815
816
  '</body></html>';
816
817
  }
@@ -907,33 +908,55 @@ errorRenderer = new WooksErrorRenderer()) {
907
908
  };
908
909
  }
909
910
 
910
- function createServer(opts, cb, hostname, onListen) {
911
- const server = http.createServer(cb);
912
- if (hostname) {
913
- server.listen(opts.port, hostname, onListen);
911
+ class WooksHttp extends WooksAdapterBase {
912
+ constructor(opts, wooks) {
913
+ super(wooks);
914
+ this.opts = opts;
915
+ this.responder = createWooksResponder();
914
916
  }
915
- else {
916
- server.listen(opts.port, onListen);
917
+ all(path, handler) {
918
+ return this.on('*', path, handler);
917
919
  }
918
- return server;
919
- }
920
-
921
- const httpShortcuts = {
922
- all: '*',
923
- get: 'GET',
924
- post: 'POST',
925
- put: 'PUT',
926
- patch: 'PATCH',
927
- delete: 'DELETE',
928
- head: 'HEAD',
929
- options: 'OPTIONS',
930
- };
931
- class WooksHttp {
932
- constructor(port, hostname, cb) {
933
- this.port = port;
934
- this.hostname = hostname;
935
- this.cb = cb;
936
- this.responder = createWooksResponder();
920
+ get(path, handler) {
921
+ return this.on('GET', path, handler);
922
+ }
923
+ post(path, handler) {
924
+ return this.on('POST', path, handler);
925
+ }
926
+ put(path, handler) {
927
+ return this.on('PUT', path, handler);
928
+ }
929
+ patch(path, handler) {
930
+ return this.on('PATCH', path, handler);
931
+ }
932
+ delete(path, handler) {
933
+ return this.on('DELETE', path, handler);
934
+ }
935
+ head(path, handler) {
936
+ return this.on('HEAD', path, handler);
937
+ }
938
+ options(path, handler) {
939
+ return this.on('OPTIONS', path, handler);
940
+ }
941
+ listen(...args) {
942
+ return __awaiter(this, void 0, void 0, function* () {
943
+ const server = this.server = http.createServer(this.getServerCb());
944
+ return new Promise((resolve, reject) => {
945
+ server.once('listening', resolve);
946
+ server.once('error', reject);
947
+ server.listen(...args);
948
+ });
949
+ });
950
+ }
951
+ close(server) {
952
+ let srv = server || this.server;
953
+ return new Promise((resolve, reject) => {
954
+ srv === null || srv === void 0 ? void 0 : srv.close((err) => {
955
+ if (err)
956
+ return reject(err);
957
+ resolve(srv);
958
+ });
959
+ });
937
960
  }
938
961
  respond(data) {
939
962
  var _a;
@@ -941,36 +964,10 @@ class WooksHttp {
941
964
  traceError('Uncought response exception:\n', e);
942
965
  }));
943
966
  }
944
- subscribe(lookup) {
945
- const port = this.port;
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* () {
967
+ getServerCb() {
968
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
972
969
  const { restoreCtx, clearCtx } = createHttpContext({ req, res });
973
- const handlers = lookup({ method: req.method, url: req.url });
970
+ const handlers = this.wooks.lookup(req.method, req.url);
974
971
  if (handlers) {
975
972
  try {
976
973
  yield this.processHandlers(handlers);
@@ -1017,16 +1014,9 @@ class WooksHttp {
1017
1014
  }
1018
1015
  });
1019
1016
  }
1020
- close() {
1021
- return new Promise((resolve, reject) => {
1022
- var _a;
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
- }
1017
+ }
1018
+ function createHttpApp(opts, wooks) {
1019
+ return new WooksHttp(opts, wooks);
1030
1020
  }
1031
1021
 
1032
- export { BaseWooksResponse, BaseWooksResponseRenderer, EHttpStatusCode, WooksError, WooksErrorRenderer, WooksHttp, WooksURLSearchParams, createHttpContext, createWooksResponder, httpShortcuts, renderCacheControl, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
1022
+ 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.1.0",
3
+ "version": "0.2.1",
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.1.0",
36
- "wooks": "0.1.0"
35
+ "@wooksjs/event-core": "0.2.1",
36
+ "wooks": "0.2.1"
37
37
  }
38
38
  }