egg 4.0.0-beta.12 → 4.0.0-beta.14

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 (133) hide show
  1. package/dist/commonjs/app/extend/context.d.ts +35 -7
  2. package/dist/commonjs/app/extend/context.js +24 -22
  3. package/dist/commonjs/app/extend/context.types.d.ts +4 -1
  4. package/dist/commonjs/app/extend/request.d.ts +7 -2
  5. package/dist/commonjs/app/extend/request.js +2 -1
  6. package/dist/commonjs/app/extend/request.types.d.ts +10 -0
  7. package/dist/commonjs/app/extend/request.types.js +3 -0
  8. package/dist/commonjs/app/extend/response.d.ts +1 -0
  9. package/dist/commonjs/app/extend/response.js +2 -1
  10. package/dist/commonjs/app/extend/response.types.d.ts +7 -0
  11. package/dist/commonjs/app/extend/response.types.js +3 -0
  12. package/dist/commonjs/app/middleware/meta.d.ts +2 -2
  13. package/dist/commonjs/app/middleware/meta.js +1 -1
  14. package/dist/commonjs/app/middleware/notfound.d.ts +2 -2
  15. package/dist/commonjs/app/middleware/notfound.js +1 -1
  16. package/dist/commonjs/app/middleware/site_file.d.ts +3 -3
  17. package/dist/commonjs/app/middleware/site_file.js +1 -1
  18. package/dist/commonjs/config/config.default.d.ts +1 -1
  19. package/dist/commonjs/index.d.ts +11 -6
  20. package/dist/commonjs/index.js +11 -22
  21. package/dist/commonjs/lib/application.d.ts +7 -3
  22. package/dist/commonjs/lib/application.js +15 -4
  23. package/dist/commonjs/lib/core/base_context_class.d.ts +6 -3
  24. package/dist/commonjs/lib/core/base_context_class.js +1 -2
  25. package/dist/commonjs/lib/core/base_hook_class.d.ts +2 -2
  26. package/dist/commonjs/lib/core/base_hook_class.js +1 -2
  27. package/dist/commonjs/lib/core/context_httpclient.d.ts +3 -3
  28. package/dist/commonjs/lib/core/context_httpclient.js +1 -1
  29. package/dist/commonjs/lib/core/httpclient.d.ts +4 -3
  30. package/dist/commonjs/lib/core/httpclient.js +1 -1
  31. package/dist/commonjs/lib/core/messenger/base.d.ts +8 -0
  32. package/dist/commonjs/lib/core/messenger/base.js +31 -0
  33. package/dist/commonjs/lib/core/messenger/index.js +3 -2
  34. package/dist/commonjs/lib/core/messenger/ipc.d.ts +2 -3
  35. package/dist/commonjs/lib/core/messenger/ipc.js +4 -6
  36. package/dist/commonjs/lib/core/messenger/local.d.ts +2 -3
  37. package/dist/commonjs/lib/core/messenger/local.js +4 -9
  38. package/dist/commonjs/lib/core/utils.d.ts +1 -1
  39. package/dist/commonjs/lib/core/utils.js +3 -3
  40. package/dist/commonjs/lib/egg.d.ts +17 -11
  41. package/dist/commonjs/lib/egg.js +13 -4
  42. package/dist/commonjs/lib/egg.types.d.ts +8 -4
  43. package/dist/commonjs/lib/error/CookieLimitExceedError.d.ts +5 -0
  44. package/dist/commonjs/lib/error/CookieLimitExceedError.js +16 -0
  45. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.d.ts +5 -0
  46. package/dist/commonjs/lib/error/MessageUnhandledRejectionError.js +16 -0
  47. package/dist/commonjs/lib/error/index.d.ts +2 -0
  48. package/dist/commonjs/lib/error/index.js +19 -0
  49. package/dist/commonjs/lib/{type.d.ts → types.d.ts} +37 -4
  50. package/dist/commonjs/lib/{type.js → types.js} +1 -1
  51. package/dist/esm/app/extend/context.d.ts +35 -7
  52. package/dist/esm/app/extend/context.js +23 -18
  53. package/dist/esm/app/extend/context.types.d.ts +4 -1
  54. package/dist/esm/app/extend/request.d.ts +7 -2
  55. package/dist/esm/app/extend/request.js +2 -1
  56. package/dist/esm/app/extend/request.types.d.ts +10 -0
  57. package/dist/esm/app/extend/request.types.js +2 -0
  58. package/dist/esm/app/extend/response.d.ts +1 -0
  59. package/dist/esm/app/extend/response.js +2 -1
  60. package/dist/esm/app/extend/response.types.d.ts +7 -0
  61. package/dist/esm/app/extend/response.types.js +2 -0
  62. package/dist/esm/app/middleware/meta.d.ts +2 -2
  63. package/dist/esm/app/middleware/meta.js +1 -1
  64. package/dist/esm/app/middleware/notfound.d.ts +2 -2
  65. package/dist/esm/app/middleware/notfound.js +1 -1
  66. package/dist/esm/app/middleware/site_file.d.ts +3 -3
  67. package/dist/esm/app/middleware/site_file.js +1 -1
  68. package/dist/esm/config/config.default.d.ts +1 -1
  69. package/dist/esm/index.d.ts +11 -6
  70. package/dist/esm/index.js +12 -7
  71. package/dist/esm/lib/application.d.ts +7 -3
  72. package/dist/esm/lib/application.js +15 -4
  73. package/dist/esm/lib/core/base_context_class.d.ts +6 -3
  74. package/dist/esm/lib/core/base_context_class.js +1 -2
  75. package/dist/esm/lib/core/base_hook_class.d.ts +2 -2
  76. package/dist/esm/lib/core/base_hook_class.js +1 -2
  77. package/dist/esm/lib/core/context_httpclient.d.ts +3 -3
  78. package/dist/esm/lib/core/context_httpclient.js +1 -1
  79. package/dist/esm/lib/core/httpclient.d.ts +4 -3
  80. package/dist/esm/lib/core/httpclient.js +1 -1
  81. package/dist/esm/lib/core/messenger/base.d.ts +8 -0
  82. package/dist/esm/lib/core/messenger/base.js +27 -0
  83. package/dist/esm/lib/core/messenger/index.js +3 -2
  84. package/dist/esm/lib/core/messenger/ipc.d.ts +2 -3
  85. package/dist/esm/lib/core/messenger/ipc.js +4 -6
  86. package/dist/esm/lib/core/messenger/local.d.ts +2 -3
  87. package/dist/esm/lib/core/messenger/local.js +4 -6
  88. package/dist/esm/lib/core/utils.d.ts +1 -1
  89. package/dist/esm/lib/core/utils.js +3 -3
  90. package/dist/esm/lib/egg.d.ts +17 -11
  91. package/dist/esm/lib/egg.js +14 -4
  92. package/dist/esm/lib/egg.types.d.ts +8 -4
  93. package/dist/esm/lib/error/CookieLimitExceedError.d.ts +5 -0
  94. package/dist/esm/lib/error/CookieLimitExceedError.js +12 -0
  95. package/dist/esm/lib/error/MessageUnhandledRejectionError.d.ts +5 -0
  96. package/dist/esm/lib/error/MessageUnhandledRejectionError.js +12 -0
  97. package/dist/esm/lib/error/index.d.ts +2 -0
  98. package/dist/esm/lib/error/index.js +3 -0
  99. package/dist/esm/lib/{type.d.ts → types.d.ts} +37 -4
  100. package/dist/esm/lib/types.js +2 -0
  101. package/dist/package.json +1 -1
  102. package/package.json +5 -7
  103. package/src/app/extend/context.ts +33 -27
  104. package/src/app/extend/context.types.ts +4 -1
  105. package/src/app/extend/request.ts +9 -2
  106. package/src/app/extend/request.types.ts +10 -0
  107. package/src/app/extend/response.ts +2 -0
  108. package/src/app/extend/response.types.ts +7 -0
  109. package/src/app/middleware/meta.ts +2 -2
  110. package/src/app/middleware/notfound.ts +2 -2
  111. package/src/app/middleware/site_file.ts +3 -3
  112. package/src/config/config.default.ts +1 -1
  113. package/src/config/config.local.ts +1 -1
  114. package/src/config/config.unittest.ts +1 -1
  115. package/src/index.ts +33 -6
  116. package/src/lib/agent.ts +1 -1
  117. package/src/lib/application.ts +18 -6
  118. package/src/lib/core/base_context_class.ts +6 -3
  119. package/src/lib/core/base_hook_class.ts +2 -2
  120. package/src/lib/core/context_httpclient.ts +3 -3
  121. package/src/lib/core/httpclient.ts +10 -4
  122. package/src/lib/core/messenger/base.ts +30 -0
  123. package/src/lib/core/messenger/index.ts +2 -1
  124. package/src/lib/core/messenger/ipc.ts +3 -5
  125. package/src/lib/core/messenger/local.ts +3 -5
  126. package/src/lib/core/utils.ts +2 -2
  127. package/src/lib/egg.ts +23 -12
  128. package/src/lib/egg.types.ts +8 -4
  129. package/src/lib/error/CookieLimitExceedError.ts +12 -0
  130. package/src/lib/error/MessageUnhandledRejectionError.ts +12 -0
  131. package/src/lib/error/index.ts +2 -0
  132. package/src/lib/{type.ts → types.ts} +46 -3
  133. package/dist/esm/lib/type.js +0 -2
