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.
- package/dist/commonjs/app/extend/context.d.ts +154 -2
- package/dist/commonjs/app/extend/context.js +82 -84
- package/dist/commonjs/app/extend/context.types.d.ts +16 -0
- package/dist/commonjs/app/extend/context.types.js +3 -0
- package/dist/commonjs/app/extend/helper.d.ts +37 -0
- package/dist/commonjs/app/extend/helper.js +49 -0
- package/dist/commonjs/app/extend/request.d.ts +128 -0
- package/dist/commonjs/app/extend/request.js +270 -0
- package/dist/commonjs/app/extend/response.d.ts +25 -0
- package/dist/commonjs/app/extend/response.js +37 -0
- package/dist/commonjs/app/middleware/meta.d.ts +2 -3
- package/dist/commonjs/app/middleware/meta.js +1 -1
- package/dist/commonjs/app/middleware/notfound.d.ts +2 -3
- package/dist/commonjs/app/middleware/notfound.js +1 -1
- package/dist/commonjs/app/middleware/site_file.d.ts +4 -2
- package/dist/commonjs/app/middleware/site_file.js +2 -3
- package/dist/commonjs/config/config.default.js +3 -2
- package/dist/commonjs/lib/application.d.ts +5 -12
- package/dist/commonjs/lib/application.js +9 -20
- package/dist/commonjs/lib/core/base_context_class.d.ts +2 -2
- package/dist/commonjs/lib/core/context_httpclient.d.ts +3 -3
- package/dist/commonjs/lib/core/context_httpclient.js +1 -1
- package/dist/commonjs/lib/core/httpclient.d.ts +2 -3
- package/dist/commonjs/lib/core/httpclient.js +3 -6
- package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +2 -2
- package/dist/commonjs/lib/core/messenger/index.js +2 -2
- package/dist/commonjs/lib/core/messenger/ipc.d.ts +5 -3
- package/dist/commonjs/lib/core/messenger/ipc.js +23 -17
- package/dist/commonjs/lib/core/messenger/local.d.ts +2 -2
- package/dist/commonjs/lib/core/messenger/local.js +14 -11
- package/dist/commonjs/lib/egg.d.ts +20 -15
- package/dist/commonjs/lib/egg.js +15 -11
- package/dist/commonjs/lib/egg.types.d.ts +6 -0
- package/dist/commonjs/lib/egg.types.js +3 -0
- package/dist/commonjs/lib/type.d.ts +4 -7
- package/dist/commonjs/lib/utils.d.ts +2 -0
- package/dist/commonjs/lib/utils.js +21 -0
- package/dist/esm/app/extend/context.d.ts +154 -2
- package/dist/esm/app/extend/context.js +82 -85
- package/dist/esm/app/extend/context.types.d.ts +16 -0
- package/dist/esm/app/extend/context.types.js +2 -0
- package/dist/esm/app/extend/helper.d.ts +37 -0
- package/dist/esm/app/extend/helper.js +43 -0
- package/dist/esm/app/extend/request.d.ts +128 -0
- package/dist/esm/app/extend/request.js +264 -0
- package/dist/esm/app/extend/response.d.ts +25 -0
- package/dist/esm/app/extend/response.js +34 -0
- package/dist/esm/app/middleware/meta.d.ts +2 -3
- package/dist/esm/app/middleware/meta.js +1 -1
- package/dist/esm/app/middleware/notfound.d.ts +2 -3
- package/dist/esm/app/middleware/notfound.js +1 -1
- package/dist/esm/app/middleware/site_file.d.ts +4 -2
- package/dist/esm/app/middleware/site_file.js +2 -3
- package/dist/esm/config/config.default.js +3 -2
- package/dist/esm/lib/application.d.ts +5 -12
- package/dist/esm/lib/application.js +9 -20
- package/dist/esm/lib/core/base_context_class.d.ts +2 -2
- package/dist/esm/lib/core/context_httpclient.d.ts +3 -3
- package/dist/esm/lib/core/context_httpclient.js +1 -1
- package/dist/esm/lib/core/httpclient.d.ts +2 -3
- package/dist/esm/lib/core/httpclient.js +2 -2
- package/dist/esm/lib/core/messenger/IMessenger.d.ts +2 -2
- package/dist/esm/lib/core/messenger/index.js +2 -2
- package/dist/esm/lib/core/messenger/ipc.d.ts +5 -3
- package/dist/esm/lib/core/messenger/ipc.js +23 -17
- package/dist/esm/lib/core/messenger/local.d.ts +2 -2
- package/dist/esm/lib/core/messenger/local.js +14 -11
- package/dist/esm/lib/egg.d.ts +20 -15
- package/dist/esm/lib/egg.js +13 -12
- package/dist/esm/lib/egg.types.d.ts +6 -0
- package/dist/esm/lib/egg.types.js +2 -0
- package/dist/esm/lib/type.d.ts +4 -7
- package/dist/esm/lib/utils.d.ts +2 -0
- package/dist/esm/lib/utils.js +14 -0
- package/dist/package.json +1 -1
- package/package.json +16 -13
- package/src/app/extend/context.ts +124 -102
- package/src/app/extend/context.types.ts +24 -0
- package/src/app/extend/{helper.js → helper.ts} +14 -13
- package/src/app/extend/{request.js → request.ts} +81 -79
- package/src/app/extend/response.ts +36 -0
- package/src/app/middleware/meta.ts +2 -3
- package/src/app/middleware/notfound.ts +2 -3
- package/src/app/middleware/site_file.ts +4 -6
- package/src/config/config.default.ts +2 -1
- package/src/lib/application.ts +14 -21
- package/src/lib/core/base_context_class.ts +2 -2
- package/src/lib/core/context_httpclient.ts +3 -3
- package/src/lib/core/httpclient.ts +4 -5
- package/src/lib/core/messenger/IMessenger.ts +2 -2
- package/src/lib/core/messenger/index.ts +1 -1
- package/src/lib/core/messenger/ipc.ts +23 -17
- package/src/lib/core/messenger/local.ts +12 -10
- package/src/lib/egg.ts +47 -24
- package/src/lib/egg.types.ts +6 -0
- package/src/lib/type.ts +6 -13
- package/src/lib/utils.ts +16 -0
- 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
|
|
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',
|
|
23
|
-
this.
|
|
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}
|
|
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(
|
|
53
|
-
debug('[%s] send %s with %j to
|
|
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(
|
|
61
|
-
receiverWorkerId: String(
|
|
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
|
|
81
|
-
this.sendTo(
|
|
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
|
|
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}
|
|
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(
|
|
43
|
-
debug('[%s] send %s with %j to %s', this.pid, action, data,
|
|
44
|
-
if (String(
|
|
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 {
|
|
7
|
-
import {
|
|
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
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
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):
|
|
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;
|
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
|
-
|
|
20
|
-
|
|
18
|
+
// import @eggjs/watcher types
|
|
19
|
+
// import '@eggjs/watcher';
|
|
21
20
|
|
|
22
|
-
|
|
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:
|
|
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';
|
package/src/lib/utils.ts
ADDED
|
@@ -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
|
-
};
|