ee-core 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.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/bin/tools.js +22 -0
- package/config/config.default.js +179 -0
- package/core/index.js +13 -0
- package/core/lib/ee.js +220 -0
- package/core/lib/loader/context_loader.js +105 -0
- package/core/lib/loader/ee_loader.js +427 -0
- package/core/lib/loader/file_loader.js +262 -0
- package/core/lib/loader/mixin/config.js +138 -0
- package/core/lib/loader/mixin/controller.js +123 -0
- package/core/lib/loader/mixin/service.js +29 -0
- package/core/lib/utils/base_context_class.js +34 -0
- package/core/lib/utils/index.js +100 -0
- package/core/lib/utils/sequencify.js +59 -0
- package/core/lib/utils/timing.js +77 -0
- package/index.js +50 -0
- package/lib/appLoader.js +45 -0
- package/lib/application.js +69 -0
- package/lib/baseApp.js +155 -0
- package/lib/constant.js +30 -0
- package/lib/eeApp.js +306 -0
- package/lib/helper.js +52 -0
- package/lib/httpclient.js +136 -0
- package/lib/logger.js +47 -0
- package/lib/socket/io.js +22 -0
- package/lib/socket/ipcServer.js +112 -0
- package/lib/socket/socketClient.js +51 -0
- package/lib/socket/socketServer.js +70 -0
- package/lib/socket/start.js +18 -0
- package/lib/storage/appStorage.js +14 -0
- package/lib/storage/index.js +22 -0
- package/lib/storage/lowdbStorage.js +144 -0
- package/package.json +39 -0
- package/resource/images/loding.gif +0 -0
- package/resource/images/tray_logo.png +0 -0
- package/resource/loading.html +22 -0
- package/resource/view_example.html +22 -0
- package/tools/codeCompress.js +202 -0
- package/tools/replaceDist.js +71 -0
- package/utils/index.js +141 -0
- package/utils/wrap.js +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Wallace Gao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/bin/tools.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const codeCompress = require('../tools/codeCompress');
|
|
4
|
+
const replaceDist = require('../tools/replaceDist');
|
|
5
|
+
|
|
6
|
+
// argv
|
|
7
|
+
const args = process.argv;
|
|
8
|
+
//console.log('[ee-core] args:', args);
|
|
9
|
+
const cmd = args[2];
|
|
10
|
+
console.log('[ee-core] cmd:', cmd);
|
|
11
|
+
|
|
12
|
+
if (cmd == 'rd') {
|
|
13
|
+
replaceDist.run();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (cmd == 'compress') {
|
|
17
|
+
codeCompress.compress();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (cmd == 'restore') {
|
|
21
|
+
codeCompress.restore();
|
|
22
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The configuration of ee application, can be access by `app.config`
|
|
6
|
+
* @class Config
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
module.exports = appInfo => {
|
|
11
|
+
|
|
12
|
+
const config = {
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The environment of ee
|
|
16
|
+
* @member {String} Config#env
|
|
17
|
+
* @see {appInfo#env}
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
env: appInfo.env,
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The name of the application
|
|
24
|
+
* @member {String} Config#name
|
|
25
|
+
* @see {appInfo#name}
|
|
26
|
+
* @since 1.0.0
|
|
27
|
+
*/
|
|
28
|
+
name: appInfo.name,
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* package.json
|
|
32
|
+
* @member {Object} Config#pkg
|
|
33
|
+
* @see {appInfo#pkg}
|
|
34
|
+
* @since 1.0.0
|
|
35
|
+
*/
|
|
36
|
+
pkg: appInfo.pkg,
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The current directory of the application
|
|
40
|
+
* @member {String} Config#baseDir
|
|
41
|
+
* @see {appInfo#baseDir}
|
|
42
|
+
* @since 1.0.0
|
|
43
|
+
*/
|
|
44
|
+
baseDir: appInfo.baseDir,
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The current HOME directory
|
|
48
|
+
* @member {String} Config#HOME
|
|
49
|
+
* @see {appInfo#HOME}
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
*/
|
|
52
|
+
HOME: appInfo.home,
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The directory of server running. You can find `application_config.json` under it that is dumpped from `app.config`.
|
|
56
|
+
* @member {String} Config#rundir
|
|
57
|
+
* @default
|
|
58
|
+
* @since 1.0.0
|
|
59
|
+
*/
|
|
60
|
+
rundir: path.join(appInfo.baseDir, 'run'),
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* dump config
|
|
64
|
+
*
|
|
65
|
+
* It will ignore special keys when dumpConfig
|
|
66
|
+
*
|
|
67
|
+
* @member Config#dump
|
|
68
|
+
* @property {Set} ignore - keys to ignore
|
|
69
|
+
*/
|
|
70
|
+
dump: {
|
|
71
|
+
ignore: new Set([
|
|
72
|
+
'pass', 'pwd', 'passd', 'passwd', 'password', 'keys', 'masterKey', 'accessKey',
|
|
73
|
+
// ignore any key contains "secret" keyword
|
|
74
|
+
/secret/i,
|
|
75
|
+
]),
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* application home directory
|
|
80
|
+
* @member {String} Config#homeDir
|
|
81
|
+
* @default
|
|
82
|
+
* @since 1.0.0
|
|
83
|
+
*/
|
|
84
|
+
homeDir: appInfo.home,
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* application data & logs directory by env
|
|
88
|
+
* @member {String} Config#root
|
|
89
|
+
* @default
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
*/
|
|
92
|
+
root: appInfo.root,
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* application data directory
|
|
96
|
+
* @member {String} Config#appUserDataDir
|
|
97
|
+
* @default
|
|
98
|
+
* @since 1.0.0
|
|
99
|
+
*/
|
|
100
|
+
appUserDataDir: appInfo.appUserDataDir
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* logger options
|
|
105
|
+
* @member Config#logger
|
|
106
|
+
* @property {String} dir - directory of log files
|
|
107
|
+
* @property {String} encoding - log file encoding, defaults to utf8
|
|
108
|
+
* @property {String} level - default log level, could be: DEBUG, INFO, WARN, ERROR or NONE, defaults to INFO in production
|
|
109
|
+
* @property {String} consoleLevel - log level of stdout, defaults to INFO in local serverEnv, defaults to WARN in unittest, defaults to NONE elsewise
|
|
110
|
+
* @property {Boolean} disableConsoleAfterReady - disable logger console after app ready. defaults to `false` on local and unittest env, others is `true`.
|
|
111
|
+
* @property {Boolean} outputJSON - log as JSON or not, defaults to false
|
|
112
|
+
* @property {Boolean} buffer - if enabled, flush logs to disk at a certain frequency to improve performance, defaults to true
|
|
113
|
+
* @property {String} errorLogName - file name of errorLogger
|
|
114
|
+
* @property {String} coreLogName - file name of coreLogger
|
|
115
|
+
* @property {String} agentLogName - file name of agent worker log
|
|
116
|
+
* @property {Object} coreLogger - custom config of coreLogger
|
|
117
|
+
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to false
|
|
118
|
+
* @property {Boolean} enablePerformanceTimer - using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to false. e.g.: logger will set 1.456ms instead of 1ms.
|
|
119
|
+
*/
|
|
120
|
+
config.logger = {
|
|
121
|
+
dir: path.join(appInfo.root, 'logs'),
|
|
122
|
+
encoding: 'utf8',
|
|
123
|
+
env: appInfo.env,
|
|
124
|
+
level: 'INFO',
|
|
125
|
+
consoleLevel: 'INFO',
|
|
126
|
+
disableConsoleAfterReady: appInfo.env !== 'local' && appInfo.env !== 'unittest',
|
|
127
|
+
outputJSON: false,
|
|
128
|
+
buffer: true,
|
|
129
|
+
appLogName: `ee.log`,
|
|
130
|
+
coreLogName: 'ee-core.log',
|
|
131
|
+
agentLogName: 'ee-agent.log',
|
|
132
|
+
errorLogName: `ee-error.log`,
|
|
133
|
+
coreLogger: {},
|
|
134
|
+
allowDebugAtProd: false,
|
|
135
|
+
enablePerformanceTimer: false,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The option for httpclient
|
|
140
|
+
* @member Config#httpclient
|
|
141
|
+
* @property {Boolean} enableDNSCache - Enable DNS lookup from local cache or not, default is false.
|
|
142
|
+
* @property {Boolean} dnsCacheLookupInterval - minimum interval of DNS query on the same hostname (default 10s).
|
|
143
|
+
*
|
|
144
|
+
* @property {Number} request.timeout - httpclient request default timeout, default is 5000 ms.
|
|
145
|
+
*
|
|
146
|
+
* @property {Boolean} httpAgent.keepAlive - Enable http agent keepalive or not, default is true
|
|
147
|
+
* @property {Number} httpAgent.freeSocketTimeout - http agent socket keepalive max free time, default is 4000 ms.
|
|
148
|
+
* @property {Number} httpAgent.maxSockets - http agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
|
|
149
|
+
* @property {Number} httpAgent.maxFreeSockets - http agent max free socket number of one host, default is 256.
|
|
150
|
+
*
|
|
151
|
+
* @property {Boolean} httpsAgent.keepAlive - Enable https agent keepalive or not, default is true
|
|
152
|
+
* @property {Number} httpsAgent.freeSocketTimeout - httpss agent socket keepalive max free time, default is 4000 ms.
|
|
153
|
+
* @property {Number} httpsAgent.maxSockets - https agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
|
|
154
|
+
* @property {Number} httpsAgent.maxFreeSockets - https agent max free socket number of one host, default is 256.
|
|
155
|
+
*/
|
|
156
|
+
config.httpclient = {
|
|
157
|
+
enableDNSCache: false,
|
|
158
|
+
dnsCacheLookupInterval: 10000,
|
|
159
|
+
dnsCacheMaxLength: 1000,
|
|
160
|
+
|
|
161
|
+
request: {
|
|
162
|
+
timeout: 5000,
|
|
163
|
+
},
|
|
164
|
+
httpAgent: {
|
|
165
|
+
keepAlive: true,
|
|
166
|
+
freeSocketTimeout: 4000,
|
|
167
|
+
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
168
|
+
maxFreeSockets: 256,
|
|
169
|
+
},
|
|
170
|
+
httpsAgent: {
|
|
171
|
+
keepAlive: true,
|
|
172
|
+
freeSocketTimeout: 4000,
|
|
173
|
+
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
174
|
+
maxFreeSockets: 256,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return config;
|
|
179
|
+
};
|
package/core/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const EeCore = require('./lib/ee');
|
|
4
|
+
const EeLoader = require('./lib/loader/ee_loader');
|
|
5
|
+
const BaseContextClass = require('./lib/utils/base_context_class');
|
|
6
|
+
const utils = require('./lib/utils');
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
EeCore,
|
|
10
|
+
EeLoader,
|
|
11
|
+
BaseContextClass,
|
|
12
|
+
utils,
|
|
13
|
+
};
|
package/core/lib/ee.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const KoaApplication = require('koa');
|
|
6
|
+
const is = require('is-type-of');
|
|
7
|
+
const co = require('co');
|
|
8
|
+
const BaseContextClass = require('./utils/base_context_class');
|
|
9
|
+
const utils = require('./utils');
|
|
10
|
+
const Timing = require('./utils/timing');
|
|
11
|
+
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
12
|
+
const debug = require('debug')('ee-core:EeCore');
|
|
13
|
+
const EE_LOADER = Symbol.for('ee#loader');
|
|
14
|
+
|
|
15
|
+
class EeCore extends KoaApplication {
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @class
|
|
19
|
+
* @param {Object} options - options
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
options.type = options.type || 'application';
|
|
24
|
+
|
|
25
|
+
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
|
|
26
|
+
assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
|
|
27
|
+
assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`);
|
|
28
|
+
|
|
29
|
+
super();
|
|
30
|
+
|
|
31
|
+
// todo
|
|
32
|
+
//this.context = null;
|
|
33
|
+
|
|
34
|
+
this.timing = new Timing();
|
|
35
|
+
|
|
36
|
+
this.console = new EggConsoleLogger();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @member {Object} EeCore#options
|
|
40
|
+
* @private
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
this._options = this.options = options;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @member {BaseContextClass} EeCore#BaseContextClass
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
this.BaseContextClass = BaseContextClass;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Base controller to be extended by controller in `app.controller`
|
|
53
|
+
* @class Controller
|
|
54
|
+
* @extends BaseContextClass
|
|
55
|
+
* @example
|
|
56
|
+
* class UserController extends app.Controller {}
|
|
57
|
+
*/
|
|
58
|
+
const Controller = this.BaseContextClass;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Retrieve base controller
|
|
62
|
+
* @member {Controller} EeCore#Controller
|
|
63
|
+
* @since 1.0.0
|
|
64
|
+
*/
|
|
65
|
+
this.Controller = Controller;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Base service to be extended by services in `app.service`
|
|
69
|
+
* @class Service
|
|
70
|
+
* @extends BaseContextClass
|
|
71
|
+
* @example
|
|
72
|
+
* class UserService extends app.Service {}
|
|
73
|
+
*/
|
|
74
|
+
const Service = this.BaseContextClass;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Retrieve base service
|
|
78
|
+
* @member {Service} EeCore#Service
|
|
79
|
+
* @since 1.0.0
|
|
80
|
+
*/
|
|
81
|
+
this.Service = Service;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The loader instance, the default class is {@link EeLoader}.
|
|
85
|
+
* If you want define
|
|
86
|
+
* @member {EeLoader} EeCore#loader
|
|
87
|
+
* @since 1.0.0
|
|
88
|
+
*/
|
|
89
|
+
const Loader = this[EE_LOADER];
|
|
90
|
+
assert(Loader, 'Symbol.for(\'ee#loader\') is required');
|
|
91
|
+
this.loader = new Loader({
|
|
92
|
+
baseDir: options.baseDir,
|
|
93
|
+
homeDir: options.homeDir,
|
|
94
|
+
logger: this.console,
|
|
95
|
+
app: this,
|
|
96
|
+
env: options.env,
|
|
97
|
+
appUserData: options.appUserData
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* override koa's app.use, support generator function
|
|
103
|
+
* @param {Function} fn - middleware
|
|
104
|
+
* @return {Application} app
|
|
105
|
+
* @since 1.0.0
|
|
106
|
+
*/
|
|
107
|
+
use(fn) {
|
|
108
|
+
assert(is.function(fn), 'app.use() requires a function');
|
|
109
|
+
debug('use %s', fn._name || fn.name || '-');
|
|
110
|
+
this.middleware.push(utils.middleware(fn));
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The home directory of application
|
|
116
|
+
* @member {String}
|
|
117
|
+
* @see {@link AppInfo#homeDir}
|
|
118
|
+
* @since 1.0.0
|
|
119
|
+
*/
|
|
120
|
+
get homeDir() {
|
|
121
|
+
return this.options.homeDir;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The electron current directory of application
|
|
126
|
+
* @member {String}
|
|
127
|
+
* @see {@link AppInfo#baseDir}
|
|
128
|
+
* @since 1.0.0
|
|
129
|
+
*/
|
|
130
|
+
get baseDir() {
|
|
131
|
+
return this.options.baseDir;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The ee-core directory of framework
|
|
136
|
+
* @member {String}
|
|
137
|
+
* @see {@link AppInfo#EeCoreDir}
|
|
138
|
+
* @since 1.0.0
|
|
139
|
+
*/
|
|
140
|
+
get eeCoreDir() {
|
|
141
|
+
return this.options.framework;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The name of application
|
|
146
|
+
* @member {String}
|
|
147
|
+
* @see {@link AppInfo#name}
|
|
148
|
+
* @since 1.0.0
|
|
149
|
+
*/
|
|
150
|
+
get name() {
|
|
151
|
+
return this.loader ? this.loader.pkg.name : '';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The configuration of application
|
|
156
|
+
* @member {Config}
|
|
157
|
+
* @since 1.0.0
|
|
158
|
+
*/
|
|
159
|
+
get config() {
|
|
160
|
+
return this.loader ? this.loader.config : {};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get [EE_LOADER]() {
|
|
164
|
+
return require('./loader/ee_loader');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Convert a generator function to a promisable one.
|
|
169
|
+
*
|
|
170
|
+
* Notice: for other kinds of functions, it directly returns you what it is.
|
|
171
|
+
*
|
|
172
|
+
* @param {Function} fn The inputted function.
|
|
173
|
+
* @return {AsyncFunction} An async promise-based function.
|
|
174
|
+
* @example
|
|
175
|
+
```javascript
|
|
176
|
+
const fn = function* (arg) {
|
|
177
|
+
return arg;
|
|
178
|
+
};
|
|
179
|
+
const wrapped = app.toAsyncFunction(fn);
|
|
180
|
+
wrapped(true).then((value) => console.log(value));
|
|
181
|
+
```
|
|
182
|
+
*/
|
|
183
|
+
toAsyncFunction(fn) {
|
|
184
|
+
if (!is.generatorFunction(fn)) return fn;
|
|
185
|
+
fn = co.wrap(fn);
|
|
186
|
+
return async function(...args) {
|
|
187
|
+
return fn.apply(this, args);
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Convert an object with generator functions to a Promisable one.
|
|
193
|
+
* @param {Mixed} obj The inputted object.
|
|
194
|
+
* @return {Promise} A Promisable result.
|
|
195
|
+
* @example
|
|
196
|
+
```javascript
|
|
197
|
+
const fn = function* (arg) {
|
|
198
|
+
return arg;
|
|
199
|
+
};
|
|
200
|
+
const arr = [ fn(1), fn(2) ];
|
|
201
|
+
const promise = app.toPromise(arr);
|
|
202
|
+
promise.then(res => console.log(res));
|
|
203
|
+
```
|
|
204
|
+
*/
|
|
205
|
+
toPromise(obj) {
|
|
206
|
+
return co(function* () {
|
|
207
|
+
return yield obj;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// delegate all router method to application
|
|
213
|
+
// utils.methods.concat([ 'all', 'resources', 'register', 'redirect' ]).forEach(method => {
|
|
214
|
+
// EeCore.prototype[method] = function(...args) {
|
|
215
|
+
// this.router[method](...args);
|
|
216
|
+
// return this;
|
|
217
|
+
// };
|
|
218
|
+
// });
|
|
219
|
+
|
|
220
|
+
module.exports = EeCore;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const FileLoader = require('./file_loader');
|
|
6
|
+
const CLASSLOADER = Symbol('classLoader');
|
|
7
|
+
const EXPORTS = FileLoader.EXPORTS;
|
|
8
|
+
|
|
9
|
+
class ClassLoader {
|
|
10
|
+
|
|
11
|
+
constructor(options) {
|
|
12
|
+
assert(options.ctx, 'options.ctx is required');
|
|
13
|
+
const properties = options.properties;
|
|
14
|
+
this._cache = new Map();
|
|
15
|
+
this._ctx = options.ctx;
|
|
16
|
+
|
|
17
|
+
for (const property in properties) {
|
|
18
|
+
this.defineProperty(property, properties[property]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
defineProperty(property, values) {
|
|
23
|
+
Object.defineProperty(this, property, {
|
|
24
|
+
get() {
|
|
25
|
+
let instance = this._cache.get(property);
|
|
26
|
+
if (!instance) {
|
|
27
|
+
instance = getInstance(values, this._ctx);
|
|
28
|
+
this._cache.set(property, instance);
|
|
29
|
+
}
|
|
30
|
+
return instance;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`.
|
|
38
|
+
* @extends FileLoader
|
|
39
|
+
* @since 1.0.0
|
|
40
|
+
*/
|
|
41
|
+
class ContextLoader extends FileLoader {
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @class
|
|
45
|
+
* @param {Object} options - options same as {@link FileLoader}
|
|
46
|
+
* @param {String} options.fieldClass - determine the field name of inject object.
|
|
47
|
+
*/
|
|
48
|
+
constructor(options) {
|
|
49
|
+
assert(options.property, 'options.property is required');
|
|
50
|
+
assert(options.inject, 'options.inject is required');
|
|
51
|
+
const target = options.target = {};
|
|
52
|
+
if (options.fieldClass) {
|
|
53
|
+
options.inject[options.fieldClass] = target;
|
|
54
|
+
}
|
|
55
|
+
super(options);
|
|
56
|
+
|
|
57
|
+
const app = this.options.inject;
|
|
58
|
+
const property = options.property;
|
|
59
|
+
|
|
60
|
+
// define ctx.service
|
|
61
|
+
Object.defineProperty(app, property, {
|
|
62
|
+
get() {
|
|
63
|
+
// distinguish property cache,
|
|
64
|
+
// cache's lifecycle is the same with this context instance
|
|
65
|
+
// e.x. ctx.service1 and ctx.service2 have different cache
|
|
66
|
+
if (!this[CLASSLOADER]) {
|
|
67
|
+
this[CLASSLOADER] = new Map();
|
|
68
|
+
}
|
|
69
|
+
const classLoader = this[CLASSLOADER];
|
|
70
|
+
|
|
71
|
+
let instance = classLoader.get(property);
|
|
72
|
+
if (!instance) {
|
|
73
|
+
instance = getInstance(target, this);
|
|
74
|
+
classLoader.set(property, instance);
|
|
75
|
+
}
|
|
76
|
+
return instance;
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = ContextLoader;
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
function getInstance(values, ctx) {
|
|
86
|
+
// it's a directory when it has no exports
|
|
87
|
+
// then use ClassLoader
|
|
88
|
+
const Class = values[EXPORTS] ? values : null;
|
|
89
|
+
let instance;
|
|
90
|
+
if (Class) {
|
|
91
|
+
if (is.class(Class)) {
|
|
92
|
+
instance = new Class(ctx);
|
|
93
|
+
} else {
|
|
94
|
+
// it's just an object
|
|
95
|
+
instance = Class;
|
|
96
|
+
}
|
|
97
|
+
// Can't set property to primitive, so check again
|
|
98
|
+
// e.x. module.exports = 1;
|
|
99
|
+
} else if (is.primitive(values)) {
|
|
100
|
+
instance = values;
|
|
101
|
+
} else {
|
|
102
|
+
instance = new ClassLoader({ ctx, properties: values });
|
|
103
|
+
}
|
|
104
|
+
return instance;
|
|
105
|
+
}
|