@innet/server 1.2.0 → 1.3.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.
package/README.md CHANGED
@@ -124,6 +124,18 @@ export default (
124
124
  )
125
125
  ```
126
126
 
127
+ ### onDestroy
128
+ You can react on destroy the server with `onDestroy` prop.
129
+
130
+ ```typescript jsx
131
+ export default (
132
+ <server
133
+ onDestroy={() => console.log('destroy')}>
134
+ Hello World!
135
+ </server>
136
+ )
137
+ ```
138
+
127
139
  ## HTML
128
140
  You can use `html` element to return html content.
129
141
 
@@ -455,9 +467,9 @@ export default (
455
467
  )
456
468
  ```
457
469
 
458
- ## Templates
470
+ ## Components
459
471
 
460
- Any template is just a function which returns content that should be run.
472
+ Any component is just a function which returns content that should be run.
461
473
 
462
474
  `server.tsx`
463
475
  ```typescript jsx
@@ -508,39 +520,7 @@ export default (
508
520
 
509
521
  The first argument is props, the second is children and the last one is a handler.
510
522
 
511
- You can use templates inside other templates and components.
512
-
513
- ## Components
514
- Component has the same functionality as template but this is a class.
515
-
516
- ```typescript jsx
517
- class Html {
518
- init ({ title }, children) {
519
- return (
520
- <header name='content-type' value='text/html'>
521
- <html>
522
- <head>
523
- <title>{title}</title>
524
- </head>
525
- <body>
526
- {children}
527
- </body>
528
- </html>
529
- </header>
530
- )
531
- }
532
- }
533
-
534
- export default (
535
- <server>
536
- <Html title='main'>
537
- Hello World!
538
- </Html>
539
- </server>
540
- )
541
- ```
542
-
543
- constructor of the class gets the same arguments as init method and the same as a template.
523
+ You can use components inside another component.
544
524
 
545
525
  ## success
546
526
  If you work on REST API, you can use `success` or `error` as an answer
@@ -700,7 +680,7 @@ export default (
700
680
  Action is an object which contains `request` and `response`.
701
681
  Also, it contains a couple of fields and methods.
702
682
 
703
- Action available in templates and components.
683
+ Action available in components.
704
684
 
705
685
  ```typescript jsx
706
686
  import { getAction } from '@innet/server'
