egg 4.0.0-beta.5 → 4.0.0-beta.6

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 (75) hide show
  1. package/dist/commonjs/app/extend/context.d.ts +151 -2
  2. package/dist/commonjs/app/extend/context.js +81 -84
  3. package/dist/commonjs/app/extend/helper.d.ts +37 -0
  4. package/dist/commonjs/app/extend/helper.js +49 -0
  5. package/dist/commonjs/app/extend/request.d.ts +128 -0
  6. package/dist/commonjs/app/extend/request.js +270 -0
  7. package/dist/commonjs/app/extend/response.d.ts +25 -0
  8. package/dist/commonjs/app/extend/response.js +37 -0
  9. package/dist/commonjs/app/middleware/meta.d.ts +2 -3
  10. package/dist/commonjs/app/middleware/meta.js +1 -1
  11. package/dist/commonjs/app/middleware/notfound.d.ts +2 -3
  12. package/dist/commonjs/app/middleware/notfound.js +1 -1
  13. package/dist/commonjs/app/middleware/site_file.d.ts +2 -2
  14. package/dist/commonjs/app/middleware/site_file.js +1 -2
  15. package/dist/commonjs/config/config.default.js +3 -2
  16. package/dist/commonjs/lib/application.d.ts +5 -12
  17. package/dist/commonjs/lib/application.js +9 -20
  18. package/dist/commonjs/lib/core/base_context_class.d.ts +2 -2
  19. package/dist/commonjs/lib/core/context_httpclient.d.ts +3 -3
  20. package/dist/commonjs/lib/core/context_httpclient.js +1 -1
  21. package/dist/commonjs/lib/core/httpclient.d.ts +2 -3
  22. package/dist/commonjs/lib/core/httpclient.js +3 -6
  23. package/dist/commonjs/lib/core/messenger/ipc.js +1 -2
  24. package/dist/commonjs/lib/egg.d.ts +19 -15
  25. package/dist/commonjs/lib/egg.js +14 -11
  26. package/dist/commonjs/lib/type.d.ts +4 -7
  27. package/dist/commonjs/lib/utils.d.ts +2 -0
  28. package/dist/commonjs/lib/utils.js +21 -0
  29. package/dist/esm/app/extend/context.d.ts +151 -2
  30. package/dist/esm/app/extend/context.js +81 -85
  31. package/dist/esm/app/extend/helper.d.ts +37 -0
  32. package/dist/esm/app/extend/helper.js +43 -0
  33. package/dist/esm/app/extend/request.d.ts +128 -0
  34. package/dist/esm/app/extend/request.js +264 -0
  35. package/dist/esm/app/extend/response.d.ts +25 -0
  36. package/dist/esm/app/extend/response.js +34 -0
  37. package/dist/esm/app/middleware/meta.d.ts +2 -3
  38. package/dist/esm/app/middleware/meta.js +1 -1
  39. package/dist/esm/app/middleware/notfound.d.ts +2 -3
  40. package/dist/esm/app/middleware/notfound.js +1 -1
  41. package/dist/esm/app/middleware/site_file.d.ts +2 -2
  42. package/dist/esm/app/middleware/site_file.js +1 -2
  43. package/dist/esm/config/config.default.js +3 -2
  44. package/dist/esm/lib/application.d.ts +5 -12
  45. package/dist/esm/lib/application.js +9 -20
  46. package/dist/esm/lib/core/base_context_class.d.ts +2 -2
  47. package/dist/esm/lib/core/context_httpclient.d.ts +3 -3
  48. package/dist/esm/lib/core/context_httpclient.js +1 -1
  49. package/dist/esm/lib/core/httpclient.d.ts +2 -3
  50. package/dist/esm/lib/core/httpclient.js +2 -2
  51. package/dist/esm/lib/core/messenger/ipc.js +1 -2
  52. package/dist/esm/lib/egg.d.ts +19 -15
  53. package/dist/esm/lib/egg.js +12 -12
  54. package/dist/esm/lib/type.d.ts +4 -7
  55. package/dist/esm/lib/utils.d.ts +2 -0
  56. package/dist/esm/lib/utils.js +14 -0
  57. package/dist/package.json +1 -1
  58. package/package.json +6 -9
  59. package/src/app/extend/context.ts +116 -100
  60. package/src/app/extend/{helper.js → helper.ts} +14 -13
  61. package/src/app/extend/{request.js → request.ts} +81 -79
  62. package/src/app/extend/response.ts +36 -0
  63. package/src/app/middleware/meta.ts +2 -3
  64. package/src/app/middleware/notfound.ts +2 -3
  65. package/src/app/middleware/site_file.ts +3 -5
  66. package/src/config/config.default.ts +2 -1
  67. package/src/lib/application.ts +14 -21
  68. package/src/lib/core/base_context_class.ts +2 -2
  69. package/src/lib/core/context_httpclient.ts +3 -3
  70. package/src/lib/core/httpclient.ts +4 -5
  71. package/src/lib/core/messenger/ipc.ts +0 -1
  72. package/src/lib/egg.ts +45 -24
  73. package/src/lib/type.ts +3 -13
  74. package/src/lib/utils.ts +16 -0
  75. package/src/app/extend/response.js +0 -101
