ee-core 2.9.2 → 2.10.1

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.
Files changed (103) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +65 -65
  3. package/addon/index.js +34 -34
  4. package/addon/window/index.js +98 -98
  5. package/bin/tools.js +8 -8
  6. package/config/cache.js +41 -38
  7. package/config/config.default.js +331 -330
  8. package/config/index.js +86 -73
  9. package/const/channel.js +17 -17
  10. package/const/index.js +8 -8
  11. package/controller/baseContextClass.js +34 -34
  12. package/controller/index.js +34 -34
  13. package/core/index.js +10 -10
  14. package/core/lib/ee.js +216 -216
  15. package/core/lib/loader/context_loader.js +106 -106
  16. package/core/lib/loader/ee_loader.js +435 -435
  17. package/core/lib/loader/file_loader.js +326 -326
  18. package/core/lib/loader/mixin/addon.js +32 -32
  19. package/core/lib/loader/mixin/config.js +130 -130
  20. package/core/lib/loader/mixin/controller.js +125 -125
  21. package/core/lib/loader/mixin/service.js +28 -28
  22. package/core/lib/utils/base_context_class.js +34 -34
  23. package/core/lib/utils/function.js +30 -30
  24. package/core/lib/utils/index.js +133 -133
  25. package/core/lib/utils/sequencify.js +59 -59
  26. package/core/lib/utils/timing.js +77 -77
  27. package/cross/index.js +183 -183
  28. package/cross/spawnProcess.js +183 -183
  29. package/ee/appLoader.js +48 -48
  30. package/ee/application.js +99 -99
  31. package/ee/baseApp.js +103 -102
  32. package/ee/eeApp.js +408 -408
  33. package/ee/index.js +57 -57
  34. package/electron/app/index.js +64 -58
  35. package/electron/index.js +19 -19
  36. package/electron/window/index.js +73 -73
  37. package/electron/window/winState.js +186 -186
  38. package/exception/index.js +112 -112
  39. package/html/boot.html +98 -98
  40. package/html/cross-failure.html +28 -28
  41. package/html/failure.html +28 -28
  42. package/html/index.js +13 -13
  43. package/httpclient/index.js +161 -161
  44. package/index.js +54 -54
  45. package/jobs/baseJobClass.js +16 -16
  46. package/jobs/child/app.js +58 -65
  47. package/jobs/child/forkProcess.js +145 -145
  48. package/jobs/child/index.js +82 -82
  49. package/jobs/child-pool/index.js +213 -213
  50. package/jobs/index.js +8 -8
  51. package/jobs/load-balancer/algorithm/index.js +11 -11
  52. package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
  53. package/jobs/load-balancer/algorithm/polling.js +11 -11
  54. package/jobs/load-balancer/algorithm/random.js +9 -9
  55. package/jobs/load-balancer/algorithm/specify.js +14 -14
  56. package/jobs/load-balancer/algorithm/weights.js +21 -21
  57. package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
  58. package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
  59. package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
  60. package/jobs/load-balancer/consts.js +9 -9
  61. package/jobs/load-balancer/index.js +201 -201
  62. package/jobs/load-balancer/scheduler.js +31 -31
  63. package/jobs/renderer/index.js +141 -141
  64. package/jobs/renderer/loadView.js +40 -40
  65. package/jobs/unification.js +63 -63
  66. package/loader/index.js +172 -172
  67. package/log/index.js +68 -68
  68. package/log/logger.js +86 -80
  69. package/main/index.js +56 -56
  70. package/message/childMessage.js +54 -54
  71. package/message/index.js +18 -18
  72. package/old-utils/index.js +91 -91
  73. package/package.json +38 -38
  74. package/ps/index.js +371 -371
  75. package/services/baseContextClass.js +34 -34
  76. package/services/index.js +40 -40
  77. package/socket/httpServer.js +147 -147
  78. package/socket/index.js +81 -81
  79. package/socket/io.js +27 -27
  80. package/socket/ipcServer.js +112 -112
  81. package/socket/socketServer.js +69 -67
  82. package/storage/index.js +38 -38
  83. package/storage/jsondb/adapters/Base.js +23 -23
  84. package/storage/jsondb/adapters/FileSync.js +64 -52
  85. package/storage/jsondb/main.js +55 -42
  86. package/storage/jsondbStorage.js +195 -195
  87. package/storage/sqliteStorage.js +123 -123
  88. package/utils/co.js +237 -237
  89. package/utils/copyto.js +160 -160
  90. package/utils/depd/index.js +538 -538
  91. package/utils/depd/lib/browser/index.js +77 -77
  92. package/utils/extend.js +73 -73
  93. package/utils/get-port/index.d.ts +64 -64
  94. package/utils/get-port/index.js +148 -148
  95. package/utils/helper.js +220 -220
  96. package/utils/index.js +160 -160
  97. package/utils/ip.js +261 -261
  98. package/utils/is.js +145 -145
  99. package/utils/json.js +72 -72
  100. package/utils/pargv.js +263 -263
  101. package/utils/time/index.js +19 -19
  102. package/utils/time/ms.js +162 -162
  103. package/utils/wrap.js +35 -35
