egg 3.29.0 → 4.0.0-beta.2
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 +2 -0
- package/dist/commonjs/app/extend/context.js +263 -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 +59 -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 +271 -0
- package/dist/commonjs/lib/egg.js +613 -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 +296 -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/extend/context.d.ts +2 -0
- package/dist/esm/app/extend/context.js +258 -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 +54 -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 +271 -0
- package/dist/esm/lib/egg.js +573 -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 +296 -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 +87 -72
- package/src/agent.ts +7 -0
- package/{app/extend/context.js → src/app/extend/context.ts} +19 -23
- 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 +70 -0
- package/{config/config.default.js → src/config/config.default.ts} +24 -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} +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} +24 -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} +262 -219
- 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 +336 -0
- package/CHANGELOG.md +0 -2395
- package/History.md +0 -52
- 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 -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/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/helper.js +0 -0
- /package/{app → src/app}/extend/request.js +0 -0
- /package/{app → src/app}/extend/response.js +0 -0
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { assign } = require('utility');
|
|
6
|
-
const eggUtils = require('egg-core').utils;
|
|
1
|
+
import { performance } from 'node:perf_hooks';
|
|
2
|
+
import delegate from 'delegates';
|
|
3
|
+
import { assign } from 'utility';
|
|
4
|
+
import { utils } from '@eggjs/core';
|
|
7
5
|
|
|
8
6
|
const HELPER = Symbol('Context#helper');
|
|
9
7
|
const LOCALS = Symbol('Context#locals');
|
|
@@ -13,8 +11,7 @@ const CONTEXT_LOGGERS = Symbol('Context#logger');
|
|
|
13
11
|
const CONTEXT_HTTPCLIENT = Symbol('Context#httpclient');
|
|
14
12
|
const CONTEXT_ROUTER = Symbol('Context#router');
|
|
15
13
|
|
|
16
|
-
const
|
|
17
|
-
|
|
14
|
+
const Context = {
|
|
18
15
|
/**
|
|
19
16
|
* Get the current visitor's cookies.
|
|
20
17
|
*/
|
|
@@ -45,7 +42,7 @@ const proto = module.exports = {
|
|
|
45
42
|
* @param {Object} [options] - options for request.
|
|
46
43
|
* @return {Object} see {@link ContextHttpClient#curl}
|
|
47
44
|
*/
|
|
48
|
-
curl(url, options) {
|
|
45
|
+
curl(url: string, options?: object) {
|
|
49
46
|
return this.httpclient.curl(url, options);
|
|
50
47
|
},
|
|
51
48
|
|
|
@@ -88,13 +85,13 @@ const proto = module.exports = {
|
|
|
88
85
|
},
|
|
89
86
|
|
|
90
87
|
/**
|
|
91
|
-
* Wrap app.loggers with context
|
|
88
|
+
* Wrap app.loggers with context information,
|
|
92
89
|
* if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
|
|
93
90
|
*
|
|
94
91
|
* @param {String} name - logger name
|
|
95
92
|
* @return {Logger} logger
|
|
96
93
|
*/
|
|
97
|
-
getLogger(name) {
|
|
94
|
+
getLogger(name: string) {
|
|
98
95
|
if (this.app.config.logger.enableFastContextLogger) {
|
|
99
96
|
return this.app.getLogger(name);
|
|
100
97
|
}
|
|
@@ -211,25 +208,22 @@ const proto = module.exports = {
|
|
|
211
208
|
* });
|
|
212
209
|
* ```
|
|
213
210
|
*/
|
|
214
|
-
runInBackground(scope) {
|
|
211
|
+
runInBackground(scope: (ctx: any) => Promise<void>) {
|
|
215
212
|
// try to use custom function name first
|
|
216
213
|
/* istanbul ignore next */
|
|
217
|
-
const taskName = scope
|
|
218
|
-
scope
|
|
219
|
-
this._runInBackground(scope);
|
|
214
|
+
const taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
|
|
215
|
+
this._runInBackground(scope, taskName);
|
|
220
216
|
},
|
|
221
217
|
|
|
222
218
|
// let plugins or frameworks to reuse _runInBackground in some cases.
|
|
223
219
|
// e.g.: https://github.com/eggjs/egg-mock/pull/78
|
|
224
|
-
_runInBackground(scope) {
|
|
220
|
+
_runInBackground(scope: (ctx: any) => Promise<void>, taskName: string) {
|
|
221
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
225
222
|
const ctx = this;
|
|
226
223
|
const start = performance.now();
|
|
227
|
-
/* istanbul ignore next */
|
|
228
|
-
const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
|
|
229
224
|
// use setImmediate to ensure all sync logic will run async
|
|
230
225
|
return new Promise(resolve => setImmediate(resolve))
|
|
231
|
-
|
|
232
|
-
.then(() => ctx.app.toAsyncFunction(scope)(ctx))
|
|
226
|
+
.then(() => scope(ctx))
|
|
233
227
|
.then(() => {
|
|
234
228
|
ctx.coreLogger.info('[egg:background] task:%s success (%dms)',
|
|
235
229
|
taskName, Math.floor((performance.now() - start) * 1000) / 1000);
|
|
@@ -244,13 +238,13 @@ const proto = module.exports = {
|
|
|
244
238
|
ctx.app.emit('error', err, ctx);
|
|
245
239
|
});
|
|
246
240
|
},
|
|
247
|
-
};
|
|
241
|
+
} as any;
|
|
248
242
|
|
|
249
243
|
/**
|
|
250
244
|
* Context delegation.
|
|
251
245
|
*/
|
|
252
246
|
|
|
253
|
-
delegate(
|
|
247
|
+
delegate(Context, 'request')
|
|
254
248
|
/**
|
|
255
249
|
* @member {Boolean} Context#acceptJSON
|
|
256
250
|
* @see Request#acceptJSON
|
|
@@ -276,10 +270,12 @@ delegate(proto, 'request')
|
|
|
276
270
|
*/
|
|
277
271
|
.access('ip');
|
|
278
272
|
|
|
279
|
-
delegate(
|
|
273
|
+
delegate(Context, 'response')
|
|
280
274
|
/**
|
|
281
275
|
* @member {Number} Context#realStatus
|
|
282
276
|
* @see Response#realStatus
|
|
283
277
|
* @since 1.0.0
|
|
284
278
|
*/
|
|
285
279
|
.access('realStatus');
|
|
280
|
+
|
|
281
|
+
export default Context;
|
|
@@ -2,12 +2,20 @@
|
|
|
2
2
|
* meta middleware, should be the first middleware
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { performance } from 'node:perf_hooks';
|
|
6
|
+
import type { EggCoreContext } from '@eggjs/core';
|
|
7
|
+
import type { Next } from '../../lib/type.js';
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
export interface MetaMiddlewareOptions {
|
|
10
|
+
enable: boolean;
|
|
11
|
+
logging: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default (options: MetaMiddlewareOptions) => {
|
|
15
|
+
return async function meta(ctx: EggCoreContext, next: Next) {
|
|
9
16
|
if (options.logging) {
|
|
10
|
-
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
|
|
17
|
+
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
|
|
18
|
+
ctx.host, ctx.header['user-agent']);
|
|
11
19
|
}
|
|
12
20
|
await next();
|
|
13
21
|
// total response time header
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import type { EggCoreContext } from '@eggjs/core';
|
|
2
|
+
import type { Next } from '../../lib/type.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
export interface NotFoundMiddlewareOptions {
|
|
5
|
+
enable: boolean;
|
|
6
|
+
pageUrl: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default (options: NotFoundMiddlewareOptions) => {
|
|
10
|
+
return async function notfound(ctx: EggCoreContext, next: Next) {
|
|
5
11
|
await next();
|
|
6
12
|
|
|
7
13
|
if (ctx.status !== 404 || ctx.body) {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import type { EggCoreContext } from '@eggjs/core';
|
|
5
|
+
import type { Next } from '../../lib/type.js';
|
|
6
|
+
|
|
7
|
+
export type SiteFileContentFun = (ctx: EggCoreContext) => Promise<Buffer | string>;
|
|
8
|
+
|
|
9
|
+
export interface SiteFileMiddlewareOptions {
|
|
10
|
+
enable: boolean;
|
|
11
|
+
cacheControl: string;
|
|
12
|
+
[key: string]: string | Buffer | boolean | SiteFileContentFun | URL;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const BUFFER_CACHE = Symbol('siteFile URL buffer cache');
|
|
16
|
+
|
|
17
|
+
module.exports = (options: SiteFileMiddlewareOptions) => {
|
|
18
|
+
return async function siteFile(ctx: EggCoreContext, next: Next) {
|
|
19
|
+
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') {
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
22
|
+
/* istanbul ignore if */
|
|
23
|
+
if (ctx.path[0] !== '/') {
|
|
24
|
+
return next();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let content = options[ctx.path];
|
|
28
|
+
if (!content) {
|
|
29
|
+
return next();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// '/favicon.ico': 'https://eggjs.org/favicon.ico' or '/favicon.ico': async (ctx) => 'https://eggjs.org/favicon.ico'
|
|
33
|
+
// content is function
|
|
34
|
+
if (typeof content === 'function') {
|
|
35
|
+
content = await content(ctx);
|
|
36
|
+
}
|
|
37
|
+
// content is url
|
|
38
|
+
if (typeof content === 'string') {
|
|
39
|
+
return ctx.redirect(content);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// URL
|
|
43
|
+
if (content instanceof URL) {
|
|
44
|
+
if (content.protocol !== 'file:') {
|
|
45
|
+
return ctx.redirect(content.href);
|
|
46
|
+
}
|
|
47
|
+
// protocol = file:
|
|
48
|
+
let buffer = Reflect.get(content, BUFFER_CACHE) as Buffer;
|
|
49
|
+
if (!buffer) {
|
|
50
|
+
buffer = await readFile(fileURLToPath(content));
|
|
51
|
+
Reflect.set(content, BUFFER_CACHE, buffer);
|
|
52
|
+
}
|
|
53
|
+
ctx.set('cache-control', options.cacheControl);
|
|
54
|
+
ctx.body = content;
|
|
55
|
+
ctx.type = path.extname(ctx.path);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// '/robots.txt': Buffer <xx..
|
|
60
|
+
// content is buffer
|
|
61
|
+
if (Buffer.isBuffer(content)) {
|
|
62
|
+
ctx.set('cache-control', options.cacheControl);
|
|
63
|
+
ctx.body = content;
|
|
64
|
+
ctx.type = path.extname(ctx.path);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return next();
|
|
69
|
+
};
|
|
70
|
+
};
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import type { EggAppInfo } from '@eggjs/core';
|
|
4
|
+
import type { EggAppConfig } from '../lib/type.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* The configuration of egg application, can be access by `app.config`
|
|
8
8
|
* @class Config
|
|
9
9
|
* @since 1.0.0
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const config = {
|
|
15
|
-
|
|
11
|
+
export default (appInfo: EggAppInfo) => {
|
|
12
|
+
const config: Partial<EggAppConfig> = {
|
|
16
13
|
/**
|
|
17
14
|
* The environment of egg
|
|
18
15
|
* @member {String} Config#env
|
|
@@ -131,7 +128,7 @@ module.exports = appInfo => {
|
|
|
131
128
|
HOME: appInfo.HOME,
|
|
132
129
|
|
|
133
130
|
/**
|
|
134
|
-
* The directory of server running. You can find `application_config.json` under it that is
|
|
131
|
+
* The directory of server running. You can find `application_config.json` under it that is dumped from `app.config`.
|
|
135
132
|
* @member {String} Config#rundir
|
|
136
133
|
* @default
|
|
137
134
|
* @since 1.0.0
|
|
@@ -153,7 +150,7 @@ module.exports = appInfo => {
|
|
|
153
150
|
/secret/i,
|
|
154
151
|
]),
|
|
155
152
|
timing: {
|
|
156
|
-
// if boot action >= slowBootActionMinDuration, egg core will print it to
|
|
153
|
+
// if boot action >= slowBootActionMinDuration, egg core will print it to warning log
|
|
157
154
|
slowBootActionMinDuration: 5000,
|
|
158
155
|
},
|
|
159
156
|
},
|
|
@@ -176,7 +173,7 @@ module.exports = appInfo => {
|
|
|
176
173
|
};
|
|
177
174
|
|
|
178
175
|
/**
|
|
179
|
-
* The
|
|
176
|
+
* The options of `notfound` middleware
|
|
180
177
|
*
|
|
181
178
|
* It will return page or json depend on negotiation when 404,
|
|
182
179
|
* If pageUrl is set, it will redirect to the page.
|
|
@@ -185,13 +182,14 @@ module.exports = appInfo => {
|
|
|
185
182
|
* @property {String} pageUrl - the 404 page url
|
|
186
183
|
*/
|
|
187
184
|
config.notfound = {
|
|
185
|
+
enable: true,
|
|
188
186
|
pageUrl: '',
|
|
189
187
|
};
|
|
190
188
|
|
|
191
189
|
/**
|
|
192
190
|
* The option of `siteFile` middleware
|
|
193
191
|
*
|
|
194
|
-
* You can map some files using this options, it will response
|
|
192
|
+
* You can map some files using this options, it will response immediately when matching.
|
|
195
193
|
*
|
|
196
194
|
* @member {Object} Config#siteFile - key is path, and value is url or buffer.
|
|
197
195
|
* @property {String} cacheControl - files cache , default is public, max-age=2592000
|
|
@@ -202,13 +200,14 @@ module.exports = appInfo => {
|
|
|
202
200
|
* };
|
|
203
201
|
*/
|
|
204
202
|
config.siteFile = {
|
|
205
|
-
|
|
203
|
+
enable: true,
|
|
204
|
+
'/favicon.ico': pathToFileURL(path.join(__dirname, 'favicon.png')),
|
|
206
205
|
// default cache in 30 days
|
|
207
206
|
cacheControl: 'public, max-age=2592000',
|
|
208
207
|
};
|
|
209
208
|
|
|
210
209
|
/**
|
|
211
|
-
* The
|
|
210
|
+
* The options of `bodyParser` middleware
|
|
212
211
|
*
|
|
213
212
|
* @member Config#bodyParser
|
|
214
213
|
* @property {Boolean} enable - enable bodyParser or not, default is true
|
|
@@ -236,8 +235,9 @@ module.exports = appInfo => {
|
|
|
236
235
|
depth: 5,
|
|
237
236
|
parameterLimit: 1000,
|
|
238
237
|
},
|
|
238
|
+
onProtoPoisoning: 'error',
|
|
239
239
|
onerror(err, ctx) {
|
|
240
|
-
err.message
|
|
240
|
+
err.message = `${err.message}, check bodyParser config`;
|
|
241
241
|
if (ctx.status === 404) {
|
|
242
242
|
// set default status to 400, meaning client bad request
|
|
243
243
|
ctx.status = 400;
|
|
@@ -264,8 +264,7 @@ module.exports = appInfo => {
|
|
|
264
264
|
* @property {String} agentLogName - file name of agent worker log
|
|
265
265
|
* @property {Object} coreLogger - custom config of coreLogger
|
|
266
266
|
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to false
|
|
267
|
-
* @property {Boolean}
|
|
268
|
-
* @property {Boolean} enableFastContextLogger - using the app logger instead of EggContextLogger, defaults to false
|
|
267
|
+
* @property {Boolean} enableFastContextLogger - using the app logger instead of EggContextLogger, defaults to true
|
|
269
268
|
*/
|
|
270
269
|
config.logger = {
|
|
271
270
|
dir: path.join(appInfo.root, 'logs', appInfo.name),
|
|
@@ -282,8 +281,7 @@ module.exports = appInfo => {
|
|
|
282
281
|
errorLogName: 'common-error.log',
|
|
283
282
|
coreLogger: {},
|
|
284
283
|
allowDebugAtProd: false,
|
|
285
|
-
|
|
286
|
-
enableFastContextLogger: false,
|
|
284
|
+
enableFastContextLogger: true,
|
|
287
285
|
};
|
|
288
286
|
|
|
289
287
|
/**
|
|
@@ -303,39 +301,20 @@ module.exports = appInfo => {
|
|
|
303
301
|
* @property {Number} httpsAgent.freeSocketTimeout - httpss agent socket keepalive max free time, default is 4000 ms.
|
|
304
302
|
* @property {Number} httpsAgent.maxSockets - https agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
|
|
305
303
|
* @property {Number} httpsAgent.maxFreeSockets - https agent max free socket number of one host, default is 256.
|
|
306
|
-
* @property {Boolean} useHttpClientNext - use urllib@3 HttpClient
|
|
307
|
-
* @property {Boolean} allowH2 - use urllib@4 HttpClient and enable H2, default is false. Only works on Node.js >= 18
|
|
304
|
+
* @property {Boolean} useHttpClientNext - use urllib@3 HttpClient
|
|
308
305
|
*/
|
|
309
306
|
config.httpclient = {
|
|
310
|
-
enableDNSCache: false,
|
|
311
|
-
dnsCacheLookupInterval: 10000,
|
|
312
|
-
dnsCacheMaxLength: 1000,
|
|
313
|
-
|
|
314
307
|
request: {
|
|
315
308
|
timeout: 5000,
|
|
316
309
|
},
|
|
317
|
-
httpAgent: {
|
|
318
|
-
keepAlive: true,
|
|
319
|
-
freeSocketTimeout: 4000,
|
|
320
|
-
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
321
|
-
maxFreeSockets: 256,
|
|
322
|
-
},
|
|
323
|
-
httpsAgent: {
|
|
324
|
-
keepAlive: true,
|
|
325
|
-
freeSocketTimeout: 4000,
|
|
326
|
-
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
327
|
-
maxFreeSockets: 256,
|
|
328
|
-
},
|
|
329
|
-
useHttpClientNext: false,
|
|
330
|
-
// allowH2: false,
|
|
331
310
|
};
|
|
332
311
|
|
|
333
312
|
/**
|
|
334
|
-
* The
|
|
313
|
+
* The options of `meta` middleware
|
|
335
314
|
*
|
|
336
315
|
* @member Config#meta
|
|
337
|
-
* @property {Boolean} enable - enable meta or not, default is true
|
|
338
|
-
* @property {Boolean} logging - enable logging start request, default is false
|
|
316
|
+
* @property {Boolean} enable - enable meta or not, default is `true`
|
|
317
|
+
* @property {Boolean} logging - enable logging start request, default is `false`
|
|
339
318
|
*/
|
|
340
319
|
config.meta = {
|
|
341
320
|
enable: true,
|
|
@@ -371,7 +350,7 @@ module.exports = appInfo => {
|
|
|
371
350
|
config.serverTimeout = null;
|
|
372
351
|
|
|
373
352
|
/**
|
|
374
|
-
*
|
|
353
|
+
* The options of cluster
|
|
375
354
|
* @member {Object} Config#cluster
|
|
376
355
|
* @property {Object} listen - listen options, see {@link https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback}
|
|
377
356
|
* @property {String} listen.path - set a unix sock path when server listen
|
|
@@ -418,7 +397,7 @@ module.exports = appInfo => {
|
|
|
418
397
|
* };
|
|
419
398
|
* }
|
|
420
399
|
*/
|
|
421
|
-
config.onClientError =
|
|
400
|
+
config.onClientError = undefined;
|
|
422
401
|
|
|
423
402
|
return config;
|
|
424
403
|
};
|
|
Binary file
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @namespace Egg
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { BaseContextClass } from './lib/core/base_context_class.js';
|
|
6
|
+
import { startEgg } from './lib/start.js';
|
|
7
|
+
|
|
8
|
+
// export types
|
|
9
|
+
export * from './lib/egg.js';
|
|
10
|
+
export * from './lib/type.js';
|
|
11
|
+
export * from './lib/start.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Start egg application with cluster mode
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
export { startCluster } from 'egg-cluster';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Start egg application with single process mode
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
export const start = startEgg;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @member {Application} Egg#Application
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
*/
|
|
31
|
+
export { Application } from './lib/application.js';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @member {Agent} Egg#Agent
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
export { Agent } from './lib/agent.js';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @member {AppWorkerLoader} Egg#AppWorkerLoader
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @member {AgentWorkerLoader} Egg#AgentWorkerLoader
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @member {Controller} Egg#Controller
|
|
53
|
+
* @since 1.1.0
|
|
54
|
+
*/
|
|
55
|
+
export const Controller = BaseContextClass;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @member {Service} Egg#Service
|
|
59
|
+
* @since 1.1.0
|
|
60
|
+
*/
|
|
61
|
+
export const Service = BaseContextClass;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @member {Subscription} Egg#Subscription
|
|
65
|
+
* @since 1.10.0
|
|
66
|
+
*/
|
|
67
|
+
export const Subscription = BaseContextClass;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @member {BaseContextClass} Egg#BaseContextClass
|
|
71
|
+
* @since 1.2.0
|
|
72
|
+
*/
|
|
73
|
+
export { BaseContextClass } from './lib/core/base_context_class.js';
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @member {Boot} Egg#Boot
|
|
77
|
+
*/
|
|
78
|
+
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
|
+
}
|