@eggjs/core 6.4.1 → 6.5.0

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 (44) hide show
  1. package/README.md +22 -22
  2. package/dist/commonjs/egg.d.ts +17 -18
  3. package/dist/commonjs/egg.js +9 -9
  4. package/dist/commonjs/lifecycle.d.ts +2 -2
  5. package/dist/commonjs/lifecycle.js +16 -13
  6. package/dist/commonjs/loader/context_loader.js +2 -2
  7. package/dist/commonjs/loader/egg_loader.d.ts +13 -13
  8. package/dist/commonjs/loader/egg_loader.js +77 -62
  9. package/dist/commonjs/loader/file_loader.d.ts +3 -3
  10. package/dist/commonjs/loader/file_loader.js +22 -24
  11. package/dist/commonjs/singleton.d.ts +2 -2
  12. package/dist/commonjs/singleton.js +5 -6
  13. package/dist/commonjs/utils/index.d.ts +2 -2
  14. package/dist/commonjs/utils/index.js +4 -4
  15. package/dist/commonjs/utils/sequencify.d.ts +1 -1
  16. package/dist/commonjs/utils/sequencify.js +18 -13
  17. package/dist/commonjs/utils/timing.js +14 -8
  18. package/dist/esm/egg.d.ts +17 -18
  19. package/dist/esm/egg.js +11 -11
  20. package/dist/esm/lifecycle.d.ts +2 -2
  21. package/dist/esm/lifecycle.js +16 -13
  22. package/dist/esm/loader/context_loader.js +2 -2
  23. package/dist/esm/loader/egg_loader.d.ts +13 -13
  24. package/dist/esm/loader/egg_loader.js +80 -65
  25. package/dist/esm/loader/file_loader.d.ts +3 -3
  26. package/dist/esm/loader/file_loader.js +23 -25
  27. package/dist/esm/singleton.d.ts +2 -2
  28. package/dist/esm/singleton.js +5 -6
  29. package/dist/esm/utils/index.d.ts +2 -2
  30. package/dist/esm/utils/index.js +4 -4
  31. package/dist/esm/utils/sequencify.d.ts +1 -1
  32. package/dist/esm/utils/sequencify.js +18 -13
  33. package/dist/esm/utils/timing.js +14 -8
  34. package/dist/package.json +1 -1
  35. package/package.json +16 -6
  36. package/src/egg.ts +159 -59
  37. package/src/lifecycle.ts +72 -33
  38. package/src/loader/context_loader.ts +9 -7
  39. package/src/loader/egg_loader.ts +445 -183
  40. package/src/loader/file_loader.ts +78 -37
  41. package/src/singleton.ts +64 -26
  42. package/src/utils/index.ts +20 -13
  43. package/src/utils/sequencify.ts +50 -15
  44. package/src/utils/timing.ts +27 -13
@@ -15,12 +15,11 @@ export interface TimingItem {
15
15
 
16
16
  export class Timing {
17
17
  #enable: boolean;
18
- #startTime: number | null;
18
+ #startTime: number;
19
19
  #map: Map<string, TimingItem>;
20
20
  #list: TimingItem[];
21
21
  constructor() {
22
22
  this.#enable = true;
23
- this.#startTime = null;
24
23
  this.#map = new Map();
25
24
  this.#list = [];
26
25
  this.init();
@@ -28,10 +27,16 @@ export class Timing {
28
27
 
29
28
  init() {
30
29
  // process start time
31
- this.start('Process Start', Date.now() - Math.floor(process.uptime() * 1000));
30
+ this.start(
31
+ 'Process Start',
32
+ Date.now() - Math.floor(process.uptime() * 1000)
33
+ );
32
34
  this.end('Process Start');
33
35
 
34
- if ('scriptStartTime' in process && typeof process.scriptStartTime === 'number') {
36
+ if (
37
+ 'scriptStartTime' in process &&
38
+ typeof process.scriptStartTime === 'number'
39
+ ) {
35
40
  // js script start execute time
36
41
  this.start('Script Start', process.scriptStartTime);
37
42
  this.end('Script Start');
@@ -46,7 +51,7 @@ export class Timing {
46
51
  }
47
52
 
48
53
  start = start || Date.now();
49
- if (this.#startTime === null) {
54
+ if (!this.#startTime) {
50
55
  this.#startTime = start;
51
56
  }
52
57
  const item: TimingItem = {
@@ -63,9 +68,8 @@ export class Timing {
63
68
 
64
69
  end(name?: string) {
65
70
  if (!name || !this.#enable) return;
66
- assert(this.#map.has(name), `should run timing.start('${name}') first`);
67
-
68
- const item = this.#map.get(name)!;
71
+ const item = this.#map.get(name);
72
+ assert(item, `should run timing.start('${name}') first`);
69
73
  item.end = Date.now();
70
74
  item.duration = item.end - item.start;
71
75
  debug('end %j', item);
@@ -91,24 +95,34 @@ export class Timing {
91
95
 
92
96
  itemToString(timelineEnd: number, item: TimingItem, times: number) {
93
97
  const isEnd = typeof item.duration === 'number';
94
- const duration = isEnd ? item.duration! : timelineEnd - item.start;
95
- const offset = item.start - this.#startTime!;
98
+ const duration = isEnd
99
+ ? (item.duration as number)
100
+ : timelineEnd - item.start;
101
+ const offset = item.start - this.#startTime;
96
102
  const status = `${duration}ms${isEnd ? '' : ' NOT_END'}`;
97
103
  const timespan = Math.floor(Number((offset * times).toFixed(6)));
98
104
  let timeline = Math.floor(Number((duration * times).toFixed(6)));
99
105
  timeline = timeline > 0 ? timeline : 1; // make sure there is at least one unit
100
106
  const message = `#${item.index} ${item.name}`;
101
- return ' '.repeat(timespan) + '▇'.repeat(timeline) + ` [${status}] - ${message}`;
107
+ return (
108
+ ' '.repeat(timespan) + '▇'.repeat(timeline) + ` [${status}] - ${message}`
109
+ );
102
110
  }
103
111
 
104
112
  toString(prefix = 'egg start timeline:', width = 50) {
105
113
  const timelineEnd = Date.now();
106
- const timelineDuration = timelineEnd - this.#startTime!;
114
+ const timelineDuration = timelineEnd - this.#startTime;
107
115
  let times = 1;
108
116
  if (timelineDuration > width) {
109
117
  times = width / timelineDuration;
110
118
  }
111
119
  // follow https://github.com/node-modules/time-profile/blob/master/lib/profiler.js#L88
112
- return prefix + EOL + this.#list.map(item => this.itemToString(timelineEnd, item, times)).join(EOL);
120
+ return (
121
+ prefix +
122
+ EOL +
123
+ this.#list
124
+ .map(item => this.itemToString(timelineEnd, item, times))
125
+ .join(EOL)
126
+ );
113
127
  }
114
128
  }