@@ -1,34 +1,34 @@
1
- 'use strict';
2
-
3
- /**
4
- * BaseContextClass is a base class that can be extended,
5
- * it's instantiated in context level,
6
- * {@link Helper}, {@link Service} is extending it.
7
- */
8
- class BaseContextClass {
9
-
10
- /**
11
- * @class
12
- * @param {Context} ctx - context instance
13
- * @since 1.0.0
14
- */
15
- constructor(ctx) {
16
- /**
17
- * @member {Application} BaseContextClass#app
18
- * @since 1.0.0
19
- */
20
- this.app = ctx;
21
- /**
22
- * @member {Config} BaseContextClass#config
23
- * @since 1.0.0
24
- */
25
- this.config = ctx.config;
26
- /**
27
- * @member {Service} BaseContextClass#service
28
- * @since 1.0.0
29
- */
30
- this.service = ctx.service;
31
- }
32
- }
33
-
34
- module.exports = BaseContextClass;
1
+ 'use strict';
2
+
3
+ /**
4
+ * BaseContextClass is a base class that can be extended,
5
+ * it's instantiated in context level,
6
+ * {@link Helper}, {@link Service} is extending it.
7
+ */
8
+ class BaseContextClass {
9
+
10
+ /**
11
+ * @class
12
+ * @param {Context} ctx - context instance
13
+ * @since 1.0.0
14
+ */
15
+ constructor(ctx) {
16
+ /**
17
+ * @member {Application} BaseContextClass#app
18
+ * @since 1.0.0
19
+ */
20
+ this.app = ctx;
21
+ /**
22
+ * @member {Config} BaseContextClass#config
23
+ * @since 1.0.0
24
+ */
25
+ this.config = ctx.config;
26
+ /**
27
+ * @member {Service} BaseContextClass#service
28
+ * @since 1.0.0
29
+ */
30
+ this.service = ctx.service;
31
+ }
32
+ }
33
+
34
+ module.exports = BaseContextClass;
@@ -1,30 +1,30 @@
1
- const assert = require('assert');
2
-
3
- /**
4
- * A empty function.
5
- *
6
- * @return {Function}
7
- * @public
8
- */
9
- exports.noop = function noop() {};
10
-
11
- /**
12
- * Get a function parameter's names.
13
- *
14
- * @param {Function} func
15
- * @param {Boolean} [useCache], default is true
16
- * @return {Array} names
17
- */
18
- exports.getParamNames = function getParamNames(func, cache) {
19
- var type = typeof func;
20
- assert(type === 'function', 'The "func" must be a function. Received type "' + type + '"');
21
-
22
- cache = cache !== false;
23
- if (cache && func.__cache_names) {
24
- return func.__cache_names;
25
- }
26
- var str = func.toString();
27
- var names = str.slice(str.indexOf('(') + 1, str.indexOf(')')).match(/([^\s,]+)/g) || [];
28
- func.__cache_names = names;
29
- return names;
30
- };
1
+ const assert = require('assert');
2
+
3
+ /**
4
+ * A empty function.
5
+ *
6
+ * @return {Function}
7
+ * @public
8
+ */
9
+ exports.noop = function noop() {};
10
+
11
+ /**
12
+ * Get a function parameter's names.
13
+ *
14
+ * @param {Function} func
15
+ * @param {Boolean} [useCache], default is true
16
+ * @return {Array} names
17
+ */
18
+ exports.getParamNames = function getParamNames(func, cache) {
19
+ var type = typeof func;
20
+ assert(type === 'function', 'The "func" must be a function. Received type "' + type + '"');
21
+
22
+ cache = cache !== false;
23
+ if (cache && func.__cache_names) {
24
+ return func.__cache_names;
25
+ }
26
+ var str = func.toString();
27
+ var names = str.slice(str.indexOf('(') + 1, str.indexOf(')')).match(/([^\s,]+)/g) || [];
28
+ func.__cache_names = names;
29
+ return names;
30
+ };
@@ -1,133 +1,133 @@
1
- 'use strict';
2
-
3
- require('bytenode');
4
- const convert = require('koa-convert');
5
- const is = require('is-type-of');
6
- const path = require('path');
7
- const fs = require('fs');
8
- const co = require('../../../utils/co');
9
- const BuiltinModule = require('module');
10
-
11
- // Guard against poorly mocked module constructors.
12
- const Module = module.constructor.length > 1
13
- ? module.constructor
14
- /* istanbul ignore next */
15
- : BuiltinModule;
16
-
17
- // Module._extensions:
18
- // '.js': [Function (anonymous)],
19
- // '.json': [Function (anonymous)],
20
- // '.node': [Function: func],
21
- // '.jsc': [Function (anonymous)]
22
-
23
- module.exports = {
24
- extensions: Module._extensions,
25
-
26
- loadFile(filepath) {
27
- try {
28
- // if not js module, just return content buffer
29
- const extname = path.extname(filepath);
30
- if (extname && !Module._extensions[extname]) {
31
- return fs.readFileSync(filepath);
32
- }
33
-
34
- // require js module
35
- const obj = require(filepath);
36
- if (!obj) return obj;
37
- // it's es module
38
- if (obj.__esModule) return 'default' in obj ? obj.default : obj;
39
- return obj;
40
- } catch (err) {
41
- err.message = `[ee-core] load file: ${filepath}, error: ${err.message}`;
42
- throw err;
43
- }
44
- },
45
-
46
- methods: [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ],
47
-
48
- async callFn(fn, args, ctx) {
49
- args = args || [];
50
- if (!is.function(fn)) return;
51
- if (is.generatorFunction(fn)) fn = co.wrap(fn);
52
- return ctx ? fn.call(ctx, ...args) : fn(...args);
53
- },
54
-
55
- middleware(fn) {
56
- return is.generatorFunction(fn) ? convert(fn) : fn;
57
- },
58
-
59
- getCalleeFromStack(withLine, stackIndex) {
60
- stackIndex = stackIndex === undefined ? 2 : stackIndex;
61
- const limit = Error.stackTraceLimit;
62
- const prep = Error.prepareStackTrace;
63
-
64
- Error.prepareStackTrace = prepareObjectStackTrace;
65
- Error.stackTraceLimit = 5;
66
-
67
- // capture the stack
68
- const obj = {};
69
- Error.captureStackTrace(obj);
70
- let callSite = obj.stack[stackIndex];
71
- let fileName;
72
- /* istanbul ignore else */
73
- if (callSite) {
74
- fileName = callSite.getFileName();
75
- /* istanbul ignore if */
76
- if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
77
- // TODO: add test
78
- callSite = obj.stack[stackIndex + 1];
79
- fileName = callSite.getFileName();
80
- }
81
- }
82
-
83
- Error.prepareStackTrace = prep;
84
- Error.stackTraceLimit = limit;
85
-
86
- /* istanbul ignore if */
87
- if (!callSite || !fileName) return '<anonymous>';
88
- if (!withLine) return fileName;
89
- return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
90
- },
91
-
92
- getResolvedFilename(filepath, baseDir) {
93
- const reg = /[/\\]/g;
94
- return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
95
- },
96
-
97
- /**
98
- * 字节码类
99
- */
100
- isBytecodeClass (exports) {
101
- let isClass = false;
102
-
103
- // 标识
104
- if (exports.toString().indexOf('[class') != -1) {
105
- isClass = true;
106
- }
107
- // TODO 更严谨的判断,应该加上文件名和路径
108
-
109
- return isClass;
110
- },
111
-
112
- /**
113
- * 文件类型
114
- */
115
- filePatterns () {
116
- const files = (process.env.EE_TYPESCRIPT === 'true' && Module._extensions['.ts'])
117
- ? [ '**/*.(js|ts)', '!**/*.d.ts' ]
118
- : [ '**/*.js','**/*.jsc' ];
119
-
120
- return files;
121
- }
122
-
123
- };
124
-
125
-
126
- /**
127
- * Capture call site stack from v8.
128
- * https://github.com/v8/v8/wiki/Stack-Trace-API
129
- */
130
-
131
- function prepareObjectStackTrace(obj, stack) {
132
- return stack;
133
- }
1
+ 'use strict';
2
+
3
+ require('bytenode');
4
+ const convert = require('koa-convert');
5
+ const is = require('is-type-of');
6
+ const path = require('path');
7
+ const fs = require('fs');
8
+ const co = require('../../../utils/co');
9
+ const BuiltinModule = require('module');
10
+
11
+ // Guard against poorly mocked module constructors.
12
+ const Module = module.constructor.length > 1
13
+ ? module.constructor
14
+ /* istanbul ignore next */
15
+ : BuiltinModule;
16
+
17
+ // Module._extensions:
18
+ // '.js': [Function (anonymous)],
19
+ // '.json': [Function (anonymous)],
20
+ // '.node': [Function: func],
21
+ // '.jsc': [Function (anonymous)]
22
+
23
+ module.exports = {
24
+ extensions: Module._extensions,
25
+
26
+ loadFile(filepath) {
27
+ try {
28
+ // if not js module, just return content buffer
29
+ const extname = path.extname(filepath);
30
+ if (extname && !Module._extensions[extname]) {
31
+ return fs.readFileSync(filepath);
32
+ }
33
+
34
+ // require js module
35
+ const obj = require(filepath);
36
+ if (!obj) return obj;
37
+ // it's es module
38
+ if (obj.__esModule) return 'default' in obj ? obj.default : obj;
39
+ return obj;
40
+ } catch (err) {
41
+ err.message = `[ee-core] load file: ${filepath}, error: ${err.message}`;
42
+ throw err;
43
+ }
44
+ },
45
+
46
+ methods: [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ],
47
+
48
+ async callFn(fn, args, ctx) {
49
+ args = args || [];
50
+ if (!is.function(fn)) return;
51
+ if (is.generatorFunction(fn)) fn = co.wrap(fn);
52
+ return ctx ? fn.call(ctx, ...args) : fn(...args);
53
+ },
54
+
55
+ middleware(fn) {
56
+ return is.generatorFunction(fn) ? convert(fn) : fn;
57
+ },
58
+
59
+ getCalleeFromStack(withLine, stackIndex) {
60
+ stackIndex = stackIndex === undefined ? 2 : stackIndex;
61
+ const limit = Error.stackTraceLimit;
62
+ const prep = Error.prepareStackTrace;
63
+
64
+ Error.prepareStackTrace = prepareObjectStackTrace;
65
+ Error.stackTraceLimit = 5;
66
+
67
+ // capture the stack
68
+ const obj = {};
69
+ Error.captureStackTrace(obj);
70
+ let callSite = obj.stack[stackIndex];
71
+ let fileName;
72
+ /* istanbul ignore else */
73
+ if (callSite) {
74
+ fileName = callSite.getFileName();
75
+ /* istanbul ignore if */
76
+ if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
77
+ // TODO: add test
78
+ callSite = obj.stack[stackIndex + 1];
79
+ fileName = callSite.getFileName();
80
+ }
81
+ }
82
+
83
+ Error.prepareStackTrace = prep;
84
+ Error.stackTraceLimit = limit;
85
+
86
+ /* istanbul ignore if */
87
+ if (!callSite || !fileName) return '<anonymous>';
88
+ if (!withLine) return fileName;
89
+ return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
90
+ },
91
+
92
+ getResolvedFilename(filepath, baseDir) {
93
+ const reg = /[/\\]/g;
94
+ return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
95
+ },
96
+
97
+ /**
98
+ * 字节码类
99
+ */
100
+ isBytecodeClass (exports) {
101
+ let isClass = false;
102
+
103
+ // 标识
104
+ if (exports.toString().indexOf('[class') != -1) {
105
+ isClass = true;
106
+ }
107
+ // TODO 更严谨的判断,应该加上文件名和路径
108
+
109
+ return isClass;
110
+ },
111
+
112
+ /**
113
+ * 文件类型
114
+ */
115
+ filePatterns () {
116
+ const files = (process.env.EE_TYPESCRIPT === 'true' && Module._extensions['.ts'])
117
+ ? [ '**/*.(js|ts)', '!**/*.d.ts' ]
118
+ : [ '**/*.js','**/*.jsc' ];
119
+
120
+ return files;
121
+ }
122
+
123
+ };
124
+
125
+
126
+ /**
127
+ * Capture call site stack from v8.
128
+ * https://github.com/v8/v8/wiki/Stack-Trace-API
129
+ */
130
+
131
+ function prepareObjectStackTrace(obj, stack) {
132
+ return stack;
133
+ }
@@ -1,59 +1,59 @@
1
- 'use strict';
2
-
3
- const debug = require('debug')('ee-core#sequencify');
4
-
5
- function sequence(tasks, names, results, missing, recursive, nest, optional, parent) {
6
- names.forEach(function(name) {
7
- if (results.requires[name]) return;
8
-
9
- const node = tasks[name];
10
-
11
- if (!node) {
12
- if (optional === true) return;
13
- missing.push(name);
14
- } else if (nest.includes(name)) {
15
- nest.push(name);
16
- recursive.push(nest.slice(0));
17
- nest.pop(name);
18
- } else if (node.dependencies.length || node.optionalDependencies.length) {
19
- nest.push(name);
20
- if (node.dependencies.length) {
21
- sequence(tasks, node.dependencies, results, missing, recursive, nest, optional, name);
22
- }
23
- if (node.optionalDependencies.length) {
24
- sequence(tasks, node.optionalDependencies, results, missing, recursive, nest, true, name);
25
- }
26
- nest.pop(name);
27
- }
28
- if (!optional) {
29
- results.requires[name] = true;
30
- debug('task: %s is enabled by %s', name, parent);
31
- }
32
- if (!results.sequence.includes(name)) {
33
- results.sequence.push(name);
34
- }
35
- });
36
- }
37
-
38
- // tasks: object with keys as task names
39
- // names: array of task names
40
- module.exports = function(tasks, names) {
41
- const results = {
42
- sequence: [],
43
- requires: {},
44
- }; // the final sequence
45
- const missing = []; // missing tasks
46
- const recursive = []; // recursive task dependencies
47
-
48
- sequence(tasks, names, results, missing, recursive, [], false, 'app');
49
-
50
- if (missing.length || recursive.length) {
51
- results.sequence = []; // results are incomplete at best, completely wrong at worst, remove them to avoid confusion
52
- }
53
-
54
- return {
55
- sequence: results.sequence.filter(item => results.requires[item]),
56
- missingTasks: missing,
57
- recursiveDependencies: recursive,
58
- };
59
- };
1
+ 'use strict';
2
+
3
+ const debug = require('debug')('ee-core#sequencify');
4
+
5
+ function sequence(tasks, names, results, missing, recursive, nest, optional, parent) {
6
+ names.forEach(function(name) {
7
+ if (results.requires[name]) return;
8
+
9
+ const node = tasks[name];
10
+
11
+ if (!node) {
12
+ if (optional === true) return;
13
+ missing.push(name);
14
+ } else if (nest.includes(name)) {
15
+ nest.push(name);
16
+ recursive.push(nest.slice(0));
17
+ nest.pop(name);
18
+ } else if (node.dependencies.length || node.optionalDependencies.length) {
19
+ nest.push(name);
20
+ if (node.dependencies.length) {
21
+ sequence(tasks, node.dependencies, results, missing, recursive, nest, optional, name);
22
+ }
23
+ if (node.optionalDependencies.length) {
24
+ sequence(tasks, node.optionalDependencies, results, missing, recursive, nest, true, name);
25
+ }
26
+ nest.pop(name);
27
+ }
28
+ if (!optional) {
29
+ results.requires[name] = true;
30
+ debug('task: %s is enabled by %s', name, parent);
31
+ }
32
+ if (!results.sequence.includes(name)) {
33
+ results.sequence.push(name);
34
+ }
35
+ });
36
+ }
37
+
38
+ // tasks: object with keys as task names
39
+ // names: array of task names
40
+ module.exports = function(tasks, names) {
41
+ const results = {
42
+ sequence: [],
43
+ requires: {},
44
+ }; // the final sequence
45
+ const missing = []; // missing tasks
46
+ const recursive = []; // recursive task dependencies
47
+
48
+ sequence(tasks, names, results, missing, recursive, [], false, 'app');
49
+
50
+ if (missing.length || recursive.length) {
51
+ results.sequence = []; // results are incomplete at best, completely wrong at worst, remove them to avoid confusion
52
+ }
53
+
54
+ return {
55
+ sequence: results.sequence.filter(item => results.requires[item]),
56
+ missingTasks: missing,
57
+ recursiveDependencies: recursive,
58
+ };
59
+ };
@@ -1,77 +1,77 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const MAP = Symbol('Timing#map');
5
- const LIST = Symbol('Timing#list');
6
-
7
-
8
- class Timing {
9
-
10
- constructor() {
11
- this._enable = true;
12
- this[MAP] = new Map();
13
- this[LIST] = [];
14
-
15
- this.init();
16
- }
17
-
18
- init() {
19
- // process start time
20
- this.start('Process Start', Date.now() - Math.floor((process.uptime() * 1000)));
21
- this.end('Process Start');
22
-
23
- if (typeof process.scriptStartTime === 'number') {
24
- // js script start execute time
25
- this.start('Script Start', process.scriptStartTime);
26
- this.end('Script Start');
27
- }
28
- }
29
-
30
- start(name, start) {
31
- if (!name || !this._enable) return;
32
-
33
- if (this[MAP].has(name)) this.end(name);
34
-
35
- start = start || Date.now();
36
- const item = {
37
- name,
38
- start,
39
- end: undefined,
40
- duration: undefined,
41
- pid: process.pid,
42
- index: this[LIST].length,
43
- };
44
- this[MAP].set(name, item);
45
- this[LIST].push(item);
46
- return item;
47
- }
48
-
49
- end(name) {
50
- if (!name || !this._enable) return;
51
- assert(this[MAP].has(name), `should run timing.start('${name}') first`);
52
-
53
- const item = this[MAP].get(name);
54
- item.end = Date.now();
55
- item.duration = item.end - item.start;
56
- return item;
57
- }
58
-
59
- enable() {
60
- this._enable = true;
61
- }
62
-
63
- disable() {
64
- this._enable = false;
65
- }
66
-
67
- clear() {
68
- this[MAP].clear();
69
- this[LIST] = [];
70
- }
71
-
72
- toJSON() {
73
- return this[LIST];
74
- }
75
- }
76
-
77
- module.exports = Timing;
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+ const MAP = Symbol('Timing#map');
5
+ const LIST = Symbol('Timing#list');
6
+
7
+
8
+ class Timing {
9
+
10
+ constructor() {
11
+ this._enable = true;
12
+ this[MAP] = new Map();
13
+ this[LIST] = [];
14
+
15
+ this.init();
16
+ }
17
+
18
+ init() {
19
+ // process start time
20
+ this.start('Process Start', Date.now() - Math.floor((process.uptime() * 1000)));
21
+ this.end('Process Start');
22
+
23
+ if (typeof process.scriptStartTime === 'number') {
24
+ // js script start execute time
25
+ this.start('Script Start', process.scriptStartTime);
26
+ this.end('Script Start');
27
+ }
28
+ }
29
+
30
+ start(name, start) {
31
+ if (!name || !this._enable) return;
32
+
33
+ if (this[MAP].has(name)) this.end(name);
34
+
35
+ start = start || Date.now();
36
+ const item = {
37
+ name,
38
+ start,
39
+ end: undefined,
40
+ duration: undefined,
41
+ pid: process.pid,
42
+ index: this[LIST].length,
43
+ };
44
+ this[MAP].set(name, item);
45
+ this[LIST].push(item);
46
+ return item;
47
+ }
48
+
49
+ end(name) {
50
+ if (!name || !this._enable) return;
51
+ assert(this[MAP].has(name), `should run timing.start('${name}') first`);
52
+
53
+ const item = this[MAP].get(name);
54
+ item.end = Date.now();
55
+ item.duration = item.end - item.start;
56
+ return item;
57
+ }
58
+
59
+ enable() {
60
+ this._enable = true;
61
+ }
62
+
63
+ disable() {
64
+ this._enable = false;
65
+ }
66
+
67
+ clear() {
68
+ this[MAP].clear();
69
+ this[LIST] = [];
70
+ }
71
+
72
+ toJSON() {
73
+ return this[LIST];
74
+ }
75
+ }
76
+
77
+ module.exports = Timing;