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
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EggApplicationCore } from "../../egg.js";
|
|
2
|
+
import { EventEmitter, captureRejectionSymbol } from "node:events";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/core/messenger/base.d.ts
|
|
5
|
+
declare class BaseMessenger extends EventEmitter {
|
|
6
|
+
protected readonly egg: EggApplicationCore;
|
|
7
|
+
constructor(egg: EggApplicationCore);
|
|
8
|
+
[captureRejectionSymbol](err: Error, event: string | symbol, ...args: any[]): void;
|
|
9
|
+
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { BaseMessenger };
|
|
@@ -0,0 +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 };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMessenger } from "./IMessenger.js";
|
|
2
|
+
import { EggApplicationCore } from "../../egg.js";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/core/messenger/index.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class Messenger
|
|
8
|
+
*/
|
|
9
|
+
declare function create(egg: EggApplicationCore): IMessenger;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type IMessenger, create };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Messenger } from "./local.js";
|
|
2
|
+
import { Messenger as Messenger$1 } from "./ipc.js";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/core/messenger/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* @class Messenger
|
|
7
|
+
*/
|
|
8
|
+
function create(egg) {
|
|
9
|
+
return egg.options.mode === "single" ? new Messenger(egg) : new Messenger$1(egg);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { create };
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Communication between app worker and agent worker by IPC channel
|
|
9
|
+
*/
|
|
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;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { Messenger };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
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");
|
|
8
|
+
/**
|
|
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 };
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
|
|
7
|
+
/**
|
|
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;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { Messenger };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { BaseMessenger } from "./base.js";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/core/messenger/local.ts
|
|
5
|
+
const debug = debuglog("egg/lib/core/messenger/local");
|
|
6
|
+
/**
|
|
7
|
+
* Communication between app worker and agent worker with EventEmitter
|
|
8
|
+
*/
|
|
9
|
+
var Messenger = class extends BaseMessenger {
|
|
10
|
+
pid;
|
|
11
|
+
constructor(egg) {
|
|
12
|
+
super(egg);
|
|
13
|
+
this.pid = String(process.pid);
|
|
14
|
+
}
|
|
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, data) {
|
|
22
|
+
debug("[%s:%s] broadcast %s with %j", this.egg.type, this.pid, action, data);
|
|
23
|
+
this.send(action, data, "both");
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* send message to the specified process
|
|
28
|
+
* Notice: in single process mode, it only can send to self process,
|
|
29
|
+
* and it will send to both agent and app's messengers.
|
|
30
|
+
* @param {String} workerId - the workerId of the receiver
|
|
31
|
+
* @param {String} action - message key
|
|
32
|
+
* @param {Object} data - message value
|
|
33
|
+
* @return {Messenger} this
|
|
34
|
+
*/
|
|
35
|
+
sendTo(workerId, action, data) {
|
|
36
|
+
debug("[%s:%s] send %s with %j to %s", this.egg.type, this.pid, action, data, workerId);
|
|
37
|
+
if (String(workerId) !== this.pid) return this;
|
|
38
|
+
this.send(action, data, "both");
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* send message to one worker by random
|
|
43
|
+
* Notice: in single process mode, we only start one agent worker and one app worker
|
|
44
|
+
* - if it's running in agent, it will send to one of app workers
|
|
45
|
+
* - if it's running in app, it will send to agent
|
|
46
|
+
* @param {String} action - message key
|
|
47
|
+
* @param {Object} data - message value
|
|
48
|
+
* @return {Messenger} this
|
|
49
|
+
*/
|
|
50
|
+
sendRandom(action, data) {
|
|
51
|
+
debug("[%s:%s] send %s with %j to opposite", this.egg.type, this.pid, action, data);
|
|
52
|
+
this.send(action, data, "opposite");
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* send message to app
|
|
57
|
+
* @param {String} action - message key
|
|
58
|
+
* @param {Object} data - message value
|
|
59
|
+
* @return {Messenger} this
|
|
60
|
+
*/
|
|
61
|
+
sendToApp(action, data) {
|
|
62
|
+
debug("[%s:%s] send %s with %j to all app", this.egg.type, this.pid, action, data);
|
|
63
|
+
this.send(action, data, "application");
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* send message to agent
|
|
68
|
+
* @param {String} action - message key
|
|
69
|
+
* @param {Object} data - message value
|
|
70
|
+
* @return {Messenger} this
|
|
71
|
+
*/
|
|
72
|
+
sendToAgent(action, data) {
|
|
73
|
+
debug("[%s:%s] send %s with %j to all agent", this.egg.type, this.pid, action, data);
|
|
74
|
+
this.send(action, data, "agent");
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @param {String} action - message key
|
|
79
|
+
* @param {Object} data - message value
|
|
80
|
+
* @param {String} to - let master know how to send message
|
|
81
|
+
* @return {Messenger} this
|
|
82
|
+
*/
|
|
83
|
+
send(action, data, to) {
|
|
84
|
+
process.nextTick(() => {
|
|
85
|
+
const { egg } = this;
|
|
86
|
+
let application;
|
|
87
|
+
let agent;
|
|
88
|
+
let opposite;
|
|
89
|
+
if (egg.type === "application") {
|
|
90
|
+
application = egg;
|
|
91
|
+
agent = egg.agent;
|
|
92
|
+
opposite = agent;
|
|
93
|
+
} else {
|
|
94
|
+
agent = egg;
|
|
95
|
+
application = egg.application;
|
|
96
|
+
opposite = application;
|
|
97
|
+
}
|
|
98
|
+
if (!to) to = egg.type === "application" ? "agent" : "application";
|
|
99
|
+
if (application && application.messenger && (to === "application" || to === "both")) application.messenger.onMessage({
|
|
100
|
+
action,
|
|
101
|
+
data
|
|
102
|
+
});
|
|
103
|
+
if (agent && agent.messenger && (to === "agent" || to === "both")) agent.messenger.onMessage({
|
|
104
|
+
action,
|
|
105
|
+
data
|
|
106
|
+
});
|
|
107
|
+
if (opposite && opposite.messenger && to === "opposite") opposite.messenger.onMessage({
|
|
108
|
+
action,
|
|
109
|
+
data
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
onMessage(message) {
|
|
115
|
+
if (typeof message?.action === "string") {
|
|
116
|
+
debug("[%s:%s] got message %s with %j", this.egg.type, this.pid, message.action, message.data);
|
|
117
|
+
this.emit(message.action, message.data);
|
|
118
|
+
} else debug("[%s:%s] got an invalid message %j", this.egg.type, this.pid, message);
|
|
119
|
+
}
|
|
120
|
+
close() {
|
|
121
|
+
this.removeAllListeners();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
export { Messenger };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import util from "node:util";
|
|
2
|
+
import { isAsyncFunction, isClass, isFunction, isGeneratorFunction, isPrimitive, isRegExp, isSymbol } from "is-type-of";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/core/utils.ts
|
|
5
|
+
function convertObject(obj, ignore = []) {
|
|
6
|
+
if (!Array.isArray(ignore)) ignore = [ignore];
|
|
7
|
+
for (const key of Object.keys(obj)) obj[key] = convertValue(key, obj[key], ignore);
|
|
8
|
+
return obj;
|
|
9
|
+
}
|
|
10
|
+
function convertValue(key, value, ignore) {
|
|
11
|
+
if (value === null || value === void 0) return value;
|
|
12
|
+
let hit = false;
|
|
13
|
+
for (const matchKey of ignore) if (typeof matchKey === "string" && matchKey === key) {
|
|
14
|
+
hit = true;
|
|
15
|
+
break;
|
|
16
|
+
} else if (isRegExp(matchKey) && matchKey.test(key)) {
|
|
17
|
+
hit = true;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
if (!hit) {
|
|
21
|
+
if (isSymbol(value) || isRegExp(value) || value instanceof URL) return value.toString();
|
|
22
|
+
if (isPrimitive(value) || Array.isArray(value)) return value;
|
|
23
|
+
}
|
|
24
|
+
if (Object.getPrototypeOf(value) === Object.prototype) return convertObject(value, ignore);
|
|
25
|
+
const name = value.name || "anonymous";
|
|
26
|
+
if (isClass(value)) return `<Class ${name}>`;
|
|
27
|
+
if (isFunction(value)) {
|
|
28
|
+
if (isGeneratorFunction(value)) return `<GeneratorFunction ${name}>`;
|
|
29
|
+
if (isAsyncFunction(value)) return `<AsyncFunction ${name}>`;
|
|
30
|
+
return `<Function ${name}>`;
|
|
31
|
+
}
|
|
32
|
+
const typeName = value.constructor.name;
|
|
33
|
+
if (typeName) {
|
|
34
|
+
if (Buffer.isBuffer(value) || typeof value === "string") return `<${typeName} len: ${value.length}>`;
|
|
35
|
+
return `<${typeName}>`;
|
|
36
|
+
}
|
|
37
|
+
return util.format(value);
|
|
38
|
+
}
|
|
39
|
+
function safeParseURL(url) {
|
|
40
|
+
try {
|
|
41
|
+
return new URL(url);
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { convertObject, safeParseURL };
|