@innet/server 1.0.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 (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +788 -0
  3. package/action/Action/Action.d.ts +41 -0
  4. package/action/Action/Action.es6.js +97 -0
  5. package/action/Action/Action.js +137 -0
  6. package/action/Action/index.d.ts +2 -0
  7. package/action/Action/index.es6.js +1 -0
  8. package/action/Action/index.js +11 -0
  9. package/action/index.d.ts +4 -0
  10. package/action/index.es6.js +2 -0
  11. package/action/index.js +13 -0
  12. package/action/withAction/index.d.ts +2 -0
  13. package/action/withAction/index.es6.js +1 -0
  14. package/action/withAction/index.js +7 -0
  15. package/action/withAction/withAction.d.ts +11 -0
  16. package/action/withAction/withAction.es6.js +12 -0
  17. package/action/withAction/withAction.js +18 -0
  18. package/constants.d.ts +1 -0
  19. package/constants.es6.js +3 -0
  20. package/constants.js +7 -0
  21. package/handler/handler.d.ts +38 -0
  22. package/handler/handler.es6.js +45 -0
  23. package/handler/handler.js +56 -0
  24. package/handler/index.d.ts +2 -0
  25. package/handler/index.es6.js +1 -0
  26. package/handler/index.js +12 -0
  27. package/index.d.ts +6 -0
  28. package/index.es6.js +12 -0
  29. package/index.js +41 -0
  30. package/package.json +37 -0
  31. package/plugins/cms/cms.d.ts +9 -0
  32. package/plugins/cms/cms.es6.js +25 -0
  33. package/plugins/cms/cms.js +34 -0
  34. package/plugins/cms/index.d.ts +1 -0
  35. package/plugins/cms/index.es6.js +1 -0
  36. package/plugins/cms/index.js +9 -0
  37. package/plugins/cookie/cookie.d.ts +14 -0
  38. package/plugins/cookie/cookie.es6.js +17 -0
  39. package/plugins/cookie/cookie.js +25 -0
  40. package/plugins/cookie/index.d.ts +1 -0
  41. package/plugins/cookie/index.es6.js +1 -0
  42. package/plugins/cookie/index.js +9 -0
  43. package/plugins/error/error.d.ts +61 -0
  44. package/plugins/error/error.es6.js +68 -0
  45. package/plugins/error/error.js +78 -0
  46. package/plugins/error/index.d.ts +1 -0
  47. package/plugins/error/index.es6.js +1 -0
  48. package/plugins/error/index.js +10 -0
  49. package/plugins/file/file.d.ts +7 -0
  50. package/plugins/file/file.es6.js +24 -0
  51. package/plugins/file/file.js +34 -0
  52. package/plugins/file/index.d.ts +1 -0
  53. package/plugins/file/index.es6.js +1 -0
  54. package/plugins/file/index.js +9 -0
  55. package/plugins/header/header.d.ts +12 -0
  56. package/plugins/header/header.es6.js +10 -0
  57. package/plugins/header/header.js +19 -0
  58. package/plugins/header/index.d.ts +1 -0
  59. package/plugins/header/index.es6.js +1 -0
  60. package/plugins/header/index.js +9 -0
  61. package/plugins/index.d.ts +8 -0
  62. package/plugins/index.es6.js +8 -0
  63. package/plugins/index.js +28 -0
  64. package/plugins/proxy/index.d.ts +1 -0
  65. package/plugins/proxy/index.es6.js +1 -0
  66. package/plugins/proxy/index.js +9 -0
  67. package/plugins/proxy/proxy.d.ts +10 -0
  68. package/plugins/proxy/proxy.es6.js +13 -0
  69. package/plugins/proxy/proxy.js +22 -0
  70. package/plugins/router/index.d.ts +1 -0
  71. package/plugins/router/index.es6.js +1 -0
  72. package/plugins/router/index.js +12 -0
  73. package/plugins/router/router.d.ts +29 -0
  74. package/plugins/router/router.es6.js +54 -0
  75. package/plugins/router/router.js +70 -0
  76. package/plugins/success/index.d.ts +1 -0
  77. package/plugins/success/index.es6.js +1 -0
  78. package/plugins/success/index.js +10 -0
  79. package/plugins/success/success.d.ts +19 -0
  80. package/plugins/success/success.es6.js +30 -0
  81. package/plugins/success/success.js +40 -0
  82. package/server/index.d.ts +2 -0
  83. package/server/index.es6.js +1 -0
  84. package/server/index.js +10 -0
  85. package/server/server.d.ts +19 -0
  86. package/server/server.es6.js +63 -0
  87. package/server/server.js +92 -0
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var router = require('./router.js');
6
+
7
+
8
+
9
+ exports.ROUTER = router.ROUTER;
10
+ exports.getMatchReg = router.getMatchReg;
11
+ exports.router = router.router;
12
+ exports.withRouter = router.withRouter;
@@ -0,0 +1,29 @@
1
+ import { Children, Component, Props } from '@innet/jsx';
2
+ import { Handler } from 'innet';
3
+ import Action from '../../action';
4
+ export declare type Methods = 'GET' | 'HEAD' | 'POST' | 'DELETE' | 'PUT' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
5
+ export interface RouterProps {
6
+ method?: Methods;
7
+ path?: string;
8
+ prefix?: string;
9
+ ish?: boolean;
10
+ onMatch?: (action: Action) => void;
11
+ }
12
+ export declare function getMatchReg(props: RouterProps): string;
13
+ export interface Router {
14
+ prefix?: string;
15
+ params?: Record<string, string>;
16
+ }
17
+ export declare const ROUTER: string;
18
+ export interface RouterComponent extends Component {
19
+ router: Router;
20
+ }
21
+ export interface RouterComponentConstructor {
22
+ new (props?: Props, children?: Children, handler?: Handler): RouterComponent;
23
+ [key: string]: any;
24
+ }
25
+ export declare function withRouter<T extends RouterComponentConstructor>(target: T): T;
26
+ export declare function router({ props, children }: {
27
+ props: any;
28
+ children: any;
29
+ }, handler: any): any;
@@ -0,0 +1,54 @@
1
+ import innet from 'innet';
2
+ import { ACTION } from '../../action/Action/Action.es6.js';
3
+
4
+ function getMatchReg(props) {
5
+ const { ish, path } = props;
6
+ return `^${path ? `${path}${ish ? '(/[^?]*)?' : ''}` : '[^?]*'}(\\?.*)?$`;
7
+ }
8
+ const ROUTER = Symbol('Parent Router');
9
+ function withRouter(target) {
10
+ const originInit = target.prototype.init;
11
+ target.prototype.init = function init(...args) {
12
+ this.router = args[2][ROUTER];
13
+ return originInit.apply(this, args);
14
+ };
15
+ return target;
16
+ }
17
+ function router({ props, children }, handler) {
18
+ var _a;
19
+ if (!children)
20
+ return;
21
+ if (!props)
22
+ return children;
23
+ const action = handler[ACTION];
24
+ const { req } = action;
25
+ if (!req) {
26
+ throw Error('`router` should be used inside `server`');
27
+ }
28
+ if (props.method && props.method !== req.method) {
29
+ return;
30
+ }
31
+ const parent = handler[ROUTER];
32
+ const parentPrefix = (parent === null || parent === void 0 ? void 0 : parent.prefix) || '';
33
+ if (parentPrefix && !req.url.startsWith(parentPrefix)) {
34
+ return;
35
+ }
36
+ const url = parentPrefix ? req.url.slice(parentPrefix.length) : req.url;
37
+ const newHandler = Object.create(handler);
38
+ const current = newHandler[ROUTER] = Object.create(parent || null);
39
+ if (props.prefix) {
40
+ current.prefix = parentPrefix + props.prefix;
41
+ }
42
+ if (props.path || props.search) {
43
+ const urlReg = new RegExp(getMatchReg(props));
44
+ if (urlReg.test(url)) {
45
+ current.params = url.match(urlReg).groups;
46
+ }
47
+ else
48
+ return;
49
+ }
50
+ (_a = props.onMatch) === null || _a === void 0 ? void 0 : _a.call(props, action);
51
+ return innet(children.length > 1 ? children : children[0], newHandler);
52
+ }
53
+
54
+ export { ROUTER, getMatchReg, router, withRouter };
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var Action = require('../../action/Action/Action.js');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
11
+
12
+ function getMatchReg(props) {
13
+ var ish = props.ish, path = props.path;
14
+ return "^".concat(path ? "".concat(path).concat(ish ? '(/[^?]*)?' : '') : '[^?]*', "(\\?.*)?$");
15
+ }
16
+ var ROUTER = Symbol('Parent Router');
17
+ function withRouter(target) {
18
+ var originInit = target.prototype.init;
19
+ target.prototype.init = function init() {
20
+ var args = [];
21
+ for (var _i = 0; _i < arguments.length; _i++) {
22
+ args[_i] = arguments[_i];
23
+ }
24
+ this.router = args[2][ROUTER];
25
+ return originInit.apply(this, args);
26
+ };
27
+ return target;
28
+ }
29
+ function router(_a, handler) {
30
+ var _b;
31
+ var props = _a.props, children = _a.children;
32
+ if (!children)
33
+ return;
34
+ if (!props)
35
+ return children;
36
+ var action = handler[Action.ACTION];
37
+ var req = action.req;
38
+ if (!req) {
39
+ throw Error('`router` should be used inside `server`');
40
+ }
41
+ if (props.method && props.method !== req.method) {
42
+ return;
43
+ }
44
+ var parent = handler[ROUTER];
45
+ var parentPrefix = (parent === null || parent === void 0 ? void 0 : parent.prefix) || '';
46
+ if (parentPrefix && !req.url.startsWith(parentPrefix)) {
47
+ return;
48
+ }
49
+ var url = parentPrefix ? req.url.slice(parentPrefix.length) : req.url;
50
+ var newHandler = Object.create(handler);
51
+ var current = newHandler[ROUTER] = Object.create(parent || null);
52
+ if (props.prefix) {
53
+ current.prefix = parentPrefix + props.prefix;
54
+ }
55
+ if (props.path || props.search) {
56
+ var urlReg = new RegExp(getMatchReg(props));
57
+ if (urlReg.test(url)) {
58
+ current.params = url.match(urlReg).groups;
59
+ }
60
+ else
61
+ return;
62
+ }
63
+ (_b = props.onMatch) === null || _b === void 0 ? void 0 : _b.call(props, action);
64
+ return innet__default["default"](children.length > 1 ? children : children[0], newHandler);
65
+ }
66
+
67
+ exports.ROUTER = ROUTER;
68
+ exports.getMatchReg = getMatchReg;
69
+ exports.router = router;
70
+ exports.withRouter = withRouter;
@@ -0,0 +1 @@
1
+ export * from './success';
@@ -0,0 +1 @@
1
+ export { success, successStatuses } from './success.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var success = require('./success.js');
6
+
7
+
8
+
9
+ exports.success = success.success;
10
+ exports.successStatuses = success.successStatuses;
@@ -0,0 +1,19 @@
1
+ export declare const successStatuses: {
2
+ readonly ok: 200;
3
+ readonly created: 201;
4
+ readonly accepted: 202;
5
+ readonly outside: 203;
6
+ readonly noContent: 204;
7
+ readonly resetContent: 205;
8
+ readonly partialContent: 206;
9
+ readonly multiStatus: 207;
10
+ readonly alreadyReported: 208;
11
+ };
12
+ export declare type SuccessStatuses = keyof typeof successStatuses;
13
+ export interface SuccessProps {
14
+ status?: SuccessStatuses | number;
15
+ }
16
+ export declare function success({ props, children }: {
17
+ props: any;
18
+ children: any;
19
+ }, handler: any): any;
@@ -0,0 +1,30 @@
1
+ import innet from 'innet';
2
+ import { ACTION } from '../../action/Action/Action.es6.js';
3
+
4
+ const successStatuses = {
5
+ ok: 200,
6
+ created: 201,
7
+ accepted: 202,
8
+ outside: 203,
9
+ noContent: 204,
10
+ resetContent: 205,
11
+ partialContent: 206,
12
+ multiStatus: 207,
13
+ alreadyReported: 208,
14
+ };
15
+ function success({ props, children }, handler) {
16
+ const { res } = handler[ACTION];
17
+ const status = props === null || props === void 0 ? void 0 : props.status;
18
+ res.statusCode = status
19
+ ? successStatuses[status] || status
20
+ : children
21
+ ? 200
22
+ : 204;
23
+ const data = innet(children, handler);
24
+ if (typeof data === 'object') {
25
+ return JSON.stringify(data);
26
+ }
27
+ return data !== null && data !== void 0 ? data : null;
28
+ }
29
+
30
+ export { success, successStatuses };
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var Action = require('../../action/Action/Action.js');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
11
+
12
+ var successStatuses = {
13
+ ok: 200,
14
+ created: 201,
15
+ accepted: 202,
16
+ outside: 203,
17
+ noContent: 204,
18
+ resetContent: 205,
19
+ partialContent: 206,
20
+ multiStatus: 207,
21
+ alreadyReported: 208,
22
+ };
23
+ function success(_a, handler) {
24
+ var props = _a.props, children = _a.children;
25
+ var res = handler[Action.ACTION].res;
26
+ var status = props === null || props === void 0 ? void 0 : props.status;
27
+ res.statusCode = status
28
+ ? successStatuses[status] || status
29
+ : children
30
+ ? 200
31
+ : 204;
32
+ var data = innet__default["default"](children, handler);
33
+ if (typeof data === 'object') {
34
+ return JSON.stringify(data);
35
+ }
36
+ return data !== null && data !== void 0 ? data : null;
37
+ }
38
+
39
+ exports.success = success;
40
+ exports.successStatuses = successStatuses;
@@ -0,0 +1,2 @@
1
+ export { default } from './server';
2
+ export * from './server';
@@ -0,0 +1 @@
1
+ export { server as default, server } from './server.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var server = require('./server.js');
6
+
7
+
8
+
9
+ exports["default"] = server.server;
10
+ exports.server = server.server;
@@ -0,0 +1,19 @@
1
+ import { Handler } from 'innet';
2
+ import Action from '../action';
3
+ export interface SSL {
4
+ cert: string;
5
+ key: string;
6
+ }
7
+ export interface ServerProps {
8
+ port?: number;
9
+ ssl?: SSL;
10
+ unknownError?: string;
11
+ onStart?: (url: string) => any;
12
+ onRequest?: (action: Action) => any;
13
+ onError?: (e: Error) => any;
14
+ }
15
+ export declare function server({ props, children }: {
16
+ props?: ServerProps;
17
+ children: any;
18
+ }, handler: Handler): Promise<unknown>;
19
+ export default server;
@@ -0,0 +1,63 @@
1
+ import { __awaiter } from 'tslib';
2
+ import fs from 'fs';
3
+ import http from 'http';
4
+ import http2 from 'https';
5
+ import innet from 'innet';
6
+ import Action, { ACTION } from '../action/Action/Action.es6.js';
7
+ import { CONTINUE } from '../constants.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
+ if (!isInvalidPath(key)) {
14
+ key = fs.readFileSync(key).toString();
15
+ }
16
+ if (!isInvalidPath(cert)) {
17
+ cert = fs.readFileSync(cert).toString();
18
+ }
19
+ const https = (key && cert);
20
+ const { port = env.PORT || (https ? 442 : 80), unknownError = '', onStart, onError, onRequest } = props;
21
+ const server = https ? http2.createServer({ key, cert }) : http.createServer();
22
+ server.on('request', (req, res) => __awaiter(this, void 0, void 0, function* () {
23
+ const childHandler = Object.create(handler);
24
+ childHandler[ACTION] = new Action(req, res);
25
+ if (onRequest) {
26
+ yield onRequest(childHandler[ACTION]);
27
+ }
28
+ if (children) {
29
+ try {
30
+ const result = yield innet(children, childHandler);
31
+ if (result === CONTINUE) {
32
+ return;
33
+ }
34
+ if (typeof result === 'string') {
35
+ res.write(result);
36
+ }
37
+ }
38
+ catch (e) {
39
+ res.statusCode = 520;
40
+ onError === null || onError === void 0 ? void 0 : onError(e);
41
+ res.write(unknownError);
42
+ }
43
+ }
44
+ res.end();
45
+ }));
46
+ let res, rej;
47
+ const promise = new Promise((resolve, reject) => {
48
+ res = resolve;
49
+ rej = reject;
50
+ });
51
+ server.on('error', e => {
52
+ rej(e);
53
+ onError === null || onError === void 0 ? void 0 : onError(e);
54
+ });
55
+ server.listen(port, () => {
56
+ const url = `http${https ? 's' : ''}://localhost:${port}`;
57
+ onStart === null || onStart === void 0 ? void 0 : onStart(url);
58
+ res(url);
59
+ });
60
+ return promise;
61
+ }
62
+
63
+ export { server as default, server };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var fs = require('fs');
7
+ var http = require('http');
8
+ var http2 = require('https');
9
+ var innet = require('innet');
10
+ var Action = require('../action/Action/Action.js');
11
+ var constants = require('../constants.js');
12
+
13
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
+
15
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
16
+ var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
17
+ var http2__default = /*#__PURE__*/_interopDefaultLegacy(http2);
18
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
19
+
20
+ var isInvalidPath = require('is-invalid-path');
21
+ function server(_a, handler) {
22
+ var _this = this;
23
+ var _b = _a.props, props = _b === void 0 ? {} : _b, children = _a.children;
24
+ var env = process.env;
25
+ 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;
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 = (key && cert);
33
+ var _g = props.port, port = _g === void 0 ? env.PORT || (https ? 442 : 80) : _g, _h = props.unknownError, unknownError = _h === void 0 ? '' : _h, 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
+ server.on('request', function (req, res) { return tslib.__awaiter(_this, void 0, void 0, function () {
36
+ var childHandler, result, e_1;
37
+ return tslib.__generator(this, function (_a) {
38
+ switch (_a.label) {
39
+ case 0:
40
+ childHandler = Object.create(handler);
41
+ childHandler[Action.ACTION] = new Action["default"](req, res);
42
+ if (!onRequest) return [3 /*break*/, 2];
43
+ return [4 /*yield*/, onRequest(childHandler[Action.ACTION])];
44
+ case 1:
45
+ _a.sent();
46
+ _a.label = 2;
47
+ case 2:
48
+ if (!children) return [3 /*break*/, 6];
49
+ _a.label = 3;
50
+ case 3:
51
+ _a.trys.push([3, 5, , 6]);
52
+ return [4 /*yield*/, innet__default["default"](children, childHandler)];
53
+ case 4:
54
+ result = _a.sent();
55
+ if (result === constants.CONTINUE) {
56
+ return [2 /*return*/];
57
+ }
58
+ if (typeof result === 'string') {
59
+ res.write(result);
60
+ }
61
+ return [3 /*break*/, 6];
62
+ case 5:
63
+ e_1 = _a.sent();
64
+ res.statusCode = 520;
65
+ onError === null || onError === void 0 ? void 0 : onError(e_1);
66
+ res.write(unknownError);
67
+ return [3 /*break*/, 6];
68
+ case 6:
69
+ res.end();
70
+ return [2 /*return*/];
71
+ }
72
+ });
73
+ }); });
74
+ var res, rej;
75
+ var promise = new Promise(function (resolve, reject) {
76
+ res = resolve;
77
+ rej = reject;
78
+ });
79
+ server.on('error', function (e) {
80
+ rej(e);
81
+ onError === null || onError === void 0 ? void 0 : onError(e);
82
+ });
83
+ server.listen(port, function () {
84
+ var url = "http".concat(https ? 's' : '', "://localhost:").concat(port);
85
+ onStart === null || onStart === void 0 ? void 0 : onStart(url);
86
+ res(url);
87
+ });
88
+ return promise;
89
+ }
90
+
91
+ exports["default"] = server;
92
+ exports.server = server;