package/src/index.ts CHANGED
@@ -7,13 +7,32 @@ import { startEgg } from './lib/start.js';
7
7
  import Helper from './app/extend/helper.js';
8
8
 
9
9
  // export extends
10
- export { Helper };
10
+ export {
11
+ Helper,
12
+ };
13
+ export type {
14
+ // keep compatible with egg v3
15
+ Helper as IHelper,
16
+ };
11
17
 
12
18
  // export types
13
19
  export * from './lib/egg.js';
14
- export * from './lib/type.js';
20
+ export * from './lib/types.js';
15
21
  export * from './lib/start.js';
16
22
 
23
+ // export errors
24
+ export * from './lib/error/index.js';
25
+
26
+ // export loggers
27
+ export type {
28
+ LoggerLevel,
29
+ EggLogger,
30
+ } from 'egg-logger';
31
+
32
+ // export httpClients
33
+ export * from './lib/core/httpclient.js';
34
+ export * from './lib/core/context_httpclient.js';
35
+
17
36
  /**
18
37
  * Start egg application with cluster mode
19
38
  * @since 1.0.0
@@ -24,7 +43,9 @@ export * from '@eggjs/cluster';
24
43
  * Start egg application with single process mode
25
44
  * @since 1.0.0
26
45
  */
27
- export const start = startEgg;
46
+ export {
47
+ startEgg as start,
48
+ };
28
49
 
