ee-core 1.2.10-beta.1 → 1.2.10-beta.4

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.
@@ -230,6 +230,7 @@ module.exports = appInfo => {
230
230
  protocol: 'http://',
231
231
  host: '127.0.0.1',
232
232
  port: 7072, // 默认端口(如果端口被使用,则随机获取一个)
233
+ options: {}
233
234
  };
234
235
 
235
236
  /**
@@ -5,6 +5,7 @@ const is = require('is-type-of');
5
5
  const FileLoader = require('./file_loader');
6
6
  const CLASSLOADER = Symbol('classLoader');
7
7
  const EXPORTS = FileLoader.EXPORTS;
8
+ const utils = require('../utils');
8
9
 
9
10
  class ClassLoader {
10
11
 
@@ -88,7 +89,7 @@ function getInstance(values, ctx) {
88
89
  const Class = values[EXPORTS] ? values : null;
89
90
  let instance;
90
91
  if (Class) {
91
- if (is.class(Class)) {
92
+ if (is.class(Class) || utils.isBytecodeClass(Class)) {
92
93
  instance = new Class(ctx);
93
94
  } else {
94
95
  // it's just an object
@@ -272,7 +272,7 @@ class EeLoader {
272
272
  if (inject.length === 0) inject = [ this.app ];
273
273
 
274
274
  let ret = this.requireFile(filepath);
275
- if (is.function(ret) && !is.class(ret)) {
275
+ if (is.function(ret) && !is.class(ret) && !utils.isBytecodeClass(ret)) {
276
276
  ret = ret(...inject);
277
277
  }
278
278
  return ret;
@@ -358,7 +358,6 @@ class EeLoader {
358
358
  const timingKey = `Load "${String(property)}" to Application`;
359
359
  this.timing.start(timingKey);
360
360
  new FileLoader(opt).load();
361
- //console.log('app property:', this.app[property]);
362
361
  this.timing.end(timingKey);
363
362
  }
364
363
 
@@ -412,6 +411,10 @@ class EeLoader {
412
411
  try {
413
412
  fullPath = require.resolve(filepath);
414
413
  } catch (e) {
414
+ let jscFile = filepath + '.jsc';
415
+ if (fs.existsSync(jscFile)) {
416
+ return jscFile;
417
+ }
415
418
  return undefined;
416
419
  }
417
420
 
@@ -65,7 +65,6 @@ class FileLoader {
65
65
  */
