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.
Files changed (202) hide show
  1. package/README.md +2 -1
  2. package/README.zh-CN.md +7 -5
  3. package/dist/commonjs/agent.d.ts +4 -0
  4. package/dist/commonjs/agent.js +10 -0
  5. package/dist/commonjs/app/extend/context.d.ts +2 -0
  6. package/dist/commonjs/app/extend/context.js +263 -0
  7. package/dist/commonjs/app/middleware/body_parser.d.ts +2 -0
  8. package/dist/commonjs/app/middleware/body_parser.js +8 -0
  9. package/dist/commonjs/app/middleware/meta.d.ts +11 -0
  10. package/dist/commonjs/app/middleware/meta.js +22 -0
  11. package/dist/commonjs/app/middleware/notfound.d.ts +8 -0
  12. package/dist/commonjs/app/middleware/notfound.js +31 -0
  13. package/dist/commonjs/app/middleware/override_method.d.ts +2 -0
  14. package/dist/commonjs/app/middleware/override_method.js +8 -0
  15. package/dist/commonjs/app/middleware/site_file.d.ts +7 -0
  16. package/dist/commonjs/app/middleware/site_file.js +59 -0
  17. package/dist/commonjs/config/config.default.d.ts +9 -0
  18. package/dist/commonjs/config/config.default.js +378 -0
  19. package/dist/commonjs/config/config.local.d.ts +8 -0
  20. package/dist/commonjs/config/config.local.js +12 -0
  21. package/dist/commonjs/config/config.unittest.d.ts +7 -0
  22. package/dist/commonjs/config/config.unittest.js +11 -0
  23. package/dist/commonjs/config/plugin.d.ts +122 -0
  24. package/dist/commonjs/config/plugin.js +125 -0
  25. package/dist/commonjs/index.d.ts +61 -0
  26. package/dist/commonjs/index.js +89 -0
  27. package/dist/commonjs/lib/agent.d.ts +19 -0
  28. package/dist/commonjs/lib/agent.js +58 -0
  29. package/dist/commonjs/lib/application.d.ts +66 -0
  30. package/dist/commonjs/lib/application.js +281 -0
  31. package/dist/commonjs/lib/core/base_context_class.d.ts +14 -0
  32. package/dist/commonjs/lib/core/base_context_class.js +22 -0
  33. package/dist/commonjs/lib/core/base_context_logger.d.ts +36 -0
  34. package/dist/commonjs/lib/core/base_context_logger.js +64 -0
  35. package/dist/commonjs/lib/core/base_hook_class.d.ts +11 -0
  36. package/dist/commonjs/lib/core/base_hook_class.js +30 -0
  37. package/dist/commonjs/lib/core/context_httpclient.d.ts +16 -0
  38. package/dist/commonjs/lib/core/context_httpclient.js +30 -0
  39. package/dist/commonjs/lib/core/httpclient.d.ts +14 -0
  40. package/dist/commonjs/lib/core/httpclient.js +40 -0
  41. package/dist/commonjs/lib/core/logger.d.ts +3 -0
  42. package/dist/commonjs/lib/core/logger.js +40 -0
  43. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +50 -0
  44. package/dist/commonjs/lib/core/messenger/IMessenger.js +3 -0
  45. package/dist/commonjs/lib/core/messenger/index.d.ts +7 -0
  46. package/dist/commonjs/lib/core/messenger/index.js +14 -0
  47. package/dist/commonjs/lib/core/messenger/ipc.d.ts +57 -0
  48. package/dist/commonjs/lib/core/messenger/ipc.js +126 -0
  49. package/dist/commonjs/lib/core/messenger/local.d.ts +61 -0
  50. package/dist/commonjs/lib/core/messenger/local.js +134 -0
  51. package/dist/commonjs/lib/core/singleton.d.ts +23 -0
  52. package/dist/commonjs/lib/core/singleton.js +120 -0
  53. package/dist/commonjs/lib/core/utils.d.ts +2 -0
  54. package/dist/commonjs/lib/core/utils.js +77 -0
  55. package/dist/commonjs/lib/egg.d.ts +271 -0
  56. package/dist/commonjs/lib/egg.js +613 -0
  57. package/dist/commonjs/lib/loader/AgentWorkerLoader.d.ts +12 -0
  58. package/dist/commonjs/lib/loader/AgentWorkerLoader.js +24 -0
  59. package/dist/commonjs/lib/loader/AppWorkerLoader.d.ts +17 -0
  60. package/dist/commonjs/lib/loader/AppWorkerLoader.js +43 -0
  61. package/dist/commonjs/lib/loader/EggApplicationLoader.d.ts +4 -0
  62. package/dist/commonjs/lib/loader/EggApplicationLoader.js +8 -0
  63. package/dist/commonjs/lib/loader/index.d.ts +3 -0
  64. package/dist/commonjs/lib/loader/index.js +22 -0
  65. package/dist/commonjs/lib/start.d.ts +15 -0
  66. package/dist/commonjs/lib/start.js +49 -0
  67. package/dist/commonjs/lib/type.d.ts +296 -0
  68. package/dist/commonjs/lib/type.js +3 -0
  69. package/dist/commonjs/package.json +3 -0
  70. package/dist/esm/agent.d.ts +4 -0
  71. package/dist/esm/agent.js +7 -0
  72. package/dist/esm/app/extend/context.d.ts +2 -0
  73. package/dist/esm/app/extend/context.js +258 -0
  74. package/dist/esm/app/middleware/body_parser.d.ts +2 -0
  75. package/dist/esm/app/middleware/body_parser.js +3 -0
  76. package/dist/esm/app/middleware/meta.d.ts +11 -0
  77. package/dist/esm/app/middleware/meta.js +20 -0
  78. package/dist/esm/app/middleware/notfound.d.ts +8 -0
  79. package/dist/esm/app/middleware/notfound.js +29 -0
  80. package/dist/esm/app/middleware/override_method.d.ts +2 -0
  81. package/dist/esm/app/middleware/override_method.js +3 -0
  82. package/dist/esm/app/middleware/site_file.d.ts +7 -0
  83. package/dist/esm/app/middleware/site_file.js +54 -0
  84. package/dist/esm/config/config.default.d.ts +9 -0
  85. package/dist/esm/config/config.default.js +373 -0
  86. package/dist/esm/config/config.local.d.ts +8 -0
  87. package/dist/esm/config/config.local.js +10 -0
  88. package/dist/esm/config/config.unittest.d.ts +7 -0
  89. package/dist/esm/config/config.unittest.js +9 -0
  90. package/dist/esm/config/favicon.png +0 -0
  91. package/dist/esm/config/plugin.d.ts +122 -0
  92. package/dist/esm/config/plugin.js +123 -0
  93. package/dist/esm/index.d.ts +61 -0
  94. package/dist/esm/index.js +65 -0
  95. package/dist/esm/lib/agent.d.ts +19 -0
  96. package/dist/esm/lib/agent.js +54 -0
  97. package/dist/esm/lib/application.d.ts +66 -0
  98. package/dist/esm/lib/application.js +274 -0
  99. package/dist/esm/lib/core/base_context_class.d.ts +14 -0
  100. package/dist/esm/lib/core/base_context_class.js +18 -0
  101. package/dist/esm/lib/core/base_context_logger.d.ts +36 -0
  102. package/dist/esm/lib/core/base_context_logger.js +60 -0
  103. package/dist/esm/lib/core/base_hook_class.d.ts +11 -0
  104. package/dist/esm/lib/core/base_hook_class.js +23 -0
  105. package/dist/esm/lib/core/context_httpclient.d.ts +16 -0
  106. package/dist/esm/lib/core/context_httpclient.js +26 -0
  107. package/dist/esm/lib/core/httpclient.d.ts +14 -0
  108. package/dist/esm/lib/core/httpclient.js +33 -0
  109. package/dist/esm/lib/core/logger.d.ts +3 -0
  110. package/dist/esm/lib/core/logger.js +37 -0
  111. package/dist/esm/lib/core/messenger/IMessenger.d.ts +50 -0
  112. package/dist/esm/lib/core/messenger/IMessenger.js +2 -0
  113. package/dist/esm/lib/core/messenger/index.d.ts +7 -0
  114. package/dist/esm/lib/core/messenger/index.js +11 -0
  115. package/dist/esm/lib/core/messenger/ipc.d.ts +57 -0
  116. package/dist/esm/lib/core/messenger/ipc.js +119 -0
  117. package/dist/esm/lib/core/messenger/local.d.ts +61 -0
  118. package/dist/esm/lib/core/messenger/local.js +127 -0
  119. package/dist/esm/lib/core/singleton.d.ts +23 -0
  120. package/dist/esm/lib/core/singleton.js +113 -0
  121. package/dist/esm/lib/core/utils.d.ts +2 -0
  122. package/dist/esm/lib/core/utils.js +70 -0
  123. package/dist/esm/lib/egg.d.ts +271 -0
  124. package/dist/esm/lib/egg.js +573 -0
  125. package/dist/esm/lib/loader/AgentWorkerLoader.d.ts +12 -0
  126. package/dist/esm/lib/loader/AgentWorkerLoader.js +20 -0
  127. package/dist/esm/lib/loader/AppWorkerLoader.d.ts +17 -0
  128. package/dist/esm/lib/loader/AppWorkerLoader.js +39 -0
  129. package/dist/esm/lib/loader/EggApplicationLoader.d.ts +4 -0
  130. package/dist/esm/lib/loader/EggApplicationLoader.js +4 -0
  131. package/dist/esm/lib/loader/index.d.ts +3 -0
  132. package/dist/esm/lib/loader/index.js +4 -0
  133. package/dist/esm/lib/start.d.ts +15 -0
  134. package/dist/esm/lib/start.js +43 -0
  135. package/dist/esm/lib/type.d.ts +296 -0
  136. package/dist/esm/lib/type.js +2 -0
  137. package/dist/esm/package.json +3 -0
  138. package/dist/package.json +4 -0
  139. package/package.json +87 -72
  140. package/src/agent.ts +7 -0
  141. package/{app/extend/context.js → src/app/extend/context.ts} +19 -23
  142. package/src/app/middleware/body_parser.ts +3 -0
  143. package/{app/middleware/meta.js → src/app/middleware/meta.ts} +12 -4
  144. package/{app/middleware/notfound.js → src/app/middleware/notfound.ts} +9 -3
  145. package/src/app/middleware/override_method.ts +3 -0
  146. package/src/app/middleware/site_file.ts +70 -0
  147. package/{config/config.default.js → src/config/config.default.ts} +24 -45
  148. package/src/config/config.local.ts +11 -0
  149. package/src/config/config.unittest.ts +10 -0
  150. package/src/config/favicon.png +0 -0
  151. package/{config/plugin.js → src/config/plugin.ts} +1 -3
  152. package/src/index.ts +78 -0
  153. package/src/lib/agent.ts +66 -0
  154. package/{lib/application.js → src/lib/application.ts} +79 -120
  155. package/src/lib/core/base_context_class.ts +21 -0
  156. package/src/lib/core/base_context_logger.ts +67 -0
  157. package/src/lib/core/base_hook_class.ts +30 -0
  158. package/src/lib/core/context_httpclient.ts +33 -0
  159. package/src/lib/core/httpclient.ts +52 -0
  160. package/src/lib/core/logger.ts +42 -0
  161. package/src/lib/core/messenger/IMessenger.ts +58 -0
  162. package/src/lib/core/messenger/index.ts +15 -0
  163. package/{lib/core/messenger/ipc.js → src/lib/core/messenger/ipc.ts} +24 -29
  164. package/{lib/core/messenger/local.js → src/lib/core/messenger/local.ts} +27 -21
  165. package/{lib/core/singleton.js → src/lib/core/singleton.ts} +56 -33
  166. package/src/lib/core/utils.ts +77 -0
  167. package/{lib/egg.js → src/lib/egg.ts} +262 -219
  168. package/src/lib/loader/AgentWorkerLoader.ts +21 -0
  169. package/src/lib/loader/AppWorkerLoader.ts +42 -0
  170. package/src/lib/loader/EggApplicationLoader.ts +5 -0
  171. package/src/lib/loader/index.ts +3 -0
  172. package/src/lib/start.ts +56 -0
  173. package/src/lib/type.ts +336 -0
  174. package/CHANGELOG.md +0 -2395
  175. package/History.md +0 -52
  176. package/agent.js +0 -11
  177. package/app/middleware/body_parser.js +0 -3
  178. package/app/middleware/override_method.js +0 -3
  179. package/app/middleware/site_file.js +0 -31
  180. package/config/config.local.js +0 -7
  181. package/config/config.unittest.js +0 -8
  182. package/index.d.ts +0 -1288
  183. package/index.js +0 -68
  184. package/lib/agent.js +0 -95
  185. package/lib/core/base_context_class.js +0 -20
  186. package/lib/core/base_context_logger.js +0 -64
  187. package/lib/core/base_hook_class.js +0 -31
  188. package/lib/core/context_httpclient.js +0 -26
  189. package/lib/core/dnscache_httpclient.js +0 -93
  190. package/lib/core/httpclient.js +0 -119
  191. package/lib/core/httpclient_next.js +0 -80
  192. package/lib/core/logger.js +0 -35
  193. package/lib/core/messenger/index.js +0 -14
  194. package/lib/core/utils.js +0 -73
  195. package/lib/loader/agent_worker_loader.js +0 -27
  196. package/lib/loader/app_worker_loader.js +0 -48
  197. package/lib/loader/index.js +0 -5
  198. package/lib/start.js +0 -39
  199. /package/{config → dist/commonjs/config}/favicon.png +0 -0
  200. /package/{app → src/app}/extend/helper.js +0 -0
  201. /package/{app → src/app}/extend/request.js +0 -0
  202. /package/{app → src/app}/extend/response.js +0 -0
