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/socket/ipcServer.js
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
const debug = require('debug')('ee-core:ipcServer');
|
|
2
|
-
const is = require('is-type-of');
|
|
3
|
-
const { ipcMain } = require('electron');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const globby = require('globby');
|
|
7
|
-
const Utils = require('../core/lib/utils');
|
|
8
|
-
const Wrap = require('../utils/wrap');
|
|
9
|
-
const Log = require('../log');
|
|
10
|
-
|
|
11
|
-
class IpcServer {
|
|
12
|
-
constructor (app) {
|
|
13
|
-
this.app = app;
|
|
14
|
-
this.register();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
register () {
|
|
18
|
-
const self = this;
|
|
19
|
-
// 遍历方法
|
|
20
|
-
const files = Utils.filePatterns();
|
|
21
|
-
const directory = path.join(this.app.config.baseDir, 'controller');
|
|
22
|
-
const filepaths = globby.sync(files, { cwd: directory });
|
|
23
|
-
|
|
24
|
-
for (const filepath of filepaths) {
|
|
25
|
-
const fullpath = path.join(directory, filepath);
|
|
26
|
-
if (!fs.statSync(fullpath).isFile()) continue;
|
|
27
|
-
|
|
28
|
-
const properties = Wrap.getProperties(filepath, {caseStyle: 'lower'});
|
|
29
|
-
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
30
|
-
|
|
31
|
-
let fileObj = Utils.loadFile(fullpath);
|
|
32
|
-
const fns = {};
|
|
33
|
-
// 为了统一,仅支持class文件
|
|
34
|
-
if (is.class(fileObj) || Utils.isBytecodeClass(fileObj)) {
|
|
35
|
-
let proto = fileObj.prototype;
|
|
36
|
-
// 不遍历父类的方法
|
|
37
|
-
//while (proto !== Object.prototype) {
|
|
38
|
-
const keys = Object.getOwnPropertyNames(proto);
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
if (key === 'constructor') {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
44
|
-
if (is.function(d.value) && !fns.hasOwnProperty(key)) {
|
|
45
|
-
fns[key] = 1;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
//proto = Object.getPrototypeOf(proto);
|
|
49
|
-
//}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
debug('register class %s fns %j', pathName, fns);
|
|
53
|
-
|
|
54
|
-
for (const key in fns) {
|
|
55
|
-
let channel = pathName + '.' + key;
|
|
56
|
-
debug('register channel %s', channel);
|
|
57
|
-
|
|
58
|
-
function findFn (app, c) {
|
|
59
|
-
try {
|
|
60
|
-
// 找函数
|
|
61
|
-
const cmd = c;
|
|
62
|
-
let fn = null;
|
|
63
|
-
if (is.string(cmd)) {
|
|
64
|
-
const actions = cmd.split('.');
|
|
65
|
-
let obj = app;
|
|
66
|
-
actions.forEach(key => {
|
|
67
|
-
obj = obj[key];
|
|
68
|
-
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
69
|
-
});
|
|
70
|
-
fn = obj;
|
|
71
|
-
}
|
|
72
|
-
if (!fn) throw new Error('function not exists');
|
|
73
|
-
|
|
74
|
-
return fn;
|
|
75
|
-
} catch (err) {
|
|
76
|
-
Log.coreLogger.error('[ee-core] [socket/IpcServer] throw error:', err);
|
|
77
|
-
}
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// send/on 模型
|
|
82
|
-
ipcMain.on(channel, async (event, params) => {
|
|
83
|
-
try {
|
|
84
|
-
const fn = findFn(self.app, channel);
|
|
85
|
-
const result = await fn.call(self.app, params, event);
|
|
86
|
-
|
|
87
|
-
event.returnValue = result;
|
|
88
|
-
event.reply(`${channel}`, result);
|
|
89
|
-
} catch(e) {
|
|
90
|
-
Log.coreLogger.error('[ee-core] [socket/IpcServer] send/on throw error:', e);
|
|
91
|
-
// event.returnValue = e;
|
|
92
|
-
// event.reply(`${channel}`, e);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// invoke/handle 模型
|
|
97
|
-
ipcMain.handle(channel, async (event, params) => {
|
|
98
|
-
try {
|
|
99
|
-
const fn = findFn(self.app, channel);
|
|
100
|
-
const result = await fn.call(self.app, params, event);
|
|
101
|
-
|
|
102
|
-
return result;
|
|
103
|
-
} catch(e) {
|
|
104
|
-
Log.coreLogger.error('[ee-core] [socket/IpcServer] invoke/handle throw error:', e);
|
|
105
|
-
return e
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
1
|
+
const debug = require('debug')('ee-core:ipcServer');
|
|
2
|
+
const is = require('is-type-of');
|
|
3
|
+
const { ipcMain } = require('electron');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const globby = require('globby');
|
|
7
|
+
const Utils = require('../core/lib/utils');
|
|
8
|
+
const Wrap = require('../utils/wrap');
|
|
9
|
+
const Log = require('../log');
|
|
10
|
+
|
|
11
|
+
class IpcServer {
|
|
12
|
+
constructor (app) {
|
|
13
|
+
this.app = app;
|
|
14
|
+
this.register();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
register () {
|
|
18
|
+
const self = this;
|
|
19
|
+
// 遍历方法
|
|
20
|
+
const files = Utils.filePatterns();
|
|
21
|
+
const directory = path.join(this.app.config.baseDir, 'controller');
|
|
22
|
+
const filepaths = globby.sync(files, { cwd: directory });
|
|
23
|
+
|
|
24
|
+
for (const filepath of filepaths) {
|
|
25
|
+
const fullpath = path.join(directory, filepath);
|
|
26
|
+
if (!fs.statSync(fullpath).isFile()) continue;
|
|
27
|
+
|
|
28
|
+
const properties = Wrap.getProperties(filepath, {caseStyle: 'lower'});
|
|
29
|
+
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
30
|
+
|
|
31
|
+
let fileObj = Utils.loadFile(fullpath);
|
|
32
|
+
const fns = {};
|
|
33
|
+
// 为了统一,仅支持class文件
|
|
34
|
+
if (is.class(fileObj) || Utils.isBytecodeClass(fileObj)) {
|
|
35
|
+
let proto = fileObj.prototype;
|
|
36
|
+
// 不遍历父类的方法
|
|
37
|
+
//while (proto !== Object.prototype) {
|
|
38
|
+
const keys = Object.getOwnPropertyNames(proto);
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
if (key === 'constructor') {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
44
|
+
if (is.function(d.value) && !fns.hasOwnProperty(key)) {
|
|
45
|
+
fns[key] = 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//proto = Object.getPrototypeOf(proto);
|
|
49
|
+
//}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
debug('register class %s fns %j', pathName, fns);
|
|
53
|
+
|
|
54
|
+
for (const key in fns) {
|
|
55
|
+
let channel = pathName + '.' + key;
|
|
56
|
+
debug('register channel %s', channel);
|
|
57
|
+
|
|
58
|
+
function findFn (app, c) {
|
|
59
|
+
try {
|
|
60
|
+
// 找函数
|
|
61
|
+
const cmd = c;
|
|
62
|
+
let fn = null;
|
|
63
|
+
if (is.string(cmd)) {
|
|
64
|
+
const actions = cmd.split('.');
|
|
65
|
+
let obj = app;
|
|
66
|
+
actions.forEach(key => {
|
|
67
|
+
obj = obj[key];
|
|
68
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
69
|
+
});
|
|
70
|
+
fn = obj;
|
|
71
|
+
}
|
|
72
|
+
if (!fn) throw new Error('function not exists');
|
|
73
|
+
|
|
74
|
+
return fn;
|
|
75
|
+
} catch (err) {
|
|
76
|
+
Log.coreLogger.error('[ee-core] [socket/IpcServer] throw error:', err);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// send/on 模型
|
|
82
|
+
ipcMain.on(channel, async (event, params) => {
|
|
83
|
+
try {
|
|
84
|
+
const fn = findFn(self.app, channel);
|
|
85
|
+
const result = await fn.call(self.app, params, event);
|
|
86
|
+
|
|
87
|
+
event.returnValue = result;
|
|
88
|
+
event.reply(`${channel}`, result);
|
|
89
|
+
} catch(e) {
|
|
90
|
+
Log.coreLogger.error('[ee-core] [socket/IpcServer] send/on throw error:', e);
|
|
91
|
+
// event.returnValue = e;
|
|
92
|
+
// event.reply(`${channel}`, e);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// invoke/handle 模型
|
|
97
|
+
ipcMain.handle(channel, async (event, params) => {
|
|
98
|
+
try {
|
|
99
|
+
const fn = findFn(self.app, channel);
|
|
100
|
+
const result = await fn.call(self.app, params, event);
|
|
101
|
+
|
|
102
|
+
return result;
|
|
103
|
+
} catch(e) {
|
|
104
|
+
Log.coreLogger.error('[ee-core] [socket/IpcServer] invoke/handle throw error:', e);
|
|
105
|
+
return e
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
113
|
module.exports = IpcServer;
|
package/socket/socketServer.js
CHANGED
|
@@ -1,66 +1,70 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { Server } = require('socket.io');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const Log = require('../log');
|
|
6
|
-
const Conf = require('../config');
|
|
7
|
-
const Ps = require('../ps');
|
|
8
|
-
const Channel = require('../const/channel');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* socket server
|
|
12
|
-
*/
|
|
13
|
-
class SocketServer {
|
|
14
|
-
constructor (app) {
|
|
15
|
-
this.app = app;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
actions.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Server } = require('socket.io');
|
|
4
|
+
const is = require('is-type-of');
|
|
5
|
+
const Log = require('../log');
|
|
6
|
+
const Conf = require('../config/cache');
|
|
7
|
+
const Ps = require('../ps');
|
|
8
|
+
const Channel = require('../const/channel');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* socket server
|
|
12
|
+
*/
|
|
13
|
+
class SocketServer {
|
|
14
|
+
constructor (app) {
|
|
15
|
+
this.app = app;
|
|
16
|
+
this.socket = undefined;
|
|
17
|
+
const options = Conf.getValue('socketServer');
|
|
18
|
+
|
|
19
|
+
if (options.enable == false) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let port = Ps.getSocketPort();
|
|
24
|
+
if (!port) {
|
|
25
|
+
throw new Error('[ee-core] [socket/socketServer] socekt port required, and must be a number !');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Log.coreLogger.info('[ee-core] [socket/socketServer] port is:', port);
|
|
29
|
+
|
|
30
|
+
this.io = new Server(port, options);
|
|
31
|
+
this.connec(options);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
connec (opt = {}) {
|
|
35
|
+
const app = this.app;
|
|
36
|
+
this.io.on('connection', (socket) => {
|
|
37
|
+
const channel = opt.channel || Channel.socketIo.partySoftware;
|
|
38
|
+
this.socket = socket;
|
|
39
|
+
socket.on(channel, async (message, callback) => {
|
|
40
|
+
Log.coreLogger.info('[ee-core] [socket/socketServer] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// 找函数
|
|
44
|
+
const cmd = message.cmd;
|
|
45
|
+
const args = message.args || message.params;
|
|
46
|
+
let fn = null;
|
|
47
|
+
if (is.string(cmd)) {
|
|
48
|
+
const actions = cmd.split('.');
|
|
49
|
+
let obj = app;
|
|
50
|
+
actions.forEach(key => {
|
|
51
|
+
obj = obj[key];
|
|
52
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
53
|
+
});
|
|
54
|
+
fn = obj;
|
|
55
|
+
}
|
|
56
|
+
if (!fn) throw new Error('function not exists');
|
|
57
|
+
|
|
58
|
+
const result = await fn.call(app, args);
|
|
59
|
+
if (callback) {
|
|
60
|
+
callback(result);
|
|
61
|
+
}
|
|
62
|
+
} catch (err) {
|
|
63
|
+
Log.coreLogger.error('[ee-core] [socket/socketServer] throw error:', err);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
66
70
|
module.exports = SocketServer;
|
package/storage/index.js
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const _ = require('lodash');
|
|
3
|
-
const DB = {};
|
|
4
|
-
|
|
5
|
-
// jsondb 要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
|
|
6
|
-
|
|
7
|
-
DB.connection = function (database, options = {}) {
|
|
8
|
-
let driver = options.driver || 'jsondb';
|
|
9
|
-
|
|
10
|
-
// 兼容之前api
|
|
11
|
-
driver = driver == 'lowdb' ? 'jsondb' : driver;
|
|
12
|
-
|
|
13
|
-
let opt = options.default || {};
|
|
14
|
-
if (!_.includes(['jsondb', 'sqlite'], driver)) {
|
|
15
|
-
assert(database, `db driver ${driver} is not supported`);
|
|
16
|
-
}
|
|
17
|
-
if (_.isEmpty(database)) {
|
|
18
|
-
assert(database, `db name ${database} Cannot be empty`);
|
|
19
|
-
}
|
|
20
|
-
let storage;
|
|
21
|
-
switch (driver) {
|
|
22
|
-
case 'jsondb':
|
|
23
|
-
const JsondbStorage = require('./jsondbStorage');
|
|
24
|
-
storage = new JsondbStorage(database);
|
|
25
|
-
break;
|
|
26
|
-
case 'sqlite':
|
|
27
|
-
const SqliteStorage = require('./sqliteStorage');
|
|
28
|
-
storage = new SqliteStorage(database, opt);
|
|
29
|
-
break;
|
|
30
|
-
default:
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return storage;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// 兼容之前的api
|
|
37
|
-
DB.JsonDB = DB;
|
|
38
|
-
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const DB = {};
|
|
4
|
+
|
|
5
|
+
// jsondb 要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
|
|
6
|
+
|
|
7
|
+
DB.connection = function (database, options = {}) {
|
|
8
|
+
let driver = options.driver || 'jsondb';
|
|
9
|
+
|
|
10
|
+
// 兼容之前api
|
|
11
|
+
driver = driver == 'lowdb' ? 'jsondb' : driver;
|
|
12
|
+
|
|
13
|
+
let opt = options.default || {};
|
|
14
|
+
if (!_.includes(['jsondb', 'sqlite'], driver)) {
|
|
15
|
+
assert(database, `db driver ${driver} is not supported`);
|
|
16
|
+
}
|
|
17
|
+
if (_.isEmpty(database)) {
|
|
18
|
+
assert(database, `db name ${database} Cannot be empty`);
|
|
19
|
+
}
|
|
20
|
+
let storage;
|
|
21
|
+
switch (driver) {
|
|
22
|
+
case 'jsondb':
|
|
23
|
+
const JsondbStorage = require('./jsondbStorage');
|
|
24
|
+
storage = new JsondbStorage(database);
|
|
25
|
+
break;
|
|
26
|
+
case 'sqlite':
|
|
27
|
+
const SqliteStorage = require('./sqliteStorage');
|
|
28
|
+
storage = new SqliteStorage(database, opt);
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return storage;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 兼容之前的api
|
|
37
|
+
DB.JsonDB = DB;
|
|
38
|
+
|
|
39
39
|
module.exports = DB;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
class Base {
|
|
2
|
-
constructor(source) {
|
|
3
|
-
this.source = source
|
|
4
|
-
this.defaultValue = {}
|
|
5
|
-
this.serialize = this._stringify
|
|
6
|
-
this.deserialize = JSON.parse
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
_canDeserialized(obj) {
|
|
10
|
-
try {
|
|
11
|
-
this.deserialize(obj)
|
|
12
|
-
return true
|
|
13
|
-
} catch (e) {
|
|
14
|
-
return false
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
_stringify(obj) {
|
|
19
|
-
return JSON.stringify(obj, null, 2)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = Base
|
|
1
|
+
class Base {
|
|
2
|
+
constructor(source) {
|
|
3
|
+
this.source = source
|
|
4
|
+
this.defaultValue = {}
|
|
5
|
+
this.serialize = this._stringify
|
|
6
|
+
this.deserialize = JSON.parse
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
_canDeserialized(obj) {
|
|
10
|
+
try {
|
|
11
|
+
this.deserialize(obj)
|
|
12
|
+
return true
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_stringify(obj) {
|
|
19
|
+
return JSON.stringify(obj, null, 2)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = Base
|
|
@@ -1,52 +1,64 @@
|
|
|
1
|
-
const Base = require('./Base')
|
|
2
|
-
const fs = require('fs')
|
|
3
|
-
const Log = require('../../../log')
|
|
4
|
-
|
|
5
|
-
class FileSync extends Base {
|
|
6
|
-
|
|
7
|
-
constructor(options = {}) {
|
|
8
|
-
const { source, isSysDB } = options;
|
|
9
|
-
super(source);
|
|
10
|
-
this.isSysDB = isSysDB;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
read() {
|
|
14
|
-
if (fs.existsSync(this.source)) {
|
|
15
|
-
// Read database
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
const Base = require('./Base')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const Log = require('../../../log')
|
|
4
|
+
|
|
5
|
+
class FileSync extends Base {
|
|
6
|
+
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
const { source, isSysDB } = options;
|
|
9
|
+
super(source);
|
|
10
|
+
this.isSysDB = isSysDB;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
read() {
|
|
14
|
+
if (fs.existsSync(this.source)) {
|
|
15
|
+
// Read database
|
|
16
|
+
let data = fs.readFileSync(this.source, {encoding: 'utf8'}).trim();
|
|
17
|
+
|
|
18
|
+
// 是否可以正常解析
|
|
19
|
+
let canDeserialized = this._canDeserialized(data);
|
|
20
|
+
if (!canDeserialized) {
|
|
21
|
+
let errMessage = `[ee-core] [storage/jsondb] malformed json in file: ${this.source}\n${data}`;
|
|
22
|
+
Log.coreLogger.error(errMessage);
|
|
23
|
+
|
|
24
|
+
// 是否文件结尾多一个括号,尝试处理
|
|
25
|
+
data = data.trim().slice(0, -1);
|
|
26
|
+
canDeserialized = this._canDeserialized(data);
|
|
27
|
+
if (canDeserialized) {
|
|
28
|
+
// 转换为对象,并写入
|
|
29
|
+
const newData = JSON.parse(data);
|
|
30
|
+
this._fsWrite(newData);
|
|
31
|
+
} else {
|
|
32
|
+
// [todo] 重置 system.json ,不处理用户数据
|
|
33
|
+
if (this.isSysDB) {
|
|
34
|
+
this._fsWrite(this.defaultValue);
|
|
35
|
+
}
|
|
36
|
+
errMessage = '[ee-core] [storage/jsondb] malformed json that cannot be handled!';
|
|
37
|
+
Log.coreLogger.error(errMessage);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const value = canDeserialized ? this.deserialize(data) : this.defaultValue;
|
|
41
|
+
return value;
|
|
42
|
+
} else {
|
|
43
|
+
// Initialize
|
|
44
|
+
this._fsWrite(this.defaultValue);
|
|
45
|
+
return this.defaultValue
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
write(data) {
|
|
50
|
+
return this._fsWrite(data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_fsWrite(data) {
|
|
54
|
+
const isObject = Object.prototype.toString.call(data) === '[object Object]';
|
|
55
|
+
if (!isObject) {
|
|
56
|
+
Log.coreLogger.error('[ee-core] [storage/jsondb] Variable is not an object :', data);
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return fs.writeFileSync(this.source, this.serialize(data), {flag:'w+'})
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = FileSync
|