egg 4.0.0-beta.0 → 4.0.0-beta.10
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/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/dist/commonjs/app/extend/context.d.ts +154 -0
- package/dist/commonjs/app/extend/context.js +261 -0
- 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 +5 -3
- package/dist/commonjs/app/middleware/site_file.js +21 -3
- package/dist/commonjs/config/config.default.js +5 -4
- package/dist/commonjs/config/plugin.js +3 -3
- package/dist/commonjs/index.d.ts +3 -1
- package/dist/commonjs/index.js +8 -6
- 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/base_hook_class.d.ts +1 -1
- 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 +3 -3
- package/dist/commonjs/lib/core/messenger/index.js +2 -2
- package/dist/commonjs/lib/core/messenger/ipc.d.ts +6 -4
- package/dist/commonjs/lib/core/messenger/ipc.js +31 -20
- package/dist/commonjs/lib/core/messenger/local.d.ts +3 -3
- package/dist/commonjs/lib/core/messenger/local.js +14 -11
- package/dist/commonjs/lib/egg.d.ts +25 -16
- package/dist/commonjs/lib/egg.js +44 -22
- 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 +3 -5
- 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 -0
- package/dist/esm/app/extend/context.js +255 -0
- 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 +5 -3
- package/dist/esm/app/middleware/site_file.js +21 -3
- package/dist/esm/config/config.default.js +5 -4
- package/dist/esm/config/plugin.js +3 -3
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +5 -4
- 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/base_hook_class.d.ts +1 -1
- 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 +3 -3
- package/dist/esm/lib/core/messenger/index.js +2 -2
- package/dist/esm/lib/core/messenger/ipc.d.ts +6 -4
- package/dist/esm/lib/core/messenger/ipc.js +29 -18
- package/dist/esm/lib/core/messenger/local.d.ts +3 -3
- package/dist/esm/lib/core/messenger/local.js +14 -11
- package/dist/esm/lib/egg.d.ts +25 -16
- package/dist/esm/lib/egg.js +24 -15
- 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 +3 -5
- 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 +32 -28
- package/src/app/extend/context.ts +303 -0
- 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 +26 -7
- package/src/config/config.default.ts +4 -3
- package/src/config/plugin.ts +2 -2
- package/src/index.ts +5 -3
- package/src/lib/application.ts +14 -21
- package/src/lib/core/base_context_class.ts +2 -2
- package/src/lib/core/base_hook_class.ts +1 -1
- package/src/lib/core/context_httpclient.ts +3 -3
- package/src/lib/core/httpclient.ts +4 -5
- package/src/lib/core/messenger/IMessenger.ts +3 -3
- package/src/lib/core/messenger/index.ts +1 -1
- package/src/lib/core/messenger/ipc.ts +31 -20
- package/src/lib/core/messenger/local.ts +13 -11
- package/src/lib/egg.ts +60 -28
- package/src/lib/egg.types.ts +6 -0
- package/src/lib/type.ts +6 -6
- package/src/lib/utils.ts +16 -0
- package/src/app/extend/context.js +0 -285
- package/src/app/extend/response.js +0 -101
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
|
|
2
|
-
import type {
|
|
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:
|
|
11
|
+
declare ctx: ContextDelegation;
|
|
12
12
|
protected pathName?: string;
|
|
13
13
|
#logger?: BaseContextLogger;
|
|
14
14
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
7
|
+
ctx: ContextDelegation;
|
|
8
8
|
app: EggApplicationCore;
|
|
9
9
|
|
|
10
|
-
constructor(ctx:
|
|
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?:
|
|
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
|
|
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}
|
|
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(
|
|
19
|
+
sendTo(workerId: string, action: string, data?: unknown): IMessenger;
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* send message to one app worker by random
|
|
@@ -50,7 +50,7 @@ export interface IMessenger extends EventEmitter {
|
|
|
50
50
|
* @param {String} to - let master know how to send message
|
|
51
51
|
* @return {Messenger} this
|
|
52
52
|
*/
|
|
53
|
-
send(action: string, data: unknown | undefined, to
|
|
53
|
+
send(action: string, data: unknown | undefined, to?: string): IMessenger;
|
|
54
54
|
|
|
55
55
|
close(): void;
|
|
56
56
|
|
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
|
|
2
1
|
import { EventEmitter } from 'node:events';
|
|
3
2
|
import { debuglog } from 'node:util';
|
|
4
3
|
import workerThreads from 'node:worker_threads';
|
|
5
|
-
import sendmessage from 'sendmessage';
|
|
4
|
+
import { sendmessage } from 'sendmessage';
|
|
6
5
|
import type { IMessenger } from './IMessenger.js';
|
|
6
|
+
import type { EggApplicationCore } from '../../egg.js';
|
|
7
7
|
|
|
8
|
-
const debug = debuglog('egg
|
|
8
|
+
const debug = debuglog('egg/lib/core/messenger/ipc');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Communication between app worker and agent worker by IPC channel
|
|
12
12
|
*/
|
|
13
13
|
export class Messenger extends EventEmitter implements IMessenger {
|
|
14
14
|
readonly pid: string;
|
|
15
|
+
readonly egg: EggApplicationCore;
|
|
15
16
|
opids: string[] = [];
|
|
16
17
|
|
|
17
|
-
constructor() {
|
|
18
|
+
constructor(egg: EggApplicationCore) {
|
|
18
19
|
super();
|
|
19
20
|
this.pid = String(process.pid);
|
|
21
|
+
this.egg = egg;
|
|
20
22
|
// pids of agent or app managed by master
|
|
21
23
|
// - retrieve app worker pids when it's an agent worker
|
|
22
24
|
// - retrieve agent worker pids when it's an app worker
|
|
23
|
-
this.on('egg-pids',
|
|
24
|
-
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));
|
|
25
28
|
});
|
|
26
29
|
this.onMessage = this.onMessage.bind(this);
|
|
27
30
|
process.on('message', this.onMessage);
|
|
@@ -37,7 +40,7 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
37
40
|
* @return {Messenger} this
|
|
38
41
|
*/
|
|
39
42
|
broadcast(action: string, data?: unknown): Messenger {
|
|
40
|
-
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);
|
|
41
44
|
this.send(action, data, 'app');
|
|
42
45
|
this.send(action, data, 'agent');
|
|
43
46
|
return this;
|
|
@@ -45,17 +48,21 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* send message to the specified process
|
|
48
|
-
* @param {String}
|
|
51
|
+
* @param {String} workerId - the workerId of the receiver
|
|
49
52
|
* @param {String} action - message key
|
|
50
53
|
* @param {Object} data - message value
|
|
51
54
|
* @return {Messenger} this
|
|
52
55
|
*/
|
|
53
|
-
sendTo(
|
|
54
|
-
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);
|
|
55
58
|
sendmessage(process, {
|
|
56
59
|
action,
|
|
57
60
|
data,
|
|
58
|
-
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated Keep compatible, please use receiverWorkerId instead
|
|
63
|
+
*/
|
|
64
|
+
receiverPid: String(workerId),
|
|
65
|
+
receiverWorkerId: String(workerId),
|
|
59
66
|
});
|
|
60
67
|
return this;
|
|
61
68
|
}
|
|
@@ -69,11 +76,13 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
69
76
|
* @return {Messenger} this
|
|
70
77
|
*/
|
|
71
78
|
sendRandom(action: string, data?: unknown): Messenger {
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
if (this.opids.length === 0) {
|
|
80
|
+
debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
74
83
|
const index = Math.floor(Math.random() * this.opids.length);
|
|
75
|
-
const
|
|
76
|
-
this.sendTo(
|
|
84
|
+
const workerId = this.opids[index];
|
|
85
|
+
this.sendTo(workerId, action, data);
|
|
77
86
|
return this;
|
|
78
87
|
}
|
|
79
88
|
|
|
@@ -84,7 +93,7 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
84
93
|
* @return {Messenger} this
|
|
85
94
|
*/
|
|
86
95
|
sendToApp(action: string, data?: unknown): Messenger {
|
|
87
|
-
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);
|
|
88
97
|
this.send(action, data, 'app');
|
|
89
98
|
return this;
|
|
90
99
|
}
|
|
@@ -96,7 +105,7 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
96
105
|
* @return {Messenger} this
|
|
97
106
|
*/
|
|
98
107
|
sendToAgent(action: string, data?: unknown): Messenger {
|
|
99
|
-
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);
|
|
100
109
|
this.send(action, data, 'agent');
|
|
101
110
|
return this;
|
|
102
111
|
}
|
|
@@ -107,7 +116,7 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
107
116
|
* @param {String} to - let master know how to send message
|
|
108
117
|
* @return {Messenger} this
|
|
109
118
|
*/
|
|
110
|
-
send(action: string, data: unknown | undefined, to
|
|
119
|
+
send(action: string, data: unknown | undefined, to?: string): Messenger {
|
|
111
120
|
sendmessage(process, {
|
|
112
121
|
action,
|
|
113
122
|
data,
|
|
@@ -118,9 +127,11 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
118
127
|
|
|
119
128
|
onMessage(message: any) {
|
|
120
129
|
if (typeof message?.action === 'string') {
|
|
121
|
-
debug('[%s] got message %s with %j,
|
|
122
|
-
this.pid, message.action, message.data, 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);
|
|
123
132
|
this.emit(message.action, message.data);
|
|
133
|
+
} else {
|
|
134
|
+
debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
|
|
124
135
|
}
|
|
125
136
|
}
|
|
126
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
|
}
|
|
@@ -93,7 +93,7 @@ export class Messenger extends EventEmitter implements IMessenger {
|
|
|
93
93
|
* @param {String} to - let master know how to send message
|
|
94
94
|
* @return {Messenger} this
|
|
95
95
|
*/
|
|
96
|
-
send(action: string, data: unknown | undefined, to
|
|
96
|
+
send(action: string, data: unknown | undefined, to?: string): Messenger {
|
|
97
97
|
// use nextTick to keep it async as IPC messenger
|
|
98
98
|
process.nextTick(() => {
|
|
99
99
|
const { egg } = this;
|
|
@@ -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,12 +3,22 @@ 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';
|
|
11
|
-
import extend from 'extend2';
|
|
21
|
+
import { extend } from 'extend2';
|
|
12
22
|
import { EggContextLogger as ContextLogger, EggLoggers, EggLogger } from 'egg-logger';
|
|
13
23
|
import { Cookies as ContextCookies } from '@eggjs/cookies';
|
|
14
24
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -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;
|
|
@@ -144,7 +174,16 @@ export class EggApplicationCore extends EggCore {
|
|
|
144
174
|
this.messenger.once('egg-ready', () => {
|
|
145
175
|
this.lifecycle.triggerServerDidReady();
|
|
146
176
|
});
|
|
147
|
-
this.
|
|
177
|
+
this.lifecycle.registerBeforeStart(async () => {
|
|
178
|
+
await this.load();
|
|
179
|
+
}, 'load files');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @deprecated please use `options` property instead
|
|
184
|
+
*/
|
|
185
|
+
get _options() {
|
|
186
|
+
return this.options;
|
|
148
187
|
}
|
|
149
188
|
|
|
150
189
|
protected async loadConfig() {
|
|
@@ -212,7 +251,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
212
251
|
* - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
|
|
213
252
|
* @return {ClientWrapper} wrapper
|
|
214
253
|
*/
|
|
215
|
-
cluster(clientClass: unknown, options
|
|
254
|
+
cluster(clientClass: unknown, options?: object) {
|
|
216
255
|
const clientClassOptions = {
|
|
217
256
|
...this.config.clusterClient,
|
|
218
257
|
...options,
|
|
@@ -403,7 +442,6 @@ export class EggApplicationCore extends EggCore {
|
|
|
403
442
|
if (!(err instanceof Error)) {
|
|
404
443
|
const newError = new Error(String(err));
|
|
405
444
|
// err maybe an object, try to copy the name, message and stack to the new error instance
|
|
406
|
-
/* istanbul ignore else */
|
|
407
445
|
if (err) {
|
|
408
446
|
if (err.name) newError.name = err.name;
|
|
409
447
|
if (err.message) newError.message = err.message;
|
|
@@ -411,7 +449,6 @@ export class EggApplicationCore extends EggCore {
|
|
|
411
449
|
}
|
|
412
450
|
err = newError;
|
|
413
451
|
}
|
|
414
|
-
/* istanbul ignore else */
|
|
415
452
|
if (err.name === 'Error') {
|
|
416
453
|
err.name = 'unhandledRejectionError';
|
|
417
454
|
}
|
|
@@ -491,12 +528,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
491
528
|
}
|
|
492
529
|
|
|
493
530
|
get [EGG_PATH]() {
|
|
494
|
-
|
|
495
|
-
return path.dirname(__dirname);
|
|
496
|
-
}
|
|
497
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
498
|
-
// @ts-ignore
|
|
499
|
-
return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
531
|
+
return getSourceDirname();
|
|
500
532
|
}
|
|
501
533
|
|
|
502
534
|
#setupTimeoutTimer() {
|
|
@@ -538,7 +570,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
538
570
|
* @deprecated
|
|
539
571
|
*/
|
|
540
572
|
get proxy() {
|
|
541
|
-
this.deprecate('please use app.config.proxy instead');
|
|
573
|
+
// this.deprecate('please use app.config.proxy instead');
|
|
542
574
|
return this.config.proxy;
|
|
543
575
|
}
|
|
544
576
|
/* eslint no-empty-function: off */
|
|
@@ -580,7 +612,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
580
612
|
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
|
|
581
613
|
* @return {Context} context
|
|
582
614
|
*/
|
|
583
|
-
createAnonymousContext(req?: any):
|
|
615
|
+
createAnonymousContext(req?: any): EggContext {
|
|
584
616
|
const request: any = {
|
|
585
617
|
headers: {
|
|
586
618
|
host: '127.0.0.1',
|
|
@@ -624,7 +656,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
624
656
|
const context = Object.create(this.context) as EggContext;
|
|
625
657
|
const request = context.request = Object.create(this.request);
|
|
626
658
|
const response = context.response = Object.create(this.response);
|
|
627
|
-
context.app = request.app = response.app = this;
|
|
659
|
+
context.app = request.app = response.app = this as any;
|
|
628
660
|
context.req = request.req = response.req = req;
|
|
629
661
|
context.res = request.res = response.res = res;
|
|
630
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
|
/**
|
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
|
+
}
|