@innet/server 1.6.6 → 1.7.0

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.
Files changed (84) hide show
  1. package/handler/handler.d.ts +3 -2
  2. package/handler/handler.es6.js +3 -1
  3. package/handler/handler.js +3 -1
  4. package/hooks/index.d.ts +1 -0
  5. package/hooks/index.es6.js +2 -1
  6. package/hooks/index.js +4 -0
  7. package/hooks/useAction/index.es6.js +1 -1
  8. package/hooks/useAction/index.js +1 -0
  9. package/hooks/useAction/useAction.d.ts +3 -1
  10. package/hooks/useAction/useAction.es6.js +8 -4
  11. package/hooks/useAction/useAction.js +7 -2
  12. package/hooks/useCookies/useCookies.d.ts +1 -1
  13. package/hooks/useServer/index.d.ts +1 -0
  14. package/hooks/useServer/index.es6.js +1 -0
  15. package/hooks/useServer/index.js +10 -0
  16. package/hooks/useServer/useServer.d.ts +7 -0
  17. package/hooks/useServer/useServer.es6.js +12 -0
  18. package/hooks/useServer/useServer.js +17 -0
  19. package/index.d.ts +0 -2
  20. package/index.es6.js +5 -3
  21. package/index.js +11 -6
  22. package/package.json +1 -1
  23. package/plugins/access/access.es6.js +4 -3
  24. package/plugins/access/access.js +4 -3
  25. package/plugins/action/action.d.ts +10 -0
  26. package/plugins/action/action.es6.js +38 -0
  27. package/plugins/action/action.js +60 -0
  28. package/plugins/action/index.d.ts +1 -0
  29. package/plugins/action/index.es6.js +1 -0
  30. package/plugins/action/index.js +9 -0
  31. package/plugins/cms/cms.es6.js +5 -5
  32. package/plugins/cms/cms.js +5 -5
  33. package/plugins/cookie/cookie.es6.js +6 -2
  34. package/plugins/cookie/cookie.js +6 -2
  35. package/plugins/error/error.es6.js +7 -2
  36. package/plugins/error/error.js +7 -2
  37. package/plugins/file/file.es6.js +8 -4
  38. package/plugins/file/file.js +8 -4
  39. package/plugins/formatter/formatter.d.ts +1 -1
  40. package/plugins/formatter/formatter.es6.js +4 -3
  41. package/plugins/formatter/formatter.js +4 -3
  42. package/plugins/header/header.es6.js +7 -3
  43. package/plugins/header/header.js +7 -3
  44. package/plugins/index.d.ts +2 -0
  45. package/plugins/index.es6.js +2 -0
  46. package/plugins/index.js +4 -0
  47. package/plugins/parseBody/parseBody.d.ts +1 -1
  48. package/plugins/parseBody/parseBody.es6.js +3 -2
  49. package/plugins/parseBody/parseBody.js +3 -2
  50. package/plugins/proxy/proxy.es6.js +7 -2
  51. package/plugins/proxy/proxy.js +7 -2
  52. package/plugins/redirect/redirect.es6.js +7 -3
  53. package/plugins/redirect/redirect.js +7 -3
  54. package/plugins/router/router.d.ts +1 -1
  55. package/plugins/router/router.es6.js +4 -3
  56. package/plugins/router/router.js +4 -3
  57. package/plugins/server/server.d.ts +22 -0
  58. package/plugins/server/server.es6.js +43 -0
  59. package/plugins/server/server.js +55 -0
  60. package/plugins/success/success.es6.js +7 -3
  61. package/plugins/success/success.js +7 -3
  62. package/plugins/validation/validation.d.ts +1 -1
  63. package/plugins/validation/validation.es6.js +4 -3
  64. package/plugins/validation/validation.js +4 -3
  65. package/{action → utils/action}/Action/Action.d.ts +0 -1
  66. package/{action → utils/action}/Action/Action.es6.js +2 -4
  67. package/{action → utils/action}/Action/Action.js +1 -4
  68. package/utils/action/Action/index.es6.js +1 -0
  69. package/{action → utils/action}/Action/index.js +0 -1
  70. package/utils/action/index.es6.js +1 -0
  71. package/{action → utils/action}/index.js +0 -1
  72. package/utils/index.d.ts +1 -0
  73. package/utils/index.es6.js +1 -0
  74. package/utils/index.js +5 -0
  75. package/action/Action/index.es6.js +0 -1
  76. package/action/index.es6.js +0 -1
  77. package/server/server.d.ts +0 -20
  78. package/server/server.es6.js +0 -69
  79. package/server/server.js +0 -97
  80. /package/{server → plugins/server}/index.d.ts +0 -0
  81. /package/{server → plugins/server}/index.es6.js +0 -0
  82. /package/{server → plugins/server}/index.js +0 -0
  83. /package/{action → utils/action}/Action/index.d.ts +0 -0
  84. /package/{action → utils/action}/index.d.ts +0 -0
