egg 4.1.0-beta.3 → 4.1.0-beta.31

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