ee-core 2.9.2-beta.1 → 2.10.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 -21
- package/README.md +65 -65
- package/addon/index.js +34 -34
- package/addon/window/index.js +98 -98
- package/bin/tools.js +8 -8
- package/config/cache.js +41 -38
- package/config/config.default.js +331 -330
- package/config/index.js +75 -73
- package/const/channel.js +17 -17
- package/const/index.js +8 -8
- package/controller/baseContextClass.js +34 -34
- package/controller/index.js +34 -34
- package/core/index.js +10 -10
- package/core/lib/ee.js +216 -216
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +435 -435
- package/core/lib/loader/file_loader.js +326 -326
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +130 -130
- package/core/lib/loader/mixin/controller.js +125 -125
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/function.js +30 -30
- package/core/lib/utils/index.js +133 -133
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/cross/index.js +183 -183
- package/cross/spawnProcess.js +183 -183
- package/ee/appLoader.js +48 -48
- package/ee/application.js +99 -99
- package/ee/baseApp.js +103 -102
- package/ee/eeApp.js +408 -408
- package/ee/index.js +57 -57
- package/electron/app/index.js +64 -58
- package/electron/index.js +19 -19
- package/electron/window/index.js +73 -73
- package/electron/window/winState.js +186 -186
- package/exception/index.js +112 -112
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.js +13 -13
- package/httpclient/index.js +161 -161
- package/index.js +54 -54
- package/jobs/baseJobClass.js +16 -16
- package/jobs/child/app.js +65 -65
- package/jobs/child/forkProcess.js +145 -145
- package/jobs/child/index.js +82 -82
- package/jobs/child-pool/index.js +213 -213
- package/jobs/index.js +8 -8
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.js +31 -31
- package/jobs/renderer/index.js +141 -141
- package/jobs/renderer/loadView.js +40 -40
- package/jobs/unification.js +63 -63
- package/loader/index.js +172 -172
- package/log/index.js +68 -68
- package/log/logger.js +86 -80
- package/main/index.js +56 -56
- package/message/childMessage.js +54 -54
- package/message/index.js +18 -18
- package/old-utils/index.js +91 -91
- package/package.json +38 -38
- package/ps/index.js +371 -371
- package/services/baseContextClass.js +34 -34
- package/services/index.js +40 -40
- package/socket/httpServer.js +147 -147
- package/socket/index.js +81 -81
- package/socket/io.js +27 -27
- package/socket/ipcServer.js +112 -112
- package/socket/socketServer.js +69 -65
- package/storage/index.js +38 -38
- package/storage/jsondb/adapters/Base.js +23 -23
- package/storage/jsondb/adapters/FileSync.js +64 -52
- package/storage/jsondb/main.js +55 -42
- package/storage/jsondbStorage.js +195 -195
- package/storage/sqliteStorage.js +123 -123
- package/utils/co.js +237 -237
- package/utils/copyto.js +160 -160
- package/utils/depd/index.js +538 -538
- package/utils/depd/lib/browser/index.js +77 -77
- package/utils/extend.js +73 -73
- package/utils/get-port/index.d.ts +64 -64
- package/utils/get-port/index.js +148 -148
- package/utils/helper.js +220 -220
- package/utils/index.js +160 -160
- package/utils/ip.js +261 -261
- package/utils/is.js +145 -145
- package/utils/json.js +72 -72
- package/utils/pargv.js +263 -263
- package/utils/time/index.js +19 -19
- package/utils/time/ms.js +162 -162
- package/utils/wrap.js +35 -35
package/core/lib/ee.js
CHANGED
|
@@ -1,216 +1,216 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const KoaApplication = require('koa');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const co = require('../../utils/co');
|
|
6
|
-
const BaseContextClass = require('./utils/base_context_class');
|
|
7
|
-
const utils = require('./utils');
|
|
8
|
-
const Timing = require('./utils/timing');
|
|
9
|
-
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
10
|
-
const debug = require('debug')('ee-core:EeCore');
|
|
11
|
-
const EE_LOADER = Symbol.for('ee#loader');
|
|
12
|
-
|
|
13
|
-
class EeCore extends KoaApplication {
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @class
|
|
17
|
-
* @param {Object} options - options
|
|
18
|
-
* @since 1.0.0
|
|
19
|
-
*/
|
|
20
|
-
constructor(options = {}) {
|
|
21
|
-
options.type = options.type || 'application';
|
|
22
|
-
|
|
23
|
-
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
|
|
24
|
-
// assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
|
|
25
|
-
// assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`);
|
|
26
|
-
|
|
27
|
-
super();
|
|
28
|
-
|
|
29
|
-
// todo
|
|
30
|
-
//this.context = null;
|
|
31
|
-
|
|
32
|
-
this.timing = new Timing();
|
|
33
|
-
|
|
34
|
-
this.console = new EggConsoleLogger({level: 'INFO'});
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @member {Object} EeCore#options
|
|
38
|
-
* @private
|
|
39
|
-
* @since 1.0.0
|
|
40
|
-
*/
|
|
41
|
-
this._options = this.options = options;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @member {BaseContextClass} EeCore#BaseContextClass
|
|
45
|
-
* @since 1.0.0
|
|
46
|
-
*/
|
|
47
|
-
this.BaseContextClass = BaseContextClass;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Base controller to be extended by controller in `app.controller`
|
|
51
|
-
* @class Controller
|
|
52
|
-
* @extends BaseContextClass
|
|
53
|
-
* @example
|
|
54
|
-
* class UserController extends app.Controller {}
|
|
55
|
-
*/
|
|
56
|
-
const Controller = this.BaseContextClass;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Retrieve base controller
|
|
60
|
-
* @member {Controller} EeCore#Controller
|
|
61
|
-
* @since 1.0.0
|
|
62
|
-
*/
|
|
63
|
-
this.Controller = Controller;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Base service to be extended by services in `app.service`
|
|
67
|
-
* @class Service
|
|
68
|
-
* @extends BaseContextClass
|
|
69
|
-
* @example
|
|
70
|
-
* class UserService extends app.Service {}
|
|
71
|
-
*/
|
|
72
|
-
const Service = this.BaseContextClass;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Retrieve base service
|
|
76
|
-
* @member {Service} EeCore#Service
|
|
77
|
-
* @since 1.0.0
|
|
78
|
-
*/
|
|
79
|
-
this.Service = Service;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* The loader instance, the default class is {@link EeLoader}.
|
|
83
|
-
* If you want define
|
|
84
|
-
* @member {EeLoader} EeCore#loader
|
|
85
|
-
* @since 1.0.0
|
|
86
|
-
*/
|
|
87
|
-
const Loader = this[EE_LOADER];
|
|
88
|
-
assert(Loader, 'Symbol.for(\'ee#loader\') is required');
|
|
89
|
-
let loaderOptions = Object.assign({
|
|
90
|
-
logger: this.console,
|
|
91
|
-
app:this
|
|
92
|
-
}, options);
|
|
93
|
-
this.loader = new Loader(loaderOptions);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* override koa's app.use, support generator function
|
|
98
|
-
* @param {Function} fn - middleware
|
|
99
|
-
* @return {Application} app
|
|
100
|
-
* @since 1.0.0
|
|
101
|
-
*/
|
|
102
|
-
use(fn) {
|
|
103
|
-
assert(is.function(fn), 'app.use() requires a function');
|
|
104
|
-
debug('use %s', fn._name || fn.name || '-');
|
|
105
|
-
this.middleware.push(utils.middleware(fn));
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The home directory of application
|
|
111
|
-
* @member {String}
|
|
112
|
-
* @see {@link AppInfo#homeDir}
|
|
113
|
-
* @since 1.0.0
|
|
114
|
-
*/
|
|
115
|
-
get homeDir() {
|
|
116
|
-
return this.options.homeDir;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* The electron current directory of application
|
|
121
|
-
* @member {String}
|
|
122
|
-
* @see {@link AppInfo#baseDir}
|
|
123
|
-
* @since 1.0.0
|
|
124
|
-
*/
|
|
125
|
-
get baseDir() {
|
|
126
|
-
return this.options.baseDir;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The ee-core directory of framework
|
|
131
|
-
* @member {String}
|
|
132
|
-
* @see {@link AppInfo#EeCoreDir}
|
|
133
|
-
* @since 1.0.0
|
|
134
|
-
*/
|
|
135
|
-
get eeCoreDir() {
|
|
136
|
-
return this.options.framework;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The name of application
|
|
141
|
-
* @member {String}
|
|
142
|
-
* @see {@link AppInfo#name}
|
|
143
|
-
* @since 1.0.0
|
|
144
|
-
*/
|
|
145
|
-
get name() {
|
|
146
|
-
return this.loader ? this.loader.pkg.name : '';
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The configuration of application
|
|
151
|
-
* @member {Config}
|
|
152
|
-
* @since 1.0.0
|
|
153
|
-
*/
|
|
154
|
-
get config() {
|
|
155
|
-
return this.loader ? this.loader.config : {};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* The addon of application
|
|
160
|
-
* @member {Addon}
|
|
161
|
-
* @since 1.0.0
|
|
162
|
-
*/
|
|
163
|
-
get addon() {
|
|
164
|
-
return this.loader ? this.loader.addon : {};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
get [EE_LOADER]() {
|
|
168
|
-
return require('./loader/ee_loader');
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Convert a generator function to a promisable one.
|
|
173
|
-
*
|
|
174
|
-
* Notice: for other kinds of functions, it directly returns you what it is.
|
|
175
|
-
*
|
|
176
|
-
* @param {Function} fn The inputted function.
|
|
177
|
-
* @return {AsyncFunction} An async promise-based function.
|
|
178
|
-
* @example
|
|
179
|
-
```javascript
|
|
180
|
-
const fn = function* (arg) {
|
|
181
|
-
return arg;
|
|
182
|
-
};
|
|
183
|
-
const wrapped = app.toAsyncFunction(fn);
|
|
184
|
-
wrapped(true).then((value) => console.log(value));
|
|
185
|
-
```
|
|
186
|
-
*/
|
|
187
|
-
toAsyncFunction(fn) {
|
|
188
|
-
if (!is.generatorFunction(fn)) return fn;
|
|
189
|
-
fn = co.wrap(fn);
|
|
190
|
-
return async function(...args) {
|
|
191
|
-
return fn.apply(this, args);
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Convert an object with generator functions to a Promisable one.
|
|
197
|
-
* @param {Mixed} obj The inputted object.
|
|
198
|
-
* @return {Promise} A Promisable result.
|
|
199
|
-
* @example
|
|
200
|
-
```javascript
|
|
201
|
-
const fn = function* (arg) {
|
|
202
|
-
return arg;
|
|
203
|
-
};
|
|
204
|
-
const arr = [ fn(1), fn(2) ];
|
|
205
|
-
const promise = app.toPromise(arr);
|
|
206
|
-
promise.then(res => console.log(res));
|
|
207
|
-
```
|
|
208
|
-
*/
|
|
209
|
-
toPromise(obj) {
|
|
210
|
-
return co(function* () {
|
|
211
|
-
return yield obj;
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
module.exports = EeCore;
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const KoaApplication = require('koa');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const co = require('../../utils/co');
|
|
6
|
+
const BaseContextClass = require('./utils/base_context_class');
|
|
7
|
+
const utils = require('./utils');
|
|
8
|
+
const Timing = require('./utils/timing');
|
|
9
|
+
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
10
|
+
const debug = require('debug')('ee-core:EeCore');
|
|
11
|
+
const EE_LOADER = Symbol.for('ee#loader');
|
|
12
|
+
|
|
13
|
+
class EeCore extends KoaApplication {
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @class
|
|
17
|
+
* @param {Object} options - options
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
options.type = options.type || 'application';
|
|
22
|
+
|
|
23
|
+
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
|
|
24
|
+
// assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
|
|
25
|
+
// assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`);
|
|
26
|
+
|
|
27
|
+
super();
|
|
28
|
+
|
|
29
|
+
// todo
|
|
30
|
+
//this.context = null;
|
|
31
|
+
|
|
32
|
+
this.timing = new Timing();
|
|
33
|
+
|
|
34
|
+
this.console = new EggConsoleLogger({level: 'INFO'});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @member {Object} EeCore#options
|
|
38
|
+
* @private
|
|
39
|
+
* @since 1.0.0
|
|
40
|
+
*/
|
|
41
|
+
this._options = this.options = options;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @member {BaseContextClass} EeCore#BaseContextClass
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
this.BaseContextClass = BaseContextClass;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Base controller to be extended by controller in `app.controller`
|
|
51
|
+
* @class Controller
|
|
52
|
+
* @extends BaseContextClass
|
|
53
|
+
* @example
|
|
54
|
+
* class UserController extends app.Controller {}
|
|
55
|
+
*/
|
|
56
|
+
const Controller = this.BaseContextClass;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Retrieve base controller
|
|
60
|
+
* @member {Controller} EeCore#Controller
|
|
61
|
+
* @since 1.0.0
|
|
62
|
+
*/
|
|
63
|
+
this.Controller = Controller;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Base service to be extended by services in `app.service`
|
|
67
|
+
* @class Service
|
|
68
|
+
* @extends BaseContextClass
|
|
69
|
+
* @example
|
|
70
|
+
* class UserService extends app.Service {}
|
|
71
|
+
*/
|
|
72
|
+
const Service = this.BaseContextClass;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Retrieve base service
|
|
76
|
+
* @member {Service} EeCore#Service
|
|
77
|
+
* @since 1.0.0
|
|
78
|
+
*/
|
|
79
|
+
this.Service = Service;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The loader instance, the default class is {@link EeLoader}.
|
|
83
|
+
* If you want define
|
|
84
|
+
* @member {EeLoader} EeCore#loader
|
|
85
|
+
* @since 1.0.0
|
|
86
|
+
*/
|
|
87
|
+
const Loader = this[EE_LOADER];
|
|
88
|
+
assert(Loader, 'Symbol.for(\'ee#loader\') is required');
|
|
89
|
+
let loaderOptions = Object.assign({
|
|
90
|
+
logger: this.console,
|
|
91
|
+
app:this
|
|
92
|
+
}, options);
|
|
93
|
+
this.loader = new Loader(loaderOptions);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* override koa's app.use, support generator function
|
|
98
|
+
* @param {Function} fn - middleware
|
|
99
|
+
* @return {Application} app
|
|
100
|
+
* @since 1.0.0
|
|
101
|
+
*/
|
|
102
|
+
use(fn) {
|
|
103
|
+
assert(is.function(fn), 'app.use() requires a function');
|
|
104
|
+
debug('use %s', fn._name || fn.name || '-');
|
|
105
|
+
this.middleware.push(utils.middleware(fn));
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The home directory of application
|
|
111
|
+
* @member {String}
|
|
112
|
+
* @see {@link AppInfo#homeDir}
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
*/
|
|
115
|
+
get homeDir() {
|
|
116
|
+
return this.options.homeDir;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The electron current directory of application
|
|
121
|
+
* @member {String}
|
|
122
|
+
* @see {@link AppInfo#baseDir}
|
|
123
|
+
* @since 1.0.0
|
|
124
|
+
*/
|
|
125
|
+
get baseDir() {
|
|
126
|
+
return this.options.baseDir;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The ee-core directory of framework
|
|
131
|
+
* @member {String}
|
|
132
|
+
* @see {@link AppInfo#EeCoreDir}
|
|
133
|
+
* @since 1.0.0
|
|
134
|
+
*/
|
|
135
|
+
get eeCoreDir() {
|
|
136
|
+
return this.options.framework;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The name of application
|
|
141
|
+
* @member {String}
|
|
142
|
+
* @see {@link AppInfo#name}
|
|
143
|
+
* @since 1.0.0
|
|
144
|
+
*/
|
|
145
|
+
get name() {
|
|
146
|
+
return this.loader ? this.loader.pkg.name : '';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The configuration of application
|
|
151
|
+
* @member {Config}
|
|
152
|
+
* @since 1.0.0
|
|
153
|
+
*/
|
|
154
|
+
get config() {
|
|
155
|
+
return this.loader ? this.loader.config : {};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The addon of application
|
|
160
|
+
* @member {Addon}
|
|
161
|
+
* @since 1.0.0
|
|
162
|
+
*/
|
|
163
|
+
get addon() {
|
|
164
|
+
return this.loader ? this.loader.addon : {};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get [EE_LOADER]() {
|
|
168
|
+
return require('./loader/ee_loader');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Convert a generator function to a promisable one.
|
|
173
|
+
*
|
|
174
|
+
* Notice: for other kinds of functions, it directly returns you what it is.
|
|
175
|
+
*
|
|
176
|
+
* @param {Function} fn The inputted function.
|
|
177
|
+
* @return {AsyncFunction} An async promise-based function.
|
|
178
|
+
* @example
|
|
179
|
+
```javascript
|
|
180
|
+
const fn = function* (arg) {
|
|
181
|
+
return arg;
|
|
182
|
+
};
|
|
183
|
+
const wrapped = app.toAsyncFunction(fn);
|
|
184
|
+
wrapped(true).then((value) => console.log(value));
|
|
185
|
+
```
|
|
186
|
+
*/
|
|
187
|
+
toAsyncFunction(fn) {
|
|
188
|
+
if (!is.generatorFunction(fn)) return fn;
|
|
189
|
+
fn = co.wrap(fn);
|
|
190
|
+
return async function(...args) {
|
|
191
|
+
return fn.apply(this, args);
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Convert an object with generator functions to a Promisable one.
|
|
197
|
+
* @param {Mixed} obj The inputted object.
|
|
198
|
+
* @return {Promise} A Promisable result.
|
|
199
|
+
* @example
|
|
200
|
+
```javascript
|
|
201
|
+
const fn = function* (arg) {
|
|
202
|
+
return arg;
|
|
203
|
+
};
|
|
204
|
+
const arr = [ fn(1), fn(2) ];
|
|
205
|
+
const promise = app.toPromise(arr);
|
|
206
|
+
promise.then(res => console.log(res));
|
|
207
|
+
```
|
|
208
|
+
*/
|
|
209
|
+
toPromise(obj) {
|
|
210
|
+
return co(function* () {
|
|
211
|
+
return yield obj;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = EeCore;
|
|
@@ -1,106 +1,106 @@
|
|
|
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
|
-
const Utils = require('../utils');
|
|
9
|
-
|
|
10
|
-
class ClassLoader {
|
|
11
|
-
|
|
12
|
-
constructor(options) {
|
|
13
|
-
assert(options.ctx, 'options.ctx is required');
|
|
14
|
-
const properties = options.properties;
|
|
15
|
-
this._cache = new Map();
|
|
16
|
-
this._ctx = options.ctx;
|
|
17
|
-
|
|
18
|
-
for (const property in properties) {
|
|
19
|
-
this.defineProperty(property, properties[property]);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
defineProperty(property, values) {
|
|
24
|
-
Object.defineProperty(this, property, {
|
|
25
|
-
get() {
|
|
26
|
-
let instance = this._cache.get(property);
|
|
27
|
-
if (!instance) {
|
|
28
|
-
instance = getInstance(values, this._ctx);
|
|
29
|
-
this._cache.set(property, instance);
|
|
30
|
-
}
|
|
31
|
-
return instance;
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`.
|
|
39
|
-
* @extends FileLoader
|
|
40
|
-
* @since 1.0.0
|
|
41
|
-
*/
|
|
42
|
-
class ContextLoader extends FileLoader {
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @class
|
|
46
|
-
* @param {Object} options - options same as {@link FileLoader}
|
|
47
|
-
* @param {String} options.fieldClass - determine the field name of inject object.
|
|
48
|
-
*/
|
|
49
|
-
constructor(options) {
|
|
50
|
-
assert(options.property, 'options.property is required');
|
|
51
|
-
assert(options.inject, 'options.inject is required');
|
|
52
|
-
const target = options.target = {};
|
|
53
|
-
if (options.fieldClass) {
|
|
54
|
-
options.inject[options.fieldClass] = target;
|
|
55
|
-
}
|
|
56
|
-
super(options);
|
|
57
|
-
|
|
58
|
-
const app = this.options.inject;
|
|
59
|
-
const property = options.property;
|
|
60
|
-
|
|
61
|
-
// define ctx.service
|
|
62
|
-
Object.defineProperty(app, property, {
|
|
63
|
-
get() {
|
|
64
|
-
// distinguish property cache,
|
|
65
|
-
// cache's lifecycle is the same with this context instance
|
|
66
|
-
// e.x. ctx.service1 and ctx.service2 have different cache
|
|
67
|
-
if (!this[CLASSLOADER]) {
|
|
68
|
-
this[CLASSLOADER] = new Map();
|
|
69
|
-
}
|
|
70
|
-
const classLoader = this[CLASSLOADER];
|
|
71
|
-
|
|
72
|
-
let instance = classLoader.get(property);
|
|
73
|
-
if (!instance) {
|
|
74
|
-
instance = getInstance(target, this);
|
|
75
|
-
classLoader.set(property, instance);
|
|
76
|
-
}
|
|
77
|
-
return instance;
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = ContextLoader;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
function getInstance(values, ctx) {
|
|
87
|
-
// it's a directory when it has no exports
|
|
88
|
-
// then use ClassLoader
|
|
89
|
-
const Class = values[EXPORTS] ? values : null;
|
|
90
|
-
let instance;
|
|
91
|
-
if (Class) {
|
|
92
|
-
if (is.class(Class) || Utils.isBytecodeClass(Class)) {
|
|
93
|
-
instance = new Class(ctx);
|
|
94
|
-
} else {
|
|
95
|
-
// it's just an object
|
|
96
|
-
instance = Class;
|
|
97
|
-
}
|
|
98
|
-
// Can't set property to primitive, so check again
|
|
99
|
-
// e.x. module.exports = 1;
|
|
100
|
-
} else if (is.primitive(values)) {
|
|
101
|
-
instance = values;
|
|
102
|
-
} else {
|
|
103
|
-
instance = new ClassLoader({ ctx, properties: values });
|
|
104
|
-
}
|
|
105
|
-
return instance;
|
|
106
|
-
}
|
|
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
|
+
const Utils = require('../utils');
|
|
9
|
+
|
|
10
|
+
class ClassLoader {
|
|
11
|
+
|
|
12
|
+
constructor(options) {
|
|
13
|
+
assert(options.ctx, 'options.ctx is required');
|
|
14
|
+
const properties = options.properties;
|
|
15
|
+
this._cache = new Map();
|
|
16
|
+
this._ctx = options.ctx;
|
|
17
|
+
|
|
18
|
+
for (const property in properties) {
|
|
19
|
+
this.defineProperty(property, properties[property]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
defineProperty(property, values) {
|
|
24
|
+
Object.defineProperty(this, property, {
|
|
25
|
+
get() {
|
|
26
|
+
let instance = this._cache.get(property);
|
|
27
|
+
if (!instance) {
|
|
28
|
+
instance = getInstance(values, this._ctx);
|
|
29
|
+
this._cache.set(property, instance);
|
|
30
|
+
}
|
|
31
|
+
return instance;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`.
|
|
39
|
+
* @extends FileLoader
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*/
|
|
42
|
+
class ContextLoader extends FileLoader {
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @class
|
|
46
|
+
* @param {Object} options - options same as {@link FileLoader}
|
|
47
|
+
* @param {String} options.fieldClass - determine the field name of inject object.
|
|
48
|
+
*/
|
|
49
|
+
constructor(options) {
|
|
50
|
+
assert(options.property, 'options.property is required');
|
|
51
|
+
assert(options.inject, 'options.inject is required');
|
|
52
|
+
const target = options.target = {};
|
|
53
|
+
if (options.fieldClass) {
|
|
54
|
+
options.inject[options.fieldClass] = target;
|
|
55
|
+
}
|
|
56
|
+
super(options);
|
|
57
|
+
|
|
58
|
+
const app = this.options.inject;
|
|
59
|
+
const property = options.property;
|
|
60
|
+
|
|
61
|
+
// define ctx.service
|
|
62
|
+
Object.defineProperty(app, property, {
|
|
63
|
+
get() {
|
|
64
|
+
// distinguish property cache,
|
|
65
|
+
// cache's lifecycle is the same with this context instance
|
|
66
|
+
// e.x. ctx.service1 and ctx.service2 have different cache
|
|
67
|
+
if (!this[CLASSLOADER]) {
|
|
68
|
+
this[CLASSLOADER] = new Map();
|
|
69
|
+
}
|
|
70
|
+
const classLoader = this[CLASSLOADER];
|
|
71
|
+
|
|
72
|
+
let instance = classLoader.get(property);
|
|
73
|
+
if (!instance) {
|
|
74
|
+
instance = getInstance(target, this);
|
|
75
|
+
classLoader.set(property, instance);
|
|
76
|
+
}
|
|
77
|
+
return instance;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = ContextLoader;
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
function getInstance(values, ctx) {
|
|
87
|
+
// it's a directory when it has no exports
|
|
88
|
+
// then use ClassLoader
|
|
89
|
+
const Class = values[EXPORTS] ? values : null;
|
|
90
|
+
let instance;
|
|
91
|
+
if (Class) {
|
|
92
|
+
if (is.class(Class) || Utils.isBytecodeClass(Class)) {
|
|
93
|
+
instance = new Class(ctx);
|
|
94
|
+
} else {
|
|
95
|
+
// it's just an object
|
|
96
|
+
instance = Class;
|
|
97
|
+
}
|
|
98
|
+
// Can't set property to primitive, so check again
|
|
99
|
+
// e.x. module.exports = 1;
|
|
100
|
+
} else if (is.primitive(values)) {
|
|
101
|
+
instance = values;
|
|
102
|
+
} else {
|
|
103
|
+
instance = new ClassLoader({ ctx, properties: values });
|
|
104
|
+
}
|
|
105
|
+
return instance;
|
|
106
|
+
}
|