ee-core 1.5.1 → 1.5.2-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/LICENSE +21 -21
- package/README.md +2 -2
- package/addon/window/index.js +91 -91
- package/bin/tools.js +18 -18
- package/config/config.default.js +280 -280
- package/core/index.js +12 -12
- package/core/lib/ee.js +218 -218
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +457 -457
- package/core/lib/loader/file_loader.js +325 -325
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +135 -135
- package/core/lib/loader/mixin/controller.js +124 -124
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/index.js +127 -127
- 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 +53 -53
- package/lib/application.js +84 -84
- package/lib/baseApp.js +131 -131
- package/lib/constant.js +9 -9
- package/lib/eeApp.js +359 -359
- package/lib/httpclient.js +136 -136
- package/lib/logger.js +46 -46
- package/lib/socket/httpServer.js +142 -142
- package/lib/socket/io.js +23 -23
- package/lib/socket/ipcServer.js +108 -108
- 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/lowdb/adapters/Base.js +15 -0
- package/lib/storage/lowdb/adapters/FileAsync.js +41 -0
- package/lib/storage/lowdb/adapters/FileSync.js +39 -0
- package/lib/storage/lowdb/adapters/LocalStorage.js +20 -0
- package/lib/storage/lowdb/adapters/Memory.js +8 -0
- package/lib/storage/lowdb/adapters/_stringify.js +4 -0
- package/lib/storage/lowdb/common.js +33 -0
- package/lib/storage/lowdb/fp.js +23 -0
- package/lib/storage/lowdb/isPromise.js +6 -0
- package/lib/storage/lowdb/main.js +46 -0
- package/lib/storage/lowdb/nano.js +5 -0
- package/lib/storage/lowdbStorage.js +98 -98
- package/lib/storage/sqliteStorage.js +127 -127
- package/package.json +48 -45
- package/tools/encrypt.js +274 -274
- package/tools/replaceDist.js +61 -61
- package/utils/common.js +90 -90
- package/utils/index.js +246 -246
- package/utils/wrap.js +37 -37
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Not using async/await on purpose to avoid adding regenerator-runtime
|
|
2
|
+
// to lowdb dependencies
|
|
3
|
+
const fs = require('graceful-fs')
|
|
4
|
+
const pify = require('pify')
|
|
5
|
+
const steno = require('steno')
|
|
6
|
+
const Base = require('./Base')
|
|
7
|
+
|
|
8
|
+
const readFile = pify(fs.readFile)
|
|
9
|
+
const writeFile = pify(steno.writeFile)
|
|
10
|
+
|
|
11
|
+
class FileAsync extends Base {
|
|
12
|
+
read() {
|
|
13
|
+
// fs.exists is deprecated but not fs.existsSync
|
|
14
|
+
if (fs.existsSync(this.source)) {
|
|
15
|
+
// Read database
|
|
16
|
+
return readFile(this.source, 'utf-8')
|
|
17
|
+
.then(data => {
|
|
18
|
+
// Handle blank file
|
|
19
|
+
const trimmed = data.trim()
|
|
20
|
+
return trimmed ? this.deserialize(trimmed) : this.defaultValue
|
|
21
|
+
})
|
|
22
|
+
.catch(e => {
|
|
23
|
+
if (e instanceof SyntaxError) {
|
|
24
|
+
e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
|
|
25
|
+
}
|
|
26
|
+
throw e
|
|
27
|
+
})
|
|
28
|
+
} else {
|
|
29
|
+
// Initialize
|
|
30
|
+
return writeFile(this.source, this.serialize(this.defaultValue)).then(
|
|
31
|
+
() => this.defaultValue
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
write(data) {
|
|
37
|
+
return writeFile(this.source, this.serialize(data))
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = FileAsync
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const fs = require('graceful-fs')
|
|
2
|
+
const Base = require('./Base')
|
|
3
|
+
const fileSystem = require('fs')
|
|
4
|
+
|
|
5
|
+
//const readFile = fs.readFileSync
|
|
6
|
+
const writeFile = fs.writeFileSync
|
|
7
|
+
|
|
8
|
+
// Same code as in FileAsync, minus `await`
|
|
9
|
+
class FileSync extends Base {
|
|
10
|
+
read() {
|
|
11
|
+
// fs.exists is deprecated but not fs.existsSync
|
|
12
|
+
if (fs.existsSync(this.source)) {
|
|
13
|
+
// Read database
|
|
14
|
+
try {
|
|
15
|
+
// 使用 fileSystem的readFileSync
|
|
16
|
+
//const data = readFile(this.source, 'utf-8').trim()
|
|
17
|
+
const data = fileSystem.readFileSync(this.source, {encoding: 'utf-8'})
|
|
18
|
+
|
|
19
|
+
// Handle blank file
|
|
20
|
+
return data ? this.deserialize(data) : this.defaultValue
|
|
21
|
+
} catch (e) {
|
|
22
|
+
if (e instanceof SyntaxError) {
|
|
23
|
+
e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
|
|
24
|
+
}
|
|
25
|
+
throw e
|
|
26
|
+
}
|
|
27
|
+
} else {
|
|
28
|
+
// Initialize
|
|
29
|
+
writeFile(this.source, this.serialize(this.defaultValue))
|
|
30
|
+
return this.defaultValue
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
write(data) {
|
|
35
|
+
return writeFile(this.source, this.serialize(data))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = FileSync
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* global localStorage */
|
|
2
|
+
const Base = require('./Base')
|
|
3
|
+
|
|
4
|
+
class LocalStorage extends Base {
|
|
5
|
+
read() {
|
|
6
|
+
const data = localStorage.getItem(this.source)
|
|
7
|
+
if (data) {
|
|
8
|
+
return this.deserialize(data)
|
|
9
|
+
} else {
|
|
10
|
+
localStorage.setItem(this.source, this.serialize(this.defaultValue))
|
|
11
|
+
return this.defaultValue
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
write(data) {
|
|
16
|
+
localStorage.setItem(this.source, this.serialize(data))
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = LocalStorage
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const isPromise = require('./isPromise')
|
|
2
|
+
|
|
3
|
+
const init = (db, key, adapter) => {
|
|
4
|
+
db.read = () => {
|
|
5
|
+
const r = adapter.read()
|
|
6
|
+
|
|
7
|
+
return isPromise(r) ? r.then(db.plant) : db.plant(r)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
db.write = (value = db.getState()) => {
|
|
11
|
+
const w = adapter.write(db.getState())
|
|
12
|
+
|
|
13
|
+
return isPromise(w) ? w.then(() => value) : value
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
db.plant = state => {
|
|
17
|
+
db[key] = state
|
|
18
|
+
return db
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
db.getState = () => db[key]
|
|
22
|
+
|
|
23
|
+
db.setState = state => {
|
|
24
|
+
db.plant(state)
|
|
25
|
+
return db
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return db.read()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
init
|
|
33
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const flow = require('lodash/flow')
|
|
2
|
+
const get = require('lodash/get')
|
|
3
|
+
const set = require('lodash/set')
|
|
4
|
+
const common = require('./common')
|
|
5
|
+
|
|
6
|
+
module.exports = function(adapter) {
|
|
7
|
+
function db(path, defaultValue) {
|
|
8
|
+
function getValue(funcs) {
|
|
9
|
+
const result = get(db.getState(), path, defaultValue)
|
|
10
|
+
return flow(funcs)(result)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getValue.write = (...funcs) => {
|
|
14
|
+
const result = getValue(...funcs)
|
|
15
|
+
set(db.getState(), path, result)
|
|
16
|
+
return db.write()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return getValue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return common.init(db, '__state__', adapter)
|
|
23
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const lodash = require('lodash')
|
|
2
|
+
const isPromise = require('./isPromise')
|
|
3
|
+
|
|
4
|
+
module.exports = function(adapter) {
|
|
5
|
+
if (typeof adapter !== 'object') {
|
|
6
|
+
throw new Error(
|
|
7
|
+
'An adapter must be provided, see https://github.com/typicode/lowdb/#usage'
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Create a fresh copy of lodash
|
|
12
|
+
const _ = lodash.runInContext()
|
|
13
|
+
const db = _.chain({})
|
|
14
|
+
|
|
15
|
+
// Add write function to lodash
|
|
16
|
+
// Calls save before returning result
|
|
17
|
+
_.prototype.write = _.wrap(_.prototype.value, function(func) {
|
|
18
|
+
const funcRes = func.apply(this)
|
|
19
|
+
return db.write(funcRes)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
function plant(state) {
|
|
23
|
+
db.__wrapped__ = state
|
|
24
|
+
return db
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Lowdb API
|
|
28
|
+
// Expose _ for mixins
|
|
29
|
+
db._ = _
|
|
30
|
+
|
|
31
|
+
db.read = () => {
|
|
32
|
+
const r = adapter.read()
|
|
33
|
+
return isPromise(r) ? r.then(plant) : plant(r)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
db.write = returnValue => {
|
|
37
|
+
const w = adapter.write(db.getState())
|
|
38
|
+
return isPromise(w) ? w.then(() => returnValue) : returnValue
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
db.getState = () => db.__wrapped__
|
|
42
|
+
|
|
43
|
+
db.setState = state => plant(state)
|
|
44
|
+
|
|
45
|
+
return db.read()
|
|
46
|
+
}
|
|
@@ -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/main');
|
|
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,128 +1,128 @@
|
|
|
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
|
-
this.mode = this.getMode(name);
|
|
15
|
-
|
|
16
|
-
const storageDir = this._createStorageDir();
|
|
17
|
-
this.storageDir = storageDir;
|
|
18
|
-
|
|
19
|
-
const fileName = this._formatFileName(name);
|
|
20
|
-
this.fileName = fileName;
|
|
21
|
-
|
|
22
|
-
this.db = this._initDB(opt);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 初始化db
|
|
27
|
-
*/
|
|
28
|
-
_initDB (opt = {}) {
|
|
29
|
-
let options = Object.assign({
|
|
30
|
-
timeout: 5000,
|
|
31
|
-
}, opt);
|
|
32
|
-
|
|
33
|
-
// 存储类型:db文件、内存(:memory:)
|
|
34
|
-
let dbPath = this.name;
|
|
35
|
-
if (this.mode != 'memory') {
|
|
36
|
-
dbPath = this.getFilePath();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const db = new Database(dbPath, options);
|
|
40
|
-
|
|
41
|
-
// 如果是文件类型,判断文件是否创建成功
|
|
42
|
-
if (this.mode != 'memory') {
|
|
43
|
-
assert(fs.existsSync(dbPath), `error: storage ${dbPath} not exists`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return db;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 获取文件名
|
|
51
|
-
*/
|
|
52
|
-
_formatFileName (name) {
|
|
53
|
-
let fileName = name;
|
|
54
|
-
if (this.mode != 'memory') {
|
|
55
|
-
fileName = path.basename(name);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return fileName;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 创建storage目录
|
|
63
|
-
*/
|
|
64
|
-
_createStorageDir () {
|
|
65
|
-
let storageDir = utilsCommon.getStorageDir();
|
|
66
|
-
|
|
67
|
-
if (this.mode == 'absolute') {
|
|
68
|
-
storageDir = path.dirname(this.name);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!fs.existsSync(storageDir)) {
|
|
72
|
-
utilsCommon.mkdir(storageDir);
|
|
73
|
-
utilsCommon.chmodPath(storageDir, '777');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return storageDir;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 获取file path 模式
|
|
81
|
-
*/
|
|
82
|
-
getMode (name) {
|
|
83
|
-
let mode = '';
|
|
84
|
-
|
|
85
|
-
// 内存模式
|
|
86
|
-
if (name == ':memory:') {
|
|
87
|
-
mode = 'memory';
|
|
88
|
-
return mode;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
assert(path.extname(name) == '.db', `error: storage ${name} file ext name must be .db`);
|
|
92
|
-
|
|
93
|
-
// 路径模式
|
|
94
|
-
name = name.replace(/[/\\]/g, '/');
|
|
95
|
-
if (name.indexOf('/') !== -1) {
|
|
96
|
-
const isAbsolute = path.isAbsolute(name);
|
|
97
|
-
if (isAbsolute) {
|
|
98
|
-
mode = 'absolute';
|
|
99
|
-
} else {
|
|
100
|
-
mode = 'relative';
|
|
101
|
-
}
|
|
102
|
-
return mode;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// 仅文件名
|
|
106
|
-
mode = 'onlyName';
|
|
107
|
-
|
|
108
|
-
return mode;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 获取storage目录
|
|
113
|
-
*/
|
|
114
|
-
getStorageDir () {
|
|
115
|
-
return this.storageDir;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 获取文件绝对路径
|
|
120
|
-
*/
|
|
121
|
-
getFilePath () {
|
|
122
|
-
const dbFile = path.join(this.storageDir, this.fileName);
|
|
123
|
-
|
|
124
|
-
return dbFile;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
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
|
+
this.mode = this.getMode(name);
|
|
15
|
+
|
|
16
|
+
const storageDir = this._createStorageDir();
|
|
17
|
+
this.storageDir = storageDir;
|
|
18
|
+
|
|
19
|
+
const fileName = this._formatFileName(name);
|
|
20
|
+
this.fileName = fileName;
|
|
21
|
+
|
|
22
|
+
this.db = this._initDB(opt);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 初始化db
|
|
27
|
+
*/
|
|
28
|
+
_initDB (opt = {}) {
|
|
29
|
+
let options = Object.assign({
|
|
30
|
+
timeout: 5000,
|
|
31
|
+
}, opt);
|
|
32
|
+
|
|
33
|
+
// 存储类型:db文件、内存(:memory:)
|
|
34
|
+
let dbPath = this.name;
|
|
35
|
+
if (this.mode != 'memory') {
|
|
36
|
+
dbPath = this.getFilePath();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const db = new Database(dbPath, options);
|
|
40
|
+
|
|
41
|
+
// 如果是文件类型,判断文件是否创建成功
|
|
42
|
+
if (this.mode != 'memory') {
|
|
43
|
+
assert(fs.existsSync(dbPath), `error: storage ${dbPath} not exists`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return db;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 获取文件名
|
|
51
|
+
*/
|
|
52
|
+
_formatFileName (name) {
|
|
53
|
+
let fileName = name;
|
|
54
|
+
if (this.mode != 'memory') {
|
|
55
|
+
fileName = path.basename(name);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return fileName;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 创建storage目录
|
|
63
|
+
*/
|
|
64
|
+
_createStorageDir () {
|
|
65
|
+
let storageDir = utilsCommon.getStorageDir();
|
|
66
|
+
|
|
67
|
+
if (this.mode == 'absolute') {
|
|
68
|
+
storageDir = path.dirname(this.name);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!fs.existsSync(storageDir)) {
|
|
72
|
+
utilsCommon.mkdir(storageDir);
|
|
73
|
+
utilsCommon.chmodPath(storageDir, '777');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return storageDir;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 获取file path 模式
|
|
81
|
+
*/
|
|
82
|
+
getMode (name) {
|
|
83
|
+
let mode = '';
|
|
84
|
+
|
|
85
|
+
// 内存模式
|
|
86
|
+
if (name == ':memory:') {
|
|
87
|
+
mode = 'memory';
|
|
88
|
+
return mode;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
assert(path.extname(name) == '.db', `error: storage ${name} file ext name must be .db`);
|
|
92
|
+
|
|
93
|
+
// 路径模式
|
|
94
|
+
name = name.replace(/[/\\]/g, '/');
|
|
95
|
+
if (name.indexOf('/') !== -1) {
|
|
96
|
+
const isAbsolute = path.isAbsolute(name);
|
|
97
|
+
if (isAbsolute) {
|
|
98
|
+
mode = 'absolute';
|
|
99
|
+
} else {
|
|
100
|
+
mode = 'relative';
|
|
101
|
+
}
|
|
102
|
+
return mode;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 仅文件名
|
|
106
|
+
mode = 'onlyName';
|
|
107
|
+
|
|
108
|
+
return mode;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 获取storage目录
|
|
113
|
+
*/
|
|
114
|
+
getStorageDir () {
|
|
115
|
+
return this.storageDir;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 获取文件绝对路径
|
|
120
|
+
*/
|
|
121
|
+
getFilePath () {
|
|
122
|
+
const dbFile = path.join(this.storageDir, this.fileName);
|
|
123
|
+
|
|
124
|
+
return dbFile;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
128
|
module.exports = SqliteStorage;
|