egg 4.0.0-beta.5 → 4.0.0-beta.7

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 (98) hide show
  1. package/dist/commonjs/app/extend/context.d.ts +154 -2
  2. package/dist/commonjs/app/extend/context.js +82 -84
  3. package/dist/commonjs/app/extend/context.types.d.ts +16 -0
  4. package/dist/commonjs/app/extend/context.types.js +3 -0
  5. package/dist/commonjs/app/extend/helper.d.ts +37 -0
  6. package/dist/commonjs/app/extend/helper.js +49 -0
  7. package/dist/commonjs/app/extend/request.d.ts +128 -0
  8. package/dist/commonjs/app/extend/request.js +270 -0
  9. package/dist/commonjs/app/extend/response.d.ts +25 -0
  10. package/dist/commonjs/app/extend/response.js +37 -0
  11. package/dist/commonjs/app/middleware/meta.d.ts +2 -3
  12. package/dist/commonjs/app/middleware/meta.js +1 -1
  13. package/dist/commonjs/app/middleware/notfound.d.ts +2 -3
  14. package/dist/commonjs/app/middleware/notfound.js +1 -1
  15. package/dist/commonjs/app/middleware/site_file.d.ts +4 -2
  16. package/dist/commonjs/app/middleware/site_file.js +2 -3
  17. package/dist/commonjs/config/config.default.js +3 -2
  18. package/dist/commonjs/lib/application.d.ts +5 -12
  19. package/dist/commonjs/lib/application.js +9 -20
  20. package/dist/commonjs/lib/core/base_context_class.d.ts +2 -2
  21. package/dist/commonjs/lib/core/context_httpclient.d.ts +3 -3
  22. package/dist/commonjs/lib/core/context_httpclient.js +1 -1
  23. package/dist/commonjs/lib/core/httpclient.d.ts +2 -3
  24. package/dist/commonjs/lib/core/httpclient.js +3 -6
  25. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +2 -2
  26. package/dist/commonjs/lib/core/messenger/index.js +2 -2
  27. package/dist/commonjs/lib/core/messenger/ipc.d.ts +5 -3
  28. package/dist/commonjs/lib/core/messenger/ipc.js +23 -17
  29. package/dist/commonjs/lib/core/messenger/local.d.ts +2 -2
  30. package/dist/commonjs/lib/core/messenger/local.js +14 -11
  31. package/dist/commonjs/lib/egg.d.ts +20 -15
  32. package/dist/commonjs/lib/egg.js +15 -11
  33. package/dist/commonjs/lib/egg.types.d.ts +6 -0
  34. package/dist/commonjs/lib/egg.types.js +3 -0
  35. package/dist/commonjs/lib/type.d.ts +4 -7
  36. package/dist/commonjs/lib/utils.d.ts +2 -0
  37. package/dist/commonjs/lib/utils.js +21 -0
  38. package/dist/esm/app/extend/context.d.ts +154 -2
  39. package/dist/esm/app/extend/context.js +82 -85
  40. package/dist/esm/app/extend/context.types.d.ts +16 -0
  41. package/dist/esm/app/extend/context.types.js +2 -0
  42. package/dist/esm/app/extend/helper.d.ts +37 -0
  43. package/dist/esm/app/extend/helper.js +43 -0
  44. package/dist/esm/app/extend/request.d.ts +128 -0
  45. package/dist/esm/app/extend/request.js +264 -0
  46. package/dist/esm/app/extend/response.d.ts +25 -0
  47. package/dist/esm/app/extend/response.js +34 -0
  48. package/dist/esm/app/middleware/meta.d.ts +2 -3
  49. package/dist/esm/app/middleware/meta.js +1 -1
  50. package/dist/esm/app/middleware/notfound.d.ts +2 -3
  51. package/dist/esm/app/middleware/notfound.js +1 -1
  52. package/dist/esm/app/middleware/site_file.d.ts +4 -2
  53. package/dist/esm/app/middleware/site_file.js +2 -3
  54. package/dist/esm/config/config.default.js +3 -2
  55. package/dist/esm/lib/application.d.ts +5 -12
  56. package/dist/esm/lib/application.js +9 -20
  57. package/dist/esm/lib/core/base_context_class.d.ts +2 -2
  58. package/dist/esm/lib/core/context_httpclient.d.ts +3 -3
  59. package/dist/esm/lib/core/context_httpclient.js +1 -1
  60. package/dist/esm/lib/core/httpclient.d.ts +2 -3
  61. package/dist/esm/lib/core/httpclient.js +2 -2
  62. package/dist/esm/lib/core/messenger/IMessenger.d.ts +2 -2
  63. package/dist/esm/lib/core/messenger/index.js +2 -2
  64. package/dist/esm/lib/core/messenger/ipc.d.ts +5 -3
  65. package/dist/esm/lib/core/messenger/ipc.js +23 -17
  66. package/dist/esm/lib/core/messenger/local.d.ts +2 -2
  67. package/dist/esm/lib/core/messenger/local.js +14 -11
  68. package/dist/esm/lib/egg.d.ts +20 -15
  69. package/dist/esm/lib/egg.js +13 -12
  70. package/dist/esm/lib/egg.types.d.ts +6 -0
  71. package/dist/esm/lib/egg.types.js +2 -0
  72. package/dist/esm/lib/type.d.ts +4 -7
  73. package/dist/esm/lib/utils.d.ts +2 -0
  74. package/dist/esm/lib/utils.js +14 -0
  75. package/dist/package.json +1 -1
  76. package/package.json +16 -13
  77. package/src/app/extend/context.ts +124 -102
  78. package/src/app/extend/context.types.ts +24 -0
  79. package/src/app/extend/{helper.js → helper.ts} +14 -13
  80. package/src/app/extend/{request.js → request.ts} +81 -79
  81. package/src/app/extend/response.ts +36 -0
  82. package/src/app/middleware/meta.ts +2 -3
  83. package/src/app/middleware/notfound.ts +2 -3
  84. package/src/app/middleware/site_file.ts +4 -6
  85. package/src/config/config.default.ts +2 -1
  86. package/src/lib/application.ts +14 -21
  87. package/src/lib/core/base_context_class.ts +2 -2
  88. package/src/lib/core/context_httpclient.ts +3 -3
  89. package/src/lib/core/httpclient.ts +4 -5
  90. package/src/lib/core/messenger/IMessenger.ts +2 -2
  91. package/src/lib/core/messenger/index.ts +1 -1
  92. package/src/lib/core/messenger/ipc.ts +23 -17
  93. package/src/lib/core/messenger/local.ts +12 -10
  94. package/src/lib/egg.ts +47 -24
  95. package/src/lib/egg.types.ts +6 -0
  96. package/src/lib/type.ts +6 -13
  97. package/src/lib/utils.ts +16 -0
  98. package/src/app/extend/response.js +0 -101
