egg 4.0.0-beta.6 → 4.0.0-beta.7

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/dist/commonjs/app/extend/context.d.ts +6 -3
  2. package/dist/commonjs/app/extend/context.js +2 -1
  3. package/dist/commonjs/app/extend/context.types.d.ts +16 -0
  4. package/dist/commonjs/app/extend/context.types.js +3 -0
  5. package/dist/commonjs/app/middleware/site_file.d.ts +3 -1
  6. package/dist/commonjs/app/middleware/site_file.js +2 -2
  7. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +2 -2
  8. package/dist/commonjs/lib/core/messenger/index.js +2 -2
  9. package/dist/commonjs/lib/core/messenger/ipc.d.ts +5 -3
  10. package/dist/commonjs/lib/core/messenger/ipc.js +23 -16
  11. package/dist/commonjs/lib/core/messenger/local.d.ts +2 -2
  12. package/dist/commonjs/lib/core/messenger/local.js +14 -11
  13. package/dist/commonjs/lib/egg.d.ts +1 -0
  14. package/dist/commonjs/lib/egg.js +2 -1
  15. package/dist/commonjs/lib/egg.types.d.ts +6 -0
  16. package/dist/commonjs/lib/egg.types.js +3 -0
  17. package/dist/esm/app/extend/context.d.ts +6 -3
  18. package/dist/esm/app/extend/context.js +2 -1
  19. package/dist/esm/app/extend/context.types.d.ts +16 -0
  20. package/dist/esm/app/extend/context.types.js +2 -0
  21. package/dist/esm/app/middleware/site_file.d.ts +3 -1
  22. package/dist/esm/app/middleware/site_file.js +2 -2
  23. package/dist/esm/lib/core/messenger/IMessenger.d.ts +2 -2
  24. package/dist/esm/lib/core/messenger/index.js +2 -2
  25. package/dist/esm/lib/core/messenger/ipc.d.ts +5 -3
  26. package/dist/esm/lib/core/messenger/ipc.js +23 -16
  27. package/dist/esm/lib/core/messenger/local.d.ts +2 -2
  28. package/dist/esm/lib/core/messenger/local.js +14 -11
  29. package/dist/esm/lib/egg.d.ts +1 -0
  30. package/dist/esm/lib/egg.js +2 -1
  31. package/dist/esm/lib/egg.types.d.ts +6 -0
  32. package/dist/esm/lib/egg.types.js +2 -0
  33. package/dist/package.json +1 -1
  34. package/package.json +11 -5
  35. package/src/app/extend/context.ts +10 -4
  36. package/src/app/extend/context.types.ts +24 -0
  37. package/src/app/middleware/site_file.ts +1 -1
  38. package/src/lib/core/messenger/IMessenger.ts +2 -2
  39. package/src/lib/core/messenger/index.ts +1 -1
  40. package/src/lib/core/messenger/ipc.ts +23 -16
  41. package/src/lib/core/messenger/local.ts +12 -10
  42. package/src/lib/egg.ts +2 -0
  43. package/src/lib/egg.types.ts +6 -0
  44. package/src/lib/type.ts +3 -0
