ee-core 1.2.7-beta.3 → 1.2.8-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/bin/tools.js CHANGED
File without changes
@@ -214,6 +214,9 @@ module.exports = appInfo => {
214
214
  port: 7071, // 默认端口(如果端口被使用,则随机获取一个)
215
215
  cors: {
216
216
  origin: "*"
217
+ },
218
+ body: {
219
+ multipart: true, // 文件类型
217
220
  }
218
221
  };
219
222
 
@@ -3,8 +3,8 @@
3
3
  const assert = require('assert');
4
4
  const is = require('is-type-of');
5
5
  const Koa = require('koa');
6
- const BodyParser = require('koa-bodyparser');
7
6
  const cors = require('koa2-cors');
7
+ const koaBody = require('koa-body');
8
8
 
9
9
  /**
10
10
  * http server
@@ -12,9 +12,9 @@ const cors = require('koa2-cors');
12
12
  class HttpServer {
13
13
  constructor (app) {
14
14
  this.app = app;
15
- const options = this.app.config.httpServer;
15
+ this.options = this.app.config.httpServer;
16
16
 
17
- if (!options.enable) {
17
+ if (!this.options.enable) {
18
18
  return;
19
19
  }
20
20
 
@@ -29,16 +29,14 @@ class HttpServer {
29
29
  */
