ee-core 1.5.2-beta.2 → 2.0.0-beta.2
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/config/config.default.js +8 -1
- package/core/lib/ee.js +1 -1
- package/core/lib/loader/ee_loader.js +23 -45
- package/core/lib/loader/mixin/config.js +6 -11
- package/core/lib/loader/mixin/controller.js +3 -2
- package/core/lib/utils/function.js +30 -0
- package/core/lib/utils/index.js +6 -0
- package/index.js +2 -2
- package/lib/appLoader.js +0 -5
- package/lib/application.js +12 -9
- package/lib/baseApp.js +9 -26
- package/lib/eeApp.js +18 -52
- package/{lib/constant.js → module/const/index.js} +3 -0
- package/module/exception/index.js +16 -0
- package/{lib/httpclient.js → module/httpclient/index.js} +45 -11
- package/module/jobs/child/app.js +23 -0
- package/module/jobs/child/forkProcess.js +104 -0
- package/module/jobs/child/index.js +35 -0
- package/module/jobs/index.js +56 -0
- package/module/jobs/renderer/index.js +140 -0
- package/module/jobs/renderer/loadView.js +40 -0
- package/module/loader/index.js +130 -0
- package/module/log/index.js +53 -0
- package/module/log/logger.js +61 -0
- package/module/message/index.js +13 -0
- package/module/message/ipcMain.js +160 -0
- package/module/message/ipcRender.js +0 -0
- package/{lib → module}/socket/ipcServer.js +6 -8
- package/{lib → module}/socket/socketClient.js +4 -3
- package/{lib → module}/socket/socketServer.js +4 -3
- package/module/socket/start.js +22 -0
- package/{lib → module}/storage/index.js +16 -13
- package/module/storage/jsondb/adapters/Base.js +14 -0
- package/module/storage/jsondb/adapters/FileSync.js +32 -0
- package/{lib/storage/lowdb → module/storage/jsondb}/main.js +6 -10
- package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +12 -14
- package/{lib → module}/storage/sqliteStorage.js +7 -11
- package/module/utils/copyto.js +161 -0
- package/module/utils/helper.js +117 -0
- package/module/utils/index.js +120 -0
- package/module/utils/json.js +72 -0
- package/module/utils/ps.js +196 -0
- package/{utils → module/utils}/wrap.js +0 -2
- package/package.json +3 -7
- package/tools/encrypt.js +2 -2
- package/utils/index.js +17 -135
- package/lib/logger.js +0 -47
- package/lib/socket/start.js +0 -22
- package/lib/storage/lowdb/adapters/Base.js +0 -15
- package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
- package/lib/storage/lowdb/adapters/FileSync.js +0 -39
- package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
- package/lib/storage/lowdb/adapters/Memory.js +0 -8
- package/lib/storage/lowdb/adapters/_stringify.js +0 -4
- package/lib/storage/lowdb/common.js +0 -33
- package/lib/storage/lowdb/fp.js +0 -23
- package/lib/storage/lowdb/isPromise.js +0 -6
- package/lib/storage/lowdb/nano.js +0 -5
- package/utils/common.js +0 -91
- /package/{lib → module}/socket/httpServer.js +0 -0
- /package/{lib → module}/socket/io.js +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const Loggers = require('egg-logger').EggLoggers;
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const Ps = require('../utils/ps');
|
|
4
|
+
const Storage = require('../storage');
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 创建
|
|
10
|
+
*/
|
|
11
|
+
create(config = {}) {
|
|
12
|
+
let opt = {};
|
|
13
|
+
|
|
14
|
+
if (Object.keys(config).length == 0) {
|
|
15
|
+
const defaultConfig = {
|
|
16
|
+
logger: {
|
|
17
|
+
type: 'application',
|
|
18
|
+
dir: Ps.getLogDir(),
|
|
19
|
+
encoding: 'utf8',
|
|
20
|
+
env: Ps.env(),
|
|
21
|
+
level: 'INFO',
|
|
22
|
+
consoleLevel: 'INFO',
|
|
23
|
+
disableConsoleAfterReady: !Ps.isDev(),
|
|
24
|
+
outputJSON: false,
|
|
25
|
+
buffer: true,
|
|
26
|
+
appLogName: `ee.log`,
|
|
27
|
+
coreLogName: 'ee-core.log',
|
|
28
|
+
agentLogName: 'ee-agent.log',
|
|
29
|
+
errorLogName: `ee-error.log`,
|
|
30
|
+
coreLogger: {},
|
|
31
|
+
allowDebugAtProd: false,
|
|
32
|
+
enablePerformanceTimer: false,
|
|
33
|
+
},
|
|
34
|
+
customLogger: {}
|
|
35
|
+
}
|
|
36
|
+
const sysConfig = this._getCoreDB().getItem('config');
|
|
37
|
+
opt = Object.assign(defaultConfig, {
|
|
38
|
+
logger: sysConfig.logger,
|
|
39
|
+
customLogger: sysConfig.customLogger || {}
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
opt.logger = config.logger;
|
|
43
|
+
opt.customLogger = config.customLogger;
|
|
44
|
+
}
|
|
45
|
+
//console.log('log---------', config);
|
|
46
|
+
|
|
47
|
+
assert(Object.keys(opt).length != 0, `logger config is null`);
|
|
48
|
+
|
|
49
|
+
const loggers = new Loggers(opt);
|
|
50
|
+
|
|
51
|
+
return loggers;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获取 coredb
|
|
56
|
+
*/
|
|
57
|
+
_getCoreDB() {
|
|
58
|
+
const coreDB = Storage.connection('system');
|
|
59
|
+
return coreDB;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
|
|
2
|
+
class IpcMain {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.event = new EventEmitter();
|
|
5
|
+
this.services = {};
|
|
6
|
+
/* 根据name获取window id */
|
|
7
|
+
ipcMain.handle('MessageChannel.getIdFromName', (e, args) => {
|
|
8
|
+
return (this.services[args.name] || {}).id;
|
|
9
|
+
});
|
|
10
|
+
/* 使用name和window id注册一个服务 */
|
|
11
|
+
ipcMain.handle('MessageChannel.registryService', (e, args) => {
|
|
12
|
+
const { name, id } = args;
|
|
13
|
+
this.registry(name, id);
|
|
14
|
+
return this.services[name];
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* invoke [在主进程(main)中向另外一个服务进程(service)发送异步请求,并取得回调Promise]
|
|
21
|
+
* @param {[String]} name [服务名]
|
|
22
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
23
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
24
|
+
* @return {[Promise]} [回调]
|
|
25
|
+
*/
|
|
26
|
+
invoke (name, channel, args={}) {
|
|
27
|
+
const pid = getRandomString();
|
|
28
|
+
const { id } = this.services[name];
|
|
29
|
+
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
if (name === 'main') reject(new Error(`MessageChannel: the main process can not send a message to itself!`))
|
|
32
|
+
if (!id) reject(new Error(`MessageChannel: can not get the id of the window names ${name}`));
|
|
33
|
+
const win = BrowserWindow.fromId(id);
|
|
34
|
+
if (!win) reject(new Error(`MessageChannel: can not find a window with id: ${id}`));
|
|
35
|
+
win.webContents.send(channel, Object.assign(args, { pid, isFromMain: true }));
|
|
36
|
+
ipcMain.once(pid, function(event, rsp) {
|
|
37
|
+
resolve(rsp);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* handle [在主进程中(main)监听来自其它渲染进程(service/window)的请求,将promiseFunc执行的结果返回]
|
|
45
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
46
|
+
* @param {[Function]} promiseFunc [此函数执行的结果会被发送到消息发送者]
|
|
47
|
+
* @return {[Promise]} [回调]
|
|
48
|
+
*/
|
|
49
|
+
handle(channel, promiseFunc) {
|
|
50
|
+
if (!promiseFunc instanceof Function) throw new Error('MessageChannel: promiseFunc must be a function!');
|
|
51
|
+
ipcMain.handle(channel, (event, ...args) => {
|
|
52
|
+
return promiseFunc(event, ...args).then((result) => {
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* handle [在主进程中(main)监听一次来自其它渲染进程(service/window)的请求,将promiseFunc执行的结果返回]
|
|
61
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
62
|
+
* @param {[Function]} promiseFunc [此函数执行的结果会被发送到消息发送者]
|
|
63
|
+
* @return {[Promise]} [回调]
|
|
64
|
+
*/
|
|
65
|
+
handleOnce(channel, promiseFunc) {
|
|
66
|
+
if (!promiseFunc instanceof Function) throw new Error('MessageChannel: promiseFunc must be a function!');
|
|
67
|
+
ipcMain.handleOnce(channel, (event, ...args) => {
|
|
68
|
+
return promiseFunc(event, ...args).then(result => {
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* send [在主进程(main)向另外一个服务进程(service)发送异步请求,不可立即取得值,请配合on监听信号使用]
|
|
77
|
+
* @param {[String]} name [服务名]
|
|
78
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
79
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
80
|
+
*/
|
|
81
|
+
send(name, channel, args={}) {
|
|
82
|
+
const id = (this.services[name] || {}).id;
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if (!id) throw new Error(`MessageChannel: can not get the id of the window names ${name}`);
|
|
86
|
+
const win = BrowserWindow.fromId(id);
|
|
87
|
+
if (!win) throw new Error(`MessageChannel: can not find a window with id: ${id}`);
|
|
88
|
+
|
|
89
|
+
win.webContents.send(channel, args);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* send [在主进程中(main)向指定某个id的渲染进程窗口(service/window)发送请求,不可立即取得值,请配合on监听信号使用]
|
|
94
|
+
* @param {[String]} id [window id]
|
|
95
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
96
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
97
|
+
*/
|
|
98
|
+
sendTo(id, channel, args) {
|
|
99
|
+
|
|
100
|
+
if (!BrowserWindow.fromId(id)) throw new Error(`MessageChannel: can not find a window with id:${id}!`);
|
|
101
|
+
BrowserWindow.fromId(id).webContents.send(channel, args);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* on [在主进程中(main)监听来自其它渲染进程(service/window)的请求]
|
|
106
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
107
|
+
* @param {[Function]} func [消息到达后,此函数会被触发,同于原生ipcRenderer.on]
|
|
108
|
+
*/
|
|
109
|
+
on(channel, func) {
|
|
110
|
+
if (!func instanceof Function) throw new Error('MessageChannel: func must be a function!');
|
|
111
|
+
ipcMain.on(channel, (event, ...args) => {
|
|
112
|
+
|
|
113
|
+
func(event, ...args);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* once [在主进程中(main)监听一次来自其它渲染进程(service/window)的请求]
|
|
119
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
120
|
+
* @param {[Function]} func [消息到达后,此函数会被触发,同于原生ipcRenderer.on]
|
|
121
|
+
*/
|
|
122
|
+
once(channel, func) {
|
|
123
|
+
if (!func instanceof Function) throw new Error('MessageChannel: func must be a function!');
|
|
124
|
+
ipcMain.once(channel, (event, ...args) => {
|
|
125
|
+
|
|
126
|
+
func(event, ...args);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* registry [注册BrowserWindow和BrowserService]
|
|
132
|
+
* @param {[String]} name [唯一的名字]
|
|
133
|
+
* @param {[String]} id [window id]
|
|
134
|
+
* @param {[String]} pid [process id]
|
|
135
|
+
*/
|
|
136
|
+
registry(name, id, pid) {
|
|
137
|
+
if (name === 'main') throw new Error(`MessageChannel: you can not registry a service named:${name}, it's reserved for the main process!`)
|
|
138
|
+
if (this.services[name]) console.warn(`MessageChannel: the service - ${name} has been registeried!`)
|
|
139
|
+
this.services[name] = { name, id, pid };
|
|
140
|
+
this.event.emit('registry', this.services[name]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* unregistry [注册BrowserWindow和BrowserService]
|
|
145
|
+
* @param {[String]} name [唯一的名字]
|
|
146
|
+
* @param {[String]} id [window id]
|
|
147
|
+
* @param {[String]} pid [process id]
|
|
148
|
+
*/
|
|
149
|
+
unregistry(name) {
|
|
150
|
+
if (name === 'main') throw new Error(`MessageChannel: you can not unregistry a service named:${name}, it's reserved for the main process!`);
|
|
151
|
+
if (this.services[name]) console.warn(`MessageChannel: the service - ${name} will be unregisteried!`);
|
|
152
|
+
if (this.services[name]) {
|
|
153
|
+
this.event.emit('unregistry', this.services[name]);
|
|
154
|
+
delete this.services[name];
|
|
155
|
+
} else {
|
|
156
|
+
console.warn(`MessageChannel: unregistry -> the service - ${name} is not found!`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
File without changes
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const debug = require('debug')('ee-core:ipcServer');
|
|
4
2
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
5
3
|
const is = require('is-type-of');
|
|
@@ -7,8 +5,8 @@ const { ipcMain } = require('electron');
|
|
|
7
5
|
const path = require('path');
|
|
8
6
|
const fs = require('fs');
|
|
9
7
|
const globby = require('globby');
|
|
10
|
-
const
|
|
11
|
-
const
|
|
8
|
+
const Utils = require('../../core/lib/utils');
|
|
9
|
+
const Wrap = require('../utils/wrap');
|
|
12
10
|
|
|
13
11
|
class IpcServer {
|
|
14
12
|
constructor (app) {
|
|
@@ -24,7 +22,7 @@ class IpcServer {
|
|
|
24
22
|
|
|
25
23
|
const self = this;
|
|
26
24
|
// 遍历方法
|
|
27
|
-
const files =
|
|
25
|
+
const files = Utils.filePatterns();
|
|
28
26
|
const directory = path.join(this.app.config.baseDir, 'controller');
|
|
29
27
|
const filepaths = globby.sync(files, { cwd: directory });
|
|
30
28
|
|
|
@@ -32,13 +30,13 @@ class IpcServer {
|
|
|
32
30
|
const fullpath = path.join(directory, filepath);
|
|
33
31
|
if (!fs.statSync(fullpath).isFile()) continue;
|
|
34
32
|
|
|
35
|
-
const properties =
|
|
33
|
+
const properties = Wrap.getProperties(filepath, {caseStyle: 'lower'});
|
|
36
34
|
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
37
35
|
|
|
38
|
-
let fileObj =
|
|
36
|
+
let fileObj = Utils.loadFile(fullpath);
|
|
39
37
|
const fns = {};
|
|
40
38
|
// 为了统一,仅支持class文件
|
|
41
|
-
if (is.class(fileObj) ||
|
|
39
|
+
if (is.class(fileObj) || Utils.isBytecodeClass(fileObj)) {
|
|
42
40
|
let proto = fileObj.prototype;
|
|
43
41
|
// 不遍历父类的方法
|
|
44
42
|
//while (proto !== Object.prototype) {
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const IoClient = require('socket.io-client');
|
|
5
|
-
const
|
|
5
|
+
const Constants = require('../const');
|
|
6
6
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
|
+
const Storage = require('../storage');
|
|
7
8
|
|
|
8
9
|
class SocketClient {
|
|
9
10
|
constructor (port) {
|
|
@@ -29,7 +30,7 @@ class SocketClient {
|
|
|
29
30
|
call (method = '', ...params) {
|
|
30
31
|
return new Promise((resolve, reject) => {
|
|
31
32
|
// 获取通信频道
|
|
32
|
-
const channel =
|
|
33
|
+
const channel = Constants.socketIo.channel.partySoftware;
|
|
33
34
|
this.client.emit(channel, { cmd: method, params: params }, (response) => {
|
|
34
35
|
resolve(response);
|
|
35
36
|
});
|
|
@@ -37,7 +38,7 @@ class SocketClient {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
getCoreDB () {
|
|
40
|
-
const coreDB =
|
|
41
|
+
const coreDB = Storage.connection('system');
|
|
41
42
|
return coreDB;
|
|
42
43
|
}
|
|
43
44
|
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const { Server } = require('socket.io');
|
|
5
|
-
const constant = require('../constant');
|
|
6
5
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
6
|
const is = require('is-type-of');
|
|
7
|
+
const Storage = require('../storage');
|
|
8
|
+
const Constants = require('../const');
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* socket server
|
|
@@ -33,7 +34,7 @@ class SocketServer {
|
|
|
33
34
|
const self = this;
|
|
34
35
|
this.consoleLogger.info('[ee-core:socket:server] connection .....');
|
|
35
36
|
this.io.on('connection', (socket) => {
|
|
36
|
-
const channel =
|
|
37
|
+
const channel = Constants.socketIo.channel.partySoftware;
|
|
37
38
|
socket.on(channel, async (message, callback) => {
|
|
38
39
|
self.consoleLogger.info('[ee-core:socket:server] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
39
40
|
|
|
@@ -63,7 +64,7 @@ class SocketServer {
|
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
getCoreDB () {
|
|
66
|
-
const coreDB =
|
|
67
|
+
const coreDB = Storage.connection('system');
|
|
67
68
|
return coreDB;
|
|
68
69
|
}
|
|
69
70
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SocketServer = require('./socketServer');
|
|
4
|
+
const IpcServer = require('./ipcServer');
|
|
5
|
+
const HttpServer = require('./httpServer');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* server
|
|
9
|
+
*/
|
|
10
|
+
module.exports = (app) => {
|
|
11
|
+
|
|
12
|
+
// 启动 socket server
|
|
13
|
+
new SocketServer(app);
|
|
14
|
+
|
|
15
|
+
// 启动 http server
|
|
16
|
+
new HttpServer(app);
|
|
17
|
+
|
|
18
|
+
// 启动 electron ipc server
|
|
19
|
+
new IpcServer(app);
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const assert = require('assert');
|
|
4
2
|
const _ = require('lodash');
|
|
5
|
-
const
|
|
3
|
+
const DB = {};
|
|
4
|
+
|
|
5
|
+
DB.connection = function (database, options = {}) {
|
|
6
|
+
let driver = options.driver || 'jsondb';
|
|
7
|
+
|
|
8
|
+
// 兼容之前api
|
|
9
|
+
driver = driver == 'lowdb' ? 'jsondb' : driver;
|
|
6
10
|
|
|
7
|
-
JsonDB.connection = function (database, options = {}) {
|
|
8
|
-
let driver = options.driver || 'lowdb';
|
|
9
11
|
let opt = options.default || {};
|
|
10
|
-
if (!_.includes(['
|
|
12
|
+
if (!_.includes(['jsondb', 'sqlite'], driver)) {
|
|
11
13
|
assert(database, `db driver ${driver} is not supported`);
|
|
12
14
|
}
|
|
13
15
|
if (_.isEmpty(database)) {
|
|
@@ -15,20 +17,21 @@ JsonDB.connection = function (database, options = {}) {
|
|
|
15
17
|
}
|
|
16
18
|
let storage;
|
|
17
19
|
switch (driver) {
|
|
18
|
-
case '
|
|
19
|
-
const
|
|
20
|
-
storage = new
|
|
20
|
+
case 'jsondb':
|
|
21
|
+
const JsondbStorage = require('./jsondbStorage');
|
|
22
|
+
storage = new JsondbStorage(database);
|
|
21
23
|
break;
|
|
22
24
|
case 'sqlite':
|
|
23
25
|
const SqliteStorage = require('./sqliteStorage');
|
|
24
26
|
storage = new SqliteStorage(database, opt);
|
|
25
27
|
break;
|
|
26
|
-
default:
|
|
28
|
+
default:
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
return storage;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
// 兼容之前的api
|
|
35
|
+
DB.JsonDB = DB;
|
|
36
|
+
|
|
37
|
+
module.exports = DB;
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
_stringify(obj) {
|
|
10
|
+
return JSON.stringify(obj, null, 2)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = Base
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const Base = require('./Base')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
|
|
4
|
+
class FileSync extends Base {
|
|
5
|
+
|
|
6
|
+
read() {
|
|
7
|
+
if (fs.existsSync(this.source)) {
|
|
8
|
+
// Read database
|
|
9
|
+
try {
|
|
10
|
+
const data = fs.readFileSync(this.source, {encoding: 'utf8'}).trim()
|
|
11
|
+
|
|
12
|
+
// Handle blank file
|
|
13
|
+
return data ? this.deserialize(data) : this.defaultValue
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (e instanceof SyntaxError) {
|
|
16
|
+
e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
|
|
17
|
+
}
|
|
18
|
+
throw e
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
// Initialize
|
|
22
|
+
fs.writeFileSync(this.source, this.serialize(this.defaultValue), {flag:'w+'})
|
|
23
|
+
return this.defaultValue
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
write(data) {
|
|
28
|
+
return fs.writeFileSync(this.source, this.serialize(data), {flag:'w+'})
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = FileSync
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
const lodash = require('lodash')
|
|
2
|
-
const
|
|
1
|
+
const lodash = require('lodash');
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const is = require('is-type-of');
|
|
3
4
|
|
|
4
5
|
module.exports = function(adapter) {
|
|
5
|
-
|
|
6
|
-
throw new Error(
|
|
7
|
-
'An adapter must be provided, see https://github.com/typicode/lowdb/#usage'
|
|
8
|
-
)
|
|
9
|
-
}
|
|
6
|
+
assert(typeof adapter === 'object', 'An adapter must be provided');
|
|
10
7
|
|
|
11
8
|
// Create a fresh copy of lodash
|
|
12
9
|
const _ = lodash.runInContext()
|
|
@@ -24,18 +21,17 @@ module.exports = function(adapter) {
|
|
|
24
21
|
return db
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
// Lowdb API
|
|
28
24
|
// Expose _ for mixins
|
|
29
25
|
db._ = _
|
|
30
26
|
|
|
31
27
|
db.read = () => {
|
|
32
28
|
const r = adapter.read()
|
|
33
|
-
return
|
|
29
|
+
return is.promise(r) ? r.then(plant) : plant(r)
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
db.write = returnValue => {
|
|
37
33
|
const w = adapter.write(db.getState())
|
|
38
|
-
return
|
|
34
|
+
return is.promise(w) ? w.then(() => returnValue) : returnValue
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
db.getState = () => db.__wrapped__
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const assert = require('assert');
|
|
4
2
|
const fs = require('fs');
|
|
5
3
|
const path = require('path');
|
|
6
|
-
const
|
|
7
|
-
const FileSync = require('./
|
|
4
|
+
const Jsondb = require('./jsondb/main');
|
|
5
|
+
const FileSync = require('./jsondb/adapters/FileSync');
|
|
8
6
|
const _ = require('lodash');
|
|
9
|
-
const
|
|
10
|
-
const
|
|
7
|
+
const Constants = require('../const');
|
|
8
|
+
const Helper = require('../utils/helper');
|
|
9
|
+
const Ps = require('../utils/ps');
|
|
11
10
|
|
|
12
|
-
class
|
|
11
|
+
class JsondbStorage {
|
|
13
12
|
constructor (name, opt = {}) {
|
|
14
13
|
assert(name, `db name ${name} Cannot be empty`);
|
|
15
14
|
|
|
16
15
|
this.name = name;
|
|
17
16
|
|
|
18
17
|
// 数据库key列表
|
|
19
|
-
this.storageKey =
|
|
18
|
+
this.storageKey = Constants.storageKey;
|
|
20
19
|
|
|
21
|
-
const storageDir =
|
|
20
|
+
const storageDir = Ps.getStorageDir();
|
|
22
21
|
if (!fs.existsSync(storageDir)) {
|
|
23
|
-
|
|
24
|
-
utilsCommon.chmodPath(storageDir, '777');
|
|
22
|
+
Helper.mkdir(storageDir);
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
this.db = this.table(name);
|
|
@@ -35,7 +33,7 @@ class LowdbStorage {
|
|
|
35
33
|
|
|
36
34
|
const dbFile = this.getFilePath(name);
|
|
37
35
|
const adapter = new FileSync(dbFile);
|
|
38
|
-
const db =
|
|
36
|
+
const db = Jsondb(adapter);
|
|
39
37
|
|
|
40
38
|
assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
|
|
41
39
|
|
|
@@ -53,7 +51,7 @@ class LowdbStorage {
|
|
|
53
51
|
* 获取文件绝对路径
|
|
54
52
|
*/
|
|
55
53
|
getFilePath (name) {
|
|
56
|
-
const storageDir =
|
|
54
|
+
const storageDir = Ps.getStorageDir();
|
|
57
55
|
const dbFile = path.join(storageDir, this.getFileName(name));
|
|
58
56
|
return dbFile;
|
|
59
57
|
}
|
|
@@ -96,4 +94,4 @@ class LowdbStorage {
|
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
module.exports =
|
|
97
|
+
module.exports = JsondbStorage;
|
|
@@ -4,7 +4,8 @@ const assert = require('assert');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const Database = require('better-sqlite3');
|
|
7
|
-
const
|
|
7
|
+
const Helper = require('../utils/helper');
|
|
8
|
+
const Ps = require('../utils/ps');
|
|
8
9
|
|
|
9
10
|
class SqliteStorage {
|
|
10
11
|
constructor (name, opt = {}) {
|
|
@@ -12,13 +13,8 @@ class SqliteStorage {
|
|
|
12
13
|
|
|
13
14
|
this.name = name;
|
|
14
15
|
this.mode = this.getMode(name);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.storageDir = storageDir;
|
|
18
|
-
|
|
19
|
-
const fileName = this._formatFileName(name);
|
|
20
|
-
this.fileName = fileName;
|
|
21
|
-
|
|
16
|
+
this.storageDir = this._createStorageDir();
|
|
17
|
+
this.fileName = this._formatFileName(name);
|
|
22
18
|
this.db = this._initDB(opt);
|
|
23
19
|
}
|
|
24
20
|
|
|
@@ -62,15 +58,15 @@ class SqliteStorage {
|
|
|
62
58
|
* 创建storage目录
|
|
63
59
|
*/
|
|
64
60
|
_createStorageDir () {
|
|
65
|
-
let storageDir =
|
|
61
|
+
let storageDir = Ps.getStorageDir();
|
|
66
62
|
|
|
67
63
|
if (this.mode == 'absolute') {
|
|
68
64
|
storageDir = path.dirname(this.name);
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
if (!fs.existsSync(storageDir)) {
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
Helper.mkdir(storageDir);
|
|
69
|
+
Helper.chmodPath(storageDir, '777');
|
|
74
70
|
}
|
|
75
71
|
|
|
76
72
|
return storageDir;
|