egg 4.0.10 → 4.1.0-beta.10
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.
- package/README.md +4 -22
- package/dist/agent.d.ts +8 -0
- package/dist/agent.js +11 -0
- package/dist/app/extend/context.d.ts +207 -0
- package/dist/app/extend/context.js +244 -0
- package/{src/app/extend/helper.ts → dist/app/extend/helper.d.ts} +8 -10
- package/dist/app/extend/helper.js +47 -0
- package/dist/app/extend/request.d.ts +145 -0
- package/dist/app/extend/request.js +226 -0
- package/{src/app/extend/response.ts → dist/app/extend/response.d.ts} +7 -16
- package/dist/app/extend/response.js +36 -0
- package/dist/app/middleware/body_parser.d.ts +2 -0
- package/dist/app/middleware/body_parser.js +7 -0
- package/dist/app/middleware/meta.d.ts +11 -0
- package/dist/app/middleware/meta.js +14 -0
- package/dist/app/middleware/notfound.d.ts +11 -0
- package/dist/app/middleware/notfound.js +26 -0
- package/dist/app/middleware/override_method.d.ts +2 -0
- package/dist/app/middleware/override_method.js +7 -0
- package/dist/app/middleware/site_file.d.ts +13 -0
- package/dist/app/middleware/site_file.js +38 -0
- package/dist/config/config.default.d.ts +12 -0
- package/dist/config/config.default.js +260 -0
- package/dist/config/config.local.d.ts +10 -0
- package/dist/config/config.local.js +7 -0
- package/dist/config/config.unittest.d.ts +9 -0
- package/dist/config/config.unittest.js +10 -0
- package/{src/config/plugin.ts → dist/config/plugin.d.ts} +41 -49
- package/dist/config/plugin.js +54 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +21 -0
- package/dist/lib/agent.d.ts +22 -0
- package/dist/lib/agent.js +56 -0
- package/dist/lib/application.d.ts +71 -0
- package/dist/lib/application.js +209 -0
- package/dist/lib/core/base_context_class.d.ts +23 -0
- package/dist/lib/core/base_context_class.js +19 -0
- package/dist/lib/core/base_context_logger.d.ts +40 -0
- package/dist/lib/core/base_context_logger.js +58 -0
- package/dist/lib/core/base_hook_class.d.ts +18 -0
- package/dist/lib/core/base_hook_class.js +26 -0
- package/dist/lib/core/context_httpclient.d.ts +22 -0
- package/dist/lib/core/context_httpclient.js +29 -0
- package/dist/lib/core/httpclient.d.ts +17 -0
- package/dist/lib/core/httpclient.js +36 -0
- package/dist/lib/core/logger.d.ts +7 -0
- package/dist/lib/core/logger.js +31 -0
- package/{src/lib/core/messenger/IMessenger.ts → dist/lib/core/messenger/IMessenger.d.ts} +6 -10
- package/dist/lib/core/messenger/IMessenger.js +1 -0
- package/dist/lib/core/messenger/base.d.ts +12 -0
- package/dist/lib/core/messenger/base.js +29 -0
- package/dist/lib/core/messenger/index.d.ts +11 -0
- package/dist/lib/core/messenger/index.js +13 -0
- package/dist/lib/core/messenger/ipc.d.ts +63 -0
- package/dist/lib/core/messenger/ipc.js +122 -0
- package/dist/lib/core/messenger/local.d.ts +65 -0
- package/dist/lib/core/messenger/local.js +126 -0
- package/dist/lib/core/utils.d.ts +5 -0
- package/dist/lib/core/utils.js +48 -0
- package/dist/lib/egg.d.ts +306 -0
- package/dist/lib/egg.js +512 -0
- package/dist/lib/error/CookieLimitExceedError.d.ts +8 -0
- package/dist/lib/error/CookieLimitExceedError.js +15 -0
- package/dist/lib/error/MessageUnhandledRejectionError.d.ts +8 -0
- package/dist/lib/error/MessageUnhandledRejectionError.js +15 -0
- package/dist/lib/error/index.d.ts +3 -0
- package/dist/lib/error/index.js +4 -0
- package/dist/lib/loader/AgentWorkerLoader.d.ts +17 -0
- package/dist/lib/loader/AgentWorkerLoader.js +24 -0
- package/dist/lib/loader/AppWorkerLoader.d.ts +22 -0
- package/dist/lib/loader/AppWorkerLoader.js +37 -0
- package/dist/lib/loader/EggApplicationLoader.d.ts +8 -0
- package/dist/lib/loader/EggApplicationLoader.js +7 -0
- package/dist/lib/loader/index.d.ts +4 -0
- package/dist/lib/loader/index.js +5 -0
- package/dist/lib/start.d.ts +28 -0
- package/dist/lib/start.js +36 -0
- package/{src/lib/types.ts → dist/lib/types.d.ts} +73 -94
- package/dist/lib/types.js +36 -0
- package/dist/lib/utils.d.ts +5 -0
- package/dist/lib/utils.js +14 -0
- package/dist/urllib.d.ts +1 -0
- package/dist/urllib.js +3 -0
- package/package.json +81 -99
- package/README.zh-CN.md +0 -61
- package/dist/commonjs/agent.d.ts +0 -4
- package/dist/commonjs/agent.js +0 -10
- package/dist/commonjs/app/extend/context.d.ts +0 -204
- package/dist/commonjs/app/extend/context.js +0 -262
- package/dist/commonjs/app/extend/helper.d.ts +0 -37
- package/dist/commonjs/app/extend/helper.js +0 -49
- package/dist/commonjs/app/extend/request.d.ts +0 -141
- package/dist/commonjs/app/extend/request.js +0 -270
- package/dist/commonjs/app/extend/response.d.ts +0 -31
- package/dist/commonjs/app/extend/response.js +0 -37
- package/dist/commonjs/app/middleware/body_parser.d.ts +0 -2
- package/dist/commonjs/app/middleware/body_parser.js +0 -8
- package/dist/commonjs/app/middleware/meta.d.ts +0 -10
- package/dist/commonjs/app/middleware/meta.js +0 -22
- package/dist/commonjs/app/middleware/notfound.d.ts +0 -7
- package/dist/commonjs/app/middleware/notfound.js +0 -31
- package/dist/commonjs/app/middleware/override_method.d.ts +0 -2
- package/dist/commonjs/app/middleware/override_method.js +0 -8
- package/dist/commonjs/app/middleware/site_file.d.ts +0 -9
- package/dist/commonjs/app/middleware/site_file.js +0 -58
- package/dist/commonjs/config/config.default.d.ts +0 -9
- package/dist/commonjs/config/config.default.js +0 -379
- package/dist/commonjs/config/config.local.d.ts +0 -8
- package/dist/commonjs/config/config.local.js +0 -12
- package/dist/commonjs/config/config.unittest.d.ts +0 -7
- package/dist/commonjs/config/config.unittest.js +0 -11
- package/dist/commonjs/config/plugin.d.ts +0 -122
- package/dist/commonjs/config/plugin.js +0 -124
- package/dist/commonjs/index.d.ts +0 -69
- package/dist/commonjs/index.js +0 -83
- package/dist/commonjs/lib/agent.d.ts +0 -19
- package/dist/commonjs/lib/agent.js +0 -58
- package/dist/commonjs/lib/application.d.ts +0 -67
- package/dist/commonjs/lib/application.js +0 -266
- package/dist/commonjs/lib/core/base_context_class.d.ts +0 -17
- package/dist/commonjs/lib/core/base_context_class.js +0 -21
- package/dist/commonjs/lib/core/base_context_logger.d.ts +0 -36
- package/dist/commonjs/lib/core/base_context_logger.js +0 -64
- package/dist/commonjs/lib/core/base_hook_class.d.ts +0 -11
- package/dist/commonjs/lib/core/base_hook_class.js +0 -29
- package/dist/commonjs/lib/core/context_httpclient.d.ts +0 -16
- package/dist/commonjs/lib/core/context_httpclient.js +0 -30
- package/dist/commonjs/lib/core/httpclient.d.ts +0 -14
- package/dist/commonjs/lib/core/httpclient.js +0 -42
- package/dist/commonjs/lib/core/logger.d.ts +0 -3
- package/dist/commonjs/lib/core/logger.js +0 -40
- package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +0 -50
- package/dist/commonjs/lib/core/messenger/IMessenger.js +0 -3
- package/dist/commonjs/lib/core/messenger/base.d.ts +0 -8
- package/dist/commonjs/lib/core/messenger/base.js +0 -31
- package/dist/commonjs/lib/core/messenger/index.d.ts +0 -7
- package/dist/commonjs/lib/core/messenger/index.js +0 -15
- package/dist/commonjs/lib/core/messenger/ipc.d.ts +0 -58
- package/dist/commonjs/lib/core/messenger/ipc.js +0 -135
- package/dist/commonjs/lib/core/messenger/local.d.ts +0 -60
- package/dist/commonjs/lib/core/messenger/local.js +0 -132
- package/dist/commonjs/lib/core/utils.d.ts +0 -2
- package/dist/commonjs/lib/core/utils.js +0 -77
- package/dist/commonjs/lib/egg.d.ts +0 -305
- package/dist/commonjs/lib/egg.js +0 -628
- package/dist/commonjs/lib/error/CookieLimitExceedError.d.ts +0 -5
- package/dist/commonjs/lib/error/CookieLimitExceedError.js +0 -16
- package/dist/commonjs/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
- package/dist/commonjs/lib/error/MessageUnhandledRejectionError.js +0 -16
- package/dist/commonjs/lib/error/index.d.ts +0 -2
- package/dist/commonjs/lib/error/index.js +0 -19
- package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +0 -12
- package/dist/commonjs/lib/loader/AgentWorkerLoader.js +0 -24
- package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +0 -17
- package/dist/commonjs/lib/loader/AppWorkerLoader.js +0 -43
- package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +0 -4
- package/dist/commonjs/lib/loader/EggApplicationLoader.js +0 -8
- package/dist/commonjs/lib/loader/index.d.ts +0 -3
- package/dist/commonjs/lib/loader/index.js +0 -22
- package/dist/commonjs/lib/start.d.ts +0 -24
- package/dist/commonjs/lib/start.js +0 -49
- package/dist/commonjs/lib/types.d.ts +0 -257
- package/dist/commonjs/lib/types.js +0 -16
- package/dist/commonjs/lib/utils.d.ts +0 -2
- package/dist/commonjs/lib/utils.js +0 -21
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/urllib.d.ts +0 -1
- package/dist/commonjs/urllib.js +0 -18
- package/dist/esm/agent.d.ts +0 -4
- package/dist/esm/agent.js +0 -7
- package/dist/esm/app/extend/context.d.ts +0 -204
- package/dist/esm/app/extend/context.js +0 -259
- package/dist/esm/app/extend/helper.d.ts +0 -37
- package/dist/esm/app/extend/helper.js +0 -43
- package/dist/esm/app/extend/request.d.ts +0 -141
- package/dist/esm/app/extend/request.js +0 -264
- package/dist/esm/app/extend/response.d.ts +0 -31
- package/dist/esm/app/extend/response.js +0 -34
- package/dist/esm/app/middleware/body_parser.d.ts +0 -2
- package/dist/esm/app/middleware/body_parser.js +0 -3
- package/dist/esm/app/middleware/meta.d.ts +0 -10
- package/dist/esm/app/middleware/meta.js +0 -20
- package/dist/esm/app/middleware/notfound.d.ts +0 -7
- package/dist/esm/app/middleware/notfound.js +0 -29
- package/dist/esm/app/middleware/override_method.d.ts +0 -2
- package/dist/esm/app/middleware/override_method.js +0 -3
- package/dist/esm/app/middleware/site_file.d.ts +0 -9
- package/dist/esm/app/middleware/site_file.js +0 -53
- package/dist/esm/config/config.default.d.ts +0 -9
- package/dist/esm/config/config.default.js +0 -374
- package/dist/esm/config/config.local.d.ts +0 -8
- package/dist/esm/config/config.local.js +0 -10
- package/dist/esm/config/config.unittest.d.ts +0 -7
- package/dist/esm/config/config.unittest.js +0 -9
- package/dist/esm/config/favicon.png +0 -0
- package/dist/esm/config/plugin.d.ts +0 -122
- package/dist/esm/config/plugin.js +0 -122
- package/dist/esm/index.d.ts +0 -69
- package/dist/esm/index.js +0 -73
- package/dist/esm/lib/agent.d.ts +0 -19
- package/dist/esm/lib/agent.js +0 -54
- package/dist/esm/lib/application.d.ts +0 -67
- package/dist/esm/lib/application.js +0 -259
- package/dist/esm/lib/core/base_context_class.d.ts +0 -17
- package/dist/esm/lib/core/base_context_class.js +0 -17
- package/dist/esm/lib/core/base_context_logger.d.ts +0 -36
- package/dist/esm/lib/core/base_context_logger.js +0 -60
- package/dist/esm/lib/core/base_hook_class.d.ts +0 -11
- package/dist/esm/lib/core/base_hook_class.js +0 -22
- package/dist/esm/lib/core/context_httpclient.d.ts +0 -16
- package/dist/esm/lib/core/context_httpclient.js +0 -26
- package/dist/esm/lib/core/httpclient.d.ts +0 -14
- package/dist/esm/lib/core/httpclient.js +0 -38
- package/dist/esm/lib/core/logger.d.ts +0 -3
- package/dist/esm/lib/core/logger.js +0 -37
- package/dist/esm/lib/core/messenger/IMessenger.d.ts +0 -50
- package/dist/esm/lib/core/messenger/IMessenger.js +0 -2
- package/dist/esm/lib/core/messenger/base.d.ts +0 -8
- package/dist/esm/lib/core/messenger/base.js +0 -27
- package/dist/esm/lib/core/messenger/index.d.ts +0 -7
- package/dist/esm/lib/core/messenger/index.js +0 -12
- package/dist/esm/lib/core/messenger/ipc.d.ts +0 -58
- package/dist/esm/lib/core/messenger/ipc.js +0 -128
- package/dist/esm/lib/core/messenger/local.d.ts +0 -60
- package/dist/esm/lib/core/messenger/local.js +0 -128
- package/dist/esm/lib/core/utils.d.ts +0 -2
- package/dist/esm/lib/core/utils.js +0 -70
- package/dist/esm/lib/egg.d.ts +0 -305
- package/dist/esm/lib/egg.js +0 -586
- package/dist/esm/lib/error/CookieLimitExceedError.d.ts +0 -5
- package/dist/esm/lib/error/CookieLimitExceedError.js +0 -12
- package/dist/esm/lib/error/MessageUnhandledRejectionError.d.ts +0 -5
- package/dist/esm/lib/error/MessageUnhandledRejectionError.js +0 -12
- package/dist/esm/lib/error/index.d.ts +0 -2
- package/dist/esm/lib/error/index.js +0 -3
- package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +0 -12
- package/dist/esm/lib/loader/AgentWorkerLoader.js +0 -20
- package/dist/esm/lib/loader/AppWorkerLoader.d.ts +0 -17
- package/dist/esm/lib/loader/AppWorkerLoader.js +0 -39
- package/dist/esm/lib/loader/EggApplicationLoader.d.ts +0 -4
- package/dist/esm/lib/loader/EggApplicationLoader.js +0 -4
- package/dist/esm/lib/loader/index.d.ts +0 -3
- package/dist/esm/lib/loader/index.js +0 -4
- package/dist/esm/lib/start.d.ts +0 -24
- package/dist/esm/lib/start.js +0 -43
- package/dist/esm/lib/types.d.ts +0 -257
- package/dist/esm/lib/types.js +0 -14
- package/dist/esm/lib/utils.d.ts +0 -2
- package/dist/esm/lib/utils.js +0 -14
- package/dist/esm/package.json +0 -3
- package/dist/esm/urllib.d.ts +0 -1
- package/dist/esm/urllib.js +0 -2
- package/dist/package.json +0 -4
- package/src/agent.ts +0 -7
- package/src/app/extend/context.ts +0 -332
- package/src/app/extend/request.ts +0 -300
- package/src/app/middleware/body_parser.ts +0 -3
- package/src/app/middleware/meta.ts +0 -27
- package/src/app/middleware/notfound.ts +0 -41
- package/src/app/middleware/override_method.ts +0 -3
- package/src/app/middleware/site_file.ts +0 -68
- package/src/config/config.default.ts +0 -404
- package/src/config/config.local.ts +0 -11
- package/src/config/config.unittest.ts +0 -10
- package/src/config/favicon.png +0 -0
- package/src/index.ts +0 -120
- package/src/lib/agent.ts +0 -66
- package/src/lib/application.ts +0 -308
- package/src/lib/core/base_context_class.ts +0 -24
- package/src/lib/core/base_context_logger.ts +0 -67
- package/src/lib/core/base_hook_class.ts +0 -30
- package/src/lib/core/context_httpclient.ts +0 -33
- package/src/lib/core/httpclient.ts +0 -64
- package/src/lib/core/logger.ts +0 -42
- package/src/lib/core/messenger/base.ts +0 -30
- package/src/lib/core/messenger/index.ts +0 -16
- package/src/lib/core/messenger/ipc.ts +0 -146
- package/src/lib/core/messenger/local.ts +0 -147
- package/src/lib/core/utils.ts +0 -77
- package/src/lib/egg.ts +0 -705
- package/src/lib/error/CookieLimitExceedError.ts +0 -12
- package/src/lib/error/MessageUnhandledRejectionError.ts +0 -12
- package/src/lib/error/index.ts +0 -2
- package/src/lib/loader/AgentWorkerLoader.ts +0 -21
- package/src/lib/loader/AppWorkerLoader.ts +0 -42
- package/src/lib/loader/EggApplicationLoader.ts +0 -5
- package/src/lib/loader/index.ts +0 -3
- package/src/lib/start.ts +0 -66
- package/src/lib/utils.ts +0 -15
- package/src/urllib.ts +0 -1
- /package/dist/{commonjs/config → config}/favicon.png +0 -0
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import workerThreads from 'node:worker_threads';
|
|
3
|
-
import { sendmessage } from 'sendmessage';
|
|
4
|
-
import type { IMessenger } from './IMessenger.js';
|
|
5
|
-
import type { EggApplicationCore } from '../../egg.js';
|
|
6
|
-
import { BaseMessenger } from './base.js';
|
|
7
|
-
|
|
8
|
-
const debug = debuglog('egg/lib/core/messenger/ipc');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Communication between app worker and agent worker by IPC channel
|
|
12
|
-
*/
|
|
13
|
-
export class Messenger extends BaseMessenger implements IMessenger {
|
|
14
|
-
readonly pid: string;
|
|
15
|
-
opids: string[] = [];
|
|
16
|
-
|
|
17
|
-
constructor(egg: EggApplicationCore) {
|
|
18
|
-
super(egg);
|
|
19
|
-
this.pid = String(process.pid);
|
|
20
|
-
// pids of agent or app managed by master
|
|
21
|
-
// - retrieve app worker pids when it's an agent worker
|
|
22
|
-
// - retrieve agent worker pids when it's an app worker
|
|
23
|
-
this.on('egg-pids', workerIds => {
|
|
24
|
-
debug('[%s:%s] got egg-pids %j', this.egg.type, this.pid, workerIds);
|
|
25
|
-
this.opids = workerIds.map((workerId: number) => String(workerId));
|
|
26
|
-
});
|
|
27
|
-
this.onMessage = this.onMessage.bind(this);
|
|
28
|
-
process.on('message', this.onMessage);
|
|
29
|
-
if (!workerThreads.isMainThread) {
|
|
30
|
-
workerThreads.parentPort!.on('message', this.onMessage);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Send message to all agent and app
|
|
36
|
-
* @param {String} action - message key
|
|
37
|
-
* @param {Object} data - message value
|
|
38
|
-
* @return {Messenger} this
|
|
39
|
-
*/
|
|
40
|
-
broadcast(action: string, data?: unknown): Messenger {
|
|
41
|
-
debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
|
|
42
|
-
this.send(action, data, 'app');
|
|
43
|
-
this.send(action, data, 'agent');
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* send message to the specified process
|
|
49
|
-
* @param {String} workerId - the workerId of the receiver
|
|
50
|
-
* @param {String} action - message key
|
|
51
|
-
* @param {Object} data - message value
|
|
52
|
-
* @return {Messenger} this
|
|
53
|
-
*/
|
|
54
|
-
sendTo(workerId: string, action: string, data?: unknown): Messenger {
|
|
55
|
-
debug('[%s:%s] send %s with %j to workerId:%s', this.egg.type, this.pid, action, data, workerId);
|
|
56
|
-
sendmessage(process, {
|
|
57
|
-
action,
|
|
58
|
-
data,
|
|
59
|
-
/**
|
|
60
|
-
* @deprecated Keep compatible, please use receiverWorkerId instead
|
|
61
|
-
*/
|
|
62
|
-
receiverPid: String(workerId),
|
|
63
|
-
receiverWorkerId: String(workerId),
|
|
64
|
-
});
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* send message to one app worker by random
|
|
70
|
-
* - if it's running in agent, it will send to one of app workers
|
|
71
|
-
* - if it's running in app, it will send to agent
|
|
72
|
-
* @param {String} action - message key
|
|
73
|
-
* @param {Object} data - message value
|
|
74
|
-
* @return {Messenger} this
|
|
75
|
-
*/
|
|
76
|
-
sendRandom(action: string, data?: unknown): Messenger {
|
|
77
|
-
if (this.opids.length === 0) {
|
|
78
|
-
debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
const index = Math.floor(Math.random() * this.opids.length);
|
|
82
|
-
const workerId = this.opids[index];
|
|
83
|
-
this.sendTo(workerId, action, data);
|
|
84
|
-
return this;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* send message to app
|
|
89
|
-
* @param {String} action - message key
|
|
90
|
-
* @param {Object} data - message value
|
|
91
|
-
* @return {Messenger} this
|
|
92
|
-
*/
|
|
93
|
-
sendToApp(action: string, data?: unknown): Messenger {
|
|
94
|
-
debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
|
|
95
|
-
this.send(action, data, 'app');
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* send message to agent
|
|
101
|
-
* @param {String} action - message key
|
|
102
|
-
* @param {Object} data - message value
|
|
103
|
-
* @return {Messenger} this
|
|
104
|
-
*/
|
|
105
|
-
sendToAgent(action: string, data?: unknown): Messenger {
|
|
106
|
-
debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
|
|
107
|
-
this.send(action, data, 'agent');
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @param {String} action - message key
|
|
113
|
-
* @param {Object} data - message value
|
|
114
|
-
* @param {String} to - let master know how to send message
|
|
115
|
-
* @return {Messenger} this
|
|
116
|
-
*/
|
|
117
|
-
send(action: string, data: unknown | undefined, to?: string): Messenger {
|
|
118
|
-
sendmessage(process, {
|
|
119
|
-
action,
|
|
120
|
-
data,
|
|
121
|
-
to,
|
|
122
|
-
});
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
onMessage(message: any) {
|
|
127
|
-
if (typeof message?.action === 'string') {
|
|
128
|
-
debug('[%s:%s] got message %s with %j, receiverWorkerId: %s',
|
|
129
|
-
this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
|
|
130
|
-
this.emit(message.action, message.data);
|
|
131
|
-
} else {
|
|
132
|
-
debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
close() {
|
|
137
|
-
process.removeListener('message', this.onMessage);
|
|
138
|
-
this.removeAllListeners();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* @function Messenger#on
|
|
143
|
-
* @param {String} action - message key
|
|
144
|
-
* @param {Object} data - message value
|
|
145
|
-
*/
|
|
146
|
-
}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import type { IMessenger } from './IMessenger.js';
|
|
3
|
-
import type { EggApplicationCore } from '../../egg.js';
|
|
4
|
-
import { BaseMessenger } from './base.js';
|
|
5
|
-
|
|
6
|
-
const debug = debuglog('egg/lib/core/messenger/local');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Communication between app worker and agent worker with EventEmitter
|
|
10
|
-
*/
|
|
11
|
-
export class Messenger extends BaseMessenger implements IMessenger {
|
|
12
|
-
readonly pid: string;
|
|
13
|
-
|
|
14
|
-
constructor(egg: EggApplicationCore) {
|
|
15
|
-
super(egg);
|
|
16
|
-
this.pid = String(process.pid);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Send message to all agent and app
|
|
21
|
-
* @param {String} action - message key
|
|
22
|
-
* @param {Object} data - message value
|
|
23
|
-
* @return {Messenger} this
|
|
24
|
-
*/
|
|
25
|
-
broadcast(action: string, data?: unknown): Messenger {
|
|
26
|
-
debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
|
|
27
|
-
this.send(action, data, 'both');
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* send message to the specified process
|
|
33
|
-
* Notice: in single process mode, it only can send to self process,
|
|
34
|
-
* and it will send to both agent and app's messengers.
|
|
35
|
-
* @param {String} workerId - the workerId of the receiver
|
|
36
|
-
* @param {String} action - message key
|
|
37
|
-
* @param {Object} data - message value
|
|
38
|
-
* @return {Messenger} this
|
|
39
|
-
*/
|
|
40
|
-
sendTo(workerId: string, action: string, data?: unknown): Messenger {
|
|
41
|
-
debug('[%s:%s] send %s with %j to %s', this.egg.type, this.pid, action, data, workerId);
|
|
42
|
-
if (String(workerId) !== this.pid) {
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
45
|
-
this.send(action, data, 'both');
|
|
46
|
-
return this;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* send message to one worker by random
|
|
51
|
-
* Notice: in single process mode, we only start one agent worker and one app worker
|
|
52
|
-
* - if it's running in agent, it will send to one of app workers
|
|
53
|
-
* - if it's running in app, it will send to agent
|
|
54
|
-
* @param {String} action - message key
|
|
55
|
-
* @param {Object} data - message value
|
|
56
|
-
* @return {Messenger} this
|
|
57
|
-
*/
|
|
58
|
-
sendRandom(action: string, data?: unknown): Messenger {
|
|
59
|
-
debug('[%s:%s] send %s with %j to opposite', this.egg.type, this.pid, action, data);
|
|
60
|
-
this.send(action, data, 'opposite');
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* send message to app
|
|
66
|
-
* @param {String} action - message key
|
|
67
|
-
* @param {Object} data - message value
|
|
68
|
-
* @return {Messenger} this
|
|
69
|
-
*/
|
|
70
|
-
sendToApp(action: string, data?: unknown): Messenger {
|
|
71
|
-
debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
|
|
72
|
-
this.send(action, data, 'application');
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* send message to agent
|
|
78
|
-
* @param {String} action - message key
|
|
79
|
-
* @param {Object} data - message value
|
|
80
|
-
* @return {Messenger} this
|
|
81
|
-
*/
|
|
82
|
-
sendToAgent(action: string, data?: unknown): Messenger {
|
|
83
|
-
debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
|
|
84
|
-
this.send(action, data, 'agent');
|
|
85
|
-
return this;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* @param {String} action - message key
|
|
90
|
-
* @param {Object} data - message value
|
|
91
|
-
* @param {String} to - let master know how to send message
|
|
92
|
-
* @return {Messenger} this
|
|
93
|
-
*/
|
|
94
|
-
send(action: string, data: unknown | undefined, to?: string): Messenger {
|
|
95
|
-
// use nextTick to keep it async as IPC messenger
|
|
96
|
-
process.nextTick(() => {
|
|
97
|
-
const { egg } = this;
|
|
98
|
-
let application;
|
|
99
|
-
let agent;
|
|
100
|
-
let opposite;
|
|
101
|
-
|
|
102
|
-
if (egg.type === 'application') {
|
|
103
|
-
application = egg;
|
|
104
|
-
agent = egg.agent;
|
|
105
|
-
opposite = agent;
|
|
106
|
-
} else {
|
|
107
|
-
agent = egg;
|
|
108
|
-
application = egg.application;
|
|
109
|
-
opposite = application;
|
|
110
|
-
}
|
|
111
|
-
if (!to) {
|
|
112
|
-
to = egg.type === 'application' ? 'agent' : 'application';
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (application && application.messenger && (to === 'application' || to === 'both')) {
|
|
116
|
-
application.messenger.onMessage({ action, data });
|
|
117
|
-
}
|
|
118
|
-
if (agent && agent.messenger && (to === 'agent' || to === 'both')) {
|
|
119
|
-
agent.messenger.onMessage({ action, data });
|
|
120
|
-
}
|
|
121
|
-
if (opposite && opposite.messenger && to === 'opposite') {
|
|
122
|
-
opposite.messenger.onMessage({ action, data });
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
return this;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
onMessage(message: any) {
|
|
130
|
-
if (typeof message?.action === 'string') {
|
|
131
|
-
debug('[%s:%s] got message %s with %j', this.egg.type, this.pid, message.action, message.data);
|
|
132
|
-
this.emit(message.action, message.data);
|
|
133
|
-
} else {
|
|
134
|
-
debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
close() {
|
|
139
|
-
this.removeAllListeners();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @function Messenger#on
|
|
144
|
-
* @param {String} action - message key
|
|
145
|
-
* @param {Object} data - message value
|
|
146
|
-
*/
|
|
147
|
-
}
|
package/src/lib/core/utils.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import util from 'node:util';
|
|
2
|
-
import {
|
|
3
|
-
isSymbol, isRegExp, isPrimitive,
|
|
4
|
-
isClass, isFunction, isGeneratorFunction, isAsyncFunction,
|
|
5
|
-
} from 'is-type-of';
|
|
6
|
-
|
|
7
|
-
export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[] = []) {
|
|
8
|
-
if (!Array.isArray(ignore)) {
|
|
9
|
-
ignore = [ ignore ];
|
|
10
|
-
}
|
|
11
|
-
for (const key of Object.keys(obj)) {
|
|
12
|
-
obj[key] = convertValue(key, obj[key], ignore);
|
|
13
|
-
}
|
|
14
|
-
return obj;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function convertValue(key: string, value: any, ignore: (string | RegExp)[]) {
|
|
18
|
-
if (value === null || value === undefined) {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let hit = false;
|
|
23
|
-
for (const matchKey of ignore) {
|
|
24
|
-
if (typeof matchKey === 'string' && matchKey === key) {
|
|
25
|
-
hit = true;
|
|
26
|
-
break;
|
|
27
|
-
} else if (isRegExp(matchKey) && matchKey.test(key)) {
|
|
28
|
-
hit = true;
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (!hit) {
|
|
33
|
-
if (isSymbol(value) || isRegExp(value) || value instanceof URL) {
|
|
34
|
-
return value.toString();
|
|
35
|
-
}
|
|
36
|
-
if (isPrimitive(value) || Array.isArray(value)) {
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// only convert recursively when it's a plain object,
|
|
42
|
-
// o = {}
|
|
43
|
-
if (Object.getPrototypeOf(value) === Object.prototype) {
|
|
44
|
-
return convertObject(value, ignore);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// support class
|
|
48
|
-
const name = value.name || 'anonymous';
|
|
49
|
-
if (isClass(value)) {
|
|
50
|
-
return `<Class ${name}>`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// support generator function
|
|
54
|
-
if (isFunction(value)) {
|
|
55
|
-
if (isGeneratorFunction(value)) return `<GeneratorFunction ${name}>`;
|
|
56
|
-
if (isAsyncFunction(value)) return `<AsyncFunction ${name}>`;
|
|
57
|
-
return `<Function ${name}>`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const typeName = value.constructor.name;
|
|
61
|
-
if (typeName) {
|
|
62
|
-
if (Buffer.isBuffer(value) || typeof value === 'string') {
|
|
63
|
-
return `<${typeName} len: ${value.length}>`;
|
|
64
|
-
}
|
|
65
|
-
return `<${typeName}>`;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return util.format(value);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function safeParseURL(url: string) {
|
|
72
|
-
try {
|
|
73
|
-
return new URL(url);
|
|
74
|
-
} catch {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
}
|