egg 4.1.0-beta.35 → 4.1.0-beta.36

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 (101) hide show
  1. package/dist/agent.d.ts +7 -3
  2. package/dist/agent.js +10 -6
  3. package/dist/ajv.d.ts +1 -1
  4. package/dist/ajv.js +3 -2
  5. package/dist/aop.d.ts +1 -1
  6. package/dist/aop.js +3 -2
  7. package/dist/app/extend/context.d.ts +181 -178
  8. package/dist/app/extend/context.js +244 -259
  9. package/dist/app/extend/helper.d.ts +40 -35
  10. package/dist/app/extend/helper.js +45 -41
  11. package/dist/app/extend/request.d.ts +135 -131
  12. package/dist/app/extend/request.js +219 -258
  13. package/dist/app/extend/response.d.ts +28 -24
  14. package/dist/app/extend/response.js +36 -34
  15. package/dist/app/middleware/body_parser.d.ts +2 -2
  16. package/dist/app/middleware/body_parser.js +7 -3
  17. package/dist/app/middleware/meta.d.ts +8 -8
  18. package/dist/app/middleware/meta.js +15 -18
  19. package/dist/app/middleware/notfound.d.ts +8 -5
  20. package/dist/app/middleware/notfound.js +25 -28
  21. package/dist/app/middleware/override_method.d.ts +2 -2
  22. package/dist/app/middleware/override_method.js +7 -3
  23. package/dist/app/middleware/site_file.d.ts +11 -7
  24. package/dist/app/middleware/site_file.js +37 -52
  25. package/dist/config/config.default.d.ts +11 -6
  26. package/dist/config/config.default.js +258 -375
  27. package/dist/config/config.local.d.ts +6 -3
  28. package/dist/config/config.local.js +7 -8
  29. package/dist/config/config.unittest.d.ts +6 -3
  30. package/dist/config/config.unittest.js +10 -8
  31. package/dist/config/plugin.d.ts +6 -2
  32. package/dist/config/plugin.js +67 -131
  33. package/dist/dal.d.ts +1 -1
  34. package/dist/dal.js +3 -2
  35. package/dist/errors.d.ts +1 -1
  36. package/dist/errors.js +3 -2
  37. package/dist/helper.d.ts +1 -1
  38. package/dist/helper.js +3 -2
  39. package/dist/index.d.ts +24 -106
  40. package/dist/index.js +23 -89
  41. package/dist/lib/agent.d.ts +21 -15
  42. package/dist/lib/agent.js +53 -45
  43. package/dist/lib/application.d.ts +60 -54
  44. package/dist/lib/application.js +199 -249
  45. package/dist/lib/core/base_context_class.d.ts +23 -17
  46. package/dist/lib/core/base_context_class.js +17 -15
  47. package/dist/lib/core/base_context_logger.d.ts +39 -35
  48. package/dist/lib/core/base_context_logger.js +58 -60
  49. package/dist/lib/core/base_hook_class.d.ts +18 -11
  50. package/dist/lib/core/base_hook_class.js +26 -22
  51. package/dist/lib/core/context_httpclient.d.ts +21 -16
  52. package/dist/lib/core/context_httpclient.js +29 -26
  53. package/dist/lib/core/httpclient.d.ts +14 -12
  54. package/dist/lib/core/httpclient.js +34 -37
  55. package/dist/lib/core/logger.d.ts +7 -3
  56. package/dist/lib/core/logger.js +30 -36
  57. package/dist/lib/core/messenger/IMessenger.d.ts +53 -49
  58. package/dist/lib/core/messenger/IMessenger.js +1 -2
  59. package/dist/lib/core/messenger/base.d.ts +11 -7
  60. package/dist/lib/core/messenger/base.js +30 -29
  61. package/dist/lib/core/messenger/index.d.ts +10 -6
  62. package/dist/lib/core/messenger/index.js +11 -8
  63. package/dist/lib/core/messenger/ipc.d.ts +62 -57
  64. package/dist/lib/core/messenger/ipc.js +126 -138
  65. package/dist/lib/core/messenger/local.d.ts +63 -58
  66. package/dist/lib/core/messenger/local.js +126 -131
  67. package/dist/lib/core/utils.d.ts +5 -2
  68. package/dist/lib/core/utils.js +44 -66
  69. package/dist/lib/define.d.ts +72 -67
  70. package/dist/lib/define.js +54 -53
  71. package/dist/lib/egg.d.ts +283 -281
  72. package/dist/lib/egg.js +512 -573
  73. package/dist/lib/error/CookieLimitExceedError.d.ts +7 -4
  74. package/dist/lib/error/CookieLimitExceedError.js +15 -12
  75. package/dist/lib/error/MessageUnhandledRejectionError.d.ts +7 -4
  76. package/dist/lib/error/MessageUnhandledRejectionError.js +15 -12
  77. package/dist/lib/error/index.d.ts +3 -2
  78. package/dist/lib/error/index.js +4 -3
  79. package/dist/lib/loader/AgentWorkerLoader.d.ts +15 -10
  80. package/dist/lib/loader/AgentWorkerLoader.js +22 -18
  81. package/dist/lib/loader/AppWorkerLoader.d.ts +20 -15
  82. package/dist/lib/loader/AppWorkerLoader.js +35 -37
  83. package/dist/lib/loader/EggApplicationLoader.d.ts +7 -3
  84. package/dist/lib/loader/EggApplicationLoader.js +7 -4
  85. package/dist/lib/loader/index.d.ts +4 -3
  86. package/dist/lib/loader/index.js +5 -4
  87. package/dist/lib/start.d.ts +24 -20
  88. package/dist/lib/start.js +32 -42
  89. package/dist/lib/types.d.ts +288 -286
  90. package/dist/lib/types.js +2 -2
  91. package/dist/lib/types.plugin.d.ts +21 -21
  92. package/dist/lib/types.plugin.js +23 -24
  93. package/dist/orm.d.ts +1 -1
  94. package/dist/orm.js +3 -2
  95. package/dist/schedule.d.ts +2 -2
  96. package/dist/schedule.js +5 -5
  97. package/dist/transaction.d.ts +1 -1
  98. package/dist/transaction.js +3 -2
  99. package/dist/urllib.d.ts +1 -1
  100. package/dist/urllib.js +3 -2
  101. package/package.json +60 -64