@@ -0,0 +1,36 @@
1
+ import { Response as KoaResponse } from '@eggjs/core';
2
+
3
+ const REAL_STATUS = Symbol('response realStatus');
4
+
5
+ export default class Response extends KoaResponse {
6
+ /**
7
+ * Get or set a real status code.
8
+ *
9
+ * e.g.: Using 302 status redirect to the global error page
10
+ * instead of show current 500 status page.
11
+ * And access log should save 500 not 302,
12
+ * then the `realStatus` can help us find out the real status code.
13
+ * @member {Number} Response#realStatus
14
+ * @return {Number} The status code to be set.
15
+ */
16
+ get realStatus(): number {
17
+ if (this[REAL_STATUS]) {
18
+ return this[REAL_STATUS] as number;
19
+ }
20
+ return this.status;
21
+ }
22
+
23
+ /**
24
+ * Set a real status code.
25
+ *
26
+ * e.g.: Using 302 status redirect to the global error page
27
+ * instead of show current 500 status page.
28
+ * And access log should save 500 not 302,
29
+ * then the `realStatus` can help us find out the real status code.
30
+ * @member {Number} Response#realStatus
31
+ * @param {Number} status The status code to be set.
32
+ */
33
+ set realStatus(status: number) {
34
+ this[REAL_STATUS] = status;
35
+ }
36
+ }
@@ -3,8 +3,7 @@
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
+ import type { ContextDelegation, Next } from '../../lib/egg.js';
8
7
 
