egg 3.29.0 → 4.0.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 +2 -1
- package/README.zh-CN.md +7 -5
- package/dist/commonjs/agent.d.ts +4 -0
- package/dist/commonjs/agent.js +10 -0
- package/dist/commonjs/app/extend/context.d.ts +154 -0
- package/dist/commonjs/app/extend/context.js +261 -0
- package/dist/commonjs/app/extend/context.types.d.ts +16 -0
- package/dist/commonjs/app/extend/context.types.js +3 -0
- package/dist/commonjs/app/extend/helper.d.ts +37 -0
- package/dist/commonjs/app/extend/helper.js +49 -0
- package/dist/commonjs/app/extend/request.d.ts +128 -0
- package/dist/commonjs/app/extend/request.js +270 -0
- package/dist/commonjs/app/extend/response.d.ts +25 -0
- package/dist/commonjs/app/extend/response.js +37 -0
- package/dist/commonjs/app/middleware/body_parser.d.ts +2 -0
- package/dist/commonjs/app/middleware/body_parser.js +8 -0
- package/dist/commonjs/app/middleware/meta.d.ts +10 -0
- package/dist/commonjs/app/middleware/meta.js +22 -0
- package/dist/commonjs/app/middleware/notfound.d.ts +7 -0
- package/dist/commonjs/app/middleware/notfound.js +31 -0
- package/dist/commonjs/app/middleware/override_method.d.ts +2 -0
- package/dist/commonjs/app/middleware/override_method.js +8 -0
- package/dist/commonjs/app/middleware/site_file.d.ts +9 -0
- package/dist/commonjs/app/middleware/site_file.js +58 -0
- package/dist/commonjs/config/config.default.d.ts +9 -0
- package/dist/commonjs/config/config.default.js +379 -0
- package/dist/commonjs/config/config.local.d.ts +8 -0
- package/dist/commonjs/config/config.local.js +12 -0
- package/dist/commonjs/config/config.unittest.d.ts +7 -0
- package/dist/commonjs/config/config.unittest.js +11 -0
- package/dist/commonjs/config/plugin.d.ts +122 -0
- package/dist/commonjs/config/plugin.js +125 -0
- package/dist/commonjs/index.d.ts +63 -0
- package/dist/commonjs/index.js +91 -0
- package/dist/commonjs/lib/agent.d.ts +19 -0
- package/dist/commonjs/lib/agent.js +58 -0
- package/dist/commonjs/lib/application.d.ts +59 -0
- package/dist/commonjs/lib/application.js +270 -0
- package/dist/commonjs/lib/core/base_context_class.d.ts +14 -0
- package/dist/commonjs/lib/core/base_context_class.js +22 -0
- package/dist/commonjs/lib/core/base_context_logger.d.ts +36 -0
- package/dist/commonjs/lib/core/base_context_logger.js +64 -0
- package/dist/commonjs/lib/core/base_hook_class.d.ts +11 -0
- package/dist/commonjs/lib/core/base_hook_class.js +30 -0
- package/dist/commonjs/lib/core/context_httpclient.d.ts +16 -0
- package/dist/commonjs/lib/core/context_httpclient.js +30 -0
- package/dist/commonjs/lib/core/httpclient.d.ts +13 -0
- package/dist/commonjs/lib/core/httpclient.js +37 -0
- package/dist/commonjs/lib/core/logger.d.ts +3 -0
- package/dist/commonjs/lib/core/logger.js +40 -0
- package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +50 -0
- package/dist/commonjs/lib/core/messenger/IMessenger.js +3 -0
- package/dist/commonjs/lib/core/messenger/index.d.ts +7 -0
- package/dist/commonjs/lib/core/messenger/index.js +14 -0
- package/dist/commonjs/lib/core/messenger/ipc.d.ts +59 -0
- package/dist/commonjs/lib/core/messenger/ipc.js +137 -0
- package/dist/commonjs/lib/core/messenger/local.d.ts +61 -0
- package/dist/commonjs/lib/core/messenger/local.js +137 -0
- package/dist/commonjs/lib/core/singleton.d.ts +23 -0
- package/dist/commonjs/lib/core/singleton.js +120 -0
- package/dist/commonjs/lib/core/utils.d.ts +2 -0
- package/dist/commonjs/lib/core/utils.js +77 -0
- package/dist/commonjs/lib/egg.d.ts +276 -0
- package/dist/commonjs/lib/egg.js +617 -0
- package/dist/commonjs/lib/egg.types.d.ts +6 -0
- package/dist/commonjs/lib/egg.types.js +3 -0
- package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +12 -0
- package/dist/commonjs/lib/loader/AgentWorkerLoader.js +24 -0
- package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +17 -0
- package/dist/commonjs/lib/loader/AppWorkerLoader.js +43 -0
- package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +4 -0
- package/dist/commonjs/lib/loader/EggApplicationLoader.js +8 -0
- package/dist/commonjs/lib/loader/index.d.ts +3 -0
- package/dist/commonjs/lib/loader/index.js +22 -0
- package/dist/commonjs/lib/start.d.ts +15 -0
- package/dist/commonjs/lib/start.js +49 -0
- package/dist/commonjs/lib/type.d.ts +293 -0
- package/dist/commonjs/lib/type.js +3 -0
- package/dist/commonjs/lib/utils.d.ts +2 -0
- package/dist/commonjs/lib/utils.js +21 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/agent.d.ts +4 -0
- package/dist/esm/agent.js +7 -0
- package/dist/esm/app/extend/context.d.ts +154 -0
- package/dist/esm/app/extend/context.js +255 -0
- package/dist/esm/app/extend/context.types.d.ts +16 -0
- package/dist/esm/app/extend/context.types.js +2 -0
- package/dist/esm/app/extend/helper.d.ts +37 -0
- package/dist/esm/app/extend/helper.js +43 -0
- package/dist/esm/app/extend/request.d.ts +128 -0
- package/dist/esm/app/extend/request.js +264 -0
- package/dist/esm/app/extend/response.d.ts +25 -0
- package/dist/esm/app/extend/response.js +34 -0
- package/dist/esm/app/middleware/body_parser.d.ts +2 -0
- package/dist/esm/app/middleware/body_parser.js +3 -0
- package/dist/esm/app/middleware/meta.d.ts +10 -0
- package/dist/esm/app/middleware/meta.js +20 -0
- package/dist/esm/app/middleware/notfound.d.ts +7 -0
- package/dist/esm/app/middleware/notfound.js +29 -0
- package/dist/esm/app/middleware/override_method.d.ts +2 -0
- package/dist/esm/app/middleware/override_method.js +3 -0
- package/dist/esm/app/middleware/site_file.d.ts +9 -0
- package/dist/esm/app/middleware/site_file.js +53 -0
- package/dist/esm/config/config.default.d.ts +9 -0
- package/dist/esm/config/config.default.js +374 -0
- package/dist/esm/config/config.local.d.ts +8 -0
- package/dist/esm/config/config.local.js +10 -0
- package/dist/esm/config/config.unittest.d.ts +7 -0
- package/dist/esm/config/config.unittest.js +9 -0
- package/dist/esm/config/favicon.png +0 -0
- package/dist/esm/config/plugin.d.ts +122 -0
- package/dist/esm/config/plugin.js +123 -0
- package/dist/esm/index.d.ts +63 -0
- package/dist/esm/index.js +66 -0
- package/dist/esm/lib/agent.d.ts +19 -0
- package/dist/esm/lib/agent.js +54 -0
- package/dist/esm/lib/application.d.ts +59 -0
- package/dist/esm/lib/application.js +263 -0
- package/dist/esm/lib/core/base_context_class.d.ts +14 -0
- package/dist/esm/lib/core/base_context_class.js +18 -0
- package/dist/esm/lib/core/base_context_logger.d.ts +36 -0
- package/dist/esm/lib/core/base_context_logger.js +60 -0
- package/dist/esm/lib/core/base_hook_class.d.ts +11 -0
- package/dist/esm/lib/core/base_hook_class.js +23 -0
- package/dist/esm/lib/core/context_httpclient.d.ts +16 -0
- package/dist/esm/lib/core/context_httpclient.js +26 -0
- package/dist/esm/lib/core/httpclient.d.ts +13 -0
- package/dist/esm/lib/core/httpclient.js +33 -0
- package/dist/esm/lib/core/logger.d.ts +3 -0
- package/dist/esm/lib/core/logger.js +37 -0
- package/dist/esm/lib/core/messenger/IMessenger.d.ts +50 -0
- package/dist/esm/lib/core/messenger/IMessenger.js +2 -0
- package/dist/esm/lib/core/messenger/index.d.ts +7 -0
- package/dist/esm/lib/core/messenger/index.js +11 -0
- package/dist/esm/lib/core/messenger/ipc.d.ts +59 -0
- package/dist/esm/lib/core/messenger/ipc.js +130 -0
- package/dist/esm/lib/core/messenger/local.d.ts +61 -0
- package/dist/esm/lib/core/messenger/local.js +130 -0
- package/dist/esm/lib/core/singleton.d.ts +23 -0
- package/dist/esm/lib/core/singleton.js +113 -0
- package/dist/esm/lib/core/utils.d.ts +2 -0
- package/dist/esm/lib/core/utils.js +70 -0
- package/dist/esm/lib/egg.d.ts +276 -0
- package/dist/esm/lib/egg.js +574 -0
- package/dist/esm/lib/egg.types.d.ts +6 -0
- package/dist/esm/lib/egg.types.js +2 -0
- package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +12 -0
- package/dist/esm/lib/loader/AgentWorkerLoader.js +20 -0
- package/dist/esm/lib/loader/AppWorkerLoader.d.ts +17 -0
- package/dist/esm/lib/loader/AppWorkerLoader.js +39 -0
- package/dist/esm/lib/loader/EggApplicationLoader.d.ts +4 -0
- package/dist/esm/lib/loader/EggApplicationLoader.js +4 -0
- package/dist/esm/lib/loader/index.d.ts +3 -0
- package/dist/esm/lib/loader/index.js +4 -0
- package/dist/esm/lib/start.d.ts +15 -0
- package/dist/esm/lib/start.js +43 -0
- package/dist/esm/lib/type.d.ts +293 -0
- package/dist/esm/lib/type.js +2 -0
- package/dist/esm/lib/utils.d.ts +2 -0
- package/dist/esm/lib/utils.js +14 -0
- package/dist/esm/package.json +3 -0
- package/dist/package.json +4 -0
- package/package.json +97 -79
- package/src/agent.ts +7 -0
- package/src/app/extend/context.ts +303 -0
- package/src/app/extend/context.types.ts +24 -0
- package/{app/extend/helper.js → src/app/extend/helper.ts} +14 -13
- package/{app/extend/request.js → src/app/extend/request.ts} +81 -79
- package/src/app/extend/response.ts +36 -0
- package/src/app/middleware/body_parser.ts +3 -0
- package/{app/middleware/meta.js → src/app/middleware/meta.ts} +11 -4
- package/{app/middleware/notfound.js → src/app/middleware/notfound.ts} +8 -3
- package/src/app/middleware/override_method.ts +3 -0
- package/src/app/middleware/site_file.ts +68 -0
- package/{config/config.default.js → src/config/config.default.ts} +25 -45
- package/src/config/config.local.ts +11 -0
- package/src/config/config.unittest.ts +10 -0
- package/src/config/favicon.png +0 -0
- package/{config/plugin.js → src/config/plugin.ts} +3 -5
- package/src/index.ts +80 -0
- package/src/lib/agent.ts +66 -0
- package/{lib/application.js → src/lib/application.ts} +76 -124
- package/src/lib/core/base_context_class.ts +21 -0
- package/src/lib/core/base_context_logger.ts +67 -0
- package/src/lib/core/base_hook_class.ts +30 -0
- package/src/lib/core/context_httpclient.ts +33 -0
- package/src/lib/core/httpclient.ts +51 -0
- package/src/lib/core/logger.ts +42 -0
- package/src/lib/core/messenger/IMessenger.ts +58 -0
- package/src/lib/core/messenger/index.ts +15 -0
- package/src/lib/core/messenger/ipc.ts +148 -0
- package/{lib/core/messenger/local.js → src/lib/core/messenger/local.ts} +36 -28
- package/{lib/core/singleton.js → src/lib/core/singleton.ts} +56 -33
- package/src/lib/core/utils.ts +77 -0
- package/{lib/egg.js → src/lib/egg.ts} +287 -221
- package/src/lib/egg.types.ts +6 -0
- package/src/lib/loader/AgentWorkerLoader.ts +21 -0
- package/src/lib/loader/AppWorkerLoader.ts +42 -0
- package/src/lib/loader/EggApplicationLoader.ts +5 -0
- package/src/lib/loader/index.ts +3 -0
- package/src/lib/start.ts +56 -0
- package/src/lib/type.ts +329 -0
- package/src/lib/utils.ts +16 -0
- package/CHANGELOG.md +0 -2395
- package/History.md +0 -52
- package/agent.js +0 -11
- package/app/extend/context.js +0 -285
- package/app/extend/response.js +0 -101
- package/app/middleware/body_parser.js +0 -3
- package/app/middleware/override_method.js +0 -3
- package/app/middleware/site_file.js +0 -31
- package/config/config.local.js +0 -7
- package/config/config.unittest.js +0 -8
- package/index.d.ts +0 -1288
- package/index.js +0 -68
- package/lib/agent.js +0 -95
- package/lib/core/base_context_class.js +0 -20
- package/lib/core/base_context_logger.js +0 -64
- package/lib/core/base_hook_class.js +0 -31
- package/lib/core/context_httpclient.js +0 -26
- package/lib/core/dnscache_httpclient.js +0 -93
- package/lib/core/httpclient.js +0 -119
- package/lib/core/httpclient_next.js +0 -80
- package/lib/core/logger.js +0 -35
- package/lib/core/messenger/index.js +0 -14
- package/lib/core/messenger/ipc.js +0 -141
- package/lib/core/utils.js +0 -73
- package/lib/loader/agent_worker_loader.js +0 -27
- package/lib/loader/app_worker_loader.js +0 -48
- package/lib/loader/index.js +0 -5
- package/lib/start.js +0 -39
- /package/{config → dist/commonjs/config}/favicon.png +0 -0
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
1
|
+
export default {
|
|
4
2
|
// enable plugins
|
|
5
3
|
|
|
6
4
|
/**
|
|
@@ -43,7 +41,7 @@ module.exports = {
|
|
|
43
41
|
*/
|
|
44
42
|
watcher: {
|
|
45
43
|
enable: true,
|
|
46
|
-
package: '
|
|
44
|
+
package: '@eggjs/watcher',
|
|
47
45
|
},
|
|
48
46
|
|
|
49
47
|
/**
|
|
@@ -98,7 +96,7 @@ module.exports = {
|
|
|
98
96
|
*/
|
|
99
97
|
schedule: {
|
|
100
98
|
enable: true,
|
|
101
|
-
package: '
|
|
99
|
+
package: '@eggjs/schedule',
|
|
102
100
|
},
|
|
103
101
|
|
|
104
102
|
/**
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @namespace Egg
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { BaseContextClass } from './lib/core/base_context_class.js';
|
|
6
|
+
import { startEgg } from './lib/start.js';
|
|
7
|
+
import Helper from './app/extend/helper.js';
|
|
8
|
+
|
|
9
|
+
// export extends
|
|
10
|
+
export { Helper };
|
|
11
|
+
|
|
12
|
+
// export types
|
|
13
|
+
export * from './lib/egg.js';
|
|
14
|
+
export * from './lib/type.js';
|
|
15
|
+
export * from './lib/start.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Start egg application with cluster mode
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
export * from '@eggjs/cluster';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Start egg application with single process mode
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export const start = startEgg;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @member {Application} Egg#Application
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
export { Application } from './lib/application.js';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @member {Agent} Egg#Agent
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
*/
|
|
39
|
+
export { Agent } from './lib/agent.js';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @member {AppWorkerLoader} Egg#AppWorkerLoader
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @member {AgentWorkerLoader} Egg#AgentWorkerLoader
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @member {Controller} Egg#Controller
|
|
55
|
+
* @since 1.1.0
|
|
56
|
+
*/
|
|
57
|
+
export const Controller = BaseContextClass;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @member {Service} Egg#Service
|
|
61
|
+
* @since 1.1.0
|
|
62
|
+
*/
|
|
63
|
+
export const Service = BaseContextClass;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @member {Subscription} Egg#Subscription
|
|
67
|
+
* @since 1.10.0
|
|
68
|
+
*/
|
|
69
|
+
export const Subscription = BaseContextClass;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @member {BaseContextClass} Egg#BaseContextClass
|
|
73
|
+
* @since 1.2.0
|
|
74
|
+
*/
|
|
75
|
+
export { BaseContextClass } from './lib/core/base_context_class.js';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @member {Boot} Egg#Boot
|
|
79
|
+
*/
|
|
80
|
+
export { BaseHookClass as Boot } from './lib/core/base_hook_class.js';
|
package/src/lib/agent.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { EggLogger } from 'egg-logger';
|
|
2
|
+
import { EggApplicationCore, EggApplicationCoreOptions } from './egg.js';
|
|
3
|
+
import { AgentWorkerLoader } from './loader/index.js';
|
|
4
|
+
|
|
5
|
+
const EGG_LOADER = Symbol.for('egg#loader');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Singleton instance in Agent Worker, extend {@link EggApplicationCore}
|
|
9
|
+
* @augments EggApplicationCore
|
|
10
|
+
*/
|
|
11
|
+
export class Agent extends EggApplicationCore {
|
|
12
|
+
readonly #agentAliveHandler: NodeJS.Timeout;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @class
|
|
16
|
+
* @param {Object} options - see {@link EggApplicationCore}
|
|
17
|
+
*/
|
|
18
|
+
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>) {
|
|
19
|
+
super({
|
|
20
|
+
...options,
|
|
21
|
+
type: 'agent',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// keep agent alive even it doesn't have any io tasks
|
|
25
|
+
this.#agentAliveHandler = setInterval(() => {
|
|
26
|
+
this.coreLogger.info('[]');
|
|
27
|
+
}, 24 * 60 * 60 * 1000);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get [EGG_LOADER]() {
|
|
31
|
+
return AgentWorkerLoader;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_wrapMessenger() {
|
|
35
|
+
for (const methodName of [
|
|
36
|
+
'broadcast',
|
|
37
|
+
'sendTo',
|
|
38
|
+
'sendToApp',
|
|
39
|
+
'sendToAgent',
|
|
40
|
+
'sendRandom',
|
|
41
|
+
]) {
|
|
42
|
+
wrapMethod(methodName, this.messenger, this.coreLogger);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function wrapMethod(methodName: string, messenger: any, logger: EggLogger) {
|
|
46
|
+
const originMethod = messenger[methodName];
|
|
47
|
+
messenger[methodName] = function(...args: any[]) {
|
|
48
|
+
const stack = new Error().stack!.split('\n').slice(1).join('\n');
|
|
49
|
+
logger.warn(
|
|
50
|
+
"agent can't call %s before server started\n%s",
|
|
51
|
+
methodName,
|
|
52
|
+
stack,
|
|
53
|
+
);
|
|
54
|
+
originMethod.apply(this, args);
|
|
55
|
+
};
|
|
56
|
+
messenger.prependOnceListener('egg-ready', () => {
|
|
57
|
+
messenger[methodName] = originMethod;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async close() {
|
|
63
|
+
clearInterval(this.#agentAliveHandler);
|
|
64
|
+
await super.close();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -1,27 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import http from 'node:http';
|
|
4
|
+
import { Socket } from 'node:net';
|
|
5
|
+
import { graceful } from 'graceful';
|
|
6
|
+
import { assign } from 'utility';
|
|
7
|
+
import { utils as eggUtils } from '@eggjs/core';
|
|
8
|
+
import {
|
|
9
|
+
EggApplicationCore,
|
|
10
|
+
type EggApplicationCoreOptions,
|
|
11
|
+
type ContextDelegation,
|
|
12
|
+
} from './egg.js';
|
|
13
|
+
import { AppWorkerLoader } from './loader/index.js';
|
|
14
|
+
import Helper from '../app/extend/helper.js';
|
|
2
15
|
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const ms = require('ms');
|
|
6
|
-
const is = require('is-type-of');
|
|
7
|
-
const graceful = require('graceful');
|
|
8
|
-
const http = require('http');
|
|
9
|
-
const cluster = require('cluster-client');
|
|
10
|
-
const onFinished = require('on-finished');
|
|
11
|
-
const { assign } = require('utility');
|
|
12
|
-
const eggUtils = require('egg-core').utils;
|
|
13
|
-
const EggApplication = require('./egg');
|
|
14
|
-
const AppWorkerLoader = require('./loader').AppWorkerLoader;
|
|
15
|
-
|
|
16
|
-
const KEYS = Symbol('Application#keys');
|
|
17
|
-
const HELPER = Symbol('Application#Helper');
|
|
18
|
-
const LOCALS = Symbol('Application#locals');
|
|
19
|
-
const BIND_EVENTS = Symbol('Application#bindEvents');
|
|
20
|
-
const WARN_CONFUSED_CONFIG = Symbol('Application#warnConfusedConfig');
|
|
21
16
|
const EGG_LOADER = Symbol.for('egg#loader');
|
|
22
|
-
const EGG_PATH = Symbol.for('egg#eggPath');
|
|
23
|
-
const CLUSTER_CLIENTS = Symbol.for('egg#clusterClients');
|
|
24
|
-
const RESPONSE_RAW = Symbol('Application#responseRaw');
|
|
25
17
|
|
|
26
18
|
// client error => 400 Bad Request
|
|
27
19
|
// Refs: https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_event_clienterror
|
|
@@ -38,59 +30,52 @@ const DEFAULT_BAD_REQUEST_RESPONSE =
|
|
|
38
30
|
`\r\n\r\n${DEFAULT_BAD_REQUEST_HTML}`;
|
|
39
31
|
|
|
40
32
|
// Refs: https://github.com/nodejs/node/blob/b38c81/lib/_http_outgoing.js#L706-L710
|
|
41
|
-
function escapeHeaderValue(value) {
|
|
33
|
+
function escapeHeaderValue(value: string) {
|
|
42
34
|
// Protect against response splitting. The regex test is there to
|
|
43
35
|
// minimize the performance impact in the common case.
|
|
44
36
|
return /[\r\n]/.test(value) ? value.replace(/[\r\n]+[ \t]*/g, '') : value;
|
|
45
37
|
}
|
|
46
38
|
|
|
47
|
-
// Refs: https://github.com/nodejs/node/blob/b38c81/lib/_http_outgoing.js#L706-L710
|
|
48
39
|
/**
|
|
49
|
-
* Singleton instance in App Worker, extend {@link
|
|
50
|
-
* @augments
|
|
40
|
+
* Singleton instance in App Worker, extend {@link EggApplicationCore}
|
|
41
|
+
* @augments EggApplicationCore
|
|
51
42
|
*/
|
|
52
|
-
class Application extends
|
|
43
|
+
export class Application extends EggApplicationCore {
|
|
44
|
+
// will auto set after 'server' event emit
|
|
45
|
+
server?: http.Server;
|
|
46
|
+
#locals: Record<string, any> = {};
|
|
47
|
+
/**
|
|
48
|
+
* reference to {@link Helper}
|
|
49
|
+
* @member {Helper} Application#Helper
|
|
50
|
+
*/
|
|
51
|
+
Helper = Helper;
|
|
53
52
|
|
|
54
53
|
/**
|
|
55
54
|
* @class
|
|
56
|
-
* @param {Object} options - see {@link
|
|
55
|
+
* @param {Object} options - see {@link EggApplicationCore}
|
|
57
56
|
*/
|
|
58
|
-
constructor(options
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
this.loader.load();
|
|
67
|
-
} catch (e) {
|
|
68
|
-
// close gracefully
|
|
69
|
-
this[CLUSTER_CLIENTS].forEach(cluster.close);
|
|
70
|
-
throw e;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// dump config after loaded, ensure all the dynamic modifications will be recorded
|
|
74
|
-
const dumpStartTime = Date.now();
|
|
75
|
-
this.dumpConfig();
|
|
76
|
-
this.coreLogger.info('[egg:core] dump config after load, %s', ms(Date.now() - dumpStartTime));
|
|
57
|
+
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>) {
|
|
58
|
+
super({
|
|
59
|
+
...options,
|
|
60
|
+
type: 'application',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
77
63
|
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
protected async load() {
|
|
65
|
+
await super.load();
|
|
66
|
+
this.#warnConfusedConfig();
|
|
67
|
+
this.#bindEvents();
|
|
80
68
|
}
|
|
81
69
|
|
|
82
70
|
get [EGG_LOADER]() {
|
|
83
71
|
return AppWorkerLoader;
|
|
84
72
|
}
|
|
85
73
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
/* istanbul ignore next */
|
|
92
|
-
if (!socket.writable) return;
|
|
93
|
-
if (!raw) return socket.end(DEFAULT_BAD_REQUEST_RESPONSE);
|
|
74
|
+
#responseRaw(socket: Socket, raw?: any) {
|
|
75
|
+
if (!socket?.writable) return;
|
|
76
|
+
if (!raw) {
|
|
77
|
+
return socket.end(DEFAULT_BAD_REQUEST_RESPONSE);
|
|
78
|
+
}
|
|
94
79
|
|
|
95
80
|
const body = (raw.body == null) ? DEFAULT_BAD_REQUEST_HTML : raw.body;
|
|
96
81
|
const headers = raw.headers || {};
|
|
@@ -114,10 +99,10 @@ class Application extends EggApplication {
|
|
|
114
99
|
socket.end(`${firstLine}\r\n${responseHeaderLines}\r\n${body.toString()}`);
|
|
115
100
|
}
|
|
116
101
|
|
|
117
|
-
onClientError(err, socket) {
|
|
102
|
+
onClientError(err: any, socket: Socket) {
|
|
118
103
|
// ignore when there is no http body, it almost like an ECONNRESET
|
|
119
104
|
if (err.rawPacket) {
|
|
120
|
-
this.logger.warn('A client (%s:%d) error [%s] occurred: %s',
|
|
105
|
+
this.logger.warn('[egg:application] A client (%s:%d) error [%s] occurred: %s',
|
|
121
106
|
socket.remoteAddress,
|
|
122
107
|
socket.remotePort,
|
|
123
108
|
err.code,
|
|
@@ -143,35 +128,34 @@ class Application extends EggApplication {
|
|
|
143
128
|
// + headers: {}
|
|
144
129
|
// + status: 400
|
|
145
130
|
p.then(ret => {
|
|
146
|
-
this
|
|
131
|
+
this.#responseRaw(socket, ret || {});
|
|
147
132
|
}).catch(err => {
|
|
148
133
|
this.logger.error(err);
|
|
149
|
-
this
|
|
134
|
+
this.#responseRaw(socket);
|
|
150
135
|
});
|
|
151
136
|
} else {
|
|
152
137
|
// because it's a raw socket object, we should return the raw HTTP response
|
|
153
138
|
// packet.
|
|
154
|
-
this
|
|
139
|
+
this.#responseRaw(socket);
|
|
155
140
|
}
|
|
156
141
|
}
|
|
157
142
|
|
|
158
|
-
onServer(server) {
|
|
143
|
+
onServer(server: http.Server) {
|
|
159
144
|
// expose app.server
|
|
160
145
|
this.server = server;
|
|
161
146
|
// set ignore code
|
|
162
147
|
const serverGracefulIgnoreCode = this.config.serverGracefulIgnoreCode || [];
|
|
163
148
|
|
|
164
|
-
/* istanbul ignore next */
|
|
165
149
|
graceful({
|
|
166
150
|
server: [ server ],
|
|
167
|
-
error: (err, throwErrorCount) => {
|
|
151
|
+
error: (err: Error, throwErrorCount: number) => {
|
|
168
152
|
const originMessage = err.message;
|
|
169
153
|
if (originMessage) {
|
|
170
154
|
// shouldjs will override error property but only getter
|
|
171
155
|
// https://github.com/shouldjs/should.js/blob/889e22ebf19a06bc2747d24cf34b25cc00b37464/lib/assertion-error.js#L26
|
|
172
156
|
Object.defineProperty(err, 'message', {
|
|
173
157
|
get() {
|
|
174
|
-
return originMessage
|
|
158
|
+
return `${originMessage} (uncaughtException throw ${throwErrorCount} times on pid: ${process.pid})`;
|
|
175
159
|
},
|
|
176
160
|
configurable: true,
|
|
177
161
|
enumerable: false,
|
|
@@ -182,10 +166,12 @@ class Application extends EggApplication {
|
|
|
182
166
|
ignoreCode: serverGracefulIgnoreCode,
|
|
183
167
|
});
|
|
184
168
|
|
|
185
|
-
server.on('clientError', (err, socket) => this.onClientError(err, socket));
|
|
169
|
+
server.on('clientError', (err, socket) => this.onClientError(err, socket as Socket));
|
|
186
170
|
|
|
187
171
|
// server timeout
|
|
188
|
-
if (
|
|
172
|
+
if (typeof this.config.serverTimeout === 'number') {
|
|
173
|
+
server.setTimeout(this.config.serverTimeout);
|
|
174
|
+
}
|
|
189
175
|
}
|
|
190
176
|
|
|
191
177
|
/**
|
|
@@ -194,24 +180,11 @@ class Application extends EggApplication {
|
|
|
194
180
|
* @see Context#locals
|
|
195
181
|
*/
|
|
196
182
|
get locals() {
|
|
197
|
-
|
|
198
|
-
this[LOCALS] = {};
|
|
199
|
-
}
|
|
200
|
-
return this[LOCALS];
|
|
183
|
+
return this.#locals;
|
|
201
184
|
}
|
|
202
185
|
|
|
203
|
-
set locals(val) {
|
|
204
|
-
|
|
205
|
-
this[LOCALS] = {};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
assign(this[LOCALS], val);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
handleRequest(ctx, fnMiddleware) {
|
|
212
|
-
this.emit('request', ctx);
|
|
213
|
-
onFinished(ctx.res, () => this.emit('response', ctx));
|
|
214
|
-
return super.handleRequest(ctx, fnMiddleware);
|
|
186
|
+
set locals(val: Record<string, any>) {
|
|
187
|
+
assign(this.#locals, val);
|
|
215
188
|
}
|
|
216
189
|
|
|
217
190
|
/**
|
|
@@ -234,11 +207,11 @@ class Application extends EggApplication {
|
|
|
234
207
|
paramNames: layer.paramNames,
|
|
235
208
|
path: layer.path,
|
|
236
209
|
regexp: layer.regexp.toString(),
|
|
237
|
-
stack: layer.stack.map(stack => stack[FULLPATH] || stack._name || stack.name || 'anonymous'),
|
|
210
|
+
stack: layer.stack.map((stack: any) => stack[FULLPATH] || stack._name || stack.name || 'anonymous'),
|
|
238
211
|
});
|
|
239
212
|
}
|
|
240
213
|
fs.writeFileSync(dumpRouterFile, JSON.stringify(routers, null, 2));
|
|
241
|
-
} catch (err) {
|
|
214
|
+
} catch (err: any) {
|
|
242
215
|
this.coreLogger.warn(`dumpConfig router.json error: ${err.message}`);
|
|
243
216
|
}
|
|
244
217
|
}
|
|
@@ -248,11 +221,13 @@ class Application extends EggApplication {
|
|
|
248
221
|
* @see Context#runInBackground
|
|
249
222
|
* @param {Function} scope - the first args is an anonymous ctx
|
|
250
223
|
*/
|
|
251
|
-
runInBackground(scope) {
|
|
252
|
-
const ctx = this.createAnonymousContext();
|
|
253
|
-
if (!scope.name)
|
|
224
|
+
runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
|
|
225
|
+
const ctx = this.createAnonymousContext(req);
|
|
226
|
+
if (!scope.name) {
|
|
227
|
+
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
|
|
228
|
+
}
|
|
254
229
|
this.ctxStorage.run(ctx, () => {
|
|
255
|
-
ctx.runInBackground(scope);
|
|
230
|
+
return ctx.runInBackground(scope);
|
|
256
231
|
});
|
|
257
232
|
}
|
|
258
233
|
|
|
@@ -262,9 +237,11 @@ class Application extends EggApplication {
|
|
|
262
237
|
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
|
|
263
238
|
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
|
|
264
239
|
*/
|
|
265
|
-
async runInAnonymousContextScope(scope
|
|
240
|
+
async runInAnonymousContextScope(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
|
|
266
241
|
const ctx = this.createAnonymousContext(req);
|
|
267
|
-
if (!scope.name)
|
|
242
|
+
if (!scope.name) {
|
|
243
|
+
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
|
|
244
|
+
}
|
|
268
245
|
return await this.ctxStorage.run(ctx, async () => {
|
|
269
246
|
return await scope(ctx);
|
|
270
247
|
});
|
|
@@ -275,7 +252,7 @@ class Application extends EggApplication {
|
|
|
275
252
|
* @member {String} Application#keys
|
|
276
253
|
*/
|
|
277
254
|
get keys() {
|
|
278
|
-
if (!this
|
|
255
|
+
if (!this._keys) {
|
|
279
256
|
if (!this.config.keys) {
|
|
280
257
|
if (this.config.env === 'local' || this.config.env === 'unittest') {
|
|
281
258
|
const configPath = path.join(this.config.baseDir, 'config/config.default.js');
|
|
@@ -284,31 +261,9 @@ class Application extends EggApplication {
|
|
|
284
261
|
}
|
|
285
262
|
throw new Error('Please set config.keys first');
|
|
286
263
|
}
|
|
287
|
-
|
|
288
|
-
this[KEYS] = this.config.keys.split(',').map(s => s.trim());
|
|
289
|
-
}
|
|
290
|
-
return this[KEYS];
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
set keys(_) {
|
|
294
|
-
// ignore
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* reference to {@link Helper}
|
|
299
|
-
* @member {Helper} Application#Helper
|
|
300
|
-
*/
|
|
301
|
-
get Helper() {
|
|
302
|
-
if (!this[HELPER]) {
|
|
303
|
-
/**
|
|
304
|
-
* The Helper class which can be used as utility function.
|
|
305
|
-
* We support developers to extend Helper through ${baseDir}/app/extend/helper.js ,
|
|
306
|
-
* then you can use all method on `ctx.helper` that is a instance of Helper.
|
|
307
|
-
*/
|
|
308
|
-
class Helper extends this.BaseContextClass {}
|
|
309
|
-
this[HELPER] = Helper;
|
|
264
|
+
this._keys = this.config.keys.split(',').map(s => s.trim());
|
|
310
265
|
}
|
|
311
|
-
return this
|
|
266
|
+
return this._keys;
|
|
312
267
|
}
|
|
313
268
|
|
|
314
269
|
/**
|
|
@@ -316,17 +271,15 @@ class Application extends EggApplication {
|
|
|
316
271
|
*
|
|
317
272
|
* @private
|
|
318
273
|
*/
|
|
319
|
-
|
|
274
|
+
#bindEvents() {
|
|
320
275
|
// Browser Cookie Limits: http://browsercookielimits.squawky.net/
|
|
321
276
|
this.on('cookieLimitExceed', ({ name, value, ctx }) => {
|
|
322
277
|
const err = new Error(`cookie ${name}'s length(${value.length}) exceed the limit(4093)`);
|
|
323
278
|
err.name = 'CookieLimitExceedError';
|
|
324
|
-
err.key = name;
|
|
325
|
-
err.cookie = value;
|
|
326
279
|
ctx.coreLogger.error(err);
|
|
327
280
|
});
|
|
328
281
|
// expose server to support websocket
|
|
329
|
-
this.once('server', server => this.onServer(server));
|
|
282
|
+
this.once('server', (server: http.Server) => this.onServer(server));
|
|
330
283
|
}
|
|
331
284
|
|
|
332
285
|
/**
|
|
@@ -334,15 +287,14 @@ class Application extends EggApplication {
|
|
|
334
287
|
*
|
|
335
288
|
* @private
|
|
336
289
|
*/
|
|
337
|
-
|
|
290
|
+
#warnConfusedConfig() {
|
|
338
291
|
const confusedConfigurations = this.config.confusedConfigurations;
|
|
339
292
|
Object.keys(confusedConfigurations).forEach(key => {
|
|
340
293
|
if (this.config[key] !== undefined) {
|
|
341
|
-
this.logger.warn('Unexpected config key `%
|
|
294
|
+
this.logger.warn('[egg:application] Unexpected config key `%o` exists, Please use `%o` instead.',
|
|
342
295
|
key, confusedConfigurations[key]);
|
|
343
296
|
}
|
|
344
297
|
});
|
|
345
298
|
}
|
|
346
299
|
}
|
|
347
300
|
|
|
348
|
-
module.exports = Application;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
|
|
2
|
+
import type { ContextDelegation } from '../egg.js';
|
|
3
|
+
import { BaseContextLogger } from './base_context_logger.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* BaseContextClass is a base class that can be extended,
|
|
7
|
+
* it's instantiated in context level,
|
|
8
|
+
* {@link Helper}, {@link Service} is extending it.
|
|
9
|
+
*/
|
|
10
|
+
export class BaseContextClass extends EggCoreBaseContextClass {
|
|
11
|
+
declare ctx: ContextDelegation;
|
|
12
|
+
protected pathName?: string;
|
|
13
|
+
#logger?: BaseContextLogger;
|
|
14
|
+
|
|
15
|
+
get logger() {
|
|
16
|
+
if (!this.#logger) {
|
|
17
|
+
this.#logger = new BaseContextLogger(this.ctx, this.pathName);
|
|
18
|
+
}
|
|
19
|
+
return this.#logger;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { EggContext } from '../egg.js';
|
|
2
|
+
|
|
3
|
+
export class BaseContextLogger {
|
|
4
|
+
readonly #ctx: EggContext;
|
|
5
|
+
readonly #pathName?: string;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @class
|
|
9
|
+
* @param {Context} ctx - context instance
|
|
10
|
+
* @param {String} pathName - class path name
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
constructor(ctx: EggContext, pathName?: string) {
|
|
14
|
+
/**
|
|
15
|
+
* @member {Context} BaseContextLogger#ctx
|
|
16
|
+
* @since 1.2.0
|
|
17
|
+
*/
|
|
18
|
+
this.#ctx = ctx;
|
|
19
|
+
this.#pathName = pathName;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
protected _log(method: 'info' | 'warn' | 'error' | 'debug', args: any[]) {
|
|
23
|
+
// add `[${pathName}]` in log
|
|
24
|
+
if (this.#pathName && typeof args[0] === 'string') {
|
|
25
|
+
args[0] = `[${this.#pathName}] ${args[0]}`;
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
this.#ctx.app.logger[method](...args);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @member {Function} BaseContextLogger#debug
|
|
34
|
+
* @param {...any} args - log msg
|
|
35
|
+
* @since 1.2.0
|
|
36
|
+
*/
|
|
37
|
+
debug(...args: any[]) {
|
|
38
|
+
this._log('debug', args);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @member {Function} BaseContextLogger#info
|
|
43
|
+
* @param {...any} args - log msg
|
|
44
|
+
* @since 1.2.0
|
|
45
|
+
*/
|
|
46
|
+
info(...args: any[]) {
|
|
47
|
+
this._log('info', args);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @member {Function} BaseContextLogger#warn
|
|
52
|
+
* @param {...any} args - log msg
|
|
53
|
+
* @since 1.2.0
|
|
54
|
+
*/
|
|
55
|
+
warn(...args: any[]) {
|
|
56
|
+
this._log('warn', args);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @member {Function} BaseContextLogger#error
|
|
61
|
+
* @param {...any} args - log msg
|
|
62
|
+
* @since 1.2.0
|
|
63
|
+
*/
|
|
64
|
+
error(...args: any[]) {
|
|
65
|
+
this._log('error', args);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import type { ILifecycleBoot } from '@eggjs/core';
|
|
3
|
+
import type { Application, Agent } from '../../index.js';
|
|
4
|
+
|
|
5
|
+
export class BaseHookClass implements ILifecycleBoot {
|
|
6
|
+
fullPath?: string;
|
|
7
|
+
#instance: Application | Agent;
|
|
8
|
+
|
|
9
|
+
constructor(instance: Application | Agent) {
|
|
10
|
+
this.#instance = instance;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get logger(): any {
|
|
14
|
+
return this.#instance.logger;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get config() {
|
|
18
|
+
return this.#instance.config;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get app() {
|
|
22
|
+
assert(this.#instance.type === 'application', 'agent boot should not use app instance');
|
|
23
|
+
return this.#instance as Application;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get agent() {
|
|
27
|
+
assert(this.#instance.type === 'agent', 'app boot should not use agent instance');
|
|
28
|
+
return this.#instance as Agent;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ContextDelegation, EggApplicationCore } from '../egg.js';
|
|
2
|
+
import type {
|
|
3
|
+
HttpClientRequestURL, HttpClientRequestOptions,
|
|
4
|
+
} from './httpclient.js';
|
|
5
|
+
|
|
6
|
+
export class ContextHttpClient {
|
|
7
|
+
ctx: ContextDelegation;
|
|
8
|
+
app: EggApplicationCore;
|
|
9
|
+
|
|
10
|
+
constructor(ctx: ContextDelegation) {
|
|
11
|
+
this.ctx = ctx;
|
|
12
|
+
this.app = ctx.app;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* http request helper base on {@link HttpClient}, it will auto save httpclient log.
|
|
17
|
+
* Keep the same api with {@link Application#curl}.
|
|
18
|
+
*
|
|
19
|
+
* @param {String|Object} url - request url address.
|
|
20
|
+
* @param {Object} [options] - options for request.
|
|
21
|
+
*/
|
|
22
|
+
async curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions) {
|
|
23
|
+
options = {
|
|
24
|
+
...options,
|
|
25
|
+
ctx: this.ctx,
|
|
26
|
+
};
|
|
27
|
+
return await this.app.curl<T>(url, options);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async request<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions) {
|
|
31
|
+
return await this.curl<T>(url, options);
|
|
32
|
+
}
|
|
33
|
+
}
|