@@ -1,29 +1,30 @@
1
- import { EventEmitter, captureRejectionSymbol } from 'node:events';
2
- import { EggApplicationCore } from "../../egg.js";
3
- import { MessageUnhandledRejectionError } from "../../error/index.js";
4
- export class BaseMessenger extends EventEmitter {
5
- egg;
6
- constructor(egg) {
7
- super({ captureRejections: true });
8
- this.egg = egg;
9
- this[captureRejectionSymbol] = this.onRejection.bind(this);
10
- }
11
- onRejection(err, event, ...args) {
12
- this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args));
13
- }
14
- emit(eventName, ...args) {
15
- const hasListeners = this.listenerCount(eventName) > 0;
16
- try {
17
- return super.emit(eventName, ...args);
18
- }
19
- catch (e) {
20
- let err = e;
21
- if (!(err instanceof Error)) {
22
- err = new Error(String(err));
23
- }
24
- this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args));
25
- return hasListeners;
26
- }
27
- }
28
- }
29
- //# sourceMappingUR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JLENBQUM7WUFDSCxPQUFPLEtBQUssQ0FBQyxJQUFJLENBQUMsU0FBUyxFQUFFLEdBQUcsSUFBSSxDQUFDLENBQUM7UUFDeEMsQ0FBQztRQUFDLE9BQU8sQ0FBVSxFQUFFLENBQUM7WUFDcEIsSUFBSSxHQUFHLEdBQUcsQ0FBVSxDQUFDO1lBQ3JCLElBQUksQ0FBQyxDQUFDLEdBQUcsWUFBWSxLQUFLLENBQUMsRUFBRSxDQUFDO2dCQUM1QixHQUFHLEdBQUcsSUFBSSxLQUFLLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7WUFDL0IsQ0FBQztZQUNELElBQUksQ0FBQyxHQUFHLENBQUMsVUFBVSxDQUFDLEtBQUssQ0FBQyxJQUFJLDhCQUE4QixDQUFDLEdBQUcsRUFBRSxTQUFTLEVBQUUsSUFBSSxDQUFDLENBQUMsQ0FBQztZQUNwRixPQUFPLFlBQVksQ0FBQztRQUN0QixDQUFDO0lBQ0gsQ0FBQztDQUNGIn0=
1
+ import { MessageUnhandledRejectionError } from "../../error/MessageUnhandledRejectionError.js";
2
+ import "../../egg.js";
3
+ import { EventEmitter, captureRejectionSymbol } from "node:events";
4
+
5
+ //#region src/lib/core/messenger/base.ts
6
+ var BaseMessenger = class extends EventEmitter {
7
+ egg;
8
+ constructor(egg) {
9
+ super({ captureRejections: true });
10
+ this.egg = egg;
11
+ this[captureRejectionSymbol] = this.onRejection.bind(this);
12
+ }
13
+ onRejection(err, event, ...args) {
14
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args));
15
+ }
16
+ emit(eventName, ...args) {
17
+ const hasListeners = this.listenerCount(eventName) > 0;
18
+ try {
19
+ return super.emit(eventName, ...args);
20
+ } catch (e) {
21
+ let err = e;
22
+ if (!(err instanceof Error)) err = new Error(String(err));
23
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args));
24
+ return hasListeners;
25
+ }
26
+ }
27
+ };
28
+
29
+ //#endregion
30
+ export { BaseMessenger };
@@ -1,7 +1,11 @@
1
- import type { EggApplicationCore } from '../../egg.ts';
2
- import type { IMessenger } from './IMessenger.ts';
3
- export type { IMessenger } from './IMessenger.ts';
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+
4
+ //#region src/lib/core/messenger/index.d.ts
5
+
4
6
  /**
5
- * @class Messenger
6
- */
7
- export declare function create(egg: EggApplicationCore): IMessenger;
7
+ * @class Messenger
8
+ */
9
+ declare function create(egg: EggApplicationCore): IMessenger;
10
+ //#endregion
11
+ export { type IMessenger, create };
@@ -1,10 +1,13 @@
1
- import { Messenger as IPCMessenger } from "./ipc.js";
2
- import { Messenger as LocalMessenger } from "./local.js";
1
+ import { Messenger } from "./ipc.js";
2
+ import { Messenger as Messenger$1 } from "./local.js";
3
+
4
+ //#region src/lib/core/messenger/index.ts
3
5
  /**
4
- * @class Messenger
5
- */
6
- export function create(egg) {
7
- const messenger = egg.options.mode === 'single' ? new LocalMessenger(egg) : new IPCMessenger(egg);
8
- return messenger;
6
+ * @class Messenger
7
+ */
8
+ function create(egg) {
9
+ return egg.options.mode === "single" ? new Messenger$1(egg) : new Messenger(egg);
9
10
  }
10
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL2NvcmUvbWVzc2VuZ2VyL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBLE9BQU8sRUFBRSxTQUFTLElBQUksWUFBWSxFQUFFLE1BQU0sVUFBVSxDQUFDO0FBQ3JELE9BQU8sRUFBRSxTQUFTLElBQUksY0FBYyxFQUFFLE1BQU0sWUFBWSxDQUFDO0FBSXpEOztHQUVHO0FBQ0gsTUFBTSxVQUFVLE1BQU0sQ0FBQyxHQUF1QjtJQUM1QyxNQUFNLFNBQVMsR0FBRyxHQUFHLENBQUMsT0FBTyxDQUFDLElBQUksS0FBSyxRQUFRLENBQUMsQ0FBQyxDQUFDLElBQUksY0FBYyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxJQUFJLFlBQVksQ0FBQyxHQUFHLENBQUMsQ0FBQztJQUNsRyxPQUFPLFNBQVMsQ0FBQztBQUNuQixDQUFDIn0=
11
+
12
+ //#endregion
13
+ export { create };
@@ -1,59 +1,64 @@
1
- import type { EggApplicationCore } from '../../egg.ts';
2
- import { BaseMessenger } from './base.ts';
3
- import type { IMessenger } from './IMessenger.ts';
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+ import { BaseMessenger } from "./base.js";
4
+
5
+ //#region src/lib/core/messenger/ipc.d.ts
6
+
4
7
  /**
5
- * Communication between app worker and agent worker by IPC channel
6
- */
7
- export declare class Messenger extends BaseMessenger implements IMessenger {
8
- #private;
9
- readonly pid: string;
10
- opids: string[];
11
- constructor(egg: EggApplicationCore);
12
- /**
13
- * Send message to all agent and app
14
- * @param {String} action - message key
15
- * @param {Object} data - message value
16
- * @return {Messenger} this
17
- */
18
- broadcast(action: string, data?: unknown): Messenger;
19
- /**
20
- * send message to the specified process
21
- * @param {String} workerId - the workerId of the receiver
22
- * @param {String} action - message key
23
- * @param {Object} data - message value
24
- * @return {Messenger} this
25
- */
26
- sendTo(workerId: string, action: string, data?: unknown): Messenger;
27
- /**
28
- * send message to one app worker by random
29
- * - if it's running in agent, it will send to one of app workers
30
- * - if it's running in app, it will send to agent
31
- * @param {String} action - message key
32
- * @param {Object} data - message value
33
- * @return {Messenger} this
34
- */
35
- sendRandom(action: string, data?: unknown): Messenger;
36
- /**
37
- * send message to app
38
- * @param {String} action - message key
39
- * @param {Object} data - message value
40
- * @return {Messenger} this
41
- */
42
- sendToApp(action: string, data?: unknown): Messenger;
43
- /**
44
- * send message to agent
45
- * @param {String} action - message key
46
- * @param {Object} data - message value
47
- * @return {Messenger} this
48
- */
49
- sendToAgent(action: string, data?: unknown): Messenger;
50
- /**
51
- * @param {String} action - message key
52
- * @param {Object} data - message value
53
- * @param {String} to - let master know how to send message
54
- * @return {Messenger} this
55
- */
56
- send(action: string, data: unknown | undefined, to?: string): Messenger;
57
- onMessage(message: any): void;
58
- close(): void;
8
+ * Communication between app worker and agent worker by IPC channel
9
+ */
10
+ declare class Messenger extends BaseMessenger implements IMessenger {
11
+ #private;
12
+ readonly pid: string;
13
+ opids: string[];
14
+ constructor(egg: EggApplicationCore);
15
+ /**
16
+ * Send message to all agent and app
17
+ * @param {String} action - message key
18
+ * @param {Object} data - message value
19
+ * @return {Messenger} this
20
+ */
21
+ broadcast(action: string, data?: unknown): Messenger;
22
+ /**
23
+ * send message to the specified process
24
+ * @param {String} workerId - the workerId of the receiver
25
+ * @param {String} action - message key
26
+ * @param {Object} data - message value
27
+ * @return {Messenger} this
28
+ */
29
+ sendTo(workerId: string, action: string, data?: unknown): Messenger;
30
+ /**
31
+ * send message to one app worker by random
32
+ * - if it's running in agent, it will send to one of app workers
33
+ * - if it's running in app, it will send to agent
34
+ * @param {String} action - message key
35
+ * @param {Object} data - message value
36
+ * @return {Messenger} this
37
+ */
38
+ sendRandom(action: string, data?: unknown): Messenger;
39
+ /**
40
+ * send message to app
41
+ * @param {String} action - message key
42
+ * @param {Object} data - message value
43
+ * @return {Messenger} this
44
+ */
45
+ sendToApp(action: string, data?: unknown): Messenger;
46
+ /**
47
+ * send message to agent
48
+ * @param {String} action - message key
49
+ * @param {Object} data - message value
50
+ * @return {Messenger} this
51
+ */
52
+ sendToAgent(action: string, data?: unknown): Messenger;
53
+ /**
54
+ * @param {String} action - message key
55
+ * @param {Object} data - message value
56
+ * @param {String} to - let master know how to send message
57
+ * @return {Messenger} this
58
+ */
59
+ send(action: string, data: unknown | undefined, to?: string): Messenger;
60
+ onMessage(message: any): void;
61
+ close(): void;
59
62
  }
63
+ //#endregion
64
+ export { Messenger };
@@ -1,140 +1,128 @@
1
- import { debuglog } from 'node:util';
2
- import workerThreads from 'node:worker_threads';
3
- import { sendmessage } from 'sendmessage';
4
1
  import { BaseMessenger } from "./base.js";
5
- const debug = debuglog('egg/lib/core/messenger/ipc');
2
+ import { debuglog } from "node:util";
3
+ import workerThreads from "node:worker_threads";
4
+ import { sendmessage } from "sendmessage";
5
+
6
+ //#region src/lib/core/messenger/ipc.ts
7
+ const debug = debuglog("egg/lib/core/messenger/ipc");
6
8
  /**
7
- * Communication between app worker and agent worker by IPC channel
8
- */
9
- export class Messenger extends BaseMessenger {
10
- pid;
11
- opids = [];
12
- constructor(egg) {
13
- super(egg);
14
- this.pid = String(process.pid);
15
- // pids of agent or app managed by master
16
- // - retrieve app worker pids when it's an agent worker
17
- // - retrieve agent worker pids when it's an app worker
18
- this.on('egg-pids', (workerIds) => {
19
- debug('[%s:%s] got egg-pids %j', this.egg.type, this.pid, workerIds);
20
- this.opids = workerIds.map((workerId) => String(workerId));
21
- });
22
- this.onMessage = this.onMessage.bind(this);
23
- process.on('message', this.onMessage);
24
- if (!workerThreads.isMainThread) {
25
- workerThreads.parentPort.on('message', this.onMessage);
26
- }
27
- }
28
- /**
29
- * Send message to all agent and app
30
- * @param {String} action - message key
31
- * @param {Object} data - message value
32
- * @return {Messenger} this
33
- */
34
- broadcast(action, data) {
35
- debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
36
- this.send(action, data, 'app');
37
- this.send(action, data, 'agent');
38
- return this;
39
- }
40
- /**
41
- * send message to the specified process
42
- * @param {String} workerId - the workerId of the receiver
43
- * @param {String} action - message key
44
- * @param {Object} data - message value
45
- * @return {Messenger} this
46
- */
47
- sendTo(workerId, action, data) {
48
- debug('[%s:%s] send %s with %j to workerId:%s', this.egg.type, this.pid, action, data, workerId);
49
- const message = {
50
- action,
51
- data,
52
- /**
53
- * @deprecated Keep compatible, please use receiverWorkerId instead
54
- */
55
- receiverPid: String(workerId),
56
- receiverWorkerId: String(workerId),
57
- };
58
- this.#sendMessage(message);
59
- return this;
60
- }
61
- /**
62
- * send message to one app worker by random
63
- * - if it's running in agent, it will send to one of app workers
64
- * - if it's running in app, it will send to agent
65
- * @param {String} action - message key
66
- * @param {Object} data - message value
67
- * @return {Messenger} this
68
- */
69
- sendRandom(action, data) {
70
- if (this.opids.length === 0) {
71
- debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
72
- return this;
73
- }
74
- const index = Math.floor(Math.random() * this.opids.length);
75
- const workerId = this.opids[index];
76
- this.sendTo(workerId, action, data);
77
- return this;
78
- }
79
- /**
80
- * send message to app
81
- * @param {String} action - message key
82
- * @param {Object} data - message value
83
- * @return {Messenger} this
84
- */
85
- sendToApp(action, data) {
86
- debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
87
- this.send(action, data, 'app');
88
- return this;
89
- }
90
- /**
91
- * send message to agent
92
- * @param {String} action - message key
93
- * @param {Object} data - message value
94
- * @return {Messenger} this
95
- */
96
- sendToAgent(action, data) {
97
- debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
98
- this.send(action, data, 'agent');
99
- return this;
100
- }
101
- /**
102
- * @param {String} action - message key
103
- * @param {Object} data - message value
104
- * @param {String} to - let master know how to send message
105
- * @return {Messenger} this
106
- */
107
- send(action, data, to) {
108
- debug('send message %s with %j to %s', action, data, to);
109
- this.#sendMessage({
110
- action,
111
- data,
112
- to,
113
- });
114
- return this;
115
- }
116
- #sendMessage(message) {
117
- debug('[%s:%s] send message %j, mode: %s', this.egg.type, this.pid, message, this.egg.options.mode);
118
- sendmessage(process, message);
119
- }
120
- onMessage(message) {
121
- if (typeof message?.action === 'string') {
122
- debug('[%s:%s] got message %s with %j, receiverWorkerId: %s', this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
123
- this.emit(message.action, message.data);
124
- }
125
- else {
126
- if (message?.type === 'Buffer') {
127
- // {"type":"Buffer","data":[255,153,....]
128
- debug('[%s:%s] got an invalid message: %s', this.egg.type, this.pid, Buffer.from(message.data));
129
- }
130
- else {
131
- debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
132
- }
133
- }
134
- }
135
- close() {
136
- process.removeListener('message', this.onMessage);
137
- this.removeAllListeners();
138
- }
139
- }
140
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaXBjLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9jb3JlL21lc3Nlbmdlci9pcGMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLFdBQVcsQ0FBQztBQUNyQyxPQUFPLGFBQWEsTUFBTSxxQkFBcUIsQ0FBQztBQUVoRCxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBRzFDLE9BQU8sRUFBRSxhQUFhLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFHMUMsTUFBTSxLQUFLLEdBQUcsUUFBUSxDQUFDLDRCQUE0QixDQUFDLENBQUM7QUFFckQ7O0dBRUc7QUFDSCxNQUFNLE9BQU8sU0FBVSxTQUFRLGFBQWE7SUFDakMsR0FBRyxDQUFTO0lBQ3JCLEtBQUssR0FBYSxFQUFFLENBQUM7SUFFckIsWUFBWSxHQUF1QjtRQUNqQyxLQUFLLENBQUMsR0FBRyxDQUFDLENBQUM7UUFDWCxJQUFJLENBQUMsR0FBRyxHQUFHLE1BQU0sQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLENBQUM7UUFDL0IseUNBQXlDO1FBQ3pDLHVEQUF1RDtRQUN2RCx1REFBdUQ7UUFDdkQsSUFBSSxDQUFDLEVBQUUsQ0FBQyxVQUFVLEVBQUUsQ0FBQyxTQUFTLEVBQUUsRUFBRTtZQUNoQyxLQUFLLENBQUMseUJBQXlCLEVBQUUsSUFBSSxDQUF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0UsQ0FBQztRQUNILENBQUM7SUFDSCxDQUFDO0lBRUQsS0FBSztRQUNILE9BQU8sQ0FBQyxjQUFjLENBQUMsU0FBUyxFQUFFLElBQUksQ0FBQyxTQUFTLENBQUMsQ0FBQztRQUNsRCxJQUFJLENBQUMsa0JBQWtCLEVBQUUsQ0FBQztJQUM1QixDQUFDO0NBT0YifQ==
9
+ * Communication between app worker and agent worker by IPC channel
10
+ */
11
+ var Messenger = class extends BaseMessenger {
12
+ pid;
13
+ opids = [];
14
+ constructor(egg) {
15
+ super(egg);
16
+ this.pid = String(process.pid);
17
+ this.on("egg-pids", (workerIds) => {
18
+ debug("[%s:%s] got egg-pids %j", this.egg.type, this.pid, workerIds);
19
+ this.opids = workerIds.map((workerId) => String(workerId));
20
+ });
21
+ this.onMessage = this.onMessage.bind(this);
22
+ process.on("message", this.onMessage);
23
+ if (!workerThreads.isMainThread) workerThreads.parentPort.on("message", this.onMessage);
24
+ }
25
+ /**
26
+ * Send message to all agent and app
27
+ * @param {String} action - message key
28
+ * @param {Object} data - message value
29
+ * @return {Messenger} this
30
+ */
31
+ broadcast(action, data) {
32
+ debug("[%s:%s] broadcast %s with %j", this.egg.type, this.pid, action, data);
33
+ this.send(action, data, "app");
34
+ this.send(action, data, "agent");
35
+ return this;
36
+ }
37
+ /**
38
+ * send message to the specified process
39
+ * @param {String} workerId - the workerId of the receiver
40
+ * @param {String} action - message key
41
+ * @param {Object} data - message value
42
+ * @return {Messenger} this
43
+ */
44
+ sendTo(workerId, action, data) {
45
+ debug("[%s:%s] send %s with %j to workerId:%s", this.egg.type, this.pid, action, data, workerId);
46
+ const message = {
47
+ action,
48
+ data,
49
+ receiverPid: String(workerId),
50
+ receiverWorkerId: String(workerId)
51
+ };
52
+ this.#sendMessage(message);
53
+ return this;
54
+ }
55
+ /**
56
+ * send message to one app worker by random
57
+ * - if it's running in agent, it will send to one of app workers
58
+ * - if it's running in app, it will send to agent
59
+ * @param {String} action - message key
60
+ * @param {Object} data - message value
61
+ * @return {Messenger} this
62
+ */
63
+ sendRandom(action, data) {
64
+ if (this.opids.length === 0) {
65
+ debug("[%s:%s] no pids, ignore sendRandom %s with %j", this.egg.type, this.pid, action, data);
66
+ return this;
67
+ }
68
+ const index = Math.floor(Math.random() * this.opids.length);
69
+ const workerId = this.opids[index];
70
+ this.sendTo(workerId, action, data);
71
+ return this;
72
+ }
73
+ /**
74
+ * send message to app
75
+ * @param {String} action - message key
76
+ * @param {Object} data - message value
77
+ * @return {Messenger} this
78
+ */
79
+ sendToApp(action, data) {
80
+ debug("[%s:%s] send %s with %j to all app", this.egg.type, this.pid, action, data);
81
+ this.send(action, data, "app");
82
+ return this;
83
+ }
84
+ /**
85
+ * send message to agent
86
+ * @param {String} action - message key
87
+ * @param {Object} data - message value
88
+ * @return {Messenger} this
89
+ */
90
+ sendToAgent(action, data) {
91
+ debug("[%s:%s] send %s with %j to all agent", this.egg.type, this.pid, action, data);
92
+ this.send(action, data, "agent");
93
+ return this;
94
+ }
95
+ /**
96
+ * @param {String} action - message key
97
+ * @param {Object} data - message value
98
+ * @param {String} to - let master know how to send message
99
+ * @return {Messenger} this
100
+ */
101
+ send(action, data, to) {
102
+ debug("send message %s with %j to %s", action, data, to);
103
+ this.#sendMessage({
104
+ action,
105
+ data,
106
+ to
107
+ });
108
+ return this;
109
+ }
110
+ #sendMessage(message) {
111
+ debug("[%s:%s] send message %j, mode: %s", this.egg.type, this.pid, message, this.egg.options.mode);
112
+ sendmessage(process, message);
113
+ }
114
+ onMessage(message) {
115
+ if (typeof message?.action === "string") {
116
+ debug("[%s:%s] got message %s with %j, receiverWorkerId: %s", this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
117
+ this.emit(message.action, message.data);
118
+ } else if (message?.type === "Buffer") debug("[%s:%s] got an invalid message: %s", this.egg.type, this.pid, Buffer.from(message.data));
119
+ else debug("[%s:%s] got an invalid message %j", this.egg.type, this.pid, message);
120
+ }
121
+ close() {
122
+ process.removeListener("message", this.onMessage);
123
+ this.removeAllListeners();
124
+ }
125
+ };
126
+
127
+ //#endregion
128
+ export { Messenger };
@@ -1,60 +1,65 @@
1
- import type { EggApplicationCore } from '../../egg.ts';
2
- import { BaseMessenger } from './base.ts';
3
- import type { IMessenger } from './IMessenger.ts';
1
+ import { IMessenger } from "./IMessenger.js";
2
+ import { EggApplicationCore } from "../../egg.js";
3
+ import { BaseMessenger } from "./base.js";
4
+
5
+ //#region src/lib/core/messenger/local.d.ts
6
+
4
7
  /**
5
- * Communication between app worker and agent worker with EventEmitter
6
- */
7
- export declare class Messenger extends BaseMessenger implements IMessenger {
8
- readonly pid: string;
9
- constructor(egg: EggApplicationCore);
10
- /**
11
- * Send message to all agent and app
12
- * @param {String} action - message key
13
- * @param {Object} data - message value
14
- * @return {Messenger} this
15
- */
16
- broadcast(action: string, data?: unknown): Messenger;
17
- /**
18
- * send message to the specified process
19
- * Notice: in single process mode, it only can send to self process,
20
- * and it will send to both agent and app's messengers.
21
- * @param {String} workerId - the workerId of the receiver
22
- * @param {String} action - message key
23
- * @param {Object} data - message value
24
- * @return {Messenger} this
25
- */
26
- sendTo(workerId: string, action: string, data?: unknown): Messenger;
27
- /**
28
- * send message to one worker by random
29
- * Notice: in single process mode, we only start one agent worker and one app worker
30
- * - if it's running in agent, it will send to one of app workers
31
- * - if it's running in app, it will send to agent
32
- * @param {String} action - message key
33
- * @param {Object} data - message value
34
- * @return {Messenger} this
35
- */
36
- sendRandom(action: string, data?: unknown): Messenger;
37
- /**
38
- * send message to app
39
- * @param {String} action - message key
40
- * @param {Object} data - message value
41
- * @return {Messenger} this
42
- */
43
- sendToApp(action: string, data?: unknown): Messenger;
44
- /**
45
- * send message to agent
46
- * @param {String} action - message key
47
- * @param {Object} data - message value
48
- * @return {Messenger} this
49
- */
50
- sendToAgent(action: string, data?: unknown): Messenger;
51
- /**
52
- * @param {String} action - message key
53
- * @param {Object} data - message value
54
- * @param {String} to - let master know how to send message
55
- * @return {Messenger} this
56
- */
57
- send(action: string, data: unknown, to?: string): Messenger;
58
- onMessage(message: any): void;
59
- close(): void;
8
+ * Communication between app worker and agent worker with EventEmitter
9
+ */
10
+ declare class Messenger extends BaseMessenger implements IMessenger {
11
+ readonly pid: string;
12
+ constructor(egg: EggApplicationCore);
13
+ /**
14
+ * Send message to all agent and app
15
+ * @param {String} action - message key
16
+ * @param {Object} data - message value
17
+ * @return {Messenger} this
18
+ */
19
+ broadcast(action: string, data?: unknown): Messenger;
20
+ /**
21
+ * send message to the specified process
22
+ * Notice: in single process mode, it only can send to self process,
23
+ * and it will send to both agent and app's messengers.
24
+ * @param {String} workerId - the workerId of the receiver
25
+ * @param {String} action - message key
26
+ * @param {Object} data - message value
27
+ * @return {Messenger} this
28
+ */
29
+ sendTo(workerId: string, action: string, data?: unknown): Messenger;
30
+ /**
31
+ * send message to one worker by random
32
+ * Notice: in single process mode, we only start one agent worker and one app worker
33
+ * - if it's running in agent, it will send to one of app workers
34
+ * - if it's running in app, it will send to agent
35
+ * @param {String} action - message key
36
+ * @param {Object} data - message value
37
+ * @return {Messenger} this
38
+ */
39
+ sendRandom(action: string, data?: unknown): Messenger;
40
+ /**
41
+ * send message to app
42
+ * @param {String} action - message key
43
+ * @param {Object} data - message value
44
+ * @return {Messenger} this
45
+ */
46
+ sendToApp(action: string, data?: unknown): Messenger;
47
+ /**
48
+ * send message to agent
49
+ * @param {String} action - message key
50
+ * @param {Object} data - message value
51
+ * @return {Messenger} this
52
+ */
53
+ sendToAgent(action: string, data?: unknown): Messenger;
54
+ /**
55
+ * @param {String} action - message key
56
+ * @param {Object} data - message value
57
+ * @param {String} to - let master know how to send message
58
+ * @return {Messenger} this
59
+ */
60
+ send(action: string, data: unknown, to?: string): Messenger;
61
+ onMessage(message: any): void;
62
+ close(): void;
60
63
  }
64
+ //#endregion
65
+ export { Messenger };