9
8
  export interface MetaMiddlewareOptions {
10
9
  enable: boolean;
@@ -12,7 +11,7 @@ export interface MetaMiddlewareOptions {
12
11
  }
13
12
 
14
13
  export default (options: MetaMiddlewareOptions) => {
15
- return async function meta(ctx: EggCoreContext, next: Next) {
14
+ return async function meta(ctx: ContextDelegation, next: Next) {
16
15
  if (options.logging) {
17
16
  ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
18
17
  ctx.host, ctx.header['user-agent']);
@@ -1,5 +1,4 @@
1
- import type { EggCoreContext } from '@eggjs/core';
2
- import type { Next } from '../../lib/type.js';
1
+ import type { Next, ContextDelegation } from '../../lib/egg.js';
3
2
 
4
3
  export interface NotFoundMiddlewareOptions {
5
4
  enable: boolean;
@@ -7,7 +6,7 @@ export interface NotFoundMiddlewareOptions {
7
6
  }
8
7
 
9
8
  export default (options: NotFoundMiddlewareOptions) => {
10
- return async function notfound(ctx: EggCoreContext, next: Next) {
9
+ return async function notfound(ctx: ContextDelegation, next: Next) {
11
10
  await next();
12
11
 
13
12
  if (ctx.status !== 404 || ctx.body) {
@@ -1,10 +1,9 @@
1
1
  import path from 'node:path';
2
2
  import { fileURLToPath } from 'node:url';
3
3
  import { readFile } from 'node:fs/promises';
4
- import type { EggCoreContext } from '@eggjs/core';
5
- import type { Next } from '../../lib/type.js';
4
+ import type { Next, ContextDelegation } from '../../lib/egg.js';
6
5
 
7
- export type SiteFileContentFun = (ctx: EggCoreContext) => Promise<Buffer | string>;
6
+ export type SiteFileContentFun = (ctx: ContextDelegation) => Promise<Buffer | string>;
8
7
 
9
8
  export interface SiteFileMiddlewareOptions {
10
9
  enable: boolean;
@@ -15,11 +14,10 @@ export interface SiteFileMiddlewareOptions {
15
14
  const BUFFER_CACHE = Symbol('siteFile URL buffer cache');
16
15
 
17
16
  module.exports = (options: SiteFileMiddlewareOptions) => {
18
- return async function siteFile(ctx: EggCoreContext, next: Next) {
17
+ return async function siteFile(ctx: ContextDelegation, next: Next) {
19
18
  if (ctx.method !== 'HEAD' && ctx.method !== 'GET') {
20
19
  return next();
21
20
  }
22
- /* istanbul ignore if */
23
21
  if (ctx.path[0] !== '/') {
24
22
  return next();
25
23
  }
@@ -2,6 +2,7 @@ import path from 'node:path';
2
2
  import { pathToFileURL } from 'node:url';
3
3
  import type { EggAppInfo } from '@eggjs/core';
4
4
  import type { EggAppConfig } from '../lib/type.js';
5
+ import { getSourceFile } from '../lib/utils.js';
5
6
 
6
7
  /**
7
8
  * The configuration of egg application, can be access by `app.config`
@@ -201,7 +202,7 @@ export default (appInfo: EggAppInfo) => {
201
202
  */
202
203
  config.siteFile = {
203
204
  enable: true,
204
- '/favicon.ico': pathToFileURL(path.join(__dirname, 'favicon.png')),
205
+ '/favicon.ico': pathToFileURL(getSourceFile('config/favicon.png')),
205
206
  // default cache in 30 days
206
207
  cacheControl: 'public, max-age=2592000',
207
208
  };
@@ -2,14 +2,16 @@ import path from 'node:path';
2
2
  import fs from 'node:fs';
3
3
  import http from 'node:http';
4
4
  import { Socket } from 'node:net';
5
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
- // @ts-ignore
7
- import graceful from 'graceful';
5
+ import { graceful } from 'graceful';
8
6
  import { assign } from 'utility';
9
7
  import { utils as eggUtils } from '@eggjs/core';
10
- import { EggApplicationCore, EggContext, EggApplicationCoreOptions } from './egg.js';
8
+ import {
9
+ EggApplicationCore,
10
+ type EggApplicationCoreOptions,
11
+ type ContextDelegation,
12
+ } from './egg.js';
11
13
  import { AppWorkerLoader } from './loader/index.js';
12
- import { BaseContextClass } from './core/base_context_class.js';
14
+ import Helper from '../app/extend/helper.js';
13
15
 
14
16
  const EGG_LOADER = Symbol.for('egg#loader');
15
17
 
@@ -34,13 +36,6 @@ function escapeHeaderValue(value: string) {
34
36
  return /[\r\n]/.test(value) ? value.replace(/[\r\n]+[ \t]*/g, '') : value;
35
37
  }
36
38
 
37
- /**
38
- * The Helper class which can be used as utility function.
39
- * We support developers to extend Helper through ${baseDir}/app/extend/helper.js ,
40
- * then you can use all method on `ctx.helper` that is a instance of Helper.
41
- */
42
- class HelperClass extends BaseContextClass {}
43
-
44
39
  /**
45
40
  * Singleton instance in App Worker, extend {@link EggApplicationCore}
46
41
  * @augments EggApplicationCore
@@ -53,7 +48,7 @@ export class Application extends EggApplicationCore {
53
48
  * reference to {@link Helper}
54
49
  * @member {Helper} Application#Helper
55
50
  */
56
- Helper = HelperClass;
51
+ Helper = Helper;
57
52
 
58
53
  /**
59
54
  * @class
@@ -77,8 +72,7 @@ export class Application extends EggApplicationCore {
77
72
  }
78
73
 
79
74
  #responseRaw(socket: Socket, raw?: any) {
80
- /* istanbul ignore next */
81
- if (!socket.writable) return;
75
+ if (!socket?.writable) return;
82
76
  if (!raw) {
83
77
  return socket.end(DEFAULT_BAD_REQUEST_RESPONSE);
84
78
  }
@@ -152,7 +146,6 @@ export class Application extends EggApplicationCore {
152
146
  // set ignore code
153
147
  const serverGracefulIgnoreCode = this.config.serverGracefulIgnoreCode || [];
154
148
 
155
- /* istanbul ignore next */
156
149
  graceful({
157
150
  server: [ server ],
158
151
  error: (err: Error, throwErrorCount: number) => {
@@ -228,13 +221,13 @@ export class Application extends EggApplicationCore {
228
221
  * @see Context#runInBackground
229
222
  * @param {Function} scope - the first args is an anonymous ctx
230
223
  */
231
- runInBackground(scope: (ctx: EggContext) => void) {
232
- const ctx = this.createAnonymousContext();
224
+ runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
225
+ const ctx = this.createAnonymousContext(req);
233
226
  if (!scope.name) {
234
227
  Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
235
228
  }
236
229
  this.ctxStorage.run(ctx, () => {
237
- ctx.runInBackground(scope);
230
+ return ctx.runInBackground(scope);
238
231
  });
239
232
  }
240
233
 
@@ -244,13 +237,13 @@ export class Application extends EggApplicationCore {
244
237
  * @param {Function} scope - the first args is an anonymous ctx, scope should be async function
245
238
  * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
246
239
  */
247
- async runInAnonymousContextScope(scope: (ctx: EggContext) => Promise<void>, req?: unknown) {
240
+ async runInAnonymousContextScope(scope: (ctx: ContextDelegation) => Promise<void>, req?: unknown) {
248
241
  const ctx = this.createAnonymousContext(req);
249
242
  if (!scope.name) {
250
243
  Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
251
244
  }
252
245
  return await this.ctxStorage.run(ctx, async () => {
253
- return await scope(ctx as EggContext);
246
+ return await scope(ctx);
254
247
  });
255
248
  }
256
249
 
@@ -1,5 +1,5 @@
1
1
  import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
2
- import type { EggContext } from '../egg.js';
2
+ import type { ContextDelegation } from '../egg.js';
3
3
  import { BaseContextLogger } from './base_context_logger.js';
4
4
 
5
5
  /**
@@ -8,7 +8,7 @@ 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: EggContext;
11
+ declare ctx: ContextDelegation;
12
12
  protected pathName?: string;
13
13
  #logger?: BaseContextLogger;
14
14
 
@@ -1,13 +1,13 @@
1
- import type { EggContext, EggApplicationCore } from '../egg.js';
1
+ import type { ContextDelegation, 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: EggContext;
7
+ ctx: ContextDelegation;
8
8
  app: EggApplicationCore;
9
9
 
10
- constructor(ctx: EggContext) {
10
+ constructor(ctx: ContextDelegation) {
11
11
  this.ctx = ctx;
12
12
  this.app = ctx.app;
13
13
  }
@@ -1,11 +1,10 @@
1
- import { EggCoreContext } from '@eggjs/core';
2
1
  import {
3
2
  HttpClient as RawHttpClient,
4
3
  RequestURL as HttpClientRequestURL,
5
4
  RequestOptions,
6
5
  } from 'urllib';
7
- import ms from 'ms';
8
- import type { EggApplicationCore } from '../egg.js';
6
+ import { ms } from 'humanize-ms';
7
+ import type { EggApplicationCore, ContextDelegation } from '../egg.js';
9
8
 
10
9
  export type {
11
10
  HttpClientResponse,
@@ -13,7 +12,7 @@ export type {
13
12
  } from 'urllib';
14
13
 
15
14
  export interface HttpClientRequestOptions extends RequestOptions {
16
- ctx?: EggCoreContext;
15
+ ctx?: ContextDelegation;
17
16
  tracer?: unknown;
18
17
  }
19
18
 
@@ -47,6 +46,6 @@ export class HttpClient extends RawHttpClient {
47
46
  function normalizeConfig(app: EggApplicationCore) {
48
47
  const config = app.config.httpclient;
49
48
  if (typeof config.request?.timeout === 'string') {
50
- config.request.timeout = ms(config.request.timeout as string);
49
+ config.request.timeout = ms(config.request.timeout);
51
50
  }
52
51
  }
@@ -72,7 +72,6 @@ export class Messenger extends EventEmitter implements IMessenger {
72
72
  * @return {Messenger} this
73
73
  */
74
74
  sendRandom(action: string, data?: unknown): Messenger {
75
- /* istanbul ignore if */
76
75
  if (this.opids.length === 0) {
77
76
  return this;
78
77
  }
package/src/lib/egg.ts CHANGED
@@ -3,8 +3,18 @@ import path from 'node:path';
3
3
  import fs from 'node:fs';
4
4
  import http, { type IncomingMessage, type ServerResponse } from 'node:http';
5
5
  import inspector from 'node:inspector';
6
- import { fileURLToPath } from 'node:url';
7
- import { EggCore, type EggCoreContext, type EggCoreOptions } from '@eggjs/core';
6
+ import { AsyncLocalStorage } from 'node:async_hooks';
7
+ import {
8
+ EggCore,
9
+ Request as EggCoreRequest,
10
+ Response as EggCoreResponse,
11
+ Router,
12
+ } from '@eggjs/core';
13
+ import type {
14
+ EggCoreOptions,
15
+ Next, MiddlewareFunc as EggCoreMiddlewareFunc,
16
+ ILifecycleBoot,
17
+ } from '@eggjs/core';
8
18
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
9
19
  // @ts-ignore
10
20
  import createClusterClient, { close as closeClusterClient } from 'cluster-client';
@@ -16,6 +26,7 @@ import { Cookies as ContextCookies } from '@eggjs/cookies';
16
26
  import CircularJSON from 'circular-json-for-egg';
17
27
  import type { Agent } from './agent.js';
18
28
  import type { Application } from './application.js';
29
+ import Context, { type ContextDelegation } from '../app/extend/context.js';
19
30
  import type { EggAppConfig } from './type.js';
20
31
  import { create as createMessenger, IMessenger } from './core/messenger/index.js';
21
32
  import { ContextHttpClient } from './core/context_httpclient.js';
@@ -30,6 +41,7 @@ import { convertObject } from './core/utils.js';
30
41
  import { BaseContextClass } from './core/base_context_class.js';
31
42
  import { BaseHookClass } from './core/base_hook_class.js';
32
43
  import type { EggApplicationLoader } from './loader/index.js';
44
+ import { getSourceDirname } from './utils.js';
33
45
 
34
46
  const EGG_PATH = Symbol.for('egg#eggPath');
35
47
 
@@ -39,20 +51,35 @@ export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir
39
51
  baseDir?: string;
40
52
  }
41
53
 
42
- export interface EggContext extends EggCoreContext {
43
- app: EggApplicationCore;
44
- /**
45
- * Request start time
46
- * @member {Number} Context#starttime
47
- */
48
- starttime: number;
49
- /**
50
- * Request start timer using `performance.now()`
51
- * @member {Number} Context#performanceStarttime
52
- */
53
- performanceStarttime: number;
54
+ // export egg classes
55
+ export {
56
+ Context,
57
+ Router,
58
+ EggLogger,
59
+ };
60
+
61
+ export class Request extends EggCoreRequest {
62
+ declare app: EggCore;
63
+ declare response: Response;
64
+ declare ctx: ContextDelegation;
54
65
  }
55
66
 
67
+ export class Response extends EggCoreResponse {
68
+ declare app: EggCore;
69
+ declare request: Request;
70
+ declare ctx: ContextDelegation;
71
+ }
72
+
73
+ // export egg types
74
+ export type {
75
+ ContextDelegation,
76
+ ILifecycleBoot,
77
+ Next,
78
+ };
79
+ // keep compatible with egg version 3.x
80
+ export type EggContext = ContextDelegation;
81
+ export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = EggCoreMiddlewareFunc<T>;
82
+
56
83
  /**
57
84
  * Based on koa's Application
58
85
  * @see https://github.com/eggjs/egg-core
@@ -60,6 +87,7 @@ export interface EggContext extends EggCoreContext {
60
87
  * @augments EggCore
61
88
  */
62
89
  export class EggApplicationCore extends EggCore {
90
+ declare ctxStorage: AsyncLocalStorage<ContextDelegation>;
63
91
  // export context base classes, let framework can impl sub class and over context extend easily.
64
92
  ContextCookies = ContextCookies;
65
93
  ContextLogger = ContextLogger;
@@ -412,7 +440,6 @@ export class EggApplicationCore extends EggCore {
412
440
  if (!(err instanceof Error)) {
413
441
  const newError = new Error(String(err));
414
442
  // err maybe an object, try to copy the name, message and stack to the new error instance
415
- /* istanbul ignore else */
416
443
  if (err) {
417
444
  if (err.name) newError.name = err.name;
418
445
  if (err.message) newError.message = err.message;
@@ -420,7 +447,6 @@ export class EggApplicationCore extends EggCore {
420
447
  }
421
448
  err = newError;
422
449
  }
423
- /* istanbul ignore else */
424
450
  if (err.name === 'Error') {
425
451
  err.name = 'unhandledRejectionError';
426
452
  }
@@ -500,12 +526,7 @@ export class EggApplicationCore extends EggCore {
500
526
  }
501
527
 
502
528
  get [EGG_PATH]() {
503
- if (typeof __dirname !== 'undefined') {
504
- return path.dirname(__dirname);
505
- }
506
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
507
- // @ts-ignore
508
- return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
529
+ return getSourceDirname();
509
530
  }
510
531
 
511
532
  #setupTimeoutTimer() {
@@ -589,7 +610,7 @@ export class EggApplicationCore extends EggCore {
589
610
  * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
590
611
  * @return {Context} context
591
612
  */
592
- createAnonymousContext(req?: any): EggCoreContext {
613
+ createAnonymousContext(req?: any): EggContext {
593
614
  const request: any = {
594
615
  headers: {
595
616
  host: '127.0.0.1',
@@ -633,7 +654,7 @@ export class EggApplicationCore extends EggCore {
633
654
  const context = Object.create(this.context) as EggContext;
634
655
  const request = context.request = Object.create(this.request);
635
656
  const response = context.response = Object.create(this.response);
636
- context.app = request.app = response.app = this;
657
+ context.app = request.app = response.app = this as any;
637
658
  context.req = request.req = response.req = req;
638
659
  context.res = request.res = response.res = res;
639
660
  request.ctx = response.ctx = context;
package/src/lib/type.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { Socket } from 'node:net';
2
- import type { EggCoreContext } from '@eggjs/core';
3
2
  import type {
4
3
  RequestOptions as HttpClientRequestOptions,
5
4
  } from 'urllib';
@@ -10,17 +9,15 @@ import type {
10
9
  FileLoaderOptions,
11
10
  } from '@eggjs/core';
12
11
  import type {
13
- EggApplicationCore,
12
+ EggApplicationCore, ContextDelegation,
14
13
  } from './egg.js';
15
14
  import type { MetaMiddlewareOptions } from '../app/middleware/meta.js';
16
15
  import type { NotFoundMiddlewareOptions } from '../app/middleware/notfound.js';
17
16
  import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
18
17
 
19
- type IgnoreItem = string | RegExp | ((ctx: EggCoreContext) => boolean);
18
+ type IgnoreItem = string | RegExp | ((ctx: ContextDelegation) => boolean);
20
19
  type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
21
20
 
22
- export type Next = () => Promise<void>;
23
-
24
21
  export interface ClientErrorResponse {
25
22
  body: string | Buffer;
26
23
  status: number;
@@ -103,7 +100,7 @@ export interface EggAppConfig {
103
100
  };
104
101
  /** Default is `'error'`, it will return `400` response when `Prototype-Poisoning` happen. */
105
102
  onProtoPoisoning: 'error' | 'remove' | 'ignore';
106
- onerror(err: any, ctx: EggCoreContext): void;
103
+ onerror(err: any, ctx: ContextDelegation): void;
107
104
  };
108
105
 
109
106
  /**
@@ -327,10 +324,3 @@ export interface EggAppConfig {
327
324
 
328
325
  [prop: string]: any;
329
326
  }
330
-
331
- export type {
332
- EggLogger,
333
- } from 'egg-logger';
334
- export type {
335
- ILifecycleBoot,
336
- } from '@eggjs/core';
@@ -0,0 +1,16 @@
1
+
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ export function getSourceDirname() {
6
+ if (typeof __dirname !== 'undefined') {
7
+ return path.dirname(__dirname);
8
+ }
9
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10
+ // @ts-ignore
11
+ return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
12
+ }
13
+
14
+ export function getSourceFile(filename: string) {
15
+ return path.join(getSourceDirname(), filename);
16
+ }
@@ -1,101 +0,0 @@
1
- 'use strict';
2
-
3
- const getType = require('cache-content-type');
4
- const isJSON = require('koa-is-json');
5
-
6
- const REAL_STATUS = Symbol('Context#realStatus');
7
-
8
- module.exports = {
9
-
10
- /**
11
- * Get or set the length of content.
12
- *
13
- * For Get: If the original content length is null or undefined, it will read out
14
- * the body's content length as the return value.
15
- *
16
- * @member {Number} Response#type
17
- * @param {Number} len The content-length to be set.
18
- */
19
- set length(len) {
20
- // copy from koa
21
- // change header name to lower case
22
- this.set('content-length', len);
23
- },
24
-
25
- get length() {
26
- // copy from koa
27
- const len = this.header['content-length'];
28
- const body = this.body;
29
-
30
- if (len == null) {
31
- if (!body) return;
32
- if (typeof body === 'string') return Buffer.byteLength(body);
33
- if (Buffer.isBuffer(body)) return body.length;
34
- if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
35
- return;
36
- }
37
-
38
- return parseInt(len, 10);
39
- },
40
-
41
- /**
42
- * Get or set the content-type.
43
- *
44
- * For Set: If type is null or undefined, this property will be removed.
45
- *
46
- * For Get: If the value is null or undefined, an empty string will be returned;
47
- * if you have multiple values seperated by `;`, ONLY the first one will be returned.
48
- *
49
- * @member {String} Response#type
50
- * @param {String} type The content-type to be set.
51
- */
52
- set type(type) {
53
- // copy from koa
54
- // Different:
55
- // - change header name to lower case
56
- type = getType(type);
57
- if (type) {
58
- this.set('content-type', type);
59
- } else {
60
- this.remove('content-type');
61
- }
62
- },
63
-
64
- get type() {
65
- // copy from koa
66
- const type = this.get('content-type');
67
- if (!type) return '';
68
- return type.split(';')[0];
69
- },
70
-
71
- /**
72
- * Get or set a real status code.
73
- *
74
- * e.g.: Using 302 status redirect to the global error page
75
- * instead of show current 500 status page.
76
- * And access log should save 500 not 302,
77
- * then the `realStatus` can help us find out the real status code.
78
- * @member {Number} Response#realStatus
79
- * @return {Number} The status code to be set.
80
- */
81
- get realStatus() {
82
- if (this[REAL_STATUS]) {
83
- return this[REAL_STATUS];
84
- }
85
- return this.status;
86
- },
87
-
88
- /**
89
- * Set a real status code.
90
- *
91
- * e.g.: Using 302 status redirect to the global error page
92
- * instead of show current 500 status page.
93
- * And access log should save 500 not 302,
94
- * then the `realStatus` can help us find out the real status code.
95
- * @member {Number} Response#realStatus
96
- * @param {Number} status The status code to be set.
97
- */
98
- set realStatus(status) {
99
- this[REAL_STATUS] = status;
100
- },
101
- };