koatty 3.8.3 → 3.9.1-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/dist/README.md CHANGED
@@ -1,162 +1,162 @@
1
- # koatty
2
-
3
- Koa2 + Typescript + IOC = koatty.
4
-
5
- Use Typescript's decorator implement IOC and AOP.
6
-
7
- [![Version npm](https://img.shields.io/npm/v/koatty.svg?style=flat-square)](https://www.npmjs.com/package/koatty)[![npm Downloads](https://img.shields.io/npm/dm/koatty.svg?style=flat-square)](https://npmcharts.com/compare/koatty?minimal=true)
8
-
9
- ## New features
10
-
11
- * HTTP、HTTPS、HTTP2、gRPC、WebSocket server.
12
- * Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env)
13
- * `@ExceptionHandler()` Register global exception handling
14
- * graceful shutdown and pre-exit event
15
-
16
-
17
- ## Documentation
18
-
19
- [koatty_doc_CN](https://koatty.org/) (In progress💪)
20
-
21
-
22
- ## Installation CLI tools
23
-
24
- ```shell
25
- npm i -g koatty_cli
26
- ```
27
-
28
- ## Quick Start
29
-
30
- ### 1.Create Project
31
-
32
- ```shell
33
- kt new projectName
34
-
35
- npm start
36
- ```
37
-
38
- ### 2. Install deps
39
-
40
- ```
41
- cd ./projectName
42
-
43
- npm i
44
- ```
45
-
46
- ### 3. Start up
47
-
48
- ```
49
- npm run dev
50
-
51
- // or
52
- npm start
53
- ```
54
-
55
- ## Code style
56
-
57
- default Controller:
58
-
59
- ```javascript
60
- import { Controller, BaseController, Autowired, GetMapping, RequestBody, PathVariable,
61
- PostMapping, RequestMapping, RequestMethod, Valid } from "koatty";
62
- import { TestDTO } from "../model/dto/TestDTO";
63
- import { TestService } from "../service/TestService";
64
- import { App } from "../App";
65
-
66
- @Controller()
67
- export class IndexController extends BaseController {
68
- app: App;
69
-
70
- @Autowired()
71
- private testService: TestService;
72
-
73
- init() {
74
- this.cache = {};
75
- }
76
-
77
- @RequestMapping("/:name", RequestMethod.ALL)
78
- async default(@PathVariable("name") @Valid("IsNotEmpty") name: string) {
79
- try {
80
- const info = await this.testService.sayHello(name);
81
- return this.ok("success", info);
82
- } catch (err: Error) {
83
- return this.fail(err.message));
84
- }
85
- }
86
-
87
- @PostMapping("/test")
88
- @Validated() //need DTOClass
89
- test(@RequestParam() params: TestDTO) {
90
- return this.ok("test", params);
91
- }
92
- }
93
- ```
94
-
95
- ## How to do Unit Testing
96
-
97
- >only support `jest` UT framework now
98
-
99
- ```javascript
100
- import request from 'supertest';
101
- import { ExecBootStrap } from 'koatty';
102
- import { App } from '../src/App';
103
-
104
- describe('UT example', () => {
105
-
106
- let server: any;
107
- beforeAll(async () => {
108
- jest.useFakeTimers();
109
- const appInstance = await ExecBootStrap()(App);
110
- server = await appInstance.listen();
111
- });
112
-
113
- afterAll(done => {
114
- server.close();
115
- done();
116
- });
117
-
118
- it('request', async () => {
119
- const rsp = await request(server).get('/');
120
- expect(rsp.status).toBe(200);
121
- });
122
- });
123
-
124
- ```
125
-
126
- ## How to debug
127
-
128
- if you use vscode , edit the `.vscode/launch.json` , like this:
129
- ```
130
- {
131
- "version": "0.2.0",
132
- "configurations": [
133
- {
134
- "type": "node",
135
- "request": "launch",
136
- "name": "TS Program",
137
- "args": [
138
- "${workspaceRoot}/src/App.ts"
139
- ],
140
- "runtimeArgs": [
141
- "--nolazy",
142
- "-r",
143
- "ts-node/register"
144
- ],
145
- "sourceMaps": true,
146
- "cwd": "${workspaceRoot}",
147
- "protocol": "inspector",
148
- "internalConsoleOptions": "neverOpen"
149
- }
150
- ]
151
- }
152
- ```
153
- Select `TS Program` to debug run. Try to call `http://localhost:3000/` .
154
-
155
- ## Example
156
-
157
- Check out the [quick start example][quick-example].
158
-
159
- [quick-example]: https://github.com/thinkkoa/koatty_demo/
160
-
161
-
162
-
1
+ # koatty
2
+
3
+ Koa2 + Typescript + IOC = koatty.
4
+
5
+ Use Typescript's decorator implement IOC and AOP.
6
+
7
+ [![Version npm](https://img.shields.io/npm/v/koatty.svg?style=flat-square)](https://www.npmjs.com/package/koatty)[![npm Downloads](https://img.shields.io/npm/dm/koatty.svg?style=flat-square)](https://npmcharts.com/compare/koatty?minimal=true)
8
+
9
+ ## New features
10
+
11
+ * HTTP、HTTPS、HTTP2、gRPC、WebSocket server.
12
+ * Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env)
13
+ * `@ExceptionHandler()` Register global exception handling
14
+ * graceful shutdown and pre-exit event
15
+
16
+
17
+ ## Documentation
18
+
19
+ [koatty_doc_CN](https://koatty.org/) (In progress💪)
20
+
21
+
22
+ ## Installation CLI tools
23
+
24
+ ```shell
25
+ npm i -g koatty_cli
26
+ ```
27
+
28
+ ## Quick Start
29
+
30
+ ### 1.Create Project
31
+
32
+ ```shell
33
+ kt new projectName
34
+
35
+ npm start
36
+ ```
37
+
38
+ ### 2. Install deps
39
+
40
+ ```
41
+ cd ./projectName
42
+
43
+ npm i
44
+ ```
45
+
46
+ ### 3. Start up
47
+
48
+ ```
49
+ npm run dev
50
+
51
+ // or
52
+ npm start
53
+ ```
54
+
55
+ ## Code style
56
+
57
+ default Controller:
58
+
59
+ ```javascript
60
+ import { Controller, BaseController, Autowired, GetMapping, RequestBody, PathVariable,
61
+ PostMapping, RequestMapping, RequestMethod, Valid } from "koatty";
62
+ import { TestDTO } from "../model/dto/TestDTO";
63
+ import { TestService } from "../service/TestService";
64
+ import { App } from "../App";
65
+
66
+ @Controller()
67
+ export class IndexController extends BaseController {
68
+ app: App;
69
+
70
+ @Autowired()
71
+ private testService: TestService;
72
+
73
+ init() {
74
+ this.cache = {};
75
+ }
76
+
77
+ @RequestMapping("/:name", RequestMethod.ALL)
78
+ async default(@PathVariable("name") @Valid("IsNotEmpty") name: string) {
79
+ try {
80
+ const info = await this.testService.sayHello(name);
81
+ return this.ok("success", info);
82
+ } catch (err: Error) {
83
+ return this.fail(err.message));
84
+ }
85
+ }
86
+
87
+ @PostMapping("/test")
88
+ @Validated() //need DTOClass
89
+ test(@RequestParam() params: TestDTO) {
90
+ return this.ok("test", params);
91
+ }
92
+ }
93
+ ```
94
+
95
+ ## How to do Unit Testing
96
+
97
+ >only support `jest` UT framework now
98
+
99
+ ```javascript
100
+ import request from 'supertest';
101
+ import { ExecBootStrap } from 'koatty';
102
+ import { App } from '../src/App';
103
+
104
+ describe('UT example', () => {
105
+
106
+ let server: any;
107
+ beforeAll(async () => {
108
+ jest.useFakeTimers();
109
+ const appInstance = await ExecBootStrap()(App);
110
+ server = await appInstance.listen();
111
+ });
112
+
113
+ afterAll(done => {
114
+ server.close();
115
+ done();
116
+ });
117
+
118
+ it('request', async () => {
119
+ const rsp = await request(server).get('/');
120
+ expect(rsp.status).toBe(200);
121
+ });
122
+ });
123
+
124
+ ```
125
+
126
+ ## How to debug
127
+
128
+ if you use vscode , edit the `.vscode/launch.json` , like this:
129
+ ```
130
+ {
131
+ "version": "0.2.0",
132
+ "configurations": [
133
+ {
134
+ "type": "node",
135
+ "request": "launch",
136
+ "name": "TS Program",
137
+ "args": [
138
+ "${workspaceRoot}/src/App.ts"
139
+ ],
140
+ "runtimeArgs": [
141
+ "--nolazy",
142
+ "-r",
143
+ "ts-node/register"
144
+ ],
145
+ "sourceMaps": true,
146
+ "cwd": "${workspaceRoot}",
147
+ "protocol": "inspector",
148
+ "internalConsoleOptions": "neverOpen"
149
+ }
150
+ ]
151
+ }
152
+ ```
153
+ Select `TS Program` to debug run. Try to call `http://localhost:3000/` .
154
+
155
+ ## Example
156
+
157
+ Check out the [quick start example][quick-example].
158
+
159
+ [quick-example]: https://github.com/thinkkoa/koatty_demo/
160
+
161
+
162
+
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-05-23 15:33:40
3
+ * @Date: 2023-07-26 22:34:48
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -100,7 +100,7 @@ export declare class BaseService implements IService {
100
100
  }
101
101
 
102
102
  /**
103
- * bind AppReadyHookFunc
103
+ * bind AppBootHookFunc
104
104
  * example:
105
105
  * export function TestDecorator(): ClassDecorator {
106
106
  * return (target: any) => {
@@ -244,7 +244,6 @@ export { Value }
244
244
  export * from "koatty_container";
245
245
  export * from "koatty_core";
246
246
  export * from "koatty_exception";
247
- export * from "koatty_router";
248
247
  export * from "koatty_serve";
249
248
 
250
249
  export { }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-05-23 15:33:18
3
+ * @Date: 2023-07-26 22:34:34
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -8,19 +8,17 @@
8
8
  'use strict';
9
9
 
10
10
  require('reflect-metadata');
11
- var fs = require('fs');
12
11
  var koatty_core = require('koatty_core');
13
- var koatty_router = require('koatty_router');
14
- var koatty_container = require('koatty_container');
15
- var koatty_serve = require('koatty_serve');
16
12
  var path = require('path');
17
13
  var koatty_loader = require('koatty_loader');
18
14
  var koatty_config = require('koatty_config');
19
15
  var koatty_logger = require('koatty_logger');
20
16
  var koatty_exception = require('koatty_exception');
21
17
  var koatty_lib = require('koatty_lib');
18
+ var koatty_container = require('koatty_container');
22
19
  var koatty_trace = require('koatty_trace');
23
20
  var koatty_payload = require('koatty_payload');
21
+ var koatty_serve = require('koatty_serve');
24
22
 
25
23
  function _interopNamespaceDefault(e) {
26
24
  var n = Object.create(null);
@@ -154,13 +152,13 @@ const COMPONENT_SCAN = 'COMPONENT_SCAN';
154
152
  const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
155
153
  const APP_BOOT_HOOK = "APP_BOOT_HOOK";
156
154
  // tslint:disable: no-irregular-whitespace
157
- const LOGO = `
158
-
159
- ┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
160
- ├┴┐│ │├─┤ │ │ └┬┘
161
- ┴ ┴└─┘┴ ┴ ┴ ┴ ┴
162
- -------------------------------------------
163
- https://github.com/koatty
155
+ const LOGO = `
156
+
157
+ ┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
158
+ ├┴┐│ │├─┤ │ │ └┬┘
159
+ ┴ ┴└─┘┴ ┴ ┴ ┴ ┴
160
+ -------------------------------------------
161
+ https://github.com/koatty
164
162
  `;
165
163
 
166
164
  /**
@@ -616,7 +614,7 @@ class Loader {
616
614
  }
617
615
  }
618
616
 
619
- var version = "3.8.3";
617
+ var version = "3.9.1-0";
620
618
  var engines = {
621
619
  node: ">12.0.0"
622
620
  };
@@ -734,10 +732,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
734
732
  const controllers = Loader.LoadControllers(app);
735
733
  // Create Server
736
734
  // app.server = newServe(app);
737
- koatty_lib.Helper.define(app, "server", newServe(app));
735
+ koatty_lib.Helper.define(app, "server", koatty_serve.NewServe(app));
738
736
  // Create router
739
737
  // app.router = newRouter(app);
740
- koatty_lib.Helper.define(app, "router", newRouter(app));
738
+ koatty_lib.Helper.define(app, "router", koatty_serve.NewRouter(app));
741
739
  // Emit app ready event
742
740
  Logger.Log('Koatty', '', 'Emit App Ready ...');
743
741
  await asyncEvent(app, 'appReady');
@@ -755,19 +753,6 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
755
753
  process.exit();
756
754
  }
757
755
  };
758
- /**
759
- * create router
760
- *
761
- * @export
762
- * @param {Koatty} app
763
- * @returns {*}
764
- */
765
- const newRouter = function (app) {
766
- const protocol = app.config("protocol") || "http";
767
- const options = app.config(undefined, 'router') ?? {};
768
- const router = koatty_router.NewRouter(app, options, protocol);
769
- return router;
770
- };
771
756
  /**
772
757
  * Listening callback function
773
758
  *
@@ -791,43 +776,6 @@ const listenCallback = (app) => {
791
776
  // Set Logger
792
777
  Loader.SetLogger(app);
793
778
  };
794
- /**
795
- * create serve
796
- *
797
- * @param {Koatty} app
798
- * @returns {*}
799
- */
800
- const newServe = function (app) {
801
- const protocol = app.config("protocol") || "http";
802
- const port = process.env.PORT || process.env.APP_PORT ||
803
- app.config('app_port') || 3000;
804
- const hostname = process.env.IP ||
805
- process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
806
- const options = {
807
- hostname, port, protocol,
808
- ext: {
809
- key: "",
810
- cert: "",
811
- protoFile: "",
812
- },
813
- };
814
- const pm = new Set(["https", "http2", "wss"]);
815
- if (pm.has(options.protocol)) {
816
- const keyFile = app.config("key_file") ?? "";
817
- const crtFile = app.config("crt_file") ?? "";
818
- options.ext.key = fs.readFileSync(keyFile).toString();
819
- options.ext.cert = fs.readFileSync(crtFile).toString();
820
- }
821
- if (options.protocol === "https" || options.protocol === "http2") {
822
- options.port = options.port == 80 ? 443 : options.port;
823
- }
824
- if (options.protocol === "grpc") {
825
- const proto = app.config("protoFile", "router");
826
- options.ext.protoFile = proto;
827
- }
828
- const server = koatty_serve.Serve(app, options);
829
- return server;
830
- };
831
779
  /**
832
780
  * Execute event as async
833
781
  *
@@ -907,7 +855,7 @@ function ConfigurationScan(scanPath) {
907
855
  };
908
856
  }
909
857
  /**
910
- * bind AppReadyHookFunc
858
+ * bind AppBootHookFunc
911
859
  * example:
912
860
  * export function TestDecorator(): ClassDecorator {
913
861
  * return (target: any) => {
@@ -956,7 +904,7 @@ function Controller(path = "") {
956
904
  return (target) => {
957
905
  const identifier = koatty_container.IOCContainer.getIdentifier(target);
958
906
  koatty_container.IOCContainer.saveClass("CONTROLLER", target, identifier);
959
- koatty_container.IOCContainer.savePropertyData(koatty_router.CONTROLLER_ROUTER, path, target, identifier);
907
+ koatty_container.IOCContainer.savePropertyData(koatty_serve.CONTROLLER_ROUTER, path, target, identifier);
960
908
  };
961
909
  }
962
910
  /**
@@ -1060,10 +1008,10 @@ Object.keys(koatty_core).forEach(function (k) {
1060
1008
  get: function () { return koatty_core[k]; }
1061
1009
  });
1062
1010
  });
1063
- Object.keys(koatty_router).forEach(function (k) {
1011
+ Object.keys(koatty_exception).forEach(function (k) {
1064
1012
  if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
1065
1013
  enumerable: true,
1066
- get: function () { return koatty_router[k]; }
1014
+ get: function () { return koatty_exception[k]; }
1067
1015
  });
1068
1016
  });
1069
1017
  Object.keys(koatty_container).forEach(function (k) {
@@ -1078,9 +1026,3 @@ Object.keys(koatty_serve).forEach(function (k) {
1078
1026
  get: function () { return koatty_serve[k]; }
1079
1027
  });
1080
1028
  });
1081
- Object.keys(koatty_exception).forEach(function (k) {
1082
- if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
1083
- enumerable: true,
1084
- get: function () { return koatty_exception[k]; }
1085
- });
1086
- });
package/dist/index.mjs CHANGED
@@ -1,20 +1,13 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-05-23 15:33:18
3
+ * @Date: 2023-07-26 22:34:34
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
8
  import 'reflect-metadata';
9
- import fs from 'fs';
10
9
  import { Koatty } from 'koatty_core';
11
10
  export * from 'koatty_core';
12
- import { NewRouter, CONTROLLER_ROUTER } from 'koatty_router';
13
- export * from 'koatty_router';
14
- import { IOCContainer, TAGGED_CLS } from 'koatty_container';
15
- export * from 'koatty_container';
16
- import { BindProcessEvent, Serve } from 'koatty_serve';
17
- export * from 'koatty_serve';
18
11
  import * as path from 'path';
19
12
  import { Load } from 'koatty_loader';
20
13
  import { LoadConfigs } from 'koatty_config';
@@ -24,8 +17,12 @@ import { prevent } from 'koatty_exception';
24
17
  export * from 'koatty_exception';
25
18
  import { Helper } from 'koatty_lib';
26
19
  export { Helper } from 'koatty_lib';
20
+ import { IOCContainer, TAGGED_CLS } from 'koatty_container';
21
+ export * from 'koatty_container';
27
22
  import { Trace } from 'koatty_trace';
28
23
  import { Payload } from 'koatty_payload';
24
+ import { NewServe, NewRouter, BindProcessEvent, CONTROLLER_ROUTER } from 'koatty_serve';
25
+ export * from 'koatty_serve';
29
26
 
30
27
  /**
31
28
  * @ author: richen
@@ -140,13 +137,13 @@ const COMPONENT_SCAN = 'COMPONENT_SCAN';
140
137
  const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
141
138
  const APP_BOOT_HOOK = "APP_BOOT_HOOK";
142
139
  // tslint:disable: no-irregular-whitespace
143
- const LOGO = `
144
-
145
- ┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
146
- ├┴┐│ │├─┤ │ │ └┬┘
147
- ┴ ┴└─┘┴ ┴ ┴ ┴ ┴
148
- -------------------------------------------
149
- https://github.com/koatty
140
+ const LOGO = `
141
+
142
+ ┬┌─┌─┐┌─┐┌┬┐┌┬┐┬ ┬
143
+ ├┴┐│ │├─┤ │ │ └┬┘
144
+ ┴ ┴└─┘┴ ┴ ┴ ┴ ┴
145
+ -------------------------------------------
146
+ https://github.com/koatty
150
147
  `;
151
148
 
152
149
  /**
@@ -602,7 +599,7 @@ class Loader {
602
599
  }
603
600
  }
604
601
 
605
- var version = "3.8.3";
602
+ var version = "3.9.1-0";
606
603
  var engines = {
607
604
  node: ">12.0.0"
608
605
  };
@@ -720,10 +717,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
720
717
  const controllers = Loader.LoadControllers(app);
721
718
  // Create Server
722
719
  // app.server = newServe(app);
723
- Helper.define(app, "server", newServe(app));
720
+ Helper.define(app, "server", NewServe(app));
724
721
  // Create router
725
722
  // app.router = newRouter(app);
726
- Helper.define(app, "router", newRouter(app));
723
+ Helper.define(app, "router", NewRouter(app));
727
724
  // Emit app ready event
728
725
  Logger.Log('Koatty', '', 'Emit App Ready ...');
729
726
  await asyncEvent(app, 'appReady');
@@ -741,19 +738,6 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
741
738
  process.exit();
742
739
  }
743
740
  };
744
- /**
745
- * create router
746
- *
747
- * @export
748
- * @param {Koatty} app
749
- * @returns {*}
750
- */
751
- const newRouter = function (app) {
752
- const protocol = app.config("protocol") || "http";
753
- const options = app.config(undefined, 'router') ?? {};
754
- const router = NewRouter(app, options, protocol);
755
- return router;
756
- };
757
741
  /**
758
742
  * Listening callback function
759
743
  *
@@ -777,43 +761,6 @@ const listenCallback = (app) => {
777
761
  // Set Logger
778
762
  Loader.SetLogger(app);
779
763
  };
780
- /**
781
- * create serve
782
- *
783
- * @param {Koatty} app
784
- * @returns {*}
785
- */
786
- const newServe = function (app) {
787
- const protocol = app.config("protocol") || "http";
788
- const port = process.env.PORT || process.env.APP_PORT ||
789
- app.config('app_port') || 3000;
790
- const hostname = process.env.IP ||
791
- process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
792
- const options = {
793
- hostname, port, protocol,
794
- ext: {
795
- key: "",
796
- cert: "",
797
- protoFile: "",
798
- },
799
- };
800
- const pm = new Set(["https", "http2", "wss"]);
801
- if (pm.has(options.protocol)) {
802
- const keyFile = app.config("key_file") ?? "";
803
- const crtFile = app.config("crt_file") ?? "";
804
- options.ext.key = fs.readFileSync(keyFile).toString();
805
- options.ext.cert = fs.readFileSync(crtFile).toString();
806
- }
807
- if (options.protocol === "https" || options.protocol === "http2") {
808
- options.port = options.port == 80 ? 443 : options.port;
809
- }
810
- if (options.protocol === "grpc") {
811
- const proto = app.config("protoFile", "router");
812
- options.ext.protoFile = proto;
813
- }
814
- const server = Serve(app, options);
815
- return server;
816
- };
817
764
  /**
818
765
  * Execute event as async
819
766
  *
@@ -893,7 +840,7 @@ function ConfigurationScan(scanPath) {
893
840
  };
894
841
  }
895
842
  /**
896
- * bind AppReadyHookFunc
843
+ * bind AppBootHookFunc
897
844
  * example:
898
845
  * export function TestDecorator(): ClassDecorator {
899
846
  * return (target: any) => {