29
50
  /**
30
51
  * @member {Application} Egg#Application
@@ -54,19 +75,25 @@ export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';
54
75
  * @member {Controller} Egg#Controller
55
76
  * @since 1.1.0
56
77
  */
57
- export const Controller = BaseContextClass;
78
+ export {
79
+ BaseContextClass as Controller,
80
+ };
58
81
 
59
82
  /**
60
83
  * @member {Service} Egg#Service
61
84
  * @since 1.1.0
62
85
  */
63
- export const Service = BaseContextClass;
86
+ export {
87
+ BaseContextClass as Service,
88
+ };
64
89
 
65
90
  /**
66
91
  * @member {Subscription} Egg#Subscription
67
92
  * @since 1.10.0
68
93
  */
69
- export const Subscription = BaseContextClass;
94
+ export {
95
+ BaseContextClass as Subscription,
96
+ };
70
97
 
71
98
  /**
72
99
  * @member {BaseContextClass} Egg#BaseContextClass
package/src/lib/agent.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { EggLogger } from 'egg-logger';
1
+ import type { EggLogger } from 'egg-logger';
2
2
  import { EggApplicationCore, EggApplicationCoreOptions } from './egg.js';
3
3
  import { AgentWorkerLoader } from './loader/index.js';
4
4
 
@@ -5,13 +5,15 @@ import { Socket } from 'node:net';
5
5
  import { graceful } from 'graceful';
6
6
  import { assign } from 'utility';
7
7
  import { utils as eggUtils } from '@eggjs/core';
8
+ import { isGeneratorFunction } from 'is-type-of';
8
9
  import {
9
10
  EggApplicationCore,
10
11
  type EggApplicationCoreOptions,
11
- type ContextDelegation,
12
+ type Context,
12
13
  } from './egg.js';
13
14
  import { AppWorkerLoader } from './loader/index.js';
14
15
  import Helper from '../app/extend/helper.js';
16
+ import { CookieLimitExceedError } from './error/index.js';
15
17
 
16
18
  const EGG_LOADER = Symbol.for('egg#loader');
17
19
 
@@ -221,7 +223,7 @@ export class Application extends EggApplicationCore {
221
223
  * @see Context#runInBackground
222
224
  * @param {Function} scope - the first args is an anonymous ctx
223
225
  */
