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
@@ -1,17 +1,21 @@
1
- 'use strict';
1
+ import querystring from 'node:querystring';
2
+ import { Request as EggCoreRequest } from '@eggjs/core';
3
+ import type { Application } from '../../lib/application.js';
4
+ import type { ContextDelegation } from './context.js';
5
+ import Response from './response.js';
2
6
 
3
- const querystring = require('querystring');
4
- const accepts = require('accepts');
5
-
6
- const _querycache = Symbol('_querycache');
7
- const _queriesCache = Symbol('_queriesCache');
8
- const PROTOCOL = Symbol('PROTOCOL');
9
- const HOST = Symbol('HOST');
10
- const ACCEPTS = Symbol('ACCEPTS');
11
- const IPS = Symbol('IPS');
7
+ const QUERY_CACHE = Symbol('request query cache');
8
+ const QUERIES_CACHE = Symbol('request queries cache');
9
+ const PROTOCOL = Symbol('request protocol');
10
+ const HOST = Symbol('request host');
11
+ const IPS = Symbol('request ips');
12
12
  const RE_ARRAY_KEY = /[^\[\]]+\[\]$/;
13
13
 
14
- module.exports = {
14
+ export default class Request extends EggCoreRequest {
15
+ declare app: Application;
16
+ declare ctx: ContextDelegation;
17
+ declare response: Response;
18
+
15
19
  /**
16
20
  * Parse the "Host" header field host
17
21
  * and support X-Forwarded-Host when a
@@ -29,17 +33,19 @@ module.exports = {
29
33
  * => 'demo.eggjs.org'
30
34
  * ```
31
35
  */
32
- get host() {
33
- if (this[HOST]) return this[HOST];
36
+ get host(): string {
37
+ let host = this[HOST] as string | undefined;
38
+ if (host) {
39
+ return host;
40
+ }
34
41
 
35
- let host;
36
42
  if (this.app.config.proxy) {
37
43
  host = getFromHeaders(this, this.app.config.hostHeaders);
38
44
  }
39
45
  host = host || this.get('host') || '';
40
- this[HOST] = host.split(/\s*,\s*/)[0];
41
- return this[HOST];
42
- },
46
+ this[HOST] = host = host.split(',')[0].trim();
47
+ return host;
48
+ }
43
49
 
44
50
  /**
45
51
  * @member {String} Request#protocol
@@ -49,25 +55,28 @@ module.exports = {
49
55
  * => 'https'
50
56
  * ```
51
57
  */
52
- get protocol() {
53
- if (this[PROTOCOL]) return this[PROTOCOL];
58
+ get protocol(): string {
59
+ let protocol = this[PROTOCOL] as string;
60
+ if (protocol) {
61
+ return protocol;
62
+ }
54
63
  // detect encrypted socket
55
- if (this.socket && this.socket.encrypted) {
56
- this[PROTOCOL] = 'https';
57
- return this[PROTOCOL];
64
+ if (this.socket?.encrypted) {
65
+ this[PROTOCOL] = protocol = 'https';
66
+ return protocol;
58
67
  }
59
68
  // get from headers specified in `app.config.protocolHeaders`
60
69
  if (this.app.config.proxy) {
61
70
  const proto = getFromHeaders(this, this.app.config.protocolHeaders);
62
71
  if (proto) {
63
- this[PROTOCOL] = proto.split(/\s*,\s*/)[0];
64
- return this[PROTOCOL];
72
+ this[PROTOCOL] = protocol = proto.split(/\s*,\s*/)[0];
73
+ return protocol;
65
74
  }
66
75
  }
67
- // use protocol specified in `app.conig.protocol`
68
- this[PROTOCOL] = this.app.config.protocol || 'http';
69
- return this[PROTOCOL];
70
- },
76
+ // use protocol specified in `app.config.protocol`
77
+ this[PROTOCOL] = protocol = this.app.config.protocol || 'http';
78
+ return protocol;
79
+ }
71
80
 
72
81
  /**
73
82
  * Get all pass through ip addresses from the request.
@@ -80,29 +89,34 @@ module.exports = {
80
89
  * => ['100.23.1.2', '201.10.10.2']
81
90
  * ```
82
91
  */
83
- get ips() {
84
- if (this[IPS]) return this[IPS];
92
+ get ips(): string[] {
93
+ let ips = this[IPS] as string[] | undefined;
94
+ if (ips) {
95
+ return ips;
96
+ }
85
97
 
86
98
  // return empty array when proxy=false
87
99
  if (!this.app.config.proxy) {
88
- this[IPS] = [];
89
- return this[IPS];
100
+ this[IPS] = ips = [];
101
+ return ips;
90
102
  }
91
103
 
92
- const val = getFromHeaders(this, this.app.config.ipHeaders) || '';
93
- this[IPS] = val ? val.split(/\s*,\s*/) : [];
104
+ const val = getFromHeaders(this, this.app.config.ipHeaders);
105
+ this[IPS] = ips = val ? val.split(/\s*,\s*/) : [];
94
106
 
95
107
  let maxIpsCount = this.app.config.maxIpsCount;
96
108
  // Compatible with maxProxyCount logic (previous logic is wrong, only for compatibility with legacy logic)
97
- if (!maxIpsCount && this.app.config.maxProxyCount) maxIpsCount = this.app.config.maxProxyCount + 1;
109
+ if (!maxIpsCount && this.app.config.maxProxyCount) {
110
+ maxIpsCount = this.app.config.maxProxyCount + 1;
111
+ }
98
112
 
99
113
  if (maxIpsCount > 0) {
100
114
  // if maxIpsCount present, only keep `maxIpsCount` ips
101
115
  // [ illegalIp, clientRealIp, proxyIp1, proxyIp2 ...]
102
- this[IPS] = this[IPS].slice(-maxIpsCount);
116
+ this[IPS] = ips = ips.slice(-maxIpsCount);
103
117
  }
104
- return this[IPS];
105
- },
118
+ return ips;
119
+ }
106
120
 
107
121
  /**
108
122
  * Get the request remote IPv4 address
@@ -115,16 +129,16 @@ module.exports = {
115
129
  * => '111.10.2.1'
116
130
  * ```
117
131
  */
118
- get ip() {
132
+ get ip(): string {
119
133
  if (this._ip) {
120
134
  return this._ip;
121
135
  }
122
- const ip = this.ips[0] || this.socket.remoteAddress;
136
+ const ip = this.ips[0] ?? this.socket.remoteAddress;
123
137
  // will be '::ffff:x.x.x.x', should convert to standard IPv4 format
124
138
  // https://zh.wikipedia.org/wiki/IPv6
125
- this._ip = ip && ip.indexOf('::ffff:') > -1 ? ip.substring(7) : ip;
139
+ this._ip = ip && ip.startsWith('::ffff:') ? ip.substring(7) : ip;
126
140
  return this._ip;
127
- },
141
+ }
128
142
 
129
143
  /**
130
144
  * Set the request remote IPv4 address
@@ -137,9 +151,9 @@ module.exports = {
137
151
  * => '111.10.2.1'
138
152
  * ```
139
153
  */
140
- set ip(ip) {
154
+ set ip(ip: string) {
141
155
  this._ip = ip;
142
- },
156
+ }
143
157
 
144
158
  /**
145
159
  * detect if response should be json
@@ -150,25 +164,25 @@ module.exports = {
150
164
  * @member {Boolean} Request#acceptJSON
151
165
  * @since 1.0.0
152
166
  */
153
- get acceptJSON() {
167
+ get acceptJSON(): boolean {
154
168
  if (this.path.endsWith('.json')) return true;
155
169
  if (this.response.type && this.response.type.indexOf('json') >= 0) return true;
156
170
  if (this.accepts('html', 'text', 'json') === 'json') return true;
157
171
  return false;
158
- },
172
+ }
159
173
 
160
174
  // How to read query safely
161
175
  // https://github.com/koajs/qs/issues/5
162
- _customQuery(cacheName, filter) {
176
+ _customQuery(cacheName: symbol, filter: (value: string | string[]) => string | string[]) {
163
177
  const str = this.querystring || '';
164
- let c = this[cacheName];
178
+ let c = this[cacheName] as Record<string, Record<string, string | string[]>>;
165
179
  if (!c) {
166
180
  c = this[cacheName] = {};
167
181
  }
168
182
  let cacheQuery = c[str];
169
183
  if (!cacheQuery) {
170
184
  cacheQuery = c[str] = {};
171
- const isQueries = cacheName === _queriesCache;
185
+ const isQueries = cacheName === QUERIES_CACHE;
172
186
  // `querystring.parse` CANNOT parse something like `a[foo]=1&a[bar]=2`
173
187
  const query = str ? querystring.parse(str) : {};
174
188
  for (const key in query) {
@@ -176,20 +190,19 @@ module.exports = {
176
190
  // key is '', like `a=b&`
177
191
  continue;
178
192
  }
179
- const value = filter(query[key]);
193
+ const value = filter(query[key]!);
180
194
  cacheQuery[key] = value;
181
195
  if (isQueries && RE_ARRAY_KEY.test(key)) {
182
196
  // `this.queries['key'] => this.queries['key[]']` is compatibly supported
183
- const subkey = key.substring(0, key.length - 2);
184
-
185
- if (!cacheQuery[subkey]) {
186
- cacheQuery[subkey] = value;
197
+ const subKey = key.substring(0, key.length - 2);
198
+ if (!cacheQuery[subKey]) {
199
+ cacheQuery[subKey] = value;
187
200
  }
188
201
  }
189
202
  }
190
203
  }
191
204
  return cacheQuery;
192
- },
205
+ }
193
206
 
194
207
  /**
195
208
  * get params pass by querystring, all values are of string type.
@@ -212,8 +225,8 @@ module.exports = {
212
225
  * ```
213
226
  */
214
227
  get query() {
215
- return this._customQuery(_querycache, firstValue);
216
- },
228
+ return this._customQuery(QUERY_CACHE, firstValue) as Record<string, string>;
229
+ }
217
230
 
218
231
  /**
219
232
  * get params pass by querystring, all value are Array type. {@link Request#query}
@@ -232,50 +245,39 @@ module.exports = {
232
245
  * ```
233
246
  */
234
247
  get queries() {
235
- return this._customQuery(_queriesCache, arrayValue);
236
- },
237
-
238
- get accept() {
239
- let accept = this[ACCEPTS];
240
- if (accept) {
241
- return accept;
242
- }
243
- accept = this[ACCEPTS] = accepts(this.req);
244
- return accept;
245
- },
248
+ return this._customQuery(QUERIES_CACHE, arrayValue) as Record<string, string[]>;
249
+ }
246
250
 
247
251
  /**
248
252
  * Set query-string as an object.
249
253
  *
250
254
  * @function Request#query
251
255
  * @param {Object} obj set querystring and query object for request.
252
- * @return {void}
253
256
  */
254
- set query(obj) {
257
+ set query(obj: Record<string, string>) {
255
258
  this.querystring = querystring.stringify(obj);
256
- },
257
- };
258
-
259
+ }
260
+ }
259
261
 
