ee-core 1.2.7 → 1.2.8-beta.4

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 (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/bin/tools.js +22 -22
  4. package/config/config.default.js +250 -247
  5. package/core/index.js +12 -12
  6. package/core/lib/ee.js +209 -209
  7. package/core/lib/loader/context_loader.js +105 -105
  8. package/core/lib/loader/ee_loader.js +451 -451
  9. package/core/lib/loader/file_loader.js +262 -262
  10. package/core/lib/loader/mixin/config.js +138 -138
  11. package/core/lib/loader/mixin/controller.js +123 -123
  12. package/core/lib/loader/mixin/service.js +29 -29
  13. package/core/lib/utils/base_context_class.js +34 -34
  14. package/core/lib/utils/index.js +100 -100
  15. package/core/lib/utils/sequencify.js +59 -59
  16. package/core/lib/utils/timing.js +77 -77
  17. package/index.js +49 -49
  18. package/lib/appLoader.js +45 -45
  19. package/lib/application.js +80 -80
  20. package/lib/baseApp.js +118 -118
  21. package/lib/constant.js +28 -28
  22. package/lib/eeApp.js +326 -326
  23. package/lib/helper.js +51 -51
  24. package/lib/httpclient.js +136 -136
  25. package/lib/logger.js +46 -46
  26. package/lib/socket/httpServer.js +102 -104
  27. package/lib/socket/io.js +23 -23
  28. package/lib/socket/ipcServer.js +128 -128
  29. package/lib/socket/socketClient.js +50 -50
  30. package/lib/socket/socketServer.js +76 -76
  31. package/lib/socket/start.js +22 -22
  32. package/lib/storage/index.js +33 -21
  33. package/lib/storage/lowdbStorage.js +98 -143
  34. package/lib/storage/sqliteStorage.js +59 -0
  35. package/package.json +45 -45
  36. package/resource/loading.html +21 -21
  37. package/resource/view_example.html +21 -21
  38. package/tools/codeCompress.js +204 -204
  39. package/tools/replaceDist.js +76 -76
  40. package/utils/common.js +91 -30
  41. package/utils/index.js +207 -207
  42. package/utils/wrap.js +37 -37
  43. package/lib/storage/appStorage.js +0 -14
package/core/lib/ee.js CHANGED
@@ -1,209 +1,209 @@
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
- let loaderOptions = Object.assign({
92
- logger: this.console,
93
- app:this
94
- }, options);
95
- this.loader = new Loader(loaderOptions);
96
- }
97
-
98
- /**
99
- * override koa's app.use, support generator function
100
- * @param {Function} fn - middleware
101
- * @return {Application} app
102
- * @since 1.0.0
103
- */
104
- use(fn) {
105
- assert(is.function(fn), 'app.use() requires a function');
106
- debug('use %s', fn._name || fn.name || '-');
107
- this.middleware.push(utils.middleware(fn));
108
- return this;
109
- }
110
-
111
- /**
112
- * The home directory of application
113
- * @member {String}
114
- * @see {@link AppInfo#homeDir}
115
- * @since 1.0.0
116
- */
117
- get homeDir() {
118
- return this.options.homeDir;
119
- }
120
-
121
- /**
122
- * The electron current directory of application
123
- * @member {String}
124
- * @see {@link AppInfo#baseDir}
125
- * @since 1.0.0
126
- */
127
- get baseDir() {
128
- return this.options.baseDir;
129
- }
130
-
131
- /**
132
- * The ee-core directory of framework
133
- * @member {String}
134
- * @see {@link AppInfo#EeCoreDir}
135
- * @since 1.0.0
136
- */
137
- get eeCoreDir() {
138
- return this.options.framework;
139
- }
140
-
141
- /**
142
- * The name of application
143
- * @member {String}
144
- * @see {@link AppInfo#name}
145
- * @since 1.0.0
146
- */
147
- get name() {
148
- return this.loader ? this.loader.pkg.name : '';
149
- }
150
-
151
- /**
152
- * The configuration of application
153
- * @member {Config}
154
- * @since 1.0.0
155
- */
156
- get config() {
157
- return this.loader ? this.loader.config : {};
158
- }
159
-
160
- get [EE_LOADER]() {
161
- return require('./loader/ee_loader');
162
- }
163
-
164
- /**
165
- * Convert a generator function to a promisable one.
166
- *
167
- * Notice: for other kinds of functions, it directly returns you what it is.
168
- *
169
- * @param {Function} fn The inputted function.
170
- * @return {AsyncFunction} An async promise-based function.
171
- * @example
172
- ```javascript
173
- const fn = function* (arg) {
174
- return arg;
175
- };
176
- const wrapped = app.toAsyncFunction(fn);
177
- wrapped(true).then((value) => console.log(value));
178
- ```
179
- */
180
- toAsyncFunction(fn) {
181
- if (!is.generatorFunction(fn)) return fn;
182
- fn = co.wrap(fn);
183
- return async function(...args) {
184
- return fn.apply(this, args);
185
- };
186
- }
187
-
188
- /**
189
- * Convert an object with generator functions to a Promisable one.
190
- * @param {Mixed} obj The inputted object.
191
- * @return {Promise} A Promisable result.
192
- * @example
193
- ```javascript
194
- const fn = function* (arg) {
195
- return arg;
196
- };
197
- const arr = [ fn(1), fn(2) ];
198
- const promise = app.toPromise(arr);
199
- promise.then(res => console.log(res));
200
- ```
201
- */
202
- toPromise(obj) {
203
- return co(function* () {
204
- return yield obj;
205
- });
206
- }
207
- }
208
-
209
- module.exports = EeCore;
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
+ let loaderOptions = Object.assign({
92
+ logger: this.console,
93
+ app:this
94
+ }, options);
95
+ this.loader = new Loader(loaderOptions);
96
+ }
97
+
98
+ /**
99
+ * override koa's app.use, support generator function
100
+ * @param {Function} fn - middleware
101
+ * @return {Application} app
102
+ * @since 1.0.0
103
+ */
104
+ use(fn) {
105
+ assert(is.function(fn), 'app.use() requires a function');
106
+ debug('use %s', fn._name || fn.name || '-');
107
+ this.middleware.push(utils.middleware(fn));
108
+ return this;
109
+ }
110
+
111
+ /**
112
+ * The home directory of application
113
+ * @member {String}
114
+ * @see {@link AppInfo#homeDir}
115
+ * @since 1.0.0
116
+ */
117
+ get homeDir() {
118
+ return this.options.homeDir;
119
+ }
120
+
121
+ /**
122
+ * The electron current directory of application
123
+ * @member {String}
124
+ * @see {@link AppInfo#baseDir}
125
+ * @since 1.0.0
126
+ */
127
+ get baseDir() {
128
+ return this.options.baseDir;
129
+ }
130
+
131
+ /**
132
+ * The ee-core directory of framework
133
+ * @member {String}
134
+ * @see {@link AppInfo#EeCoreDir}
135
+ * @since 1.0.0
136
+ */
137
+ get eeCoreDir() {
138
+ return this.options.framework;
139
+ }
140
+
141
+ /**
142
+ * The name of application
143
+ * @member {String}
144
+ * @see {@link AppInfo#name}
145
+ * @since 1.0.0
146
+ */
147
+ get name() {
148
+ return this.loader ? this.loader.pkg.name : '';
149
+ }
150
+
151
+ /**
152
+ * The configuration of application
153
+ * @member {Config}
154
+ * @since 1.0.0
155
+ */
156
+ get config() {
157
+ return this.loader ? this.loader.config : {};
158
+ }
159
+
160
+ get [EE_LOADER]() {
161
+ return require('./loader/ee_loader');
162
+ }
163
+
164
+ /**
165
+ * Convert a generator function to a promisable one.
166
+ *
167
+ * Notice: for other kinds of functions, it directly returns you what it is.
168
+ *
169
+ * @param {Function} fn The inputted function.
170
+ * @return {AsyncFunction} An async promise-based function.
171
+ * @example
172
+ ```javascript
173
+ const fn = function* (arg) {
174
+ return arg;
175
+ };
176
+ const wrapped = app.toAsyncFunction(fn);
177
+ wrapped(true).then((value) => console.log(value));
178
+ ```
179
+ */
180
+ toAsyncFunction(fn) {
181
+ if (!is.generatorFunction(fn)) return fn;
182
+ fn = co.wrap(fn);
183
+ return async function(...args) {
184
+ return fn.apply(this, args);
185
+ };
186
+ }
187
+
188
+ /**
189
+ * Convert an object with generator functions to a Promisable one.
190
+ * @param {Mixed} obj The inputted object.
191
+ * @return {Promise} A Promisable result.
192
+ * @example
193
+ ```javascript
194
+ const fn = function* (arg) {
195
+ return arg;
196
+ };
197
+ const arr = [ fn(1), fn(2) ];
198
+ const promise = app.toPromise(arr);
199
+ promise.then(res => console.log(res));
200
+ ```
201
+ */
202
+ toPromise(obj) {
203
+ return co(function* () {
204
+ return yield obj;
205
+ });
206
+ }
207
+ }
208
+
209
+ module.exports = EeCore;
@@ -1,105 +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
- }
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
+ }