@@ -796,7 +776,7 @@ const Body = async (props, child, handler) => {
796
776
 
797
777
  ## getRouter
798
778
 
799
- You can get router data in a template or component
779
+ You can get router data in a component
800
780
 
801
781
  ```typescript jsx
802
782
  import { getRouter } from '@innet/server'
@@ -0,0 +1 @@
1
+ export * from './serverFn';
@@ -0,0 +1 @@
1
+ export { serverFn } from './serverFn.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var serverFn = require('./serverFn.js');
6
+
7
+
8
+
9
+ exports.serverFn = serverFn.serverFn;
@@ -0,0 +1,2 @@
1
+ import { PluginHandler } from 'innet';
2
+ export declare function serverFn(): PluginHandler;
@@ -0,0 +1,12 @@
1
+ import innet from 'innet';
2
+ import { Watch } from 'watch-state';
3
+
4
+ function serverFn() {
5
+ return (fn, next, handler) => {
6
+ let result;
7
+ new Watch(update => result = innet(fn(update), handler));
8
+ return result;
9
+ };
10
+ }
11
+
12
+ export { serverFn };
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var watchState = require('watch-state');
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 serverFn() {
13
+ return function (fn, next, handler) {
14
+ var result;
15
+ new watchState.Watch(function (update) { return result = innet__default["default"](fn(update), handler); });
16
+ return result;
17
+ };
18
+ }
19
+
20
+ exports.serverFn = serverFn;
@@ -3,6 +3,7 @@ import { switchAsync, SwitchProps } from '@innet/switch';
3
3
  import { arrayAsync, async } from '@innet/utils';
4
4
  import { cms, CmsProps, cookie, CookieProps, error, ErrorProps, file, FileProps, header, HeaderProps, proxy, ProxyProps, router, RouterProps, success, SuccessProps, redirect, RedirectProps } from '../plugins';
5
5
  import { server, ServerProps } from '../server';
6
+ import { serverFn } from '../experimental/serverFn';
6
7
  export declare const arrayPlugins: (typeof arrayAsync)[];
7
8
  export declare const JSXPlugins: {
8
9
  server: typeof server;
@@ -18,6 +19,7 @@ export declare const JSXPlugins: {
18
19
  proxy: typeof proxy;
19
20
  redirect: typeof redirect;
20
21
  };
22
+ export declare const fnPlugins: (typeof serverFn)[];
21
23
  export declare const objectPlugins: ((handler: any) => import("innet").PluginHandler)[];
22
24
  export declare const promisePlugins: (typeof async)[];
23
25
  declare const _default: import("innet").Handler;
@@ -1,7 +1,7 @@
1
1
  import html from '@innet/html';
2
- import { jsxPlugins, jsxComponent, jsxTemplate } from '@innet/jsx';
2
+ import { jsxPlugins, jsxTemplate } from '@innet/jsx';
3
3
  import { switchAsync } from '@innet/switch';
4
- import { promise, array, object, arrayAsync, arrayClear, arraySingleLess, async } from '@innet/utils';
4
+ import { promise, array, object, fn, arrayAsync, arrayClear, arraySingleLess, async } from '@innet/utils';
5
5
  import { createHandler } from 'innet';
6
6
  import { cookie } from '../plugins/cookie/cookie.es6.js';
7
7
  import { header } from '../plugins/header/header.es6.js';
@@ -13,6 +13,7 @@ import { file } from '../plugins/file/file.es6.js';
13
13
  import { proxy } from '../plugins/proxy/proxy.es6.js';
14
14
  import { redirect } from '../plugins/redirect/redirect.es6.js';
15
15
  import { server } from '../server/server.es6.js';
16
+ import { serverFn } from '../experimental/serverFn/serverFn.es6.js';
16
17
 
17
18
  const arrayPlugins = [
18
19
  arrayAsync,
@@ -33,9 +34,11 @@ const JSXPlugins = {
33
34
  proxy,
34
35
  redirect,
35
36
  };
37
+ const fnPlugins = [
38
+ serverFn,
39
+ ];
36
40
  const objectPlugins = [
37
41
  jsxPlugins(JSXPlugins),
38
- jsxComponent,
39
42
  jsxTemplate,
40
43
  ];
41
44
  const promisePlugins = [
@@ -45,6 +48,7 @@ var handler = createHandler([
45
48
  promise(promisePlugins),
46
49
  array(arrayPlugins),
47
50
  object(objectPlugins),
51
+ fn(fnPlugins),
48
52
  ]);
49
53
 
50
- export { JSXPlugins, arrayPlugins, handler as default, objectPlugins, promisePlugins };
54
+ export { JSXPlugins, arrayPlugins, handler as default, fnPlugins, objectPlugins, promisePlugins };
@@ -17,6 +17,7 @@ var file = require('../plugins/file/file.js');
17
17
  var proxy = require('../plugins/proxy/proxy.js');
18
18
  var redirect = require('../plugins/redirect/redirect.js');
19
19
  var server = require('../server/server.js');
20
+ var serverFn = require('../experimental/serverFn/serverFn.js');
20
21
 
21
22
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
22
23
 
@@ -41,9 +42,11 @@ var JSXPlugins = {
41
42
  proxy: proxy.proxy,
42
43
  redirect: redirect.redirect,
43
44
  };
45
+ var fnPlugins = [
46
+ serverFn.serverFn,
47
+ ];
44
48
  var objectPlugins = [
45
49
  jsx.jsxPlugins(JSXPlugins),
46
- jsx.jsxComponent,
47
50
  jsx.jsxTemplate,
48
51
  ];
49
52
  var promisePlugins = [
@@ -53,10 +56,12 @@ var handler = innet.createHandler([
53
56
  utils.promise(promisePlugins),
54
57
  utils.array(arrayPlugins),
55
58
  utils.object(objectPlugins),
59
+ utils.fn(fnPlugins),
56
60
  ]);
57
61
 
58
62
  exports.JSXPlugins = JSXPlugins;
59
63
  exports.arrayPlugins = arrayPlugins;
60
64
  exports["default"] = handler;
65
+ exports.fnPlugins = fnPlugins;
61
66
  exports.objectPlugins = objectPlugins;
62
67
  exports.promisePlugins = promisePlugins;
@@ -1 +1 @@
1
- export { JSXPlugins, arrayPlugins, default, objectPlugins, promisePlugins } from './handler.es6.js';
1
+ export { JSXPlugins, arrayPlugins, default, fnPlugins, objectPlugins, promisePlugins } from './handler.es6.js';
package/handler/index.js CHANGED
@@ -9,5 +9,6 @@ var handler = require('./handler.js');
9
9
  exports.JSXPlugins = handler.JSXPlugins;
10
10
  exports.arrayPlugins = handler.arrayPlugins;
11
11
  exports["default"] = handler["default"];
12
+ exports.fnPlugins = handler.fnPlugins;
12
13
  exports.objectPlugins = handler.objectPlugins;
13
14
  exports.promisePlugins = handler.promisePlugins;
package/index.es6.js CHANGED
@@ -1,4 +1,4 @@
1
- export { JSXPlugins, arrayPlugins, default, objectPlugins, promisePlugins } from './handler/handler.es6.js';
1
+ export { JSXPlugins, arrayPlugins, default, fnPlugins, objectPlugins, promisePlugins } from './handler/handler.es6.js';
2
2
  export { server } from './server/server.es6.js';
3
3
  export { cookie } from './plugins/cookie/cookie.es6.js';
4
4
  export { header } from './plugins/header/header.es6.js';
package/index.js CHANGED
@@ -22,6 +22,7 @@ var getAction = require('./action/getAction/getAction.js');
22
22
  exports.JSXPlugins = handler.JSXPlugins;
23
23
  exports.arrayPlugins = handler.arrayPlugins;
24
24
  exports["default"] = handler["default"];
25
+ exports.fnPlugins = handler.fnPlugins;
25
26
  exports.objectPlugins = handler.objectPlugins;
26
27
  exports.promisePlugins = handler.promisePlugins;
27
28
  exports.server = server.server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -34,6 +34,7 @@
34
34
  "is-invalid-path": "^1.0.2",
35
35
  "mime": "^3.0.0",
36
36
  "multiparty": "^4.2.3",
37
- "tslib": "^2.3.1"
37
+ "tslib": "^2.3.1",
38
+ "watch-state": "^3.4.3"
38
39
  }
39
40
  }
@@ -11,6 +11,7 @@ export interface ServerProps {
11
11
  onStart?: (url: string) => any;
12
12
  onRequest?: (action: Action) => any;
13
13
  onError?: (e: Error) => any;
14
+ onDestroy?: () => any;
14
15
  }
15
16
  export declare function server({ props, children }: {
16
17
  props?: ServerProps;
@@ -3,6 +3,7 @@ import fs from 'fs';
3
3
  import http from 'http';
4
4
  import http2 from 'https';
5
5
  import innet from 'innet';
6
+ import { onDestroy } from 'watch-state';
6
7
  import { ACTION, Action } from '../action/Action/Action.es6.js';
7
8
  import { CONTINUE } from '../constants.es6.js';
8
9
 
@@ -19,6 +20,11 @@ function server({ props = {}, children }, handler) {
19
20
  const https = (key && cert);
20
21
  const { port = env.PORT || (https ? 442 : 80), unknownError = '', onStart, onError, onRequest } = props;
21
22
  const server = https ? http2.createServer({ key, cert }) : http.createServer();
23
+ onDestroy(() => {
24
+ var _a;
25
+ (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
26
+ server.close();
27
+ });
22
28
  server.on('request', (req, res) => __awaiter(this, void 0, void 0, function* () {
23
29
  const childHandler = Object.create(handler);
24
30
  childHandler[ACTION] = new Action(req, res);
package/server/server.js CHANGED
@@ -7,6 +7,7 @@ var fs = require('fs');
7
7
  var http = require('http');
8
8
  var http2 = require('https');
9
9
  var innet = require('innet');
10
+ var watchState = require('watch-state');
10
11
  var Action = require('../action/Action/Action.js');
11
12
  var constants = require('../constants.js');
12
13
 
@@ -32,6 +33,11 @@ function server(_a, handler) {
32
33
  var https = (key && cert);
33
34
  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
35
  var server = https ? http2__default["default"].createServer({ key: key, cert: cert }) : http__default["default"].createServer();
36
+ watchState.onDestroy(function () {
37
+ var _a;
38
+ (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
39
+ server.close();
40
+ });
35
41
  server.on('request', function (req, res) { return tslib.__awaiter(_this, void 0, void 0, function () {
36
42
  var childHandler, result, e_1;
37
43
  return tslib.__generator(this, function (_a) {