260
- function firstValue(value) {
262
+ function firstValue(value: string | string[]) {
261
263
  if (Array.isArray(value)) {
262
264
  value = value[0];
263
265
  }
264
266
  return value;
265
267
  }
266
268
 
267
- function arrayValue(value) {
269
+ function arrayValue(value: string | string[]) {
268
270
  if (!Array.isArray(value)) {
269
271
  value = [ value ];
270
272
  }
271
273
  return value;
272
274
  }
273
275
 
274
- function getFromHeaders(ctx, names) {
276
+ function getFromHeaders(request: Request, names: string) {
275
277
  if (!names) return '';
276
- names = names.split(/\s*,\s*/);
277
- for (const name of names) {
278
- const value = ctx.get(name);
278
+ const fields = names.split(/\s*,\s*/);
279
+ for (const name of fields) {
280
+ const value = request.get<string>(name);
279
281
  if (value) return value;
280
282
  }
281
283
  return '';
@@ -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;
@@ -14,12 +13,11 @@ export interface SiteFileMiddlewareOptions {
14
13
 
15
14
  const BUFFER_CACHE = Symbol('siteFile URL buffer cache');
16
15
 
17
- module.exports = (options: SiteFileMiddlewareOptions) => {
18
- return async function siteFile(ctx: EggCoreContext, next: Next) {
16
+ export default (options: SiteFileMiddlewareOptions) => {
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
  }
@@ -11,12 +11,12 @@ export interface IMessenger extends EventEmitter {
11
11
 
12
12
  /**
13
13
  * send message to the specified process
14
- * @param {String} pid - the process id of the receiver
14
+ * @param {String} workerId - the workerId of the receiver
15
15
  * @param {String} action - message key
16
16
  * @param {Object} data - message value
17
17
  * @return {Messenger} this
18
18
  */
19
- sendTo(pid: string, action: string, data?: unknown): IMessenger;
19
+ sendTo(workerId: string, action: string, data?: unknown): IMessenger;
20
20
 
21
21
  /**
22
22
  * send message to one app worker by random
@@ -11,5 +11,5 @@ export type { IMessenger } from './IMessenger.js';
11
11
  export function create(egg: EggApplicationCore): IMessenger {
12
12
  return egg.options.mode === 'single'
13
13
  ? new LocalMessenger(egg)
14
- : new IPCMessenger();
14
+ : new IPCMessenger(egg);
15
15
  }