224
- runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
226
+ runInBackground(scope: (ctx: Context) => Promise<void>, req?: unknown) {
225
227
  const ctx = this.createAnonymousContext(req);
226
228
  if (!scope.name) {
227
229
  Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
@@ -237,7 +239,7 @@ export class Application extends EggApplicationCore {
237
239
  * @param {Function} scope - the first args is an anonymous ctx, scope should be async function
238
240
  * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
239
241
  */
240
- async runInAnonymousContextScope(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
242
+ async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
241
243
  const ctx = this.createAnonymousContext(req);
242
244
  if (!scope.name) {
243
245
  Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
@@ -266,16 +268,26 @@ export class Application extends EggApplicationCore {
266
268
  return this._keys;
267
269
  }
268
270
 
271
+ /**
272
+ * @deprecated keep compatible with egg 3.x
273
+ */
274
+ toAsyncFunction(fn: (...args: any[]) => any) {
275
+ if (isGeneratorFunction(fn)) {
276
+ throw new Error('Generator function is not supported');
277
+ }
278
+ return fn;
279
+ }
280
+
269
281
  /**
270
282
  * bind app's events
271
283
  *
272
284
  * @private
273
285
  */
274
286
  #bindEvents() {
275
- // Browser Cookie Limits: http://browsercookielimits.squawky.net/
287
+ // Browser Cookie Limits: http://browsercookielimits.iain.guru/
288
+ // https://github.com/eggjs/egg-cookies/blob/58ef4ea497a0eb4dd711d7e9751e56bc5fcee004/src/cookies.ts#L145
276
289
  this.on('cookieLimitExceed', ({ name, value, ctx }) => {
277
- const err = new Error(`cookie ${name}'s length(${value.length}) exceed the limit(4093)`);
278
- err.name = 'CookieLimitExceedError';
290
+ const err = new CookieLimitExceedError(name, value);
279
291
  ctx.coreLogger.error(err);
280
292
  });
281
293
  // expose server to support websocket
@@ -1,5 +1,5 @@
1
1
  import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
2
- import type { ContextDelegation } from '../egg.js';
2
+ import type { Context, EggApplicationCore } from '../egg.js';
3
3
  import { BaseContextLogger } from './base_context_logger.js';
4
4
 
5
5
  /**
@@ -8,8 +8,11 @@ import { BaseContextLogger } from './base_context_logger.js';
8
8
  * {@link Helper}, {@link Service} is extending it.
9
9
  */
10
10
  export class BaseContextClass extends EggCoreBaseContextClass {
11
- declare ctx: ContextDelegation;
12
- protected pathName?: string;
11
+ [key: string | symbol]: any;
12
+ declare ctx: Context;
13
+ declare pathName?: string;
14
+ declare app: EggApplicationCore;
15
+ declare service: BaseContextClass;
13
16
  #logger?: BaseContextLogger;
14
17
 
15
18
  get logger() {
@@ -3,14 +3,14 @@ import type { ILifecycleBoot } from '@eggjs/core';
3
3
  import type { Application, Agent } from '../../index.js';
4
4
 
5
5
  export class BaseHookClass implements ILifecycleBoot {
6
- fullPath?: string;
6
+ declare fullPath?: string;
7
7
  #instance: Application | Agent;
8
8
 
9
9
  constructor(instance: Application | Agent) {
10
10
  this.#instance = instance;
11
11
  }
12
12
 
13
- get logger(): any {
13
+ get logger() {
14
14
  return this.#instance.logger;
15
15
  }
16
16
 
@@ -1,13 +1,13 @@
1
- import type { ContextDelegation, EggApplicationCore } from '../egg.js';
1
+ import type { Context, EggApplicationCore } from '../egg.js';
2
2
  import type {
3
3
  HttpClientRequestURL, HttpClientRequestOptions,
4
4
  } from './httpclient.js';
5
5
 
6
6
  export class ContextHttpClient {
7
- ctx: ContextDelegation;
7
+ ctx: Context;
8
8
  app: EggApplicationCore;
9
9
 
10
- constructor(ctx: ContextDelegation) {
10
+ constructor(ctx: Context) {
11
11
  this.ctx = ctx;
12
12
  this.app = ctx.app;
13
13
  }
@@ -4,7 +4,7 @@ import {
4
4
  RequestOptions,
5
5
  } from 'urllib';
6
6
  import { ms } from 'humanize-ms';
7
- import type { EggApplicationCore, ContextDelegation } from '../egg.js';
7
+ import type { EggApplicationCore } from '../egg.js';
8
8
 
9
9
  export type {
10
10
  HttpClientResponse,
@@ -12,12 +12,12 @@ export type {
12
12
  } from 'urllib';
13
13
 
14
14
  export interface HttpClientRequestOptions extends RequestOptions {
15
- ctx?: ContextDelegation;
16
- tracer?: unknown;
15
+ ctx?: any;
16
+ tracer?: any;
17
17
  }
18
18
 
19
19
  export class HttpClient extends RawHttpClient {
20
- readonly #app: EggApplicationCore & { tracer?: unknown };
20
+ readonly #app: EggApplicationCore & { tracer?: any };
21
21
 
22
22
  constructor(app: EggApplicationCore) {
23
23
  normalizeConfig(app);
@@ -43,6 +43,12 @@ export class HttpClient extends RawHttpClient {
43
43
  }
44
44
  }
45
45
 
46
+ // keep compatible
47
+ export type {
48
+ HttpClient as EggHttpClient,
49
+ HttpClient as EggContextHttpClient,
50
+ };
51
+
46
52
  function normalizeConfig(app: EggApplicationCore) {
47
53
  const config = app.config.httpclient;
48
54
  if (typeof config.request?.timeout === 'string') {
@@ -0,0 +1,30 @@
1
+ import { EventEmitter, captureRejectionSymbol } from 'node:events';
2
+ import { MessageUnhandledRejectionError } from '../../error/index.js';
3
+ import { EggApplicationCore } from '../../egg.js';
4
+
5
+ export class BaseMessenger extends EventEmitter {
6
+ protected readonly egg: EggApplicationCore;
7
+
8
+ constructor(egg: EggApplicationCore) {
9
+ super({ captureRejections: true });
10
+ this.egg = egg;
11
+ }
12
+
13
+ [captureRejectionSymbol](err: Error, event: string | symbol, ...args: any[]) {
14
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args));
15
+ }
16
+
17
+ emit(eventName: string | symbol, ...args: any[]): boolean {
18
+ const hasListeners = this.listenerCount(eventName) > 0;
19
+ try {
20
+ return super.emit(eventName, ...args);
21
+ } catch (e: unknown) {
22
+ let err = e as Error;
23
+ if (!(err instanceof Error)) {
24
+ err = new Error(String(err));
25
+ }
26
+ this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args));
27
+ return hasListeners;
28
+ }
29
+ }
30
+ }
@@ -9,7 +9,8 @@ export type { IMessenger } from './IMessenger.js';
9
9
  * @class Messenger
10
10
  */
11
11
  export function create(egg: EggApplicationCore): IMessenger {
12
- return egg.options.mode === 'single'
12
+ const messenger = egg.options.mode === 'single'
13
13
  ? new LocalMessenger(egg)
14
14
  : new IPCMessenger(egg);
15
+ return messenger;
15
16
  }
@@ -1,24 +1,22 @@
1
- import { EventEmitter } from 'node:events';
2
1
  import { debuglog } from 'node:util';
3
2
  import workerThreads from 'node:worker_threads';
4
3
  import { sendmessage } from 'sendmessage';
5
4
  import type { IMessenger } from './IMessenger.js';
6
5
  import type { EggApplicationCore } from '../../egg.js';
6
+ import { BaseMessenger } from './base.js';
7
7
 
8
8
  const debug = debuglog('egg/lib/core/messenger/ipc');
9
9
 
10
10
  /**
11
11
  * Communication between app worker and agent worker by IPC channel
12
12
  */
13
- export class Messenger extends EventEmitter implements IMessenger {
13
+ export class Messenger extends BaseMessenger implements IMessenger {
14
14
  readonly pid: string;
15
- readonly egg: EggApplicationCore;
16
15
  opids: string[] = [];
17
16
 
18
17
  constructor(egg: EggApplicationCore) {
19
- super();
18
+ super(egg);
20
19
  this.pid = String(process.pid);
21
- this.egg = egg;
22
20
  // pids of agent or app managed by master
23
21
  // - retrieve app worker pids when it's an agent worker
24
22
  // - retrieve agent worker pids when it's an app worker
@@ -1,20 +1,18 @@
1
1
  import { debuglog } from 'node:util';
2
- import EventEmitter from 'node:events';
3
2
  import type { IMessenger } from './IMessenger.js';
4
3
  import type { EggApplicationCore } from '../../egg.js';
4
+ import { BaseMessenger } from './base.js';
5
5
 
6
6
  const debug = debuglog('egg/lib/core/messenger/local');
7
7
 
8
8
  /**
9
9
  * Communication between app worker and agent worker with EventEmitter
10
10
  */
11
- export class Messenger extends EventEmitter implements IMessenger {
11
+ export class Messenger extends BaseMessenger implements IMessenger {
12
12
  readonly pid: string;
13
- readonly egg: EggApplicationCore;
14
13
 
15
14
  constructor(egg: EggApplicationCore) {
16
- super();
17
- this.egg = egg;
15
+ super(egg);
18
16
  this.pid = String(process.pid);
19
17
  }
20
18
 
@@ -4,7 +4,7 @@ import {
4
4
  isClass, isFunction, isGeneratorFunction, isAsyncFunction,
5
5
  } from 'is-type-of';
6
6
 
7
- export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[]) {
7
+ export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[] = []) {
8
8
  if (!Array.isArray(ignore)) {
9
9
  ignore = [ ignore ];
10
10
  }
@@ -30,7 +30,7 @@ function convertValue(key: string, value: any, ignore: (string | RegExp)[]) {
30
30
  }
31
31
  }
32
32
  if (!hit) {
33
- if (isSymbol(value) || isRegExp(value)) {
33
+ if (isSymbol(value) || isRegExp(value) || value instanceof URL) {
34
34
  return value.toString();
35
35
  }
36
36
  if (isPrimitive(value) || Array.isArray(value)) {
package/src/lib/egg.ts CHANGED
@@ -26,8 +26,8 @@ import { Cookies as ContextCookies } from '@eggjs/cookies';
26
26
  import CircularJSON from 'circular-json-for-egg';
27
27
  import type { Agent } from './agent.js';
28
28
  import type { Application } from './application.js';
29
- import Context, { type ContextDelegation } from '../app/extend/context.js';
30
- import type { EggAppConfig } from './type.js';
29
+ import Context from '../app/extend/context.js';
30
+ import type { EggAppConfig } from './types.js';
31
31
  import { create as createMessenger, IMessenger } from './core/messenger/index.js';
32
32
  import { ContextHttpClient } from './core/context_httpclient.js';
33
33
  import {
@@ -56,30 +56,30 @@ export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir
56
56
  export class Request extends EggCoreRequest {
57
57
  declare app: EggCore;
58
58
  declare response: Response;
59
- declare ctx: ContextDelegation;
59
+ declare ctx: Context;
60
60
  }
61
61
 
62
62
  export class Response extends EggCoreResponse {
63
63
  declare app: EggCore;
64
64
  declare request: Request;
65
- declare ctx: ContextDelegation;
65
+ declare ctx: Context;
66
66
  }
67
67
 
68
68
  // export egg types
69
69
  export type {
70
- ContextDelegation,
71
70
  ILifecycleBoot,
71
+ // keep compatible with egg version 3.x
72
+ ILifecycleBoot as IBoot,
72
73
  Next,
73
74
  };
74
75
  // keep compatible with egg version 3.x
75
- export type EggContext = ContextDelegation;
76
- export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = EggCoreMiddlewareFunc<T>;
76
+ export type EggContext = Context;
77
+ export type MiddlewareFunc<T extends Context = Context> = EggCoreMiddlewareFunc<T>;
77
78
 
78
79
  // export egg classes
79
80
  export {
80
81
  Context,
81
82
  Router,
82
- EggLogger,
83
83
  };
84
84
 
85
85
  /**
@@ -89,12 +89,14 @@ export {
89
89
  * @augments EggCore
90
90
  */
91
91
  export class EggApplicationCore extends EggCore {
92
- declare ctxStorage: AsyncLocalStorage<ContextDelegation>;
92
+ declare ctxStorage: AsyncLocalStorage<Context>;
93
93
  // export context base classes, let framework can impl sub class and over context extend easily.
94
94
  ContextCookies = ContextCookies;
95
95
  ContextLogger = ContextLogger;
96
96
  ContextHttpClient = ContextHttpClient;
97
97
  HttpClient = HttpClient;
98
+ // keep compatible with egg version 3.x
99
+ HttpClientNext = HttpClient;
98
100
  /**
99
101
  * Retrieve base context class
100
102
  * @member {BaseContextClass} BaseContextClass
@@ -132,6 +134,7 @@ export class EggApplicationCore extends EggCore {
132
134
  /**
133
135
  * Retrieve base boot
134
136
  * @member {Boot}
137
+ * @alias BaseHookClass
135
138
  */
136
139
  Boot = BaseHookClass;
137
140
 
@@ -233,6 +236,13 @@ export class EggApplicationCore extends EggCore {
233
236
  await this.loader.load();
234
237
  }
235
238
 
239
+ /**
240
+ * Usage: new ApiClient({ cluster: app.cluster })
241
+ */
242
+ get cluster() {
243
+ return this.clusterWrapper.bind(this);
244
+ }
245
+
236
246
  /**
237
247
  * Wrap the Client with Leader/Follower Pattern
238
248
  *
@@ -251,7 +261,7 @@ export class EggApplicationCore extends EggCore {
251
261
  * - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
252
262
  * @return {ClientWrapper} wrapper
253
263
  */
254
- cluster(clientClass: unknown, options?: object) {
264
+ clusterWrapper(clientClass: unknown, options?: object) {
255
265
  const clientClassOptions = {
256
266
  ...this.config.clusterClient,
257
267
  ...options,
@@ -439,6 +449,7 @@ export class EggApplicationCore extends EggCore {
439
449
  }
440
450
 
441
451
  _unhandledRejectionHandler(err: any) {
452
+ this.coreLogger.error('[egg:unhandledRejection] %s', err && err.message || err);
442
453
  if (!(err instanceof Error)) {
443
454
  const newError = new Error(String(err));
444
455
  // err maybe an object, try to copy the name, message and stack to the new error instance
@@ -652,8 +663,8 @@ export class EggApplicationCore extends EggCore {
652
663
  * @param {Res} res - node native Response object
653
664
  * @return {Context} context object
654
665
  */
655
- createContext(req: IncomingMessage, res: ServerResponse): EggContext {
656
- const context = Object.create(this.context) as EggContext;
666
+ createContext(req: IncomingMessage, res: ServerResponse): Context {
667
+ const context = Object.create(this.context) as Context;
657
668
  const request = context.request = Object.create(this.request);
658
669
  const response = context.response = Object.create(this.response);
659
670
  context.app = request.app = response.app = this as any;
@@ -1,11 +1,15 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
- import { ContextDelegation } from '../app/extend/context.js';
1
+ import type { AsyncLocalStorage } from 'node:async_hooks';
2
+ import type EggContext from '../app/extend/context.js';
3
+ import type { EggLogger } from 'egg-logger';
3
4
 
4
5
  declare module '@eggjs/core' {
5
6
  // add EggApplicationCore overrides types
6
7
  interface EggCore {
7
8
  inspect(): any;
8
- get currentContext(): ContextDelegation | undefined;
9
- ctxStorage: AsyncLocalStorage<ContextDelegation>;
9
+ get currentContext(): EggContext | undefined;
10
+ ctxStorage: AsyncLocalStorage<EggContext>;
11
+ get logger(): EggLogger;
12
+ get coreLogger(): EggLogger;
13
+ getLogger(name: string): EggLogger;
10
14
  }
11
15
  }
@@ -0,0 +1,12 @@
1
+ export class CookieLimitExceedError extends Error {
2
+ key: string;
3
+ cookie: string;
4
+
5
+ constructor(key: string, cookie: string) {
6
+ super(`cookie ${key}'s length(${cookie.length}) exceed the limit(4093)`);
7
+ this.name = this.constructor.name;
8
+ this.key = key;
9
+ this.cookie = cookie;
10
+ Error.captureStackTrace(this, this.constructor);
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ export class MessageUnhandledRejectionError extends Error {
2
+ event: string | symbol;
3
+ args: any[];
4
+
5
+ constructor(err: Error, event: string | symbol, ...args: any[]) {
6
+ super(`event: ${String(event)}, error: ${err.message}`, { cause: err });
7
+ this.name = this.constructor.name;
8
+ this.event = event;
9
+ this.args = args;
10
+ Error.captureStackTrace(this, this.constructor);
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ export * from './CookieLimitExceedError.js';
2
+ export * from './MessageUnhandledRejectionError.js';
@@ -9,7 +9,7 @@ import type {
9
9
  FileLoaderOptions,
10
10
  } from '@eggjs/core';
11
11
  import type {
12
- EggApplicationCore, ContextDelegation,
12
+ EggApplicationCore, Context,
13
13
  } from './egg.js';
14
14
  import type { MetaMiddlewareOptions } from '../app/middleware/meta.js';
15
15
  import type { NotFoundMiddlewareOptions } from '../app/middleware/notfound.js';
@@ -18,7 +18,12 @@ import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
18
18
  // import @eggjs/watcher types
19
19
  // import '@eggjs/watcher';
20
20
 
21
- type IgnoreItem = string | RegExp | ((ctx: ContextDelegation) => boolean);
21
+ export type {
22
+ EggAppInfo,
23
+ } from '@eggjs/core';
24
+
25
+
26
+ type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
22
27
  type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
23
28
 
24
29
  export interface ClientErrorResponse {
@@ -103,7 +108,7 @@ export interface EggAppConfig {
103
108
  };
104
109
  /** Default is `'error'`, it will return `400` response when `Prototype-Poisoning` happen. */
105
110
  onProtoPoisoning: 'error' | 'remove' | 'ignore';
106
- onerror(err: any, ctx: ContextDelegation): void;
111
+ onerror(err: any, ctx: Context): void;
107
112
  };
108
113
 
109
114
  /**
@@ -136,6 +141,10 @@ export interface EggAppConfig {
136
141
  timeout?: number;
137
142
  /** Default request args for httpclient */
138
143
  request?: HttpClientRequestOptions;
144
+ /**
145
+ * @deprecated keep compatible with egg 3.x, no more used
146
+ */
147
+ useHttpClientNext?: boolean;
139
148
  };
140
149
 
141
150
  development: {
@@ -327,3 +336,37 @@ export interface EggAppConfig {
327
336
 
328
337
  [prop: string]: any;
329
338
  }
339
+
340
+ export type RequestObjectBody = Record<string, any>;
341
+
342
+ /**
343
+ * plugin config item interface
344
+ */
345
+ export interface IEggPluginItem {
346
+ env?: EggEnvType[];
347
+ path?: string;
348
+ package?: string;
349
+ enable?: boolean;
350
+ }
351
+
352
+ export type EggPluginItem = IEggPluginItem | boolean;
353
+
354
+ /**
355
+ * build-in plugin list
356
+ */
357
+ export interface EggPlugin {
358
+ [key: string]: EggPluginItem | undefined;
359
+ onerror?: EggPluginItem;
360
+ session?: EggPluginItem;
361
+ i18n?: EggPluginItem;
362
+ watcher?: EggPluginItem;
363
+ multipart?: EggPluginItem;
364
+ security?: EggPluginItem;
365
+ development?: EggPluginItem;
366
+ logrotator?: EggPluginItem;
367
+ schedule?: EggPluginItem;
368
+ static?: EggPluginItem;
369
+ jsonp?: EggPluginItem;
370
+ view?: EggPluginItem;
371
+ }
372
+
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9saWIvdHlwZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=