30
30
  create () {
31
31
  const self = this;
32
- const httpServer = this.app.config.httpServer;
32
+ const httpServer = this.options;
33
33
  const url = httpServer.protocol + httpServer.host + ':' + httpServer.port;
34
34
  const corsOptions = httpServer.cors;
35
35
 
36
36
  const koaApp = new Koa();
37
- const bodyparser= new BodyParser();
38
-
39
37
  koaApp
40
38
  .use(cors(corsOptions))
41
- .use(bodyparser)
39
+ .use(koaBody(httpServer.body))
42
40
  .use(async (ctx, next) => {
43
41
  ctx.eeApp = self.app;
44
42
  await next();
@@ -7,7 +7,7 @@ const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
7
7
  const is = require('is-type-of');
8
8
 
9
9
  /**
10
- * 类文件,顶部new egglogger 后,出现代码无法正常加载
10
+ * socket server
11
11
  */
12
12
  class SocketServer {
13
13
  constructor (app) {
@@ -1,19 +1,31 @@
1
1
  'use strict';
2
2
 
3
+ const assert = require('assert');
4
+ const _ = require('lodash');
3
5
  const JsonDB = {};
4
6
 
5
- JsonDB.connection = function (database) {
7
+ JsonDB.connection = function (database, type = 'lowdb') {
8
+ // todo sqlite
9
+ if (!_.includes(['lowdb'], type)) {
10
+ assert(database, `db type ${type} is not supported`);
11
+ }
12
+ if (_.isEmpty(database)) {
13
+ assert(database, `db name ${database} Cannot be empty`);
14
+ }
15
+ let storage;
16
+ switch (type) {
17
+ case 'lowdb':
18
+ const LowdbStorage = require('./lowdbStorage');
19
+ storage = new LowdbStorage(database);
20
+ break;
21
+ case 'sqlite':
22
+ // todo
23
+ const SqliteStorage = require('./sqliteStorage');
24
+ storage = new SqliteStorage(database);
25
+ break;
26
+ default:
27
+ }
6
28
 
7
- // console.log('this::::::', this);
8
- // todo
9
- // if (typeof this.instance === 'object') {
10
- // return this.instance;
11
- // }
12
-
13
- const LowdbStorage = require('./lowdbStorage');
14
- const storage = new LowdbStorage(database);
15
-
16
- // this.instance = storage;
17
29
  return storage;
18
30
  }
19
31
 
@@ -7,6 +7,7 @@ const lowdb = require('lowdb');
7
7
  const FileSync = require('lowdb/adapters/FileSync');
8
8
  const _ = require('lodash');
9
9
  const constant = require('../constant');
10
+ const utilsCommon = require('../../utils/common');
10
11
 
11
12
  class LowdbStorage {
12
13
  constructor (name, opt = {}) {
@@ -17,10 +18,10 @@ class LowdbStorage {
17
18
  // 数据库key列表
18
19
  this.storageKey = constant.storageKey;
19
20
 
20
- const storageDir = this.getStorageDir();
21
+ const storageDir = utilsCommon.getStorageDir();
21
22
  if (!fs.existsSync(storageDir)) {
22
- this.mkdir(storageDir);
23
- this.chmodPath(storageDir, '777');
23
+ utilsCommon.mkdir(storageDir);
24
+ utilsCommon.chmodPath(storageDir, '777');
24
25
  }
25
26
 
26
27
  this.db = this.table(name);
@@ -49,24 +50,14 @@ class LowdbStorage {
49
50
  }
50
51
 
51
52
  /**
52
- * 获取db文件名
53
+ * 获取文件绝对路径
53
54
  */
54
55
  getFilePath (name) {
55
- const storageDir = this.getStorageDir();
56
+ const storageDir = utilsCommon.getStorageDir();
56
57
  const dbFile = path.join(storageDir, this.getFileName(name));
57
58
  return dbFile;
58
59
  }
59
60
 
60
- /**
61
- * 获取数据存储路径
62
- */
63
- getStorageDir () {
64
- let env = process.env.EE_SERVER_ENV;
65
- const appDir = env === 'local' || env === 'unittest' ? process.env.EE_HOME : process.env.EE_APP_USER_DATA;
66
- const storageDir = path.join(appDir, 'data');
67
- return storageDir;
68
- }
69
-
70
61
  /**
71
62
  * 为指定的 name 设置一个对应的值
72
63
  */
@@ -103,42 +94,6 @@ class LowdbStorage {
103
94
 
104
95
  return data;
105
96
  }
106
-
107
- mkdir (dirpath, dirname) {
108
- if (typeof dirname === 'undefined') {
109
- if (fs.existsSync(dirpath)) {
110
- return;
111
- }
112
- this.mkdir(dirpath, path.dirname(dirpath));
113
- } else {
114
- if (dirname !== path.dirname(dirpath)) {
115
- this.mkdir(dirpath);
116
- return;
117
- }
118
- if (fs.existsSync(dirname)) {
119
- fs.mkdirSync(dirpath);
120
- } else {
121
- this.mkdir(dirname, path.dirname(dirname));
122
- fs.mkdirSync(dirpath);
123
- }
124
- }
125
- };
126
-
127
- chmodPath (path, mode) {
128
- let files = [];
129
- if (fs.existsSync(path)) {
130
- files = fs.readdirSync(path);
131
- files.forEach((file, index) => {
132
- const curPath = path + '/' + file;
133
- if (fs.statSync(curPath).isDirectory()) {
134
- this.chmodPath(curPath, mode);
135
- } else {
136
- fs.chmodSync(curPath, mode);
137
- }
138
- });
139
- fs.chmodSync(path, mode);
140
- }
141
- };
142
97
  }
143
98
 
144
99
  module.exports = LowdbStorage;
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const constant = require('../constant');
7
+ const Database = require('better-sqlite3');
8
+ const utilsCommon = require('../../utils/common');
9
+
10
+ class SqliteStorage {
11
+ constructor (name, opt = {}) {
12
+ assert(name, `db name ${name} Cannot be empty`);
13
+
14
+ this.name = name;
15
+
16
+ // 数据库key列表
17
+ this.storageKey = constant.storageKey;
18
+
19
+ const storageDir = utilsCommon.getStorageDir();
20
+ if (!fs.existsSync(storageDir)) {
21
+ utilsCommon.mkdir(storageDir);
22
+ utilsCommon.chmodPath(storageDir, '777');
23
+ }
24
+
25
+ this.db = this.initDB(name, opt);
26
+ }
27
+
28
+ /**
29
+ * 初始化db
30
+ */
31
+ initDB (name, opt = {}) {
32
+ let options = Object.assign({
33
+ timeout: 5000,
34
+ verbose: console.log
35
+ }, opt);
36
+
37
+ // 存储类型:db文件、内存(:memory:)
38
+ let isFileDB = false;
39
+ if (path.extname(name) == '.db') {
40
+ name = this.getFilePath(name);
41
+ isFileDB = true;
42
+ }
43
+ const db = new Database(name, options);
44
+
45
+ // 如果是文件类型,判断文件是否创建成功
46
+ if (isFileDB) {
47
+ assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
48
+ }
49
+
50
+ return db;
51
+ }
52
+
53
+ /**
54
+ * 获取文件绝对路径
55
+ */
56
+ getFilePath (name) {
57
+ const storageDir = utilsCommon.getStorageDir();
58
+ const dbFile = path.join(storageDir, name);
59
+ return dbFile;
60
+ }
61
+
62
+ /**
63
+ * 为指定的 name 设置一个对应的值
64
+ */
65
+ setItem (key, value) {
66
+ }
67
+
68
+ /**
69
+ * 根据指定的名字 name 获取对应的值
70
+ */
71
+ getItem (key) {
72
+ }
73
+
74
+
75
+ }
76
+
77
+ module.exports = SqliteStorage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "1.2.7-beta.3",
3
+ "version": "1.2.8-beta.2",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,7 +27,7 @@
27
27
  "humanize-ms": "^1.2.1",
28
28
  "is-type-of": "^1.2.1",
29
29
  "koa": "^2.13.4",
30
- "koa-bodyparser": "^4.3.0",
30
+ "koa-body": "^5.0.0",
31
31
  "koa-convert": "^2.0.0",
32
32
  "koa-static": "^5.0.0",
33
33
  "koa2-cors": "^2.0.6",
package/utils/common.js CHANGED
@@ -1,3 +1,11 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * ee-core使用
5
+ */
6
+
7
+ const fs = require('fs');
8
+ const path = require('path');
1
9
 
2
10
  /**
3
11
  * 版本号比较
@@ -28,3 +36,56 @@ exports.compareVersion = function (v1, v2) {
28
36
  return 0
29
37
  }
30
38
 
39
+ /**
40
+ * 创建文件夹
41
+ */
42
+ exports.mkdir = function(dirpath, dirname) {
43
+ // 判断是否是第一次调用
44
+ if (typeof dirname === 'undefined') {
45
+ if (fs.existsSync(dirpath)) {
46
+ return;
47
+ }
48
+ this.mkdir(dirpath, path.dirname(dirpath));
49
+ } else {
50
+ // 判断第二个参数是否正常,避免调用时传入错误参数
51
+ if (dirname !== path.dirname(dirpath)) {
52
+ this.mkdir(dirpath);
53
+ return;
54
+ }
55
+ if (fs.existsSync(dirname)) {
56
+ fs.mkdirSync(dirpath);
57
+ } else {
58
+ this.mkdir(dirname, path.dirname(dirname));
59
+ fs.mkdirSync(dirpath);
60
+ }
61
+ }
62
+ };
63
+
64
+ /**
65
+ * 修改文件权限
66
+ */
67
+ exports.chmodPath = function(path, mode) {
68
+ let files = [];
69
+ if (fs.existsSync(path)) {
70
+ files = fs.readdirSync(path);
71
+ files.forEach((file, index) => {
72
+ const curPath = path + '/' + file;
73
+ if (fs.statSync(curPath).isDirectory()) {
74
+ this.chmodPath(curPath, mode); // 递归删除文件夹
75
+ } else {
76
+ fs.chmodSync(curPath, mode);
77
+ }
78
+ });
79
+ fs.chmodSync(path, mode);
80
+ }
81
+ };
82
+
83
+ /**
84
+ * 获取数据存储路径
85
+ */
86
+ exports.getStorageDir = function () {
87
+ let env = process.env.EE_SERVER_ENV;
88
+ const appDir = env === 'local' || env === 'unittest' ? process.env.EE_HOME : process.env.EE_APP_USER_DATA;
89
+ const storageDir = path.join(appDir, 'data');
90
+ return storageDir;
91
+ }
package/utils/index.js CHANGED
@@ -7,6 +7,7 @@ const convert = require('koa-convert');
7
7
  const is = require('is-type-of');
8
8
  const co = require('co');
9
9
  const utility = require('utility');
10
+ const eis = require('electron-is');
10
11
 
11
12
  /**
12
13
  * 创建文件夹
@@ -151,7 +152,7 @@ exports.getExtraResourcesDir = function() {
151
152
  // 打包后 execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
152
153
  // windows和MacOs不一样
153
154
  dir = path.join(execDir, "resources", "extraResources");
154
- if (is.macOS()) {
155
+ if (eis.macOS()) {
155
156
  dir = path.join(execDir, "..", "Resources", "extraResources");
156
157
  }
157
158
  } else {
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- class AppStorage {
4
- constructor() {
5
- const Storage = require('./storage');
6
- const sObj = Storage.getInstance('appData');
7
-
8
- for (const attr of sObj) {
9
- Object.assign(UserStorage.prototype, attr);
10
- }
11
- }
12
- }
13
-
14
- module.exports = AppStorage;