@@ -3,24 +3,28 @@ import { debuglog } from 'node:util';
3
3
  import workerThreads from 'node:worker_threads';
4
4
  import { sendmessage } from 'sendmessage';
5
5
  import type { IMessenger } from './IMessenger.js';
6
+ import type { EggApplicationCore } from '../../egg.js';
6
7
 
7
- const debug = debuglog('egg:lib:core:messenger:ipc');
8
+ const debug = debuglog('egg/lib/core/messenger/ipc');
8
9
 
9
10
  /**
10
11
  * Communication between app worker and agent worker by IPC channel
11
12
  */
12
13
  export class Messenger extends EventEmitter implements IMessenger {
13
14
  readonly pid: string;
15
+ readonly egg: EggApplicationCore;
14
16
  opids: string[] = [];
15
17
 
16
- constructor() {
18
+ constructor(egg: EggApplicationCore) {
17
19
  super();
18
20
  this.pid = String(process.pid);
21
+ this.egg = egg;
19
22
  // pids of agent or app managed by master
20
23
  // - retrieve app worker pids when it's an agent worker
21
24
  // - retrieve agent worker pids when it's an app worker
22
- this.on('egg-pids', pids => {
23
- this.opids = pids;
25
+ this.on('egg-pids', workerIds => {
26
+ debug('[%s:%s] got egg-pids %j', this.egg.type, this.pid, workerIds);
27
+ this.opids = workerIds.map((workerId: number) => String(workerId));
24
28
  });
25
29
  this.onMessage = this.onMessage.bind(this);
26
30
  process.on('message', this.onMessage);
@@ -36,7 +40,7 @@ export class Messenger extends EventEmitter implements IMessenger {
36
40
  * @return {Messenger} this
37
41
  */
38
42
  broadcast(action: string, data?: unknown): Messenger {
39
- debug('[%s] broadcast %s with %j', this.pid, action, data);
43
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
40
44
  this.send(action, data, 'app');
41
45
  this.send(action, data, 'agent');
42
46
  return this;
@@ -44,21 +48,21 @@ export class Messenger extends EventEmitter implements IMessenger {
44
48
 
45
49
  /**
46
50
  * send message to the specified process
47
- * @param {String} pid - the process id of the receiver
51
+ * @param {String} workerId - the workerId of the receiver
48
52
  * @param {String} action - message key
49
53
  * @param {Object} data - message value
50
54
  * @return {Messenger} this
51
55
  */
52
- sendTo(pid: string, action: string, data?: unknown): Messenger {
53
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
56
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
57
+ debug('[%s:%s] send %s with %j to workerId:%s', this.egg.type, this.pid, action, data, workerId);
54
58
  sendmessage(process, {
55
59
  action,
56
60
  data,
57
61
  /**
58
62
  * @deprecated Keep compatible, please use receiverWorkerId instead
59
63
  */
60
- receiverPid: String(pid),
61
- receiverWorkerId: String(pid),
64
+ receiverPid: String(workerId),
65
+ receiverWorkerId: String(workerId),
62
66
  });
63
67
  return this;
64
68
  }
@@ -72,13 +76,13 @@ export class Messenger extends EventEmitter implements IMessenger {
72
76
  * @return {Messenger} this
73
77
  */
74
78
  sendRandom(action: string, data?: unknown): Messenger {
75
- /* istanbul ignore if */
76
79
  if (this.opids.length === 0) {
80
+ debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
77
81
  return this;
78
82
  }
79
83
  const index = Math.floor(Math.random() * this.opids.length);
80
- const pid = this.opids[index];
81
- this.sendTo(String(pid), action, data);
84
+ const workerId = this.opids[index];
85
+ this.sendTo(workerId, action, data);
82
86
  return this;
83
87
  }
84
88
 
@@ -89,7 +93,7 @@ export class Messenger extends EventEmitter implements IMessenger {
89
93
  * @return {Messenger} this
90
94
  */
91
95
  sendToApp(action: string, data?: unknown): Messenger {
92
- debug('[%s] send %s with %j to all app', this.pid, action, data);
96
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
93
97
  this.send(action, data, 'app');
94
98
  return this;
95
99
  }
@@ -101,7 +105,7 @@ export class Messenger extends EventEmitter implements IMessenger {
101
105
  * @return {Messenger} this
102
106
  */
103
107
  sendToAgent(action: string, data?: unknown): Messenger {
104
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
108
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
105
109
  this.send(action, data, 'agent');
106
110
  return this;
107
111
  }
@@ -123,9 +127,11 @@ export class Messenger extends EventEmitter implements IMessenger {
123
127
 
124
128
  onMessage(message: any) {
125
129
  if (typeof message?.action === 'string') {
126
- debug('[%s] got message %s with %j, receiverWorkerId: %s',
127
- this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
130
+ debug('[%s:%s] got message %s with %j, receiverWorkerId: %s',
131
+ this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
128
132
  this.emit(message.action, message.data);
133
+ } else {
134
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
129
135
  }
130
136
  }
131
137
 
@@ -3,7 +3,7 @@ import EventEmitter from 'node:events';
3
3
  import type { IMessenger } from './IMessenger.js';
4
4
  import type { EggApplicationCore } from '../../egg.js';
5
5
 
6
- const debug = debuglog('egg:lib:core:messenger:local');
6
+ const debug = debuglog('egg/lib/core/messenger/local');
7
7
 
8
8
  /**
9
9
  * Communication between app worker and agent worker with EventEmitter
@@ -25,7 +25,7 @@ export class Messenger extends EventEmitter implements IMessenger {
25
25
  * @return {Messenger} this
26
26
  */
27
27
  broadcast(action: string, data?: unknown): Messenger {
28
- debug('[%s] broadcast %s with %j', this.pid, action, data);
28
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
29
29
  this.send(action, data, 'both');
30
30
  return this;
31
31
  }
@@ -34,14 +34,14 @@ export class Messenger extends EventEmitter implements IMessenger {
34
34
  * send message to the specified process
35
35
  * Notice: in single process mode, it only can send to self process,
36
36
  * and it will send to both agent and app's messengers.
37
- * @param {String} pid - the process id of the receiver
37
+ * @param {String} workerId - the workerId of the receiver
38
38
  * @param {String} action - message key
39
39
  * @param {Object} data - message value
40
40
  * @return {Messenger} this
41
41
  */
42
- sendTo(pid: string, action: string, data?: unknown): Messenger {
43
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
44
- if (String(pid) !== this.pid) {
42
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
43
+ debug('[%s:%s] send %s with %j to %s', this.egg.type, this.pid, action, data, workerId);
44
+ if (String(workerId) !== this.pid) {
45
45
  return this;
46
46
  }
47
47
  this.send(action, data, 'both');
@@ -58,7 +58,7 @@ export class Messenger extends EventEmitter implements IMessenger {
58
58
  * @return {Messenger} this
59
59
  */
60
60
  sendRandom(action: string, data?: unknown): Messenger {
61
- debug('[%s] send %s with %j to opposite', this.pid, action, data);
61
+ debug('[%s:%s] send %s with %j to opposite', this.egg.type, this.pid, action, data);
62
62
  this.send(action, data, 'opposite');
63
63
  return this;
64
64
  }
@@ -70,7 +70,7 @@ export class Messenger extends EventEmitter implements IMessenger {
70
70
  * @return {Messenger} this
71
71
  */
72
72
  sendToApp(action: string, data?: unknown): Messenger {
73
- debug('[%s] send %s with %j to all app', this.pid, action, data);
73
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
74
74
  this.send(action, data, 'application');
75
75
  return this;
76
76
  }
@@ -82,7 +82,7 @@ export class Messenger extends EventEmitter implements IMessenger {
82
82
  * @return {Messenger} this
83
83
  */
84
84
  sendToAgent(action: string, data?: unknown): Messenger {
85
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
85
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
86
86
  this.send(action, data, 'agent');
87
87
  return this;
88
88
  }
@@ -130,8 +130,10 @@ export class Messenger extends EventEmitter implements IMessenger {
130
130
 
131
131
  onMessage(message: any) {
132
132
  if (typeof message?.action === 'string') {
133
- debug('[%s] got message %s with %j', this.pid, message.action, message.data);
133
+ debug('[%s:%s] got message %s with %j', this.egg.type, this.pid, message.action, message.data);
134
134
  this.emit(message.action, message.data);
135
+ } else {
136
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
135
137
  }
136
138
  }
137
139
 
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,9 @@ 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';
45
+
46
+ import './egg.types.js';
33
47
 
34
48
  const EGG_PATH = Symbol.for('egg#eggPath');
35
49
 
@@ -39,20 +53,35 @@ export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir
39
53
  baseDir?: string;
40
54
  }
41
55
 
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;
56
+ // export egg classes
57
+ export {
58
+ Context,
59
+ Router,
60
+ EggLogger,
61
+ };
62
+
63
+ export class Request extends EggCoreRequest {
64
+ declare app: EggCore;
65
+ declare response: Response;
66
+ declare ctx: ContextDelegation;
67
+ }
68
+
69
+ export class Response extends EggCoreResponse {
70
+ declare app: EggCore;
71
+ declare request: Request;
72
+ declare ctx: ContextDelegation;
54
73
  }
55
74
 
75
+ // export egg types
76
+ export type {
77
+ ContextDelegation,
78
+ ILifecycleBoot,
79
+ Next,
80
+ };
81
+ // keep compatible with egg version 3.x
82
+ export type EggContext = ContextDelegation;
83
+ export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = EggCoreMiddlewareFunc<T>;
84
+
56
85
  /**
57
86
  * Based on koa's Application
58
87
  * @see https://github.com/eggjs/egg-core
@@ -60,6 +89,7 @@ export interface EggContext extends EggCoreContext {
60
89
  * @augments EggCore
61
90
  */
62
91
  export class EggApplicationCore extends EggCore {
92
+ declare ctxStorage: AsyncLocalStorage<ContextDelegation>;
63
93
  // export context base classes, let framework can impl sub class and over context extend easily.
64
94
  ContextCookies = ContextCookies;
65
95
  ContextLogger = ContextLogger;
@@ -412,7 +442,6 @@ export class EggApplicationCore extends EggCore {
412
442
  if (!(err instanceof Error)) {
413
443
  const newError = new Error(String(err));
414
444
  // err maybe an object, try to copy the name, message and stack to the new error instance
415
- /* istanbul ignore else */
416
445
  if (err) {
417
446
  if (err.name) newError.name = err.name;
418
447
  if (err.message) newError.message = err.message;
@@ -420,7 +449,6 @@ export class EggApplicationCore extends EggCore {
420
449
  }
421
450
  err = newError;
422
451
  }
423
- /* istanbul ignore else */
424
452
  if (err.name === 'Error') {
425
453
  err.name = 'unhandledRejectionError';
426
454
  }
@@ -500,12 +528,7 @@ export class EggApplicationCore extends EggCore {
500
528
  }
501
529
 
502
530
  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)));
531
+ return getSourceDirname();
509
532
  }
510
533
 
511
534
  #setupTimeoutTimer() {
@@ -589,7 +612,7 @@ export class EggApplicationCore extends EggCore {
589
612
  * @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
590
613
  * @return {Context} context
591
614
  */
592
- createAnonymousContext(req?: any): EggCoreContext {
615
+ createAnonymousContext(req?: any): EggContext {
593
616
  const request: any = {
594
617
  headers: {
595
618
  host: '127.0.0.1',
@@ -633,7 +656,7 @@ export class EggApplicationCore extends EggCore {
633
656
  const context = Object.create(this.context) as EggContext;
634
657
  const request = context.request = Object.create(this.request);
635
658
  const response = context.response = Object.create(this.response);
636
- context.app = request.app = response.app = this;
659
+ context.app = request.app = response.app = this as any;
637
660
  context.req = request.req = response.req = req;
638
661
  context.res = request.res = response.res = res;
639
662
  request.ctx = response.ctx = context;
@@ -0,0 +1,6 @@
1
+ declare module '@eggjs/core' {
2
+ // add EggApplicationCore overrides types
3
+ interface EggCore {
4
+ inspect(): any;
5
+ }
6
+ }
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,16 +9,17 @@ 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);
20
- type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
18
+ // import @eggjs/watcher types
19
+ // import '@eggjs/watcher';
21
20
 
22
- export type Next = () => Promise<void>;
21
+ type IgnoreItem = string | RegExp | ((ctx: ContextDelegation) => boolean);
22
+ type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
23
23
 
24
24
  export interface ClientErrorResponse {
25
25
  body: string | Buffer;
@@ -103,7 +103,7 @@ export interface EggAppConfig {
103
103
  };
104
104
  /** Default is `'error'`, it will return `400` response when `Prototype-Poisoning` happen. */
105
105
  onProtoPoisoning: 'error' | 'remove' | 'ignore';
106
- onerror(err: any, ctx: EggCoreContext): void;
106
+ onerror(err: any, ctx: ContextDelegation): void;
107
107
  };
108
108
 
109
109
  /**
@@ -327,10 +327,3 @@ export interface EggAppConfig {
327
327
 
328
328
  [prop: string]: any;
329
329
  }
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
- };