egg 3.26.1 → 4.0.0-beta.0
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 +1 -1
- package/README.zh-CN.md +6 -5
- package/dist/commonjs/agent.d.ts +4 -0
- package/dist/commonjs/agent.js +10 -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 +11 -0
- package/dist/commonjs/app/middleware/meta.js +22 -0
- package/dist/commonjs/app/middleware/notfound.d.ts +8 -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 +7 -0
- package/dist/commonjs/app/middleware/site_file.js +40 -0
- package/dist/commonjs/config/config.default.d.ts +9 -0
- package/dist/commonjs/config/config.default.js +378 -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 +61 -0
- package/dist/commonjs/index.js +89 -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 +66 -0
- package/dist/commonjs/lib/application.js +281 -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 +14 -0
- package/dist/commonjs/lib/core/httpclient.js +40 -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 +57 -0
- package/dist/commonjs/lib/core/messenger/ipc.js +126 -0
- package/dist/commonjs/lib/core/messenger/local.d.ts +61 -0
- package/dist/commonjs/lib/core/messenger/local.js +134 -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 +267 -0
- package/dist/commonjs/lib/egg.js +595 -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 +295 -0
- package/dist/commonjs/lib/type.js +3 -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/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 +11 -0
- package/dist/esm/app/middleware/meta.js +20 -0
- package/dist/esm/app/middleware/notfound.d.ts +8 -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 +7 -0
- package/dist/esm/app/middleware/site_file.js +35 -0
- package/dist/esm/config/config.default.d.ts +9 -0
- package/dist/esm/config/config.default.js +373 -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 +61 -0
- package/dist/esm/index.js +65 -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 +66 -0
- package/dist/esm/lib/application.js +274 -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 +14 -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 +57 -0
- package/dist/esm/lib/core/messenger/ipc.js +119 -0
- package/dist/esm/lib/core/messenger/local.d.ts +61 -0
- package/dist/esm/lib/core/messenger/local.js +127 -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 +267 -0
- package/dist/esm/lib/egg.js +565 -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 +295 -0
- package/dist/esm/lib/type.js +2 -0
- package/dist/esm/package.json +3 -0
- package/dist/package.json +4 -0
- package/package.json +83 -70
- package/src/agent.ts +7 -0
- package/src/app/middleware/body_parser.ts +3 -0
- package/{app/middleware/meta.js → src/app/middleware/meta.ts} +12 -4
- package/{app/middleware/notfound.js → src/app/middleware/notfound.ts} +9 -3
- package/src/app/middleware/override_method.ts +3 -0
- package/src/app/middleware/site_file.ts +49 -0
- package/{config/config.default.js → src/config/config.default.ts} +21 -42
- 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} +1 -3
- package/src/index.ts +78 -0
- package/src/lib/agent.ts +66 -0
- package/{lib/application.js → src/lib/application.ts} +79 -120
- 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 +52 -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/{lib/core/messenger/ipc.js → src/lib/core/messenger/ipc.ts} +25 -29
- package/{lib/core/messenger/local.js → src/lib/core/messenger/local.ts} +27 -21
- 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} +252 -218
- 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/agent.js +0 -11
- 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 -1285
- 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 -108
- package/lib/core/httpclient_next.js +0 -45
- package/lib/core/logger.js +0 -35
- package/lib/core/messenger/index.js +0 -14
- 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
- /package/{app → src/app}/extend/context.js +0 -0
- /package/{app → src/app}/extend/helper.js +0 -0
- /package/{app → src/app}/extend/request.js +0 -0
- /package/{app → src/app}/extend/response.js +0 -0
package/index.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @namespace Egg
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Start egg application with cluster mode
|
|
7
|
-
* @since 1.0.0
|
|
8
|
-
*/
|
|
9
|
-
exports.startCluster = require('egg-cluster').startCluster;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Start egg application with single process mode
|
|
13
|
-
* @since 1.0.0
|
|
14
|
-
*/
|
|
15
|
-
exports.start = require('./lib/start');
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @member {Application} Egg#Application
|
|
19
|
-
* @since 1.0.0
|
|
20
|
-
*/
|
|
21
|
-
exports.Application = require('./lib/application');
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @member {Agent} Egg#Agent
|
|
25
|
-
* @since 1.0.0
|
|
26
|
-
*/
|
|
27
|
-
exports.Agent = require('./lib/agent');
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @member {AppWorkerLoader} Egg#AppWorkerLoader
|
|
31
|
-
* @since 1.0.0
|
|
32
|
-
*/
|
|
33
|
-
exports.AppWorkerLoader = require('./lib/loader').AppWorkerLoader;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @member {AgentWorkerLoader} Egg#AgentWorkerLoader
|
|
37
|
-
* @since 1.0.0
|
|
38
|
-
*/
|
|
39
|
-
exports.AgentWorkerLoader = require('./lib/loader').AgentWorkerLoader;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @member {Controller} Egg#Controller
|
|
43
|
-
* @since 1.1.0
|
|
44
|
-
*/
|
|
45
|
-
exports.Controller = require('./lib/core/base_context_class');
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @member {Service} Egg#Service
|
|
49
|
-
* @since 1.1.0
|
|
50
|
-
*/
|
|
51
|
-
exports.Service = require('./lib/core/base_context_class');
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @member {Subscription} Egg#Subscription
|
|
55
|
-
* @since 1.10.0
|
|
56
|
-
*/
|
|
57
|
-
exports.Subscription = require('./lib/core/base_context_class');
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @member {BaseContextClass} Egg#BaseContextClass
|
|
61
|
-
* @since 1.2.0
|
|
62
|
-
*/
|
|
63
|
-
exports.BaseContextClass = require('./lib/core/base_context_class');
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* @member {Boot} Egg#Boot
|
|
67
|
-
*/
|
|
68
|
-
exports.Boot = require('./lib/core/base_hook_class');
|
package/lib/agent.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const ms = require('ms');
|
|
5
|
-
const EggApplication = require('./egg');
|
|
6
|
-
const AgentWorkerLoader = require('./loader').AgentWorkerLoader;
|
|
7
|
-
|
|
8
|
-
const EGG_LOADER = Symbol.for('egg#loader');
|
|
9
|
-
const EGG_PATH = Symbol.for('egg#eggPath');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Singleton instance in Agent Worker, extend {@link EggApplication}
|
|
13
|
-
* @augments EggApplication
|
|
14
|
-
*/
|
|
15
|
-
class Agent extends EggApplication {
|
|
16
|
-
/**
|
|
17
|
-
* @class
|
|
18
|
-
* @param {Object} options - see {@link EggApplication}
|
|
19
|
-
*/
|
|
20
|
-
constructor(options = {}) {
|
|
21
|
-
options.type = 'agent';
|
|
22
|
-
super(options);
|
|
23
|
-
|
|
24
|
-
this.loader.load();
|
|
25
|
-
|
|
26
|
-
// dump config after loaded, ensure all the dynamic modifications will be recorded
|
|
27
|
-
const dumpStartTime = Date.now();
|
|
28
|
-
this.dumpConfig();
|
|
29
|
-
this.coreLogger.info(
|
|
30
|
-
'[egg:core] dump config after load, %s',
|
|
31
|
-
ms(Date.now() - dumpStartTime)
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
// keep agent alive even it doesn't have any io tasks
|
|
35
|
-
this.agentAliveHandler = setInterval(() => {}, 24 * 60 * 60 * 1000);
|
|
36
|
-
|
|
37
|
-
this._uncaughtExceptionHandler = this._uncaughtExceptionHandler.bind(this);
|
|
38
|
-
process.on('uncaughtException', this._uncaughtExceptionHandler);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
_uncaughtExceptionHandler(err) {
|
|
42
|
-
if (!(err instanceof Error)) {
|
|
43
|
-
err = new Error(String(err));
|
|
44
|
-
}
|
|
45
|
-
/* istanbul ignore else */
|
|
46
|
-
if (err.name === 'Error') {
|
|
47
|
-
err.name = 'unhandledExceptionError';
|
|
48
|
-
}
|
|
49
|
-
this.coreLogger.error(err);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get [EGG_LOADER]() {
|
|
53
|
-
return AgentWorkerLoader;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get [EGG_PATH]() {
|
|
57
|
-
return path.join(__dirname, '..');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
_wrapMessenger() {
|
|
61
|
-
for (const methodName of [
|
|
62
|
-
'broadcast',
|
|
63
|
-
'sendTo',
|
|
64
|
-
'sendToApp',
|
|
65
|
-
'sendToAgent',
|
|
66
|
-
'sendRandom',
|
|
67
|
-
]) {
|
|
68
|
-
wrapMethod(methodName, this.messenger, this.coreLogger);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function wrapMethod(methodName, messenger, logger) {
|
|
72
|
-
const originMethod = messenger[methodName];
|
|
73
|
-
messenger[methodName] = function() {
|
|
74
|
-
const stack = new Error().stack.split('\n').slice(1).join('\n');
|
|
75
|
-
logger.warn(
|
|
76
|
-
"agent can't call %s before server started\n%s",
|
|
77
|
-
methodName,
|
|
78
|
-
stack
|
|
79
|
-
);
|
|
80
|
-
originMethod.apply(this, arguments);
|
|
81
|
-
};
|
|
82
|
-
messenger.prependOnceListener('egg-ready', () => {
|
|
83
|
-
messenger[methodName] = originMethod;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
close() {
|
|
89
|
-
process.removeListener('uncaughtException', this._uncaughtExceptionHandler);
|
|
90
|
-
clearInterval(this.agentAliveHandler);
|
|
91
|
-
return super.close();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
module.exports = Agent;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EggCoreBaseContextClass = require('egg-core').BaseContextClass;
|
|
4
|
-
const BaseContextLogger = require('./base_context_logger');
|
|
5
|
-
|
|
6
|
-
const LOGGER = Symbol('BaseContextClass#logger');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* BaseContextClass is a base class that can be extended,
|
|
10
|
-
* it's instantiated in context level,
|
|
11
|
-
* {@link Helper}, {@link Service} is extending it.
|
|
12
|
-
*/
|
|
13
|
-
class BaseContextClass extends EggCoreBaseContextClass {
|
|
14
|
-
get logger() {
|
|
15
|
-
if (!this[LOGGER]) this[LOGGER] = new BaseContextLogger(this.ctx, this.pathName);
|
|
16
|
-
return this[LOGGER];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = BaseContextClass;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const CALL = Symbol('BaseContextLogger#call');
|
|
2
|
-
|
|
3
|
-
class BaseContextLogger {
|
|
4
|
-
/**
|
|
5
|
-
* @class
|
|
6
|
-
* @param {Context} ctx - context instance
|
|
7
|
-
* @param {String} pathName - class path name
|
|
8
|
-
* @since 1.0.0
|
|
9
|
-
*/
|
|
10
|
-
constructor(ctx, pathName) {
|
|
11
|
-
/**
|
|
12
|
-
* @member {Context} BaseContextLogger#ctx
|
|
13
|
-
* @since 1.2.0
|
|
14
|
-
*/
|
|
15
|
-
this.ctx = ctx;
|
|
16
|
-
this.pathName = pathName;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
[CALL](method, args) {
|
|
20
|
-
// add `[${pathName}]` in log
|
|
21
|
-
if (this.pathName && typeof args[0] === 'string') {
|
|
22
|
-
args[0] = `[${this.pathName}] ${args[0]}`;
|
|
23
|
-
}
|
|
24
|
-
this.ctx.app.logger[method](...args);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @member {Function} BaseContextLogger#debug
|
|
29
|
-
* @param {...any} args - log msg
|
|
30
|
-
* @since 1.2.0
|
|
31
|
-
*/
|
|
32
|
-
debug(...args) {
|
|
33
|
-
this[CALL]('debug', args);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @member {Function} BaseContextLogger#info
|
|
38
|
-
* @param {...any} args - log msg
|
|
39
|
-
* @since 1.2.0
|
|
40
|
-
*/
|
|
41
|
-
info(...args) {
|
|
42
|
-
this[CALL]('info', args);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @member {Function} BaseContextLogger#warn
|
|
47
|
-
* @param {...any} args - log msg
|
|
48
|
-
* @since 1.2.0
|
|
49
|
-
*/
|
|
50
|
-
warn(...args) {
|
|
51
|
-
this[CALL]('warn', args);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @member {Function} BaseContextLogger#error
|
|
56
|
-
* @param {...any} args - log msg
|
|
57
|
-
* @since 1.2.0
|
|
58
|
-
*/
|
|
59
|
-
error(...args) {
|
|
60
|
-
this[CALL]('error', args);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = BaseContextLogger;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const INSTANCE = Symbol('BaseHookClass#instance');
|
|
5
|
-
|
|
6
|
-
class BaseHookClass {
|
|
7
|
-
|
|
8
|
-
constructor(instance) {
|
|
9
|
-
this[INSTANCE] = instance;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get logger() {
|
|
13
|
-
return this[INSTANCE].logger;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get config() {
|
|
17
|
-
return this[INSTANCE].config;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
get app() {
|
|
21
|
-
assert(this[INSTANCE].type === 'application', 'agent boot should not use app instance');
|
|
22
|
-
return this[INSTANCE];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get agent() {
|
|
26
|
-
assert(this[INSTANCE].type === 'agent', 'app boot should not use agent instance');
|
|
27
|
-
return this[INSTANCE];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = BaseHookClass;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
class ContextHttpClient {
|
|
2
|
-
constructor(ctx) {
|
|
3
|
-
this.ctx = ctx;
|
|
4
|
-
this.app = ctx.app;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* http request helper base on {@link HttpClient}, it will auto save httpclient log.
|
|
9
|
-
* Keep the same api with {@link Application#curl}.
|
|
10
|
-
*
|
|
11
|
-
* @param {String|Object} url - request url address.
|
|
12
|
-
* @param {Object} [options] - options for request.
|
|
13
|
-
* @return {Object} see {@link Application#curl}
|
|
14
|
-
*/
|
|
15
|
-
async curl(url, options) {
|
|
16
|
-
options = options || {};
|
|
17
|
-
options.ctx = this.ctx;
|
|
18
|
-
return await this.app.curl(url, options);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async request(url, options) {
|
|
22
|
-
return await this.curl(url, options);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
module.exports = ContextHttpClient;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
const dns = require('node:dns').promises;
|
|
2
|
-
const LRU = require('ylru');
|
|
3
|
-
const { assign } = require('utility');
|
|
4
|
-
const HttpClient = require('./httpclient');
|
|
5
|
-
const utils = require('./utils');
|
|
6
|
-
|
|
7
|
-
const IP_REGEX = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
|
|
8
|
-
const DNSLOOKUP = Symbol('DNSCacheHttpClient#dnslookup');
|
|
9
|
-
const UPDATE_DNS = Symbol('DNSCacheHttpClient#updateDNS');
|
|
10
|
-
|
|
11
|
-
class DNSCacheHttpClient extends HttpClient {
|
|
12
|
-
constructor(app) {
|
|
13
|
-
super(app);
|
|
14
|
-
this.dnsCacheLookupInterval = this.app.config.httpclient.dnsCacheLookupInterval;
|
|
15
|
-
this.dnsCache = new LRU(this.app.config.httpclient.dnsCacheMaxLength);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async request(url, args) {
|
|
19
|
-
// disable dns cache in request by args handle
|
|
20
|
-
if (args && args.enableDNSCache === false) {
|
|
21
|
-
return await super.request(url, args);
|
|
22
|
-
}
|
|
23
|
-
const result = await this[DNSLOOKUP](url, args);
|
|
24
|
-
return await super.request(result.url, result.args);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async [DNSLOOKUP](url, args) {
|
|
28
|
-
let parsed;
|
|
29
|
-
if (typeof url === 'string') {
|
|
30
|
-
parsed = utils.safeParseURL(url);
|
|
31
|
-
// invalid url or relative url
|
|
32
|
-
if (!parsed) return { url, args };
|
|
33
|
-
} else {
|
|
34
|
-
parsed = url;
|
|
35
|
-
}
|
|
36
|
-
// hostname must exists
|
|
37
|
-
const hostname = parsed.hostname;
|
|
38
|
-
|
|
39
|
-
// don't lookup when hostname is IP
|
|
40
|
-
if (hostname && IP_REGEX.test(hostname)) {
|
|
41
|
-
return { url, args };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
args = args || {};
|
|
45
|
-
args.headers = args.headers || {};
|
|
46
|
-
// set when host header doesn't exist
|
|
47
|
-
if (!args.headers.host && !args.headers.Host) {
|
|
48
|
-
// host must combine with hostname:port, node won't use `parsed.host`
|
|
49
|
-
args.headers.host = parsed.port ? `${hostname}:${parsed.port}` : hostname;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const record = this.dnsCache.get(hostname);
|
|
53
|
-
const now = Date.now();
|
|
54
|
-
if (record) {
|
|
55
|
-
if (now - record.timestamp >= this.dnsCacheLookupInterval) {
|
|
56
|
-
// make sure the next request doesn't refresh dns query
|
|
57
|
-
record.timestamp = now;
|
|
58
|
-
this[UPDATE_DNS](hostname, args).catch(err => this.app.emit('error', err));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return { url: formatDnsLookupUrl(hostname, url, record.ip), args };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const address = await this[UPDATE_DNS](hostname, args);
|
|
65
|
-
return { url: formatDnsLookupUrl(hostname, url, address), args };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async [UPDATE_DNS](hostname, args) {
|
|
69
|
-
const logger = args.ctx ? args.ctx.coreLogger : this.app.coreLogger;
|
|
70
|
-
try {
|
|
71
|
-
const { address } = await dns.lookup(hostname, { family: 4 });
|
|
72
|
-
logger.info('[dnscache_httpclient] dns lookup success: %s => %s',
|
|
73
|
-
hostname, address);
|
|
74
|
-
this.dnsCache.set(hostname, { timestamp: Date.now(), ip: address });
|
|
75
|
-
return address;
|
|
76
|
-
} catch (err) {
|
|
77
|
-
err.message = `[dnscache_httpclient] dns lookup error: ${hostname} => ${err.message}`;
|
|
78
|
-
throw err;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = DNSCacheHttpClient;
|
|
84
|
-
|
|
85
|
-
function formatDnsLookupUrl(host, url, address) {
|
|
86
|
-
if (typeof url === 'string') return url.replace(host, address);
|
|
87
|
-
const urlObj = assign({}, url);
|
|
88
|
-
urlObj.hostname = urlObj.hostname.replace(host, address);
|
|
89
|
-
if (urlObj.host) {
|
|
90
|
-
urlObj.host = urlObj.host.replace(host, address);
|
|
91
|
-
}
|
|
92
|
-
return urlObj;
|
|
93
|
-
}
|
package/lib/core/httpclient.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
const Agent = require('agentkeepalive');
|
|
2
|
-
const HttpsAgent = require('agentkeepalive').HttpsAgent;
|
|
3
|
-
const urllib = require('urllib');
|
|
4
|
-
const ms = require('humanize-ms');
|
|
5
|
-
const { FrameworkBaseError } = require('egg-errors');
|
|
6
|
-
|
|
7
|
-
class HttpClientError extends FrameworkBaseError {
|
|
8
|
-
get module() {
|
|
9
|
-
return 'httpclient';
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
class HttpClient extends urllib.HttpClient2 {
|
|
14
|
-
constructor(app) {
|
|
15
|
-
normalizeConfig(app);
|
|
16
|
-
const config = app.config.httpclient;
|
|
17
|
-
super({
|
|
18
|
-
app,
|
|
19
|
-
defaultArgs: config.request,
|
|
20
|
-
agent: new Agent(config.httpAgent),
|
|
21
|
-
httpsAgent: new HttpsAgent(config.httpsAgent),
|
|
22
|
-
});
|
|
23
|
-
this.app = app;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async request(url, args) {
|
|
27
|
-
args = args || {};
|
|
28
|
-
if (args.ctx && args.ctx.tracer) {
|
|
29
|
-
args.tracer = args.ctx.tracer;
|
|
30
|
-
} else {
|
|
31
|
-
args.tracer = args.tracer || this.app.tracer;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
return await super.request(url, args);
|
|
36
|
-
} catch (err) {
|
|
37
|
-
if (err.code === 'ENETUNREACH') {
|
|
38
|
-
throw HttpClientError.create(err.message, err.code);
|
|
39
|
-
}
|
|
40
|
-
throw err;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async curl(...args) {
|
|
45
|
-
return await this.request(...args);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function normalizeConfig(app) {
|
|
50
|
-
const config = app.config.httpclient;
|
|
51
|
-
|
|
52
|
-
// compatibility
|
|
53
|
-
if (typeof config.keepAlive === 'boolean') {
|
|
54
|
-
config.httpAgent.keepAlive = config.keepAlive;
|
|
55
|
-
config.httpsAgent.keepAlive = config.keepAlive;
|
|
56
|
-
}
|
|
57
|
-
if (config.timeout) {
|
|
58
|
-
config.timeout = ms(config.timeout);
|
|
59
|
-
config.httpAgent.timeout = config.timeout;
|
|
60
|
-
config.httpsAgent.timeout = config.timeout;
|
|
61
|
-
}
|
|
62
|
-
// compatibility httpclient.freeSocketKeepAliveTimeout => httpclient.freeSocketTimeout
|
|
63
|
-
if (config.freeSocketKeepAliveTimeout && !config.freeSocketTimeout) {
|
|
64
|
-
config.freeSocketTimeout = config.freeSocketKeepAliveTimeout;
|
|
65
|
-
delete config.freeSocketKeepAliveTimeout;
|
|
66
|
-
}
|
|
67
|
-
if (config.freeSocketTimeout) {
|
|
68
|
-
config.freeSocketTimeout = ms(config.freeSocketTimeout);
|
|
69
|
-
config.httpAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
70
|
-
config.httpsAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
71
|
-
} else {
|
|
72
|
-
// compatibility agent.freeSocketKeepAliveTimeout
|
|
73
|
-
if (config.httpAgent.freeSocketKeepAliveTimeout && !config.httpAgent.freeSocketTimeout) {
|
|
74
|
-
config.httpAgent.freeSocketTimeout = config.httpAgent.freeSocketKeepAliveTimeout;
|
|
75
|
-
delete config.httpAgent.freeSocketKeepAliveTimeout;
|
|
76
|
-
}
|
|
77
|
-
if (config.httpsAgent.freeSocketKeepAliveTimeout && !config.httpsAgent.freeSocketTimeout) {
|
|
78
|
-
config.httpsAgent.freeSocketTimeout = config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
79
|
-
delete config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (typeof config.maxSockets === 'number') {
|
|
84
|
-
config.httpAgent.maxSockets = config.maxSockets;
|
|
85
|
-
config.httpsAgent.maxSockets = config.maxSockets;
|
|
86
|
-
}
|
|
87
|
-
if (typeof config.maxFreeSockets === 'number') {
|
|
88
|
-
config.httpAgent.maxFreeSockets = config.maxFreeSockets;
|
|
89
|
-
config.httpsAgent.maxFreeSockets = config.maxFreeSockets;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (config.httpAgent.timeout < 30000) {
|
|
93
|
-
app.coreLogger.warn('[egg:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
94
|
-
config.httpAgent.timeout);
|
|
95
|
-
config.httpAgent.timeout = 30000;
|
|
96
|
-
}
|
|
97
|
-
if (config.httpsAgent.timeout < 30000) {
|
|
98
|
-
app.coreLogger.warn('[egg:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
99
|
-
config.httpsAgent.timeout);
|
|
100
|
-
config.httpsAgent.timeout = 30000;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (typeof config.request.timeout === 'string') {
|
|
104
|
-
config.request.timeout = ms(config.request.timeout);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = HttpClient;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const { HttpClient } = require('urllib-next');
|
|
2
|
-
const ms = require('humanize-ms');
|
|
3
|
-
|
|
4
|
-
class HttpClientNext extends HttpClient {
|
|
5
|
-
constructor(app, options) {
|
|
6
|
-
normalizeConfig(app);
|
|
7
|
-
options = options || {};
|
|
8
|
-
options = {
|
|
9
|
-
...app.config.httpclient,
|
|
10
|
-
...options,
|
|
11
|
-
};
|
|
12
|
-
super({
|
|
13
|
-
app,
|
|
14
|
-
defaultArgs: options.request,
|
|
15
|
-
allowH2: options.allowH2,
|
|
16
|
-
// use on egg-security ssrf
|
|
17
|
-
// https://github.com/eggjs/egg-security/blob/master/lib/extend/safe_curl.js#L11
|
|
18
|
-
checkAddress: options.checkAddress,
|
|
19
|
-
});
|
|
20
|
-
this.app = app;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async request(url, options) {
|
|
24
|
-
options = options || {};
|
|
25
|
-
if (options.ctx && options.ctx.tracer) {
|
|
26
|
-
options.tracer = options.ctx.tracer;
|
|
27
|
-
} else {
|
|
28
|
-
options.tracer = options.tracer || this.app.tracer;
|
|
29
|
-
}
|
|
30
|
-
return await super.request(url, options);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async curl(...args) {
|
|
34
|
-
return await this.request(...args);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function normalizeConfig(app) {
|
|
39
|
-
const config = app.config.httpclient;
|
|
40
|
-
if (typeof config.request.timeout === 'string') {
|
|
41
|
-
config.request.timeout = ms(config.request.timeout);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
module.exports = HttpClientNext;
|
package/lib/core/logger.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const { EggLoggers } = require('egg-logger');
|
|
2
|
-
const { setCustomLogger } = require('onelogger');
|
|
3
|
-
|
|
4
|
-
module.exports = function createLoggers(app) {
|
|
5
|
-
const loggerConfig = app.config.logger;
|
|
6
|
-
loggerConfig.type = app.type;
|
|
7
|
-
loggerConfig.localStorage = app.ctxStorage;
|
|
8
|
-
|
|
9
|
-
if (app.config.env === 'prod' && loggerConfig.level === 'DEBUG' && !loggerConfig.allowDebugAtProd) {
|
|
10
|
-
loggerConfig.level = 'INFO';
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const loggers = new EggLoggers(app.config);
|
|
14
|
-
|
|
15
|
-
// won't print to console after started, except for local and unittest
|
|
16
|
-
app.ready(() => {
|
|
17
|
-
if (loggerConfig.disableConsoleAfterReady) {
|
|
18
|
-
loggers.disableConsole();
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// set global logger
|
|
23
|
-
for (const loggerName of Object.keys(loggers)) {
|
|
24
|
-
setCustomLogger(loggerName, loggers[loggerName]);
|
|
25
|
-
}
|
|
26
|
-
// reset global logger on beforeClose hook
|
|
27
|
-
app.beforeClose(() => {
|
|
28
|
-
for (const loggerName of Object.keys(loggers)) {
|
|
29
|
-
setCustomLogger(loggerName, undefined);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
loggers.coreLogger.info('[egg:logger] init all loggers with options: %j', loggerConfig);
|
|
33
|
-
|
|
34
|
-
return loggers;
|
|
35
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const LocalMessenger = require('./local');
|
|
4
|
-
const IPCMessenger = require('./ipc');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @class Messenger
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
exports.create = egg => {
|
|
11
|
-
return egg.options.mode === 'single'
|
|
12
|
-
? new LocalMessenger(egg)
|
|
13
|
-
: new IPCMessenger(egg);
|
|
14
|
-
};
|
package/lib/core/utils.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const util = require('util');
|
|
4
|
-
const is = require('is-type-of');
|
|
5
|
-
const URL = require('url').URL;
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
convertObject,
|
|
9
|
-
safeParseURL,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
function convertObject(obj, ignore) {
|
|
13
|
-
if (!is.array(ignore)) ignore = [ ignore ];
|
|
14
|
-
for (const key of Object.keys(obj)) {
|
|
15
|
-
obj[key] = convertValue(key, obj[key], ignore);
|
|
16
|
-
}
|
|
17
|
-
return obj;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function convertValue(key, value, ignore) {
|
|
21
|
-
if (is.nullOrUndefined(value)) return value;
|
|
22
|
-
|
|
23
|
-
let hit = false;
|
|
24
|
-
for (const matchKey of ignore) {
|
|
25
|
-
if (is.string(matchKey) && matchKey === key) {
|
|
26
|
-
hit = true;
|
|
27
|
-
break;
|
|
28
|
-
} else if (is.regExp(matchKey) && matchKey.test(key)) {
|
|
29
|
-
hit = true;
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (!hit) {
|
|
34
|
-
if (is.symbol(value) || is.regExp(value)) return value.toString();
|
|
35
|
-
if (is.primitive(value) || is.array(value)) return value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// only convert recursively when it's a plain object,
|
|
39
|
-
// o = {}
|
|
40
|
-
if (Object.getPrototypeOf(value) === Object.prototype) {
|
|
41
|
-
return convertObject(value, ignore);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// support class
|
|
45
|
-
const name = value.name || 'anonymous';
|
|
46
|
-
if (is.class(value)) {
|
|
47
|
-
return `<Class ${name}>`;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// support generator function
|
|
51
|
-
if (is.function(value)) {
|
|
52
|
-
if (is.generatorFunction(value)) return `<GeneratorFunction ${name}>`;
|
|
53
|
-
if (is.asyncFunction(value)) return `<AsyncFunction ${name}>`;
|
|
54
|
-
return `<Function ${name}>`;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const typeName = value.constructor.name;
|
|
58
|
-
if (typeName) {
|
|
59
|
-
if (is.buffer(value) || is.string(value)) return `<${typeName} len: ${value.length}>`;
|
|
60
|
-
return `<${typeName}>`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/* istanbul ignore next */
|
|
64
|
-
return util.format(value);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function safeParseURL(url) {
|
|
68
|
-
try {
|
|
69
|
-
return new URL(url);
|
|
70
|
-
} catch (err) {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
}
|