@@ -1,10 +1,14 @@
1
1
  import innet from 'innet';
2
2
  import fs from 'fs';
3
3
  import mime from 'mime';
4
- import { ACTION } from '../../action/Action/Action.es6.js';
4
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
5
+ import '../../hooks/useServer/useServer.es6.js';
5
6
 
6
7
  function file({ props, children = null }, handler) {
7
- const { res } = handler[ACTION];
8
+ const action = actionContext.get(handler);
9
+ if (!action) {
10
+ throw Error('Use <file> inside <action>');
11
+ }
8
12
  const { path } = props;
9
13
  if (fs.existsSync(path)) {
10
14
  const stat = fs.statSync(path);
@@ -12,11 +16,11 @@ function file({ props, children = null }, handler) {
12
16
  const readStream = fs.createReadStream(path);
13
17
  const result = innet(children, handler);
14
18
  const run = () => {
15
- res.writeHead(200, {
19
+ action.res.writeHead(200, {
16
20
  'Content-Type': mime.getType(path),
17
21
  'Content-Length': stat.size,
18
22
  });
19
- readStream.pipe(res);
23
+ readStream.pipe(action.res);
20
24
  };
21
25
  if (result instanceof Promise) {
22
26
  result.then(run);
@@ -5,7 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var innet = require('innet');
6
6
  var fs = require('fs');
7
7
  var mime = require('mime');
8
- var Action = require('../../action/Action/Action.js');
8
+ var useAction = require('../../hooks/useAction/useAction.js');
9
+ require('../../hooks/useServer/useServer.js');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
@@ -15,7 +16,10 @@ var mime__default = /*#__PURE__*/_interopDefaultLegacy(mime);
15
16
 
16
17
  function file(_a, handler) {
17
18
  var props = _a.props, _b = _a.children, children = _b === void 0 ? null : _b;
18
- var res = handler[Action.ACTION].res;
19
+ var action = useAction.actionContext.get(handler);
20
+ if (!action) {
21
+ throw Error('Use <file> inside <action>');
22
+ }
19
23
  var path = props.path;
20
24
  if (fs__default["default"].existsSync(path)) {
21
25
  var stat_1 = fs__default["default"].statSync(path);
@@ -23,11 +27,11 @@ function file(_a, handler) {
23
27
  var readStream_1 = fs__default["default"].createReadStream(path);
24
28
  var result_1 = innet__default["default"](children, handler);
25
29
  var run = function () {
26
- res.writeHead(200, {
30
+ action.res.writeHead(200, {
27
31
  'Content-Type': mime__default["default"].getType(path),
28
32
  'Content-Length': stat_1.size,
29
33
  });
30
- readStream_1.pipe(res);
34
+ readStream_1.pipe(action.res);
31
35
  };
32
36
  if (result_1 instanceof Promise) {
33
37
  result_1.then(run);
@@ -1,4 +1,4 @@
1
- import { Resources } from '../../action';
1
+ import { Resources } from '../../utils';
2
2
  export interface Formatter<V> {
3
3
  (value?: any): V;
4
4
  }
@@ -1,10 +1,11 @@
1
1
  import innet from 'innet';
2
- import { ACTION } from '../../action/Action/Action.es6.js';
2
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
3
+ import '../../hooks/useServer/useServer.es6.js';
3
4
 
4
5
  function formatter({ props, children }, handler) {
5
- const action = handler[ACTION];
6
+ const action = actionContext.get(handler);
6
7
  if (!action) {
7
- throw Error('`formatter` should be inside `server`');
8
+ throw Error('Use <formatter> inside <action>');
8
9
  }
9
10
  const { map, resource = 'body' } = props;
10
11
  const run = () => {
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
6
  var innet = require('innet');
7
- var Action = require('../../action/Action/Action.js');
7
+ var useAction = require('../../hooks/useAction/useAction.js');
8
+ require('../../hooks/useServer/useServer.js');
8
9
 
9
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
11
 
@@ -12,9 +13,9 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
12
13
 
13
14
  function formatter(_a, handler) {
14
15
  var props = _a.props, children = _a.children;
15
- var action = handler[Action.ACTION];
16
+ var action = useAction.actionContext.get(handler);
16
17
  if (!action) {
17
- throw Error('`formatter` should be inside `server`');
18
+ throw Error('Use <formatter> inside <action>');
18
19
  }
19
20
  var map = props.map, _b = props.resource, resource = _b === void 0 ? 'body' : _b;
20
21
  var run = function () {
@@ -1,9 +1,13 @@
1
1
  import innet from 'innet';
2
- import { ACTION } from '../../action/Action/Action.es6.js';
2
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
3
+ import '../../hooks/useServer/useServer.es6.js';
3
4
 
4
5
  function header({ props: { name, value }, children }, handler) {
5
- const { res } = handler[ACTION];
6
- res.setHeader(name, value);
6
+ const action = actionContext.get(handler);
7
+ if (!action) {
8
+ throw Error('Use <header> inside <action>');
9
+ }
10
+ action.res.setHeader(name, value);
7
11
  return innet(children, handler);
8
12
  }
9
13
 
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var innet = require('innet');
6
- var Action = require('../../action/Action/Action.js');
6
+ var useAction = require('../../hooks/useAction/useAction.js');
7
+ require('../../hooks/useServer/useServer.js');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
@@ -11,8 +12,11 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
11
12
 
12
13
  function header(_a, handler) {
13
14
  var _b = _a.props, name = _b.name, value = _b.value, children = _a.children;
14
- var res = handler[Action.ACTION].res;
15
- res.setHeader(name, value);
15
+ var action = useAction.actionContext.get(handler);
16
+ if (!action) {
17
+ throw Error('Use <header> inside <action>');
18
+ }
19
+ action.res.setHeader(name, value);
16
20
  return innet__default["default"](children, handler);
17
21
  }
18
22
 
@@ -11,3 +11,5 @@ export * from './validation';
11
11
  export * from './formatter';
12
12
  export * from './access';
13
13
  export * from './parseBody';
14
+ export * from './server';
15
+ export * from './action';
@@ -11,3 +11,5 @@ export { validation, validationContext } from './validation/validation.es6.js';
11
11
  export { formatter } from './formatter/formatter.es6.js';
12
12
  export { access, accessContext } from './access/access.es6.js';
13
13
  export { parseBody } from './parseBody/parseBody.es6.js';
14
+ export { server } from './server/server.es6.js';
15
+ export { action } from './action/action.es6.js';
package/plugins/index.js CHANGED
@@ -15,6 +15,8 @@ var validation = require('./validation/validation.js');
15
15
  var formatter = require('./formatter/formatter.js');
16
16
  var access = require('./access/access.js');
17
17
  var parseBody = require('./parseBody/parseBody.js');
18
+ var server = require('./server/server.js');
19
+ var action = require('./action/action.js');
18
20
 
19
21
 
20
22
 
@@ -39,3 +41,5 @@ exports.formatter = formatter.formatter;
39
41
  exports.access = access.access;
40
42
  exports.accessContext = access.accessContext;
41
43
  exports.parseBody = parseBody.parseBody;
44
+ exports.server = server.server;
45
+ exports.action = action.action;
@@ -4,4 +4,4 @@ export interface ParseBodyJsxElement {
4
4
  props: ParseBodyProps;
5
5
  children?: any;
6
6
  }
7
- export declare function parseBody({ props, children }: ParseBodyJsxElement, handler: any): any;
7
+ export declare function parseBody({ props, children }: ParseBodyJsxElement, handler: any): Promise<any>;
@@ -1,8 +1,9 @@
1
1
  import innet from 'innet';
2
- import { ACTION } from '../../action/Action/Action.es6.js';
2
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
3
+ import '../../hooks/useServer/useServer.es6.js';
3
4
 
4
5
  function parseBody({ props, children }, handler) {
5
- return handler[ACTION].parseBody().then(() => innet(children, handler));
6
+ return actionContext.get(handler).parseBody().then(() => innet(children, handler));
6
7
  }
7
8
 
8
9
  export { parseBody };
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var innet = require('innet');
6
- var Action = require('../../action/Action/Action.js');
6
+ var useAction = require('../../hooks/useAction/useAction.js');
7
+ require('../../hooks/useServer/useServer.js');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
@@ -11,7 +12,7 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
11
12
 
12
13
  function parseBody(_a, handler) {
13
14
  _a.props; var children = _a.children;
14
- return handler[Action.ACTION].parseBody().then(function () { return innet__default["default"](children, handler); });
15
+ return useAction.actionContext.get(handler).parseBody().then(function () { return innet__default["default"](children, handler); });
15
16
  }
16
17
 
17
18
  exports.parseBody = parseBody;
@@ -1,10 +1,15 @@
1
1
  import httpProxy from 'http-proxy';
2
- import { ACTION } from '../../action/Action/Action.es6.js';
3
2
  import { CONTINUE } from '../../constants.es6.js';
3
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
4
+ import '../../hooks/useServer/useServer.es6.js';
4
5
 
5
6
  const proxyServer = httpProxy.createProxyServer({});
6
7
  function proxy({ props: { to, secure = false } }, handler) {
7
- const { req, res } = handler[ACTION];
8
+ const action = actionContext.get(handler);
9
+ if (!action) {
10
+ throw Error('Use <proxy> inside <action>');
11
+ }
12
+ const { req, res } = action;
8
13
  delete req.headers.host;
9
14
  proxyServer.web(req, res, { target: to, secure });
10
15
  return CONTINUE;
@@ -3,8 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var httpProxy = require('http-proxy');
6
- var Action = require('../../action/Action/Action.js');
7
6
  var constants = require('../../constants.js');
7
+ var useAction = require('../../hooks/useAction/useAction.js');
8
+ require('../../hooks/useServer/useServer.js');
8
9
 
9
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
11
 
@@ -13,7 +14,11 @@ var httpProxy__default = /*#__PURE__*/_interopDefaultLegacy(httpProxy);
13
14
  var proxyServer = httpProxy__default["default"].createProxyServer({});
14
15
  function proxy(_a, handler) {
15
16
  var _b = _a.props, to = _b.to, _c = _b.secure, secure = _c === void 0 ? false : _c;
16
- var _d = handler[Action.ACTION], req = _d.req, res = _d.res;
17
+ var action = useAction.actionContext.get(handler);
18
+ if (!action) {
19
+ throw Error('Use <proxy> inside <action>');
20
+ }
21
+ var req = action.req, res = action.res;
17
22
  delete req.headers.host;
18
23
  proxyServer.web(req, res, { target: to, secure: secure });
19
24
  return constants.CONTINUE;
@@ -1,4 +1,5 @@
1
- import { ACTION } from '../../action/Action/Action.es6.js';
1
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
2
+ import '../../hooks/useServer/useServer.es6.js';
2
3
 
3
4
  const redirectStatuses = {
4
5
  multipleChoices: 300,
@@ -19,9 +20,12 @@ function customEncode(url) {
19
20
  return encodeURI(url.replaceAll('%20', ' '));
20
21
  }
21
22
  function redirect({ props, children }, handler) {
22
- const { res } = handler[ACTION];
23
+ const action = actionContext.get(handler);
24
+ if (!action) {
25
+ throw Error('Use <redirect> inside <action>');
26
+ }
23
27
  const { to, status = 301, encode } = props;
24
- res.writeHead(getStatus(status), {
28
+ action.res.writeHead(getStatus(status), {
25
29
  location: encode ? customEncode(to) : to,
26
30
  });
27
31
  return null;
@@ -2,7 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var Action = require('../../action/Action/Action.js');
5
+ var useAction = require('../../hooks/useAction/useAction.js');
6
+ require('../../hooks/useServer/useServer.js');
6
7
 
7
8
  var redirectStatuses = {
8
9
  multipleChoices: 300,
@@ -24,9 +25,12 @@ function customEncode(url) {
24
25
  }
25
26
  function redirect(_a, handler) {
26
27
  var props = _a.props; _a.children;
27
- var res = handler[Action.ACTION].res;
28
+ var action = useAction.actionContext.get(handler);
29
+ if (!action) {
30
+ throw Error('Use <redirect> inside <action>');
31
+ }
28
32
  var to = props.to, _b = props.status, status = _b === void 0 ? 301 : _b, encode = props.encode;
29
- res.writeHead(getStatus(status), {
33
+ action.res.writeHead(getStatus(status), {
30
34
  location: encode ? customEncode(to) : to,
31
35
  });
32
36
  return null;
@@ -1,4 +1,4 @@
1
- import { Action } from '../../action';
1
+ import { type Action } from 'src/utils';
2
2
  export declare type Methods = 'GET' | 'HEAD' | 'POST' | 'DELETE' | 'PUT' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
3
3
  export interface RouterProps {
4
4
  method?: Methods;
@@ -1,6 +1,7 @@
1
1
  import innet from 'innet';
2
2
  import { useHandler } from '@innet/jsx';
3
- import { ACTION } from '../../action/Action/Action.es6.js';
3
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
4
+ import '../../hooks/useServer/useServer.es6.js';
4
5
 
5
6
  function getMatchReg(props) {
6
7
  const { ish, path } = props;
@@ -16,9 +17,9 @@ function router({ props, children }, handler) {
16
17
  return;
17
18
  if (!props)
18
19
  return children;
19
- const action = handler[ACTION];
20
+ const action = actionContext.get(handler);
20
21
  if (!action) {
21
- throw Error('`router` should be used inside `server`');
22
+ throw Error('Use <router> inside <action>');
22
23
  }
23
24
  const { req, path } = action;
24
25
  if (props.method && props.method !== req.method) {
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var innet = require('innet');
6
6
  var jsx = require('@innet/jsx');
7
- var Action = require('../../action/Action/Action.js');
7
+ var useAction = require('../../hooks/useAction/useAction.js');
8
+ require('../../hooks/useServer/useServer.js');
8
9
 
9
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
11
 
@@ -25,9 +26,9 @@ function router(_a, handler) {
25
26
  return;
26
27
  if (!props)
27
28
  return children;
28
- var action = handler[Action.ACTION];
29
+ var action = useAction.actionContext.get(handler);
29
30
  if (!action) {
30
- throw Error('`router` should be used inside `server`');
31
+ throw Error('Use <router> inside <action>');
31
32
  }
32
33
  var req = action.req, path = action.path;
33
34
  if (props.method && props.method !== req.method) {
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Handler } from 'innet';
4
+ import http from 'http';
5
+ import http2 from 'https';
6
+ import { Request, Response } from '../../utils';
7
+ export interface SSL {
8
+ cert: string;
9
+ key: string;
10
+ }
11
+ export interface ServerProps {
12
+ port?: number;
13
+ ssl?: SSL;
14
+ onStart?: (url: string) => any;
15
+ onRequest?: (req: Request, res: Response) => any;
16
+ onError?: (e: Error) => any;
17
+ onDestroy?: () => any;
18
+ }
19
+ export declare function server({ props, children }: {
20
+ props?: ServerProps;
21
+ children: any;
22
+ }, handler: Handler): http2.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
@@ -0,0 +1,43 @@
1
+ import innet from 'innet';
2
+ import fs from 'fs';
3
+ import http from 'http';
4
+ import http2 from 'https';
5
+ import { onDestroy } from 'watch-state';
6
+ import '../../hooks/useAction/useAction.es6.js';
7
+ import { serverContext } from '../../hooks/useServer/useServer.es6.js';
8
+
9
+ const isInvalidPath = require('is-invalid-path');
10
+ function server({ props = {}, children }, handler) {
11
+ const { env } = process;
12
+ let { ssl: { key = env.SSL_KEY, cert = env.SSL_CRT } = {} } = props;
13
+ const childHandler = Object.create(handler);
14
+ if (!isInvalidPath(key)) {
15
+ key = fs.readFileSync(key).toString();
16
+ }
17
+ if (!isInvalidPath(cert)) {
18
+ cert = fs.readFileSync(cert).toString();
19
+ }
20
+ const https = Boolean(key && cert);
21
+ const { port = env.PORT || (https ? 442 : 80), onStart, onError, onRequest } = props;
22
+ const server = https ? http2.createServer({ key, cert }) : http.createServer();
23
+ childHandler[serverContext.key] = server;
24
+ onDestroy(() => {
25
+ var _a;
26
+ (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
27
+ server.close();
28
+ });
29
+ if (onError) {
30
+ server.on('error', onError);
31
+ }
32
+ if (onRequest) {
33
+ server.on('request', onRequest);
34
+ }
35
+ innet(children, childHandler);
36
+ server.listen(port, () => {
37
+ const url = `http${https ? 's' : ''}://localhost:${port}`;
38
+ onStart === null || onStart === void 0 ? void 0 : onStart(url);
39
+ });
40
+ return server;
41
+ }
42
+
43
+ export { server };
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var fs = require('fs');
7
+ var http = require('http');
8
+ var http2 = require('https');
9
+ var watchState = require('watch-state');
10
+ require('../../hooks/useAction/useAction.js');
11
+ var useServer = require('../../hooks/useServer/useServer.js');
12
+
13
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
+
15
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
16
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
17
+ var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
18
+ var http2__default = /*#__PURE__*/_interopDefaultLegacy(http2);
19
+
20
+ var isInvalidPath = require('is-invalid-path');
21
+ function server(_a, handler) {
22
+ var _b = _a.props, props = _b === void 0 ? {} : _b, children = _a.children;
23
+ var env = process.env;
24
+ var _c = props.ssl, _d = _c === void 0 ? {} : _c, _e = _d.key, key = _e === void 0 ? env.SSL_KEY : _e, _f = _d.cert, cert = _f === void 0 ? env.SSL_CRT : _f;
25
+ var childHandler = Object.create(handler);
26
+ if (!isInvalidPath(key)) {
27
+ key = fs__default["default"].readFileSync(key).toString();
28
+ }
29
+ if (!isInvalidPath(cert)) {
30
+ cert = fs__default["default"].readFileSync(cert).toString();
31
+ }
32
+ var https = Boolean(key && cert);
33
+ var _g = props.port, port = _g === void 0 ? env.PORT || (https ? 442 : 80) : _g, onStart = props.onStart, onError = props.onError, onRequest = props.onRequest;
34
+ var server = https ? http2__default["default"].createServer({ key: key, cert: cert }) : http__default["default"].createServer();
35
+ childHandler[useServer.serverContext.key] = server;
36
+ watchState.onDestroy(function () {
37
+ var _a;
38
+ (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
39
+ server.close();
40
+ });
41
+ if (onError) {
42
+ server.on('error', onError);
43
+ }
44
+ if (onRequest) {
45
+ server.on('request', onRequest);
46
+ }
47
+ innet__default["default"](children, childHandler);
48
+ server.listen(port, function () {
49
+ var url = "http".concat(https ? 's' : '', "://localhost:").concat(port);
50
+ onStart === null || onStart === void 0 ? void 0 : onStart(url);
51
+ });
52
+ return server;
53
+ }
54
+
55
+ exports.server = server;
@@ -1,5 +1,6 @@
1
1
  import innet from 'innet';
2
- import { ACTION } from '../../action/Action/Action.es6.js';
2
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
3
+ import '../../hooks/useServer/useServer.es6.js';
3
4
 
4
5
  const successStatuses = {
5
6
  ok: 200,
@@ -13,9 +14,12 @@ const successStatuses = {
13
14
  alreadyReported: 208,
14
15
  };
15
16
  function success({ props, children }, handler) {
16
- const { res } = handler[ACTION];
17
+ const action = actionContext.get(handler);
18
+ if (!action) {
19
+ throw Error('Use <success> inside <action>');
20
+ }
17
21
  const status = props === null || props === void 0 ? void 0 : props.status;
18
- res.statusCode = status
22
+ action.res.statusCode = status
19
23
  ? successStatuses[status] || status
20
24
  : children
21
25
  ? 200
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var innet = require('innet');
6
- var Action = require('../../action/Action/Action.js');
6
+ var useAction = require('../../hooks/useAction/useAction.js');
7
+ require('../../hooks/useServer/useServer.js');
7
8
 
8
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10
 
@@ -22,9 +23,12 @@ var successStatuses = {
22
23
  };
23
24
  function success(_a, handler) {
24
25
  var props = _a.props, children = _a.children;
25
- var res = handler[Action.ACTION].res;
26
+ var action = useAction.actionContext.get(handler);
27
+ if (!action) {
28
+ throw Error('Use <success> inside <action>');
29
+ }
26
30
  var status = props === null || props === void 0 ? void 0 : props.status;
27
- res.statusCode = status
31
+ action.res.statusCode = status
28
32
  ? successStatuses[status] || status
29
33
  : children
30
34
  ? 200
@@ -1,6 +1,6 @@
1
1
  import { ValidationMap, ValidationResponse } from '@cantinc/utils';
2
2
  import { Context } from '@innet/jsx';
3
- import { Resources } from '../../action';
3
+ import { Resources } from '../../utils';
4
4
  export interface ValidationProps<T> {
5
5
  map: ValidationMap<T>;
6
6
  resource?: Resources;
@@ -1,13 +1,14 @@
1
1
  import innet from 'innet';
2
2
  import { validation as validation$1 } from '@cantinc/utils';
3
3
  import { Context } from '@innet/jsx';
4
- import { ACTION } from '../../action/Action/Action.es6.js';
4
+ import { actionContext } from '../../hooks/useAction/useAction.es6.js';
5
+ import '../../hooks/useServer/useServer.es6.js';
5
6
 
6
7
  const validationContext = new Context({});
7
8
  function validation({ props, children }, handler) {
8
- const action = handler[ACTION];
9
+ const action = actionContext.get(handler);
9
10
  if (!action) {
10
- throw Error('`validation` should be inside `server`');
11
+ throw Error('Use <validation> inside <action>');
11
12
  }
12
13
  const { map, resource = 'body' } = props;
13
14
  const run = () => {
@@ -5,7 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var innet = require('innet');
6
6
  var utils = require('@cantinc/utils');
7
7
  var jsx = require('@innet/jsx');
8
- var Action = require('../../action/Action/Action.js');
8
+ var useAction = require('../../hooks/useAction/useAction.js');
9
+ require('../../hooks/useServer/useServer.js');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
@@ -14,9 +15,9 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
14
15
  var validationContext = new jsx.Context({});
15
16
  function validation(_a, handler) {
16
17
  var props = _a.props, children = _a.children;
17
- var action = handler[Action.ACTION];
18
+ var action = useAction.actionContext.get(handler);
18
19
  if (!action) {
19
- throw Error('`validation` should be inside `server`');
20
+ throw Error('Use <validation> inside <action>');
20
21
  }
21
22
  var map = props.map, _b = props.resource, resource = _b === void 0 ? 'body' : _b;
22
23
  var run = function () {
@@ -3,7 +3,6 @@ import { CookieSerializeOptions } from 'cookie';
3
3
  import { IncomingMessage, ServerResponse } from 'http';
4
4
  import { FormOptions } from 'multiparty';
5
5
  import { ParsedQs } from 'qs';
6
- export declare const ACTION: string;
7
6
  export declare type Body = Record<string, any>;
8
7
  export declare type Search = ParsedQs;
9
8
  export declare type Cookies = Record<string, string | string[]>;
@@ -2,10 +2,8 @@ import { __decorate } from 'tslib';
2
2
  import { once } from '@cantinc/utils';
3
3
  import cookie from 'cookie';
4
4
  import multiparty from 'multiparty';
5
- import { parseSearch } from '../../utils/parseSearch/parseSearch.es6.js';
6
- import 'qs';
5
+ import { parseSearch } from '../../parseSearch/parseSearch.es6.js';
7
6
 
8
- const ACTION = Symbol('Action');
9
7
  const URL_PARSER = /^(?<path>[^?]+)(\?(?<search>.*))?/;
10
8
  const KEY_FIELD = /^(?<field>[^[]+)(?<rest>.*)$/;
11
9
  const KEY_KEYS = /^\[(?<key>[^\]]+)\](?<rest>.*)$/;
@@ -133,4 +131,4 @@ __decorate([
133
131
  once
134
132
  ], Action.prototype, "parsedUrl", null);
135
133
 
136
- export { ACTION, Action, KEY_FIELD, KEY_KEYS, URL_PARSER };
134
+ export { Action, KEY_FIELD, KEY_KEYS, URL_PARSER };