@@ -1,9 +1,7 @@
1
- 'use strict';
2
-
3
- const { performance } = require('perf_hooks');
4
- const delegate = require('delegates');
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 proto = module.exports = {
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 infomation,
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._name || scope.name || eggUtils.getCalleeFromStack(true);
218
- scope._name = taskName;
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
- // use app.toAsyncFunction to support both generator function and async function
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(proto, 'request')
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(proto, 'response')
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;
@@ -0,0 +1,3 @@
1
+ import bodyparser from 'koa-bodyparser';
2
+
3
+ export default bodyparser;
@@ -2,12 +2,20 @@
2
2
  * meta middleware, should be the first middleware
3
3
  */
4
4
 
5
- const { performance } = require('perf_hooks');
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
- module.exports = options => {
8
- return async function meta(ctx, next) {
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', ctx.host, ctx.header['user-agent']);
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
- 'use strict';
1
+ import type { EggCoreContext } from '@eggjs/core';
2
+ import type { Next } from '../../lib/type.js';
2
3
 
3
- module.exports = options => {
4
- return async function notfound(ctx, next) {
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,3 @@
1
+ import override from 'koa-override';
2
+
3
+ export default override;
@@ -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
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
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
- module.exports = appInfo => {
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 dumpped from `app.config`.
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 warnning log
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 option of `notfound` middleware
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 immdiately when matching.
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
- '/favicon.ico': fs.readFileSync(path.join(__dirname, 'favicon.png')),
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 option of `bodyParser` middleware
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 += ', check bodyParser config';
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} enablePerformanceTimer - using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to false. e.g.: logger will set 1.456ms instead of 1ms.
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
- enablePerformanceTimer: false,
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, default is false
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 option of `meta` middleware
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 = null;
400
+ config.onClientError = undefined;
422
401
 
423
402
  return config;
424
403
  };
@@ -0,0 +1,11 @@
1
+ import type { EggAppConfig } from '../lib/type.js';
2
+
3
+ export default () => {
4
+ return {
5
+ logger: {
6
+ coreLogger: {
7
+ consoleLevel: 'WARN',
8
+ },
9
+ },
10
+ } satisfies Partial<EggAppConfig>;
11
+ };
@@ -0,0 +1,10 @@
1
+ import type { EggAppConfig } from '../lib/type.js';
2
+
3
+ export default () => {
4
+ return {
5
+ logger: {
6
+ consoleLevel: 'WARN',
7
+ buffer: false,
8
+ },
9
+ } satisfies Partial<EggAppConfig>;
10
+ };
Binary file
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- module.exports = {
1
+ export default {
4
2
  // enable plugins
5
3
 
6
4
  /**
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';
@@ -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
+ }