ee-core 1.2.8 → 1.2.9
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 +2 -2
- package/bin/tools.js +22 -22
- package/config/config.default.js +262 -250
- package/core/index.js +12 -12
- package/core/lib/ee.js +209 -209
- package/core/lib/loader/context_loader.js +105 -105
- package/core/lib/loader/ee_loader.js +447 -451
- package/core/lib/loader/file_loader.js +262 -262
- package/core/lib/loader/mixin/config.js +138 -138
- package/core/lib/loader/mixin/controller.js +123 -123
- package/core/lib/loader/mixin/service.js +29 -29
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/index.js +100 -100
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/index.js +49 -49
- package/lib/appLoader.js +45 -45
- package/lib/application.js +81 -80
- package/lib/baseApp.js +133 -118
- package/lib/constant.js +28 -28
- package/lib/eeApp.js +321 -326
- package/lib/helper.js +51 -51
- package/lib/httpclient.js +136 -136
- package/lib/logger.js +46 -46
- package/lib/socket/httpServer.js +134 -102
- package/lib/socket/io.js +23 -23
- package/lib/socket/ipcServer.js +128 -128
- package/lib/socket/socketClient.js +50 -50
- package/lib/socket/socketServer.js +76 -76
- package/lib/socket/start.js +22 -22
- package/lib/storage/index.js +33 -33
- package/lib/storage/lowdbStorage.js +98 -98
- package/lib/storage/sqliteStorage.js +58 -58
- package/package.json +45 -45
- package/tools/codeCompress.js +204 -204
- package/tools/replaceDist.js +76 -76
- package/utils/common.js +90 -90
- package/utils/index.js +151 -207
- package/utils/wrap.js +37 -37
- package/resource/images/loding.gif +0 -0
- package/resource/images/tray_logo.png +0 -0
- package/resource/loading.html +0 -22
- package/resource/view_example.html +0 -22
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const { Server } = require('socket.io');
|
|
5
|
-
const constant = require('../constant');
|
|
6
|
-
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
|
-
const is = require('is-type-of');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* socket server
|
|
11
|
-
*/
|
|
12
|
-
class SocketServer {
|
|
13
|
-
constructor (app) {
|
|
14
|
-
this.app = app;
|
|
15
|
-
const options = this.app.config.socketServer;
|
|
16
|
-
this.consoleLogger = new EggConsoleLogger();
|
|
17
|
-
|
|
18
|
-
if (!options.enable) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketPort());
|
|
23
|
-
assert(typeof port === 'number', 'socekt port required, and must be a number');
|
|
24
|
-
this.consoleLogger.info('[ee-core:socket:server] socket server port is:', port);
|
|
25
|
-
|
|
26
|
-
// let opt = Object.assign({}, options);
|
|
27
|
-
// delete opt.enable;
|
|
28
|
-
this.io = new Server(port, options);
|
|
29
|
-
this.connec();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
connec () {
|
|
33
|
-
const self = this;
|
|
34
|
-
this.consoleLogger.info('[ee-core:socket:server] connection .....');
|
|
35
|
-
this.io.on('connection', (socket) => {
|
|
36
|
-
const channel = constant.socketIo.channel.partySoftware;
|
|
37
|
-
socket.on(channel, async (message, callback) => {
|
|
38
|
-
self.consoleLogger.info('[ee-core:socket:server] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
// 找函数
|
|
42
|
-
const cmd = message.cmd;
|
|
43
|
-
const args = message.params;
|
|
44
|
-
let fn = null;
|
|
45
|
-
if (is.string(cmd)) {
|
|
46
|
-
const actions = cmd.split('.');
|
|
47
|
-
let obj = self.app;
|
|
48
|
-
actions.forEach(key => {
|
|
49
|
-
obj = obj[key];
|
|
50
|
-
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
51
|
-
});
|
|
52
|
-
fn = obj;
|
|
53
|
-
}
|
|
54
|
-
if (!fn) throw new Error('function not exists');
|
|
55
|
-
|
|
56
|
-
const result = await fn.call(self.app, args);
|
|
57
|
-
callback(result);
|
|
58
|
-
} catch (err) {
|
|
59
|
-
self.app.console.error('[ee-core:socket:server] throw error:', err);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getCoreDB () {
|
|
66
|
-
const coreDB = require('../storage/index').JsonDB.connection('system');
|
|
67
|
-
return coreDB;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
getSocketPort () {
|
|
71
|
-
const cdb = this.getCoreDB();
|
|
72
|
-
const port = cdb.getItem('config').socketServer.port;
|
|
73
|
-
return port;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const { Server } = require('socket.io');
|
|
5
|
+
const constant = require('../constant');
|
|
6
|
+
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
|
+
const is = require('is-type-of');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* socket server
|
|
11
|
+
*/
|
|
12
|
+
class SocketServer {
|
|
13
|
+
constructor (app) {
|
|
14
|
+
this.app = app;
|
|
15
|
+
const options = this.app.config.socketServer;
|
|
16
|
+
this.consoleLogger = new EggConsoleLogger();
|
|
17
|
+
|
|
18
|
+
if (!options.enable) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketPort());
|
|
23
|
+
assert(typeof port === 'number', 'socekt port required, and must be a number');
|
|
24
|
+
this.consoleLogger.info('[ee-core:socket:server] socket server port is:', port);
|
|
25
|
+
|
|
26
|
+
// let opt = Object.assign({}, options);
|
|
27
|
+
// delete opt.enable;
|
|
28
|
+
this.io = new Server(port, options);
|
|
29
|
+
this.connec();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
connec () {
|
|
33
|
+
const self = this;
|
|
34
|
+
this.consoleLogger.info('[ee-core:socket:server] connection .....');
|
|
35
|
+
this.io.on('connection', (socket) => {
|
|
36
|
+
const channel = constant.socketIo.channel.partySoftware;
|
|
37
|
+
socket.on(channel, async (message, callback) => {
|
|
38
|
+
self.consoleLogger.info('[ee-core:socket:server] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
// 找函数
|
|
42
|
+
const cmd = message.cmd;
|
|
43
|
+
const args = message.params;
|
|
44
|
+
let fn = null;
|
|
45
|
+
if (is.string(cmd)) {
|
|
46
|
+
const actions = cmd.split('.');
|
|
47
|
+
let obj = self.app;
|
|
48
|
+
actions.forEach(key => {
|
|
49
|
+
obj = obj[key];
|
|
50
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
51
|
+
});
|
|
52
|
+
fn = obj;
|
|
53
|
+
}
|
|
54
|
+
if (!fn) throw new Error('function not exists');
|
|
55
|
+
|
|
56
|
+
const result = await fn.call(self.app, args);
|
|
57
|
+
callback(result);
|
|
58
|
+
} catch (err) {
|
|
59
|
+
self.app.console.error('[ee-core:socket:server] throw error:', err);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getCoreDB () {
|
|
66
|
+
const coreDB = require('../storage/index').JsonDB.connection('system');
|
|
67
|
+
return coreDB;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getSocketPort () {
|
|
71
|
+
const cdb = this.getCoreDB();
|
|
72
|
+
const port = cdb.getItem('config').socketServer.port;
|
|
73
|
+
return port;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
77
|
module.exports = SocketServer;
|
package/lib/socket/start.js
CHANGED
|
@@ -1,22 +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
|
+
'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
|
+
|
package/lib/storage/index.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const _ = require('lodash');
|
|
5
|
-
const JsonDB = {};
|
|
6
|
-
|
|
7
|
-
JsonDB.connection = function (database, options = {}) {
|
|
8
|
-
let driver = options.driver || 'lowdb';
|
|
9
|
-
let opt = options.default || {};
|
|
10
|
-
if (!_.includes(['lowdb', 'sqlite'], driver)) {
|
|
11
|
-
assert(database, `db driver ${driver} is not supported`);
|
|
12
|
-
}
|
|
13
|
-
if (_.isEmpty(database)) {
|
|
14
|
-
assert(database, `db name ${database} Cannot be empty`);
|
|
15
|
-
}
|
|
16
|
-
let storage;
|
|
17
|
-
switch (driver) {
|
|
18
|
-
case 'lowdb':
|
|
19
|
-
const LowdbStorage = require('./lowdbStorage');
|
|
20
|
-
storage = new LowdbStorage(database);
|
|
21
|
-
break;
|
|
22
|
-
case 'sqlite':
|
|
23
|
-
const SqliteStorage = require('./sqliteStorage');
|
|
24
|
-
storage = new SqliteStorage(database, opt);
|
|
25
|
-
break;
|
|
26
|
-
default:
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return storage;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
module.exports = {
|
|
33
|
-
JsonDB
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const _ = require('lodash');
|
|
5
|
+
const JsonDB = {};
|
|
6
|
+
|
|
7
|
+
JsonDB.connection = function (database, options = {}) {
|
|
8
|
+
let driver = options.driver || 'lowdb';
|
|
9
|
+
let opt = options.default || {};
|
|
10
|
+
if (!_.includes(['lowdb', 'sqlite'], driver)) {
|
|
11
|
+
assert(database, `db driver ${driver} is not supported`);
|
|
12
|
+
}
|
|
13
|
+
if (_.isEmpty(database)) {
|
|
14
|
+
assert(database, `db name ${database} Cannot be empty`);
|
|
15
|
+
}
|
|
16
|
+
let storage;
|
|
17
|
+
switch (driver) {
|
|
18
|
+
case 'lowdb':
|
|
19
|
+
const LowdbStorage = require('./lowdbStorage');
|
|
20
|
+
storage = new LowdbStorage(database);
|
|
21
|
+
break;
|
|
22
|
+
case 'sqlite':
|
|
23
|
+
const SqliteStorage = require('./sqliteStorage');
|
|
24
|
+
storage = new SqliteStorage(database, opt);
|
|
25
|
+
break;
|
|
26
|
+
default:
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return storage;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = {
|
|
33
|
+
JsonDB
|
|
34
34
|
};
|
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const lowdb = require('lowdb');
|
|
7
|
-
const FileSync = require('lowdb/adapters/FileSync');
|
|
8
|
-
const _ = require('lodash');
|
|
9
|
-
const constant = require('../constant');
|
|
10
|
-
const utilsCommon = require('../../utils/common');
|
|
11
|
-
|
|
12
|
-
class LowdbStorage {
|
|
13
|
-
constructor (name, opt = {}) {
|
|
14
|
-
assert(name, `db name ${name} Cannot be empty`);
|
|
15
|
-
|
|
16
|
-
this.name = name;
|
|
17
|
-
|
|
18
|
-
// 数据库key列表
|
|
19
|
-
this.storageKey = constant.storageKey;
|
|
20
|
-
|
|
21
|
-
const storageDir = utilsCommon.getStorageDir();
|
|
22
|
-
if (!fs.existsSync(storageDir)) {
|
|
23
|
-
utilsCommon.mkdir(storageDir);
|
|
24
|
-
utilsCommon.chmodPath(storageDir, '777');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.db = this.table(name);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 创建 table
|
|
32
|
-
*/
|
|
33
|
-
table (name) {
|
|
34
|
-
assert(name, 'table name is required');
|
|
35
|
-
|
|
36
|
-
const dbFile = this.getFilePath(name);
|
|
37
|
-
const adapter = new FileSync(dbFile);
|
|
38
|
-
const db = lowdb(adapter);
|
|
39
|
-
|
|
40
|
-
assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
|
|
41
|
-
|
|
42
|
-
return db;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 获取db文件名
|
|
47
|
-
*/
|
|
48
|
-
getFileName (name) {
|
|
49
|
-
return name + ".json";
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 获取文件绝对路径
|
|
54
|
-
*/
|
|
55
|
-
getFilePath (name) {
|
|
56
|
-
const storageDir = utilsCommon.getStorageDir();
|
|
57
|
-
const dbFile = path.join(storageDir, this.getFileName(name));
|
|
58
|
-
return dbFile;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 为指定的 name 设置一个对应的值
|
|
63
|
-
*/
|
|
64
|
-
setItem (key, value) {
|
|
65
|
-
assert(_.isString(key), `key must be a string`);
|
|
66
|
-
assert(key.length != 0, `key cannot be empty`);
|
|
67
|
-
assert(!this.storageKey.hasOwnProperty(key), `${key} is not allowed`);
|
|
68
|
-
|
|
69
|
-
let cacheKey = this.storageKey.cache;
|
|
70
|
-
if (!this.db.has(cacheKey).value()) {
|
|
71
|
-
this.db.set(cacheKey, {}).write();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let keyId = cacheKey + "." + key;
|
|
75
|
-
this.db
|
|
76
|
-
.set(keyId, value)
|
|
77
|
-
.write();
|
|
78
|
-
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 根据指定的名字 name 获取对应的值
|
|
84
|
-
*/
|
|
85
|
-
getItem (key) {
|
|
86
|
-
assert(_.isString(key), `key must be a string`);
|
|
87
|
-
assert(key.length != 0, `key cannot be empty`);
|
|
88
|
-
|
|
89
|
-
let cacheKey = this.storageKey.cache;
|
|
90
|
-
let keyId = cacheKey + "." + key;
|
|
91
|
-
const data = this.db
|
|
92
|
-
.get(keyId)
|
|
93
|
-
.value();
|
|
94
|
-
|
|
95
|
-
return data;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const lowdb = require('lowdb');
|
|
7
|
+
const FileSync = require('lowdb/adapters/FileSync');
|
|
8
|
+
const _ = require('lodash');
|
|
9
|
+
const constant = require('../constant');
|
|
10
|
+
const utilsCommon = require('../../utils/common');
|
|
11
|
+
|
|
12
|
+
class LowdbStorage {
|
|
13
|
+
constructor (name, opt = {}) {
|
|
14
|
+
assert(name, `db name ${name} Cannot be empty`);
|
|
15
|
+
|
|
16
|
+
this.name = name;
|
|
17
|
+
|
|
18
|
+
// 数据库key列表
|
|
19
|
+
this.storageKey = constant.storageKey;
|
|
20
|
+
|
|
21
|
+
const storageDir = utilsCommon.getStorageDir();
|
|
22
|
+
if (!fs.existsSync(storageDir)) {
|
|
23
|
+
utilsCommon.mkdir(storageDir);
|
|
24
|
+
utilsCommon.chmodPath(storageDir, '777');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.db = this.table(name);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 创建 table
|
|
32
|
+
*/
|
|
33
|
+
table (name) {
|
|
34
|
+
assert(name, 'table name is required');
|
|
35
|
+
|
|
36
|
+
const dbFile = this.getFilePath(name);
|
|
37
|
+
const adapter = new FileSync(dbFile);
|
|
38
|
+
const db = lowdb(adapter);
|
|
39
|
+
|
|
40
|
+
assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
|
|
41
|
+
|
|
42
|
+
return db;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 获取db文件名
|
|
47
|
+
*/
|
|
48
|
+
getFileName (name) {
|
|
49
|
+
return name + ".json";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 获取文件绝对路径
|
|
54
|
+
*/
|
|
55
|
+
getFilePath (name) {
|
|
56
|
+
const storageDir = utilsCommon.getStorageDir();
|
|
57
|
+
const dbFile = path.join(storageDir, this.getFileName(name));
|
|
58
|
+
return dbFile;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 为指定的 name 设置一个对应的值
|
|
63
|
+
*/
|
|
64
|
+
setItem (key, value) {
|
|
65
|
+
assert(_.isString(key), `key must be a string`);
|
|
66
|
+
assert(key.length != 0, `key cannot be empty`);
|
|
67
|
+
assert(!this.storageKey.hasOwnProperty(key), `${key} is not allowed`);
|
|
68
|
+
|
|
69
|
+
let cacheKey = this.storageKey.cache;
|
|
70
|
+
if (!this.db.has(cacheKey).value()) {
|
|
71
|
+
this.db.set(cacheKey, {}).write();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let keyId = cacheKey + "." + key;
|
|
75
|
+
this.db
|
|
76
|
+
.set(keyId, value)
|
|
77
|
+
.write();
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 根据指定的名字 name 获取对应的值
|
|
84
|
+
*/
|
|
85
|
+
getItem (key) {
|
|
86
|
+
assert(_.isString(key), `key must be a string`);
|
|
87
|
+
assert(key.length != 0, `key cannot be empty`);
|
|
88
|
+
|
|
89
|
+
let cacheKey = this.storageKey.cache;
|
|
90
|
+
let keyId = cacheKey + "." + key;
|
|
91
|
+
const data = this.db
|
|
92
|
+
.get(keyId)
|
|
93
|
+
.value();
|
|
94
|
+
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
99
|
module.exports = LowdbStorage;
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const Database = require('better-sqlite3');
|
|
7
|
-
const utilsCommon = require('../../utils/common');
|
|
8
|
-
|
|
9
|
-
class SqliteStorage {
|
|
10
|
-
constructor (name, opt = {}) {
|
|
11
|
-
assert(name, `db name ${name} Cannot be empty`);
|
|
12
|
-
|
|
13
|
-
this.name = name;
|
|
14
|
-
|
|
15
|
-
const storageDir = utilsCommon.getStorageDir();
|
|
16
|
-
if (!fs.existsSync(storageDir)) {
|
|
17
|
-
utilsCommon.mkdir(storageDir);
|
|
18
|
-
utilsCommon.chmodPath(storageDir, '777');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
this.db = this.initDB(name, opt);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 初始化db
|
|
26
|
-
*/
|
|
27
|
-
initDB (name, opt = {}) {
|
|
28
|
-
let options = Object.assign({
|
|
29
|
-
timeout: 5000,
|
|
30
|
-
}, opt);
|
|
31
|
-
|
|
32
|
-
// 存储类型:db文件、内存(:memory:)
|
|
33
|
-
let isFileDB = false;
|
|
34
|
-
if (path.extname(name) == '.db') {
|
|
35
|
-
name = this.getFilePath(name);
|
|
36
|
-
isFileDB = true;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const db = new Database(name, options);
|
|
40
|
-
|
|
41
|
-
// 如果是文件类型,判断文件是否创建成功
|
|
42
|
-
if (isFileDB) {
|
|
43
|
-
assert(fs.existsSync(name), `error: storage ${name} not exists`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return db;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 获取文件绝对路径
|
|
51
|
-
*/
|
|
52
|
-
getFilePath (name) {
|
|
53
|
-
const storageDir = utilsCommon.getStorageDir();
|
|
54
|
-
const dbFile = path.join(storageDir, name);
|
|
55
|
-
return dbFile;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const Database = require('better-sqlite3');
|
|
7
|
+
const utilsCommon = require('../../utils/common');
|
|
8
|
+
|
|
9
|
+
class SqliteStorage {
|
|
10
|
+
constructor (name, opt = {}) {
|
|
11
|
+
assert(name, `db name ${name} Cannot be empty`);
|
|
12
|
+
|
|
13
|
+
this.name = name;
|
|
14
|
+
|
|
15
|
+
const storageDir = utilsCommon.getStorageDir();
|
|
16
|
+
if (!fs.existsSync(storageDir)) {
|
|
17
|
+
utilsCommon.mkdir(storageDir);
|
|
18
|
+
utilsCommon.chmodPath(storageDir, '777');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.db = this.initDB(name, opt);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 初始化db
|
|
26
|
+
*/
|
|
27
|
+
initDB (name, opt = {}) {
|
|
28
|
+
let options = Object.assign({
|
|
29
|
+
timeout: 5000,
|
|
30
|
+
}, opt);
|
|
31
|
+
|
|
32
|
+
// 存储类型:db文件、内存(:memory:)
|
|
33
|
+
let isFileDB = false;
|
|
34
|
+
if (path.extname(name) == '.db') {
|
|
35
|
+
name = this.getFilePath(name);
|
|
36
|
+
isFileDB = true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const db = new Database(name, options);
|
|
40
|
+
|
|
41
|
+
// 如果是文件类型,判断文件是否创建成功
|
|
42
|
+
if (isFileDB) {
|
|
43
|
+
assert(fs.existsSync(name), `error: storage ${name} not exists`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return db;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 获取文件绝对路径
|
|
51
|
+
*/
|
|
52
|
+
getFilePath (name) {
|
|
53
|
+
const storageDir = utilsCommon.getStorageDir();
|
|
54
|
+
const dbFile = path.join(storageDir, name);
|
|
55
|
+
return dbFile;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
59
|
module.exports = SqliteStorage;
|
package/package.json
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ee-core",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "ee core",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"author": "",
|
|
10
|
-
"license": "ISC",
|
|
11
|
-
"bin": {
|
|
12
|
-
"ee-core": "./bin/tools.js"
|
|
13
|
-
},
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"agentkeepalive": "^4.2.0",
|
|
16
|
-
"better-sqlite3": "^7.6.0",
|
|
17
|
-
"co": "^4.6.0",
|
|
18
|
-
"debug": "^4.3.3",
|
|
19
|
-
"depd": "^2.0.0",
|
|
20
|
-
"egg-errors": "^2.3.0",
|
|
21
|
-
"egg-logger": "^2.7.1",
|
|
22
|
-
"electron-is": "^3.0.0",
|
|
23
|
-
"electron-updater": "^4.6.1",
|
|
24
|
-
"extend2": "^1.0.1",
|
|
25
|
-
"fs-extra": "^10.0.0",
|
|
26
|
-
"get-port": "^5.1.1",
|
|
27
|
-
"globby": "^10.0.0",
|
|
28
|
-
"humanize-ms": "^1.2.1",
|
|
29
|
-
"is-type-of": "^1.2.1",
|
|
30
|
-
"koa": "^2.13.4",
|
|
31
|
-
"koa-body": "^5.0.0",
|
|
32
|
-
"koa-convert": "^2.0.0",
|
|
33
|
-
"koa-static": "^5.0.0",
|
|
34
|
-
"koa2-cors": "^2.0.6",
|
|
35
|
-
"lodash": "^4.17.21",
|
|
36
|
-
"lowdb": "^1.0.0",
|
|
37
|
-
"path-to-regexp": "^6.2.0",
|
|
38
|
-
"socket.io": "^4.4.1",
|
|
39
|
-
"socket.io-client": "^4.4.1",
|
|
40
|
-
"uglify-js": "^3.14.5",
|
|
41
|
-
"urllib": "^2.38.0",
|
|
42
|
-
"utility": "^1.17.0"
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {}
|
|
45
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ee-core",
|
|
3
|
+
"version": "1.2.9",
|
|
4
|
+
"description": "ee core",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"bin": {
|
|
12
|
+
"ee-core": "./bin/tools.js"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"agentkeepalive": "^4.2.0",
|
|
16
|
+
"better-sqlite3": "^7.6.0",
|
|
17
|
+
"co": "^4.6.0",
|
|
18
|
+
"debug": "^4.3.3",
|
|
19
|
+
"depd": "^2.0.0",
|
|
20
|
+
"egg-errors": "^2.3.0",
|
|
21
|
+
"egg-logger": "^2.7.1",
|
|
22
|
+
"electron-is": "^3.0.0",
|
|
23
|
+
"electron-updater": "^4.6.1",
|
|
24
|
+
"extend2": "^1.0.1",
|
|
25
|
+
"fs-extra": "^10.0.0",
|
|
26
|
+
"get-port": "^5.1.1",
|
|
27
|
+
"globby": "^10.0.0",
|
|
28
|
+
"humanize-ms": "^1.2.1",
|
|
29
|
+
"is-type-of": "^1.2.1",
|
|
30
|
+
"koa": "^2.13.4",
|
|
31
|
+
"koa-body": "^5.0.0",
|
|
32
|
+
"koa-convert": "^2.0.0",
|
|
33
|
+
"koa-static": "^5.0.0",
|
|
34
|
+
"koa2-cors": "^2.0.6",
|
|
35
|
+
"lodash": "^4.17.21",
|
|
36
|
+
"lowdb": "^1.0.0",
|
|
37
|
+
"path-to-regexp": "^6.2.0",
|
|
38
|
+
"socket.io": "^4.4.1",
|
|
39
|
+
"socket.io-client": "^4.4.1",
|
|
40
|
+
"uglify-js": "^3.14.5",
|
|
41
|
+
"urllib": "^2.38.0",
|
|
42
|
+
"utility": "^1.17.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {}
|
|
45
|
+
}
|