66
66
  load() {
67
67
  const items = this.parse();
68
- //console.log('FileLoader load items:', items);
69
68
  const target = this.options.target;
70
69
  for (const item of items) {
71
70
  // item { properties: [ 'a', 'b', 'c'], exports }
@@ -123,11 +122,10 @@ class FileLoader {
123
122
  */
124
123
  parse() {
125
124
  let files = this.options.match;
126
-
127
125
  if (!files) {
128
126
  files = (process.env.EE_TYPESCRIPT === 'true' && utils.extensions['.ts'])
129
127
  ? [ '**/*.(js|ts)', '!**/*.d.ts' ]
130
- : [ '**/*.js' ];
128
+ : [ '**/*.js', '**/*.jsc'];
131
129
  } else {
132
130
  files = Array.isArray(files) ? files : [ files ];
133
131
  }
@@ -150,7 +148,6 @@ class FileLoader {
150
148
 
151
149
  for (const directory of directories) {
152
150
  const filepaths = globby.sync(files, { cwd: directory });
153
-
154
151
  for (const filepath of filepaths) {
155
152
  const fullpath = path.join(directory, filepath);
156
153
  if (!fs.statSync(fullpath).isFile()) continue;
@@ -160,17 +157,16 @@ class FileLoader {
160
157
  // app/service/foo/bar.js => service.foo.bar
161
158
  const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
162
159
  // get exports from the file
163
- const exports = getExports(fullpath, this.options, pathName);
164
-
160
+ let exports = getExports(fullpath, this.options, pathName);
165
161
  // ignore exports when it's null or false returned by filter function
166
162
  if (exports == null || (filter && filter(exports) === false)) continue;
167
163
 
168
164
  // set properties of class
169
- if (is.class(exports)) {
165
+ if (is.class(exports) || utils.isBytecodeClass(exports)) {
170
166
  exports.prototype.pathName = pathName;
171
167
  exports.prototype.fullPath = fullpath;
172
168
  }
173
- //console.log('lll---------------pathName:', pathName);
169
+
174
170
  items.push({ fullpath, properties, exports });
175
171
  debug('parse %s, properties %j, export %O', fullpath, properties, exports);
176
172
  }
@@ -202,6 +198,7 @@ function getProperties(filepath, { caseStyle }) {
202
198
  // If exports is null/undefined, it will be ignored
203
199
  function getExports(fullpath, { initializer, call, inject }, pathName) {
204
200
  let exports = utils.loadFile(fullpath);
201
+
205
202
  // process exports as you like
206
203
  if (initializer) {
207
204
  exports = initializer(exports, { path: fullpath, pathName });
@@ -212,7 +209,9 @@ function getExports(fullpath, { initializer, call, inject }, pathName) {
212
209
  // module.exports = class Service {};
213
210
  // or
214
211
  // module.exports = function*() {}
215
- if (is.class(exports) || is.generatorFunction(exports) || is.asyncFunction(exports)) {
212
+ //new exports;
213
+
214
+ if (is.class(exports) || is.generatorFunction(exports) || is.asyncFunction(exports) || utils.isBytecodeClass(exports)) {
216
215
  return exports;
217
216
  }
218
217
 
@@ -75,11 +75,8 @@ module.exports = {
75
75
  _loadConfig(dirpath, filename, extraInject, type) {
76
76
  const isPlugin = type === 'plugin';
77
77
  const isApp = type === 'app';
78
-
79
78
  let filepath = this.resolveModule(path.join(dirpath, 'config', filename));
80
79
 
81
- //console.log("_loadConfig filepath:", filepath);
82
-
83
80
  // let config.js compatible
84
81
  if (filename === 'config.default' && !filepath) {
85
82
  filepath = this.resolveModule(path.join(dirpath, 'config/config'));
@@ -6,7 +6,6 @@ const utility = require('utility');
6
6
  const utils = require('../../utils');
7
7
  const FULLPATH = require('../file_loader').FULLPATH;
8
8
 
9
-
10
9
  module.exports = {
11
10
 
12
11
  /**
@@ -26,10 +25,11 @@ module.exports = {
26
25
  // return class HomeController extends app.Controller {};
27
26
  // }
28
27
  // ```
29
- if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj) && !is.asyncFunction(obj)) {
28
+
29
+ if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj) && !is.asyncFunction(obj) && !utils.isBytecodeClass(obj)) {
30
30
  obj = obj(this.app);
31
31
  }
32
- if (is.class(obj)) {
32
+ if (is.class(obj) || utils.isBytecodeClass(obj)) {
33
33
  obj.prototype.pathName = opt.pathName;
34
34
  obj.prototype.fullPath = opt.path;
35
35
  return wrapClass(obj);
@@ -76,6 +76,7 @@ function wrapClass(Controller) {
76
76
  }
77
77
  proto = Object.getPrototypeOf(proto);
78
78
  }
79
+
79
80
  return ret;
80
81
 
81
82
  function methodToMiddleware(Controller, key) {
@@ -22,6 +22,7 @@ module.exports = {
22
22
  }, opt);
23
23
 
24
24
  const servicePaths = opt.directory;
25
+ console.log('servicePaths:', servicePaths);
25
26
  this.loadToContext(servicePaths, 'service', opt);
26
27
  this.timing.end('Load Service');
27
28
  },
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ require('bytenode');
3
4
  const convert = require('koa-convert');
4
5
  const is = require('is-type-of');
5
6
  const path = require('path');
@@ -23,6 +24,7 @@ module.exports = {
23
24
  if (extname && !Module._extensions[extname]) {
24
25
  return fs.readFileSync(filepath);
25
26
  }
27
+
26
28
  // require js module
27
29
  const obj = require(filepath);
28
30
  if (!obj) return obj;
@@ -87,6 +89,21 @@ module.exports = {
87
89
  const reg = /[/\\]/g;
88
90
  return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
89
91
  },
92
+
93
+ /**
94
+ * 字节码类
95
+ */
96
+ isBytecodeClass (exports) {
97
+ let isClass = false;
98
+
99
+ // 标识
100
+ if (exports.toString().indexOf('[class') != -1) {
101
+ isClass = true;
102
+ }
103
+ // TODO 更严谨的判断,应该加上文件名和路径
104
+
105
+ return isClass;
106
+ },
90
107
  };
91
108
 
92
109
 
@@ -6,12 +6,7 @@ const fs = require('fs');
6
6
 
7
7
  class Appliaction extends EeApp {
8
8
  constructor() {
9
-
10
- // 初始化环境变量
11
- // const opt = this.initEnv();
12
9
  const { env } = process;
13
-
14
- // 路径不能使用绝对,打包前后有问题
15
10
  let options = {
16
11
  env: 'prod',
17
12
  serverScope: '',
package/lib/eeApp.js CHANGED
@@ -70,12 +70,12 @@ class EeApp extends BaseApp {
70
70
 
71
71
  app.whenReady().then(() => {
72
72
  self.createWindow();
73
- app.on('activate', function () {
73
+ app.on('activate', () => {
74
74
  self.restoreMainWindow();
75
75
  })
76
76
  })
77
77
 
78
- app.on('window-all-closed', function () {
78
+ app.on('window-all-closed', () => {
79
79
  if (process.platform !== 'darwin') {
80
80
  self.coreLogger.info('[Appliaction] [initialize] window-all-closed quit');
81
81
  self.appQuit();
@@ -243,8 +243,9 @@ class EeApp extends BaseApp {
243
243
  * 主页面
244
244
  */
245
245
  loadMainUrl (type, url) {
246
+ const mainServer = this.config.mainServer;
246
247
  this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
247
- this.electron.mainWindow.loadURL(url);
248
+ this.electron.mainWindow.loadURL(url, mainServer.options);
248
249
  }
249
250
 
250
251
  /**
@@ -252,7 +253,6 @@ class EeApp extends BaseApp {
252
253
  */
253
254
  async appQuit () {
254
255
  await this.beforeClose();
255
-
256
256
  app.quit();
257
257
  }
258
258
 
@@ -299,7 +299,6 @@ class EeApp extends BaseApp {
299
299
  */
300
300
  async electronAppReady () {
301
301
  // do some things
302
-
303
302
  }
304
303
 
305
304
  /**
@@ -307,7 +306,6 @@ class EeApp extends BaseApp {
307
306
  */
308
307
  async windowReady () {
309
308
  // do some things
310
-
311
309
  }
312
310
 
313
311
  /**
@@ -315,7 +313,6 @@ class EeApp extends BaseApp {
315
313
  */
316
314
  async beforeClose () {
317
315
  // do some things
318
-
319
316
  }
320
317
  }
321
318
 
@@ -9,7 +9,6 @@ const fs = require('fs');
9
9
  const globby = require('globby');
10
10
  const utils = require('../../core/lib/utils');
11
11
  const wrap = require('../../utils/wrap');
12
- const utility = require('utility');
13
12
 
14
13
  class IpcServer {
15
14
  constructor (app) {
@@ -27,23 +26,23 @@ class IpcServer {
27
26
  // 遍历方法
28
27
  const files = (process.env.EE_TYPESCRIPT === 'true' && utils.extensions['.ts'])
29
28
  ? [ '**/*.(js|ts)', '!**/*.d.ts' ]
30
- : [ '**/*.js' ];
29
+ : [ '**/*.js','**/*.jsc' ];
31
30
  const directory = path.join(this.app.config.baseDir, 'controller');
32
31
  const filepaths = globby.sync(files, { cwd: directory });
32
+
33
33
  for (const filepath of filepaths) {
34
34
  const fullpath = path.join(directory, filepath);
35
35
  if (!fs.statSync(fullpath).isFile()) continue;
36
36
 
37
37
  const properties = wrap.getProperties(filepath, {caseStyle: 'lower'});
38
38
  const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
39
-
39
+
40
40
  let fileObj = utils.loadFile(fullpath);
41
41
  const fns = {};
42
- if (is.function(fileObj) && !is.generatorFunction(fileObj) && !is.class(fileObj) && !is.asyncFunction(fileObj)) {
43
- //obj = obj(this.app);
44
- }
45
- if (is.class(fileObj)) {
42
+ // 为了统一,仅支持class文件
43
+ if (is.class(fileObj) || utils.isBytecodeClass(fileObj)) {
46
44
  let proto = fileObj.prototype;
45
+ // 不遍历父类的方法
47
46
  //while (proto !== Object.prototype) {
48
47
  const keys = Object.getOwnPropertyNames(proto);
49
48
  for (const key of keys) {
@@ -58,23 +57,6 @@ class IpcServer {
58
57
  //proto = Object.getPrototypeOf(proto);
59
58
  //}
60
59
  }
61
- if (is.object(fileObj)) {
62
- const keys = Object.keys(fileObj);
63
- for (const key of keys) {
64
- if (is.function(fileObj[key])) {
65
- const names = utility.getParamNames(fileObj[key]);
66
- if (names[0] === 'next') {
67
- throw new Error(`controller \`${prefix || ''}${key}\` should not use next as argument from file ${path}`);
68
- }
69
- fns[key] = 1;
70
- }
71
- // else if (is.object(fileObj[key])) {
72
- // ret[key] = wrapObject(obj[key], path, `${prefix || ''}${key}.`);
73
- // }
74
- }
75
- }
76
- // if (is.generatorFunction(obj) || is.asyncFunction(obj)) {
77
- // }
78
60
 
79
61
  debug('register class %s fns %j', pathName, fns);
80
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "1.2.10-beta.1",
3
+ "version": "1.2.10-beta.4",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,7 @@
14
14
  "dependencies": {
15
15
  "agentkeepalive": "^4.2.0",
16
16
  "better-sqlite3": "^7.6.0",
17
+ "bytenode": "^1.3.6",
17
18
  "co": "^4.6.0",
18
19
  "debug": "^4.3.3",
19
20
  "depd": "^2.0.0",
package/tools/encrypt.js CHANGED
@@ -3,31 +3,36 @@
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
5
  const fsPro = require('fs-extra');
6
+ const is = require('is-type-of');
6
7
  const UglifyJS = require('uglify-js');
8
+ const bytenode = require('bytenode');
7
9
 
8
10
  class Encrypt {
9
11
  constructor() {
12
+ this.basePath = process.cwd();
13
+ const directory = [
14
+ 'electron',
15
+ ];
16
+ this.dirs = [];
17
+ this.type = '';
18
+ this.configPath = '';
19
+ this.config = null;
20
+ this.filesExt = ['.js', '.json', '.node'];
21
+ this.encryptCodeDir = path.join(this.basePath, 'public');
10
22
 
11
23
  // argv
12
- this.type = '';
13
24
  for (let i = 0; i < process.argv.length; i++) {
14
25
  let tmpArgv = process.argv[i];
15
26
  if (tmpArgv.indexOf('--type=') !== -1) {
16
27
  this.type = tmpArgv.substring(7);
17
28
  }
29
+ if (tmpArgv.indexOf('--config=') !== -1) {
30
+ let configPathStr = tmpArgv.substring(9);
31
+ this.configPath = path.join(this.basePath, configPathStr);
32
+ this.config = fs.existsSync(this.configPath) ? require(this.configPath) : null;
33
+ }
18
34
  }
19
35
 
20
- const directory = [
21
- 'electron',
22
- ];
23
- this.dirs = [];
24
-
25
- this.basePath = process.cwd();
26
- // if (this.type == 'uglify') {
27
- // this.encryptCodeDir = path.join(this.basePath, 'public');
28
- // }
29
- this.encryptCodeDir = path.join(this.basePath, 'public');
30
-
31
36
  // 检查存在的目录
32
37
  for (let i = 0; i < directory.length; i++) {
33
38
  let codeDirPath = path.join(this.basePath, directory[i]);
@@ -35,11 +40,22 @@ class Encrypt {
35
40
  this.dirs.push(directory[i]);
36
41
  }
37
42
  }
38
- console.log('dirs:', this.dirs);
43
+ console.log('[ee-core] [encrypt] dirs:', this.dirs);
44
+ }
45
+
46
+ /**
47
+ * 检查
48
+ */
49
+ check () {
50
+ if (this.configPath.length > 0 && !is.object(this.config)) {
51
+ console.log('[ee-core] [encrypt] ERROR: config file is invalid');
52
+ return false;
53
+ }
54
+ return true;
39
55
  }
40
56
 
41
57
  /**
42
- * 备份 electron目录代码
58
+ * 备份 electron 目录代码
43
59
  */
44
60
  backup () {
45
61
  console.log('[ee-core] [encrypt] backup start');
@@ -48,7 +64,7 @@ class Encrypt {
48
64
  // check code dir
49
65
  let codeDirPath = path.join(this.basePath, this.dirs[i]);
50
66
  if (!fs.existsSync(codeDirPath)) {
51
- console.log('[ee-core] [encrypt] backup ERROR: %s is not exist', codeDirPath);
67
+ console.log('[ee-core] [encrypt] ERROR: backup %s is not exist', codeDirPath);
52
68
  return
53
69
  }
54
70
 
@@ -58,7 +74,7 @@ class Encrypt {
58
74
  this.rmBackup(targetDir);
59
75
 
60
76
  // copy
61
- console.log('[ee-core] [encrypt] backup targetDir:', targetDir);
77
+ console.log('[ee-core] [encrypt] backup target Dir:', targetDir);
62
78
  if (!fs.existsSync(targetDir)) {
63
79
  this.mkdir(targetDir);
64
80
  this.chmodPath(targetDir, '777');
@@ -66,50 +82,98 @@ class Encrypt {
66
82
 
67
83
  fsPro.copySync(codeDirPath, targetDir);
68
84
  }
69
- console.log('[ee-core] [encrypt] backup success');
85
+ console.log('[ee-core] [encrypt] backup end');
86
+ return true;
70
87
  }
71
88
 
72
89
  /**
73
- * 压缩代码
90
+ * 加密代码
74
91
  */
75
- compress () {
76
- console.log('[ee-core] [encrypt] compress start');
92
+ encrypt () {
93
+ console.log('[ee-core] [encrypt] start ciphering');
77
94
  for (let i = 0; i < this.dirs.length; i++) {
78
95
  let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
79
- this.compressLoop(codeDirPath);
96
+ this.loop(codeDirPath);
80
97
  }
81
- console.log('[ee-core] [encrypt] compress success');
98
+
99
+ console.log('[ee-core] [encrypt] end ciphering');
82
100
  };
83
101
 
84
- compressLoop (dirPath) {
102
+ /**
103
+ * 递归
104
+ */
105
+ loop (dirPath) {
85
106
  let files = [];
86
107
  if (fs.existsSync(dirPath)) {
87
108
  files = fs.readdirSync(dirPath);
88
109
  files.forEach((file, index) => {
89
110
  let curPath = dirPath + '/' + file;
90
111
  if (fs.statSync(curPath).isDirectory()) {
91
- this.compressLoop(curPath);
112
+ this.loop(curPath);
92
113
  } else {
93
- if (path.extname(curPath) === '.js') {
94
- this.miniFile(curPath);
114
+ const extname = path.extname(curPath);
115
+ if (this.filesExt.indexOf(extname) !== -1) {
116
+ this.generate(curPath);
95
117
  }
96
118
  }
97
119
  });
98
120
  }
99
121
  }
100
122
 
101
- miniFile (file) {
102
- let code = fs.readFileSync(file, "utf8");
103
- const options = {
123
+ /**
124
+ * 生成文件
125
+ */
126
+ generate (curPath) {
127
+ if (this.type == 'bytecode') {
128
+ this.generateBytecodeFile(curPath);
129
+ } else {
130
+ this.generateConfuseFile(curPath);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * 生成压缩/混淆文件
136
+ */
137
+ generateConfuseFile (file) {
138
+ let defaultOpt = {
104
139
  mangle: {
105
140
  toplevel: false,
106
141
  },
107
- };
108
-
142
+ compress: {
143
+ drop_console: true,
144
+ passes: 2
145
+ },
146
+ output: {
147
+ beautify: false
148
+ },
149
+ }
150
+ let options = defaultOpt;
151
+ if (is.object(this.config)) {
152
+ options = Object.assign(defaultOpt, this.config);
153
+ }
154
+
155
+ let code = fs.readFileSync(file, "utf8");
109
156
  let result = UglifyJS.minify(code, options);
110
157
  fs.writeFileSync(file, result.code, "utf8");
111
158
  }
112
159
 
160
+ /**
161
+ * 生成字节码文件
162
+ */
163
+ generateBytecodeFile (curPath) {
164
+ if (path.extname(curPath) !== '.js') {
165
+ return
166
+ }
167
+ //let jscFile = curPath.replace(/.js/g, '.jsc');
168
+ let jscFile = curPath + 'c';
169
+ bytenode.compileFile({
170
+ filename: curPath,
171
+ output: jscFile,
172
+ electron: true
173
+ });
174
+ fs.rmSync(curPath, {force: true});
175
+ }
176
+
113
177
  /**
114
178
  * 移除备份
115
179
  */
@@ -173,8 +237,9 @@ class Encrypt {
173
237
 
174
238
  const run = () => {
175
239
  const e = new Encrypt();
176
- e.backup();
177
- e.compress();
240
+ if (!e.check()) return;
241
+ if (!e.backup()) return;
242
+ e.encrypt();
178
243
  }
179
244
 
180
245
  module.exports = {