@@ -11,12 +11,12 @@ export interface IMessenger extends EventEmitter {
11
11
 
12
12
  /**
13
13
  * send message to the specified process
14
- * @param {String} pid - the process id of the receiver
14
+ * @param {String} workerId - the workerId of the receiver
15
15
  * @param {String} action - message key
16
16
  * @param {Object} data - message value
17
17
  * @return {Messenger} this
18
18
  */
19
- sendTo(pid: string, action: string, data?: unknown): IMessenger;
19
+ sendTo(workerId: string, action: string, data?: unknown): IMessenger;
20
20
 
21
21
  /**
22
22
  * send message to one app worker by random
@@ -11,5 +11,5 @@ export type { IMessenger } from './IMessenger.js';
11
11
  export function create(egg: EggApplicationCore): IMessenger {
12
12
  return egg.options.mode === 'single'
13
13
  ? new LocalMessenger(egg)
14
- : new IPCMessenger();
14
+ : new IPCMessenger(egg);
15
15
  }
@@ -3,24 +3,28 @@ import { debuglog } from 'node:util';
3
3
  import workerThreads from 'node:worker_threads';
4
4
  import { sendmessage } from 'sendmessage';
5
5
  import type { IMessenger } from './IMessenger.js';
6
+ import type { EggApplicationCore } from '../../egg.js';
6
7
 
7
- const debug = debuglog('egg:lib:core:messenger:ipc');
8
+ const debug = debuglog('egg/lib/core/messenger/ipc');
8
9
 
9
10
  /**
10
11
  * Communication between app worker and agent worker by IPC channel
11
12
  */
12
13
  export class Messenger extends EventEmitter implements IMessenger {
13
14
  readonly pid: string;
15
+ readonly egg: EggApplicationCore;
14
16
  opids: string[] = [];
15
17
 
16
- constructor() {
18
+ constructor(egg: EggApplicationCore) {
17
19
  super();
18
20
  this.pid = String(process.pid);
21
+ this.egg = egg;
19
22
  // pids of agent or app managed by master
20
23
  // - retrieve app worker pids when it's an agent worker
21
24
  // - retrieve agent worker pids when it's an app worker
22
- this.on('egg-pids', pids => {
23
- this.opids = pids;
25
+ this.on('egg-pids', workerIds => {
26
+ debug('[%s:%s] got egg-pids %j', this.egg.type, this.pid, workerIds);
27
+ this.opids = workerIds.map((workerId: number) => String(workerId));
24
28
  });
25
29
  this.onMessage = this.onMessage.bind(this);
26
30
  process.on('message', this.onMessage);
@@ -36,7 +40,7 @@ export class Messenger extends EventEmitter implements IMessenger {
36
40
  * @return {Messenger} this
37
41
  */
38
42
  broadcast(action: string, data?: unknown): Messenger {
39
- debug('[%s] broadcast %s with %j', this.pid, action, data);
43
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
40
44
  this.send(action, data, 'app');
41
45
  this.send(action, data, 'agent');
42
46
  return this;
@@ -44,21 +48,21 @@ export class Messenger extends EventEmitter implements IMessenger {
44
48
 
45
49
  /**
46
50
  * send message to the specified process
47
- * @param {String} pid - the process id of the receiver
51
+ * @param {String} workerId - the workerId of the receiver
48
52
  * @param {String} action - message key
49
53
  * @param {Object} data - message value
50
54
  * @return {Messenger} this
51
55
  */
52
- sendTo(pid: string, action: string, data?: unknown): Messenger {
53
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
56
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
57
+ debug('[%s:%s] send %s with %j to workerId:%s', this.egg.type, this.pid, action, data, workerId);
54
58
  sendmessage(process, {
55
59
  action,
56
60
  data,
57
61
  /**
58
62
  * @deprecated Keep compatible, please use receiverWorkerId instead
59
63
  */
60
- receiverPid: String(pid),
61
- receiverWorkerId: String(pid),
64
+ receiverPid: String(workerId),
65
+ receiverWorkerId: String(workerId),
62
66
  });
63
67
  return this;
64
68
  }
@@ -73,11 +77,12 @@ export class Messenger extends EventEmitter implements IMessenger {
73
77
  */
74
78
  sendRandom(action: string, data?: unknown): Messenger {
75
79
  if (this.opids.length === 0) {
80
+ debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
76
81
  return this;
77
82
  }
78
83
  const index = Math.floor(Math.random() * this.opids.length);
79
- const pid = this.opids[index];
80
- this.sendTo(String(pid), action, data);
84
+ const workerId = this.opids[index];
85
+ this.sendTo(workerId, action, data);
81
86
  return this;
82
87
  }
83
88
 
@@ -88,7 +93,7 @@ export class Messenger extends EventEmitter implements IMessenger {
88
93
  * @return {Messenger} this
89
94
  */
90
95
  sendToApp(action: string, data?: unknown): Messenger {
91
- debug('[%s] send %s with %j to all app', this.pid, action, data);
96
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
92
97
  this.send(action, data, 'app');
93
98
  return this;
94
99
  }
@@ -100,7 +105,7 @@ export class Messenger extends EventEmitter implements IMessenger {
100
105
  * @return {Messenger} this
101
106
  */
102
107
  sendToAgent(action: string, data?: unknown): Messenger {
103
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
108
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
104
109
  this.send(action, data, 'agent');
105
110
  return this;
106
111
  }
@@ -122,9 +127,11 @@ export class Messenger extends EventEmitter implements IMessenger {
122
127
 
123
128
  onMessage(message: any) {
124
129
  if (typeof message?.action === 'string') {
125
- debug('[%s] got message %s with %j, receiverWorkerId: %s',
126
- this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
130
+ debug('[%s:%s] got message %s with %j, receiverWorkerId: %s',
131
+ this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
127
132
  this.emit(message.action, message.data);
133
+ } else {
134
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
128
135
  }
129
136
  }
130
137
 
@@ -3,7 +3,7 @@ import EventEmitter from 'node:events';
3
3
  import type { IMessenger } from './IMessenger.js';
4
4
  import type { EggApplicationCore } from '../../egg.js';
5
5
 
6
- const debug = debuglog('egg:lib:core:messenger:local');
6
+ const debug = debuglog('egg/lib/core/messenger/local');
7
7
 
8
8
  /**
9
9
  * Communication between app worker and agent worker with EventEmitter
@@ -25,7 +25,7 @@ export class Messenger extends EventEmitter implements IMessenger {
25
25
  * @return {Messenger} this
26
26
  */
27
27
  broadcast(action: string, data?: unknown): Messenger {
28
- debug('[%s] broadcast %s with %j', this.pid, action, data);
28
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
29
29
  this.send(action, data, 'both');
30
30
  return this;
31
31
  }
@@ -34,14 +34,14 @@ export class Messenger extends EventEmitter implements IMessenger {
34
34
  * send message to the specified process
35
35
  * Notice: in single process mode, it only can send to self process,
36
36
  * and it will send to both agent and app's messengers.
37
- * @param {String} pid - the process id of the receiver
37
+ * @param {String} workerId - the workerId of the receiver
38
38
  * @param {String} action - message key
39
39
  * @param {Object} data - message value
40
40
  * @return {Messenger} this
41
41
  */
42
- sendTo(pid: string, action: string, data?: unknown): Messenger {
43
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
44
- if (String(pid) !== this.pid) {
42
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
43
+ debug('[%s:%s] send %s with %j to %s', this.egg.type, this.pid, action, data, workerId);
44
+ if (String(workerId) !== this.pid) {
45
45
  return this;
46
46
  }
47
47
  this.send(action, data, 'both');
@@ -58,7 +58,7 @@ export class Messenger extends EventEmitter implements IMessenger {
58
58
  * @return {Messenger} this
59
59
  */
60
60
  sendRandom(action: string, data?: unknown): Messenger {
61
- debug('[%s] send %s with %j to opposite', this.pid, action, data);
61
+ debug('[%s:%s] send %s with %j to opposite', this.egg.type, this.pid, action, data);
62
62
  this.send(action, data, 'opposite');
63
63
  return this;
64
64
  }
@@ -70,7 +70,7 @@ export class Messenger extends EventEmitter implements IMessenger {
70
70
  * @return {Messenger} this
71
71
  */
72
72
  sendToApp(action: string, data?: unknown): Messenger {
73
- debug('[%s] send %s with %j to all app', this.pid, action, data);
73
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
74
74
  this.send(action, data, 'application');
75
75
  return this;
76
76
  }
@@ -82,7 +82,7 @@ export class Messenger extends EventEmitter implements IMessenger {
82
82
  * @return {Messenger} this
83
83
  */
84
84
  sendToAgent(action: string, data?: unknown): Messenger {
85
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
85
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
86
86
  this.send(action, data, 'agent');
87
87
  return this;
88
88
  }
@@ -130,8 +130,10 @@ export class Messenger extends EventEmitter implements IMessenger {
130
130
 
131
131
  onMessage(message: any) {
132
132
  if (typeof message?.action === 'string') {
133
- debug('[%s] got message %s with %j', this.pid, message.action, message.data);
133
+ debug('[%s:%s] got message %s with %j', this.egg.type, this.pid, message.action, message.data);
134
134
  this.emit(message.action, message.data);
135
+ } else {
136
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
135
137
  }
136
138
  }
137
139
 
package/src/lib/egg.ts CHANGED
@@ -43,6 +43,8 @@ import { BaseHookClass } from './core/base_hook_class.js';
43
43
  import type { EggApplicationLoader } from './loader/index.js';
44
44
  import { getSourceDirname } from './utils.js';
45
45
 
46
+ import './egg.types.js';
47
+
46
48
  const EGG_PATH = Symbol.for('egg#eggPath');
47
49
 
48
50
  export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
@@ -0,0 +1,6 @@
1
+ declare module '@eggjs/core' {
2
+ // add EggApplicationCore overrides types
3
+ interface EggCore {
4
+ inspect(): any;
5
+ }
6
+ }
package/src/lib/type.ts CHANGED
@@ -15,6 +15,9 @@ import type { MetaMiddlewareOptions } from '../app/middleware/meta.js';
15
15
  import type { NotFoundMiddlewareOptions } from '../app/middleware/notfound.js';
16
16
  import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
17
17
 
18
+ // import @eggjs/watcher types
19
+ // import '@eggjs/watcher';
20
+
18
21
  type IgnoreItem = string | RegExp | ((ctx: ContextDelegation) => boolean);
19
22
  type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
20
23