egg 4.0.0-beta.12 → 4.0.0-beta.13
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 +35 -7
- package/dist/commonjs/app/extend/context.js +24 -22
- package/dist/commonjs/app/extend/context.types.d.ts +4 -1
- package/dist/commonjs/app/extend/request.d.ts +6 -2
- package/dist/commonjs/app/extend/request.js +1 -1
- package/dist/commonjs/app/extend/request.types.d.ts +10 -0
- package/dist/commonjs/app/extend/request.types.js +3 -0
- package/dist/commonjs/app/extend/response.types.d.ts +7 -0
- package/dist/commonjs/app/extend/response.types.js +3 -0
- package/dist/commonjs/app/middleware/meta.d.ts +2 -2
- package/dist/commonjs/app/middleware/meta.js +1 -1
- package/dist/commonjs/app/middleware/notfound.d.ts +2 -2
- package/dist/commonjs/app/middleware/notfound.js +1 -1
- package/dist/commonjs/app/middleware/site_file.d.ts +3 -3
- package/dist/commonjs/app/middleware/site_file.js +1 -1
- package/dist/commonjs/config/config.default.d.ts +1 -1
- package/dist/commonjs/index.d.ts +11 -6
- package/dist/commonjs/index.js +11 -22
- package/dist/commonjs/lib/application.d.ts +7 -3
- package/dist/commonjs/lib/application.js +15 -4
- package/dist/commonjs/lib/core/base_context_class.d.ts +6 -3
- package/dist/commonjs/lib/core/base_context_class.js +1 -2
- package/dist/commonjs/lib/core/base_hook_class.d.ts +2 -2
- package/dist/commonjs/lib/core/base_hook_class.js +1 -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 +4 -3
- package/dist/commonjs/lib/core/httpclient.js +1 -1
- package/dist/commonjs/lib/core/messenger/base.d.ts +8 -0
- package/dist/commonjs/lib/core/messenger/base.js +31 -0
- package/dist/commonjs/lib/core/messenger/index.js +3 -2
- package/dist/commonjs/lib/core/messenger/ipc.d.ts +2 -3
- package/dist/commonjs/lib/core/messenger/ipc.js +4 -6
- package/dist/commonjs/lib/core/messenger/local.d.ts +2 -3
- package/dist/commonjs/lib/core/messenger/local.js +4 -9
- package/dist/commonjs/lib/core/utils.d.ts +1 -1
- package/dist/commonjs/lib/core/utils.js +3 -3
- package/dist/commonjs/lib/egg.d.ts +17 -11
- package/dist/commonjs/lib/egg.js +13 -4
- package/dist/commonjs/lib/egg.types.d.ts +8 -4
- package/dist/commonjs/lib/error/CookieLimitExceedError.d.ts +5 -0
- package/dist/commonjs/lib/error/CookieLimitExceedError.js +16 -0
- package/dist/commonjs/lib/error/MessageUnhandledRejectionError.d.ts +5 -0
- package/dist/commonjs/lib/error/MessageUnhandledRejectionError.js +16 -0
- package/dist/commonjs/lib/error/index.d.ts +2 -0
- package/dist/commonjs/lib/error/index.js +19 -0
- package/dist/commonjs/lib/{type.d.ts → types.d.ts} +36 -3
- package/dist/commonjs/lib/{type.js → types.js} +1 -1
- package/dist/esm/app/extend/context.d.ts +35 -7
- package/dist/esm/app/extend/context.js +23 -18
- package/dist/esm/app/extend/context.types.d.ts +4 -1
- package/dist/esm/app/extend/request.d.ts +6 -2
- package/dist/esm/app/extend/request.js +1 -1
- package/dist/esm/app/extend/request.types.d.ts +10 -0
- package/dist/esm/app/extend/request.types.js +2 -0
- package/dist/esm/app/extend/response.types.d.ts +7 -0
- package/dist/esm/app/extend/response.types.js +2 -0
- package/dist/esm/app/middleware/meta.d.ts +2 -2
- package/dist/esm/app/middleware/meta.js +1 -1
- package/dist/esm/app/middleware/notfound.d.ts +2 -2
- package/dist/esm/app/middleware/notfound.js +1 -1
- package/dist/esm/app/middleware/site_file.d.ts +3 -3
- package/dist/esm/app/middleware/site_file.js +1 -1
- package/dist/esm/config/config.default.d.ts +1 -1
- package/dist/esm/index.d.ts +11 -6
- package/dist/esm/index.js +12 -7
- package/dist/esm/lib/application.d.ts +7 -3
- package/dist/esm/lib/application.js +15 -4
- package/dist/esm/lib/core/base_context_class.d.ts +6 -3
- package/dist/esm/lib/core/base_context_class.js +1 -2
- package/dist/esm/lib/core/base_hook_class.d.ts +2 -2
- package/dist/esm/lib/core/base_hook_class.js +1 -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 +4 -3
- package/dist/esm/lib/core/httpclient.js +1 -1
- package/dist/esm/lib/core/messenger/base.d.ts +8 -0
- package/dist/esm/lib/core/messenger/base.js +27 -0
- package/dist/esm/lib/core/messenger/index.js +3 -2
- package/dist/esm/lib/core/messenger/ipc.d.ts +2 -3
- package/dist/esm/lib/core/messenger/ipc.js +4 -6
- package/dist/esm/lib/core/messenger/local.d.ts +2 -3
- package/dist/esm/lib/core/messenger/local.js +4 -6
- package/dist/esm/lib/core/utils.d.ts +1 -1
- package/dist/esm/lib/core/utils.js +3 -3
- package/dist/esm/lib/egg.d.ts +17 -11
- package/dist/esm/lib/egg.js +14 -4
- package/dist/esm/lib/egg.types.d.ts +8 -4
- package/dist/esm/lib/error/CookieLimitExceedError.d.ts +5 -0
- package/dist/esm/lib/error/CookieLimitExceedError.js +12 -0
- package/dist/esm/lib/error/MessageUnhandledRejectionError.d.ts +5 -0
- package/dist/esm/lib/error/MessageUnhandledRejectionError.js +12 -0
- package/dist/esm/lib/error/index.d.ts +2 -0
- package/dist/esm/lib/error/index.js +3 -0
- package/dist/esm/lib/{type.d.ts → types.d.ts} +36 -3
- package/dist/esm/lib/types.js +2 -0
- package/dist/package.json +1 -1
- package/package.json +5 -7
- package/src/app/extend/context.ts +33 -27
- package/src/app/extend/context.types.ts +4 -1
- package/src/app/extend/request.ts +7 -2
- package/src/app/extend/request.types.ts +10 -0
- package/src/app/extend/response.types.ts +7 -0
- package/src/app/middleware/meta.ts +2 -2
- package/src/app/middleware/notfound.ts +2 -2
- package/src/app/middleware/site_file.ts +3 -3
- package/src/config/config.default.ts +1 -1
- package/src/config/config.local.ts +1 -1
- package/src/config/config.unittest.ts +1 -1
- package/src/index.ts +33 -6
- package/src/lib/agent.ts +1 -1
- package/src/lib/application.ts +18 -6
- package/src/lib/core/base_context_class.ts +6 -3
- package/src/lib/core/base_hook_class.ts +2 -2
- package/src/lib/core/context_httpclient.ts +3 -3
- package/src/lib/core/httpclient.ts +10 -4
- package/src/lib/core/messenger/base.ts +30 -0
- package/src/lib/core/messenger/index.ts +2 -1
- package/src/lib/core/messenger/ipc.ts +3 -5
- package/src/lib/core/messenger/local.ts +3 -5
- package/src/lib/core/utils.ts +2 -2
- package/src/lib/egg.ts +23 -12
- package/src/lib/egg.types.ts +8 -4
- package/src/lib/error/CookieLimitExceedError.ts +12 -0
- package/src/lib/error/MessageUnhandledRejectionError.ts +12 -0
- package/src/lib/error/index.ts +2 -0
- package/src/lib/{type.ts → types.ts} +41 -3
- package/dist/esm/lib/type.js +0 -2
package/src/index.ts
CHANGED
|
@@ -7,13 +7,32 @@ import { startEgg } from './lib/start.js';
|
|
|
7
7
|
import Helper from './app/extend/helper.js';
|
|
8
8
|
|
|
9
9
|
// export extends
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
Helper,
|
|
12
|
+
};
|
|
13
|
+
export type {
|
|
14
|
+
// keep compatible with egg v3
|
|
15
|
+
Helper as IHelper,
|
|
16
|
+
};
|
|
11
17
|
|
|
12
18
|
// export types
|
|
13
19
|
export * from './lib/egg.js';
|
|
14
|
-
export * from './lib/
|
|
20
|
+
export * from './lib/types.js';
|
|
15
21
|
export * from './lib/start.js';
|
|
16
22
|
|
|
23
|
+
// export errors
|
|
24
|
+
export * from './lib/error/index.js';
|
|
25
|
+
|
|
26
|
+
// export loggers
|
|
27
|
+
export type {
|
|
28
|
+
LoggerLevel,
|
|
29
|
+
EggLogger,
|
|
30
|
+
} from 'egg-logger';
|
|
31
|
+
|
|
32
|
+
// export httpClients
|
|
33
|
+
export * from './lib/core/httpclient.js';
|
|
34
|
+
export * from './lib/core/context_httpclient.js';
|
|
35
|
+
|
|
17
36
|
/**
|
|
18
37
|
* Start egg application with cluster mode
|
|
19
38
|
* @since 1.0.0
|
|
@@ -24,7 +43,9 @@ export * from '@eggjs/cluster';
|
|
|
24
43
|
* Start egg application with single process mode
|
|
25
44
|
* @since 1.0.0
|
|
26
45
|
*/
|
|
27
|
-
export
|
|
46
|
+
export {
|
|
47
|
+
startEgg as start,
|
|
48
|
+
};
|
|
28
49
|
|
|
29
50
|
/**
|
|
30
51
|
* @member {Application} Egg#Application
|
|
@@ -54,19 +75,25 @@ export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';
|
|
|
54
75
|
* @member {Controller} Egg#Controller
|
|
55
76
|
* @since 1.1.0
|
|
56
77
|
*/
|
|
57
|
-
export
|
|
78
|
+
export {
|
|
79
|
+
BaseContextClass as Controller,
|
|
80
|
+
};
|
|
58
81
|
|
|
59
82
|
/**
|
|
60
83
|
* @member {Service} Egg#Service
|
|
61
84
|
* @since 1.1.0
|
|
62
85
|
*/
|
|
63
|
-
export
|
|
86
|
+
export {
|
|
87
|
+
BaseContextClass as Service,
|
|
88
|
+
};
|
|
64
89
|
|
|
65
90
|
/**
|
|
66
91
|
* @member {Subscription} Egg#Subscription
|
|
67
92
|
* @since 1.10.0
|
|
68
93
|
*/
|
|
69
|
-
export
|
|
94
|
+
export {
|
|
95
|
+
BaseContextClass as Subscription,
|
|
96
|
+
};
|
|
70
97
|
|
|
71
98
|
/**
|
|
72
99
|
* @member {BaseContextClass} Egg#BaseContextClass
|
package/src/lib/agent.ts
CHANGED
package/src/lib/application.ts
CHANGED
|
@@ -5,13 +5,15 @@ import { Socket } from 'node:net';
|
|
|
5
5
|
import { graceful } from 'graceful';
|
|
6
6
|
import { assign } from 'utility';
|
|
7
7
|
import { utils as eggUtils } from '@eggjs/core';
|
|
8
|
+
import { isGeneratorFunction } from 'is-type-of';
|
|
8
9
|
import {
|
|
9
10
|
EggApplicationCore,
|
|
10
11
|
type EggApplicationCoreOptions,
|
|
11
|
-
type
|
|
12
|
+
type Context,
|
|
12
13
|
} from './egg.js';
|
|
13
14
|
import { AppWorkerLoader } from './loader/index.js';
|
|
14
15
|
import Helper from '../app/extend/helper.js';
|
|
16
|
+
import { CookieLimitExceedError } from './error/index.js';
|
|
15
17
|
|
|
16
18
|
const EGG_LOADER = Symbol.for('egg#loader');
|
|
17
19
|
|
|
@@ -221,7 +223,7 @@ export class Application extends EggApplicationCore {
|
|
|
221
223
|
* @see Context#runInBackground
|
|
222
224
|
* @param {Function} scope - the first args is an anonymous ctx
|
|
223
225
|
*/
|
|
224
|
-
runInBackground(scope: (ctx:
|
|
226
|
+
runInBackground(scope: (ctx: Context) => Promise<void>, req?: unknown) {
|
|
225
227
|
const ctx = this.createAnonymousContext(req);
|
|
226
228
|
if (!scope.name) {
|
|
227
229
|
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
|
|
@@ -237,7 +239,7 @@ export class Application extends EggApplicationCore {
|
|
|
237
239
|
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
|
|
238
240
|
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
|
|
239
241
|
*/
|
|
240
|
-
async runInAnonymousContextScope(scope: (ctx:
|
|
242
|
+
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
|
|
241
243
|
const ctx = this.createAnonymousContext(req);
|
|
242
244
|
if (!scope.name) {
|
|
243
245
|
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
|
|
@@ -266,16 +268,26 @@ export class Application extends EggApplicationCore {
|
|
|
266
268
|
return this._keys;
|
|
267
269
|
}
|
|
268
270
|
|
|
271
|
+
/**
|
|
272
|
+
* @deprecated keep compatible with egg 3.x
|
|
273
|
+
*/
|
|
274
|
+
toAsyncFunction(fn: (...args: any[]) => any) {
|
|
275
|
+
if (isGeneratorFunction(fn)) {
|
|
276
|
+
throw new Error('Generator function is not supported');
|
|
277
|
+
}
|
|
278
|
+
return fn;
|
|
279
|
+
}
|
|
280
|
+
|
|
269
281
|
/**
|
|
270
282
|
* bind app's events
|
|
271
283
|
*
|
|
272
284
|
* @private
|
|
273
285
|
*/
|
|
274
286
|
#bindEvents() {
|
|
275
|
-
// Browser Cookie Limits: http://browsercookielimits.
|
|
287
|
+
// Browser Cookie Limits: http://browsercookielimits.iain.guru/
|
|
288
|
+
// https://github.com/eggjs/egg-cookies/blob/58ef4ea497a0eb4dd711d7e9751e56bc5fcee004/src/cookies.ts#L145
|
|
276
289
|
this.on('cookieLimitExceed', ({ name, value, ctx }) => {
|
|
277
|
-
const err = new
|
|
278
|
-
err.name = 'CookieLimitExceedError';
|
|
290
|
+
const err = new CookieLimitExceedError(name, value);
|
|
279
291
|
ctx.coreLogger.error(err);
|
|
280
292
|
});
|
|
281
293
|
// expose server to support websocket
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Context, EggApplicationCore } from '../egg.js';
|
|
3
3
|
import { BaseContextLogger } from './base_context_logger.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -8,8 +8,11 @@ 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
|
-
|
|
12
|
-
|
|
11
|
+
[key: string | symbol]: any;
|
|
12
|
+
declare ctx: Context;
|
|
13
|
+
declare pathName?: string;
|
|
14
|
+
declare app: EggApplicationCore;
|
|
15
|
+
declare service: BaseContextClass;
|
|
13
16
|
#logger?: BaseContextLogger;
|
|
14
17
|
|
|
15
18
|
get logger() {
|
|
@@ -3,14 +3,14 @@ import type { ILifecycleBoot } from '@eggjs/core';
|
|
|
3
3
|
import type { Application, Agent } from '../../index.js';
|
|
4
4
|
|
|
5
5
|
export class BaseHookClass implements ILifecycleBoot {
|
|
6
|
-
fullPath?: string;
|
|
6
|
+
declare fullPath?: string;
|
|
7
7
|
#instance: Application | Agent;
|
|
8
8
|
|
|
9
9
|
constructor(instance: Application | Agent) {
|
|
10
10
|
this.#instance = instance;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
get logger()
|
|
13
|
+
get logger() {
|
|
14
14
|
return this.#instance.logger;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context, 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: Context;
|
|
8
8
|
app: EggApplicationCore;
|
|
9
9
|
|
|
10
|
-
constructor(ctx:
|
|
10
|
+
constructor(ctx: Context) {
|
|
11
11
|
this.ctx = ctx;
|
|
12
12
|
this.app = ctx.app;
|
|
13
13
|
}
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
RequestOptions,
|
|
5
5
|
} from 'urllib';
|
|
6
6
|
import { ms } from 'humanize-ms';
|
|
7
|
-
import type { EggApplicationCore
|
|
7
|
+
import type { EggApplicationCore } from '../egg.js';
|
|
8
8
|
|
|
9
9
|
export type {
|
|
10
10
|
HttpClientResponse,
|
|
@@ -12,12 +12,12 @@ export type {
|
|
|
12
12
|
} from 'urllib';
|
|
13
13
|
|
|
14
14
|
export interface HttpClientRequestOptions extends RequestOptions {
|
|
15
|
-
ctx?:
|
|
16
|
-
tracer?:
|
|
15
|
+
ctx?: any;
|
|
16
|
+
tracer?: any;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export class HttpClient extends RawHttpClient {
|
|
20
|
-
readonly #app: EggApplicationCore & { tracer?:
|
|
20
|
+
readonly #app: EggApplicationCore & { tracer?: any };
|
|
21
21
|
|
|
22
22
|
constructor(app: EggApplicationCore) {
|
|
23
23
|
normalizeConfig(app);
|
|
@@ -43,6 +43,12 @@ export class HttpClient extends RawHttpClient {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
// keep compatible
|
|
47
|
+
export type {
|
|
48
|
+
HttpClient as EggHttpClient,
|
|
49
|
+
HttpClient as EggContextHttpClient,
|
|
50
|
+
};
|
|
51
|
+
|
|
46
52
|
function normalizeConfig(app: EggApplicationCore) {
|
|
47
53
|
const config = app.config.httpclient;
|
|
48
54
|
if (typeof config.request?.timeout === 'string') {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EventEmitter, captureRejectionSymbol } from 'node:events';
|
|
2
|
+
import { MessageUnhandledRejectionError } from '../../error/index.js';
|
|
3
|
+
import { EggApplicationCore } from '../../egg.js';
|
|
4
|
+
|
|
5
|
+
export class BaseMessenger extends EventEmitter {
|
|
6
|
+
protected readonly egg: EggApplicationCore;
|
|
7
|
+
|
|
8
|
+
constructor(egg: EggApplicationCore) {
|
|
9
|
+
super({ captureRejections: true });
|
|
10
|
+
this.egg = egg;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
[captureRejectionSymbol](err: Error, event: string | symbol, ...args: any[]) {
|
|
14
|
+
this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
emit(eventName: string | symbol, ...args: any[]): boolean {
|
|
18
|
+
const hasListeners = this.listenerCount(eventName) > 0;
|
|
19
|
+
try {
|
|
20
|
+
return super.emit(eventName, ...args);
|
|
21
|
+
} catch (e: unknown) {
|
|
22
|
+
let err = e as Error;
|
|
23
|
+
if (!(err instanceof Error)) {
|
|
24
|
+
err = new Error(String(err));
|
|
25
|
+
}
|
|
26
|
+
this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args));
|
|
27
|
+
return hasListeners;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -9,7 +9,8 @@ export type { IMessenger } from './IMessenger.js';
|
|
|
9
9
|
* @class Messenger
|
|
10
10
|
*/
|
|
11
11
|
export function create(egg: EggApplicationCore): IMessenger {
|
|
12
|
-
|
|
12
|
+
const messenger = egg.options.mode === 'single'
|
|
13
13
|
? new LocalMessenger(egg)
|
|
14
14
|
: new IPCMessenger(egg);
|
|
15
|
+
return messenger;
|
|
15
16
|
}
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
1
|
import { debuglog } from 'node:util';
|
|
3
2
|
import workerThreads from 'node:worker_threads';
|
|
4
3
|
import { sendmessage } from 'sendmessage';
|
|
5
4
|
import type { IMessenger } from './IMessenger.js';
|
|
6
5
|
import type { EggApplicationCore } from '../../egg.js';
|
|
6
|
+
import { BaseMessenger } from './base.js';
|
|
7
7
|
|
|
8
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
|
-
export class Messenger extends
|
|
13
|
+
export class Messenger extends BaseMessenger implements IMessenger {
|
|
14
14
|
readonly pid: string;
|
|
15
|
-
readonly egg: EggApplicationCore;
|
|
16
15
|
opids: string[] = [];
|
|
17
16
|
|
|
18
17
|
constructor(egg: EggApplicationCore) {
|
|
19
|
-
super();
|
|
18
|
+
super(egg);
|
|
20
19
|
this.pid = String(process.pid);
|
|
21
|
-
this.egg = egg;
|
|
22
20
|
// pids of agent or app managed by master
|
|
23
21
|
// - retrieve app worker pids when it's an agent worker
|
|
24
22
|
// - retrieve agent worker pids when it's an app worker
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { debuglog } from 'node:util';
|
|
2
|
-
import EventEmitter from 'node:events';
|
|
3
2
|
import type { IMessenger } from './IMessenger.js';
|
|
4
3
|
import type { EggApplicationCore } from '../../egg.js';
|
|
4
|
+
import { BaseMessenger } from './base.js';
|
|
5
5
|
|
|
6
6
|
const debug = debuglog('egg/lib/core/messenger/local');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Communication between app worker and agent worker with EventEmitter
|
|
10
10
|
*/
|
|
11
|
-
export class Messenger extends
|
|
11
|
+
export class Messenger extends BaseMessenger implements IMessenger {
|
|
12
12
|
readonly pid: string;
|
|
13
|
-
readonly egg: EggApplicationCore;
|
|
14
13
|
|
|
15
14
|
constructor(egg: EggApplicationCore) {
|
|
16
|
-
super();
|
|
17
|
-
this.egg = egg;
|
|
15
|
+
super(egg);
|
|
18
16
|
this.pid = String(process.pid);
|
|
19
17
|
}
|
|
20
18
|
|
package/src/lib/core/utils.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
isClass, isFunction, isGeneratorFunction, isAsyncFunction,
|
|
5
5
|
} from 'is-type-of';
|
|
6
6
|
|
|
7
|
-
export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[]) {
|
|
7
|
+
export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[] = []) {
|
|
8
8
|
if (!Array.isArray(ignore)) {
|
|
9
9
|
ignore = [ ignore ];
|
|
10
10
|
}
|
|
@@ -30,7 +30,7 @@ function convertValue(key: string, value: any, ignore: (string | RegExp)[]) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
if (!hit) {
|
|
33
|
-
if (isSymbol(value) || isRegExp(value)) {
|
|
33
|
+
if (isSymbol(value) || isRegExp(value) || value instanceof URL) {
|
|
34
34
|
return value.toString();
|
|
35
35
|
}
|
|
36
36
|
if (isPrimitive(value) || Array.isArray(value)) {
|
package/src/lib/egg.ts
CHANGED
|
@@ -26,8 +26,8 @@ import { Cookies as ContextCookies } from '@eggjs/cookies';
|
|
|
26
26
|
import CircularJSON from 'circular-json-for-egg';
|
|
27
27
|
import type { Agent } from './agent.js';
|
|
28
28
|
import type { Application } from './application.js';
|
|
29
|
-
import Context
|
|
30
|
-
import type { EggAppConfig } from './
|
|
29
|
+
import Context from '../app/extend/context.js';
|
|
30
|
+
import type { EggAppConfig } from './types.js';
|
|
31
31
|
import { create as createMessenger, IMessenger } from './core/messenger/index.js';
|
|
32
32
|
import { ContextHttpClient } from './core/context_httpclient.js';
|
|
33
33
|
import {
|
|
@@ -56,30 +56,30 @@ export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir
|
|
|
56
56
|
export class Request extends EggCoreRequest {
|
|
57
57
|
declare app: EggCore;
|
|
58
58
|
declare response: Response;
|
|
59
|
-
declare ctx:
|
|
59
|
+
declare ctx: Context;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
export class Response extends EggCoreResponse {
|
|
63
63
|
declare app: EggCore;
|
|
64
64
|
declare request: Request;
|
|
65
|
-
declare ctx:
|
|
65
|
+
declare ctx: Context;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// export egg types
|
|
69
69
|
export type {
|
|
70
|
-
ContextDelegation,
|
|
71
70
|
ILifecycleBoot,
|
|
71
|
+
// keep compatible with egg version 3.x
|
|
72
|
+
ILifecycleBoot as IBoot,
|
|
72
73
|
Next,
|
|
73
74
|
};
|
|
74
75
|
// keep compatible with egg version 3.x
|
|
75
|
-
export type EggContext =
|
|
76
|
-
export type MiddlewareFunc<T extends
|
|
76
|
+
export type EggContext = Context;
|
|
77
|
+
export type MiddlewareFunc<T extends Context = Context> = EggCoreMiddlewareFunc<T>;
|
|
77
78
|
|
|
78
79
|
// export egg classes
|
|
79
80
|
export {
|
|
80
81
|
Context,
|
|
81
82
|
Router,
|
|
82
|
-
EggLogger,
|
|
83
83
|
};
|
|
84
84
|
|
|
85
85
|
/**
|
|
@@ -89,12 +89,14 @@ export {
|
|
|
89
89
|
* @augments EggCore
|
|
90
90
|
*/
|
|
91
91
|
export class EggApplicationCore extends EggCore {
|
|
92
|
-
declare ctxStorage: AsyncLocalStorage<
|
|
92
|
+
declare ctxStorage: AsyncLocalStorage<Context>;
|
|
93
93
|
// export context base classes, let framework can impl sub class and over context extend easily.
|
|
94
94
|
ContextCookies = ContextCookies;
|
|
95
95
|
ContextLogger = ContextLogger;
|
|
96
96
|
ContextHttpClient = ContextHttpClient;
|
|
97
97
|
HttpClient = HttpClient;
|
|
98
|
+
// keep compatible with egg version 3.x
|
|
99
|
+
HttpClientNext = HttpClient;
|
|
98
100
|
/**
|
|
99
101
|
* Retrieve base context class
|
|
100
102
|
* @member {BaseContextClass} BaseContextClass
|
|
@@ -132,6 +134,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
132
134
|
/**
|
|
133
135
|
* Retrieve base boot
|
|
134
136
|
* @member {Boot}
|
|
137
|
+
* @alias BaseHookClass
|
|
135
138
|
*/
|
|
136
139
|
Boot = BaseHookClass;
|
|
137
140
|
|
|
@@ -233,6 +236,13 @@ export class EggApplicationCore extends EggCore {
|
|
|
233
236
|
await this.loader.load();
|
|
234
237
|
}
|
|
235
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Usage: new ApiClient({ cluster: app.cluster })
|
|
241
|
+
*/
|
|
242
|
+
get cluster() {
|
|
243
|
+
return this.clusterWrapper.bind(this);
|
|
244
|
+
}
|
|
245
|
+
|
|
236
246
|
/**
|
|
237
247
|
* Wrap the Client with Leader/Follower Pattern
|
|
238
248
|
*
|
|
@@ -251,7 +261,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
251
261
|
* - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
|
|
252
262
|
* @return {ClientWrapper} wrapper
|
|
253
263
|
*/
|
|
254
|
-
|
|
264
|
+
clusterWrapper(clientClass: unknown, options?: object) {
|
|
255
265
|
const clientClassOptions = {
|
|
256
266
|
...this.config.clusterClient,
|
|
257
267
|
...options,
|
|
@@ -439,6 +449,7 @@ export class EggApplicationCore extends EggCore {
|
|
|
439
449
|
}
|
|
440
450
|
|
|
441
451
|
_unhandledRejectionHandler(err: any) {
|
|
452
|
+
this.coreLogger.error('[egg:unhandledRejection] %s', err && err.message || err);
|
|
442
453
|
if (!(err instanceof Error)) {
|
|
443
454
|
const newError = new Error(String(err));
|
|
444
455
|
// err maybe an object, try to copy the name, message and stack to the new error instance
|
|
@@ -652,8 +663,8 @@ export class EggApplicationCore extends EggCore {
|
|
|
652
663
|
* @param {Res} res - node native Response object
|
|
653
664
|
* @return {Context} context object
|
|
654
665
|
*/
|
|
655
|
-
createContext(req: IncomingMessage, res: ServerResponse):
|
|
656
|
-
const context = Object.create(this.context) as
|
|
666
|
+
createContext(req: IncomingMessage, res: ServerResponse): Context {
|
|
667
|
+
const context = Object.create(this.context) as Context;
|
|
657
668
|
const request = context.request = Object.create(this.request);
|
|
658
669
|
const response = context.response = Object.create(this.response);
|
|
659
670
|
context.app = request.app = response.app = this as any;
|
package/src/lib/egg.types.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import
|
|
1
|
+
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import type EggContext from '../app/extend/context.js';
|
|
3
|
+
import type { EggLogger } from 'egg-logger';
|
|
3
4
|
|
|
4
5
|
declare module '@eggjs/core' {
|
|
5
6
|
// add EggApplicationCore overrides types
|
|
6
7
|
interface EggCore {
|
|
7
8
|
inspect(): any;
|
|
8
|
-
get currentContext():
|
|
9
|
-
ctxStorage: AsyncLocalStorage<
|
|
9
|
+
get currentContext(): EggContext | undefined;
|
|
10
|
+
ctxStorage: AsyncLocalStorage<EggContext>;
|
|
11
|
+
get logger(): EggLogger;
|
|
12
|
+
get coreLogger(): EggLogger;
|
|
13
|
+
getLogger(name: string): EggLogger;
|
|
10
14
|
}
|
|
11
15
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class CookieLimitExceedError extends Error {
|
|
2
|
+
key: string;
|
|
3
|
+
cookie: string;
|
|
4
|
+
|
|
5
|
+
constructor(key: string, cookie: string) {
|
|
6
|
+
super(`cookie ${key}'s length(${cookie.length}) exceed the limit(4093)`);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
this.key = key;
|
|
9
|
+
this.cookie = cookie;
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class MessageUnhandledRejectionError extends Error {
|
|
2
|
+
event: string | symbol;
|
|
3
|
+
args: any[];
|
|
4
|
+
|
|
5
|
+
constructor(err: Error, event: string | symbol, ...args: any[]) {
|
|
6
|
+
super(`event: ${String(event)}, error: ${err.message}`, { cause: err });
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
this.event = event;
|
|
9
|
+
this.args = args;
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
FileLoaderOptions,
|
|
10
10
|
} from '@eggjs/core';
|
|
11
11
|
import type {
|
|
12
|
-
EggApplicationCore,
|
|
12
|
+
EggApplicationCore, Context,
|
|
13
13
|
} from './egg.js';
|
|
14
14
|
import type { MetaMiddlewareOptions } from '../app/middleware/meta.js';
|
|
15
15
|
import type { NotFoundMiddlewareOptions } from '../app/middleware/notfound.js';
|
|
@@ -18,7 +18,7 @@ import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
|
|
|
18
18
|
// import @eggjs/watcher types
|
|
19
19
|
// import '@eggjs/watcher';
|
|
20
20
|
|
|
21
|
-
type IgnoreItem = string | RegExp | ((ctx:
|
|
21
|
+
type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
|
|
22
22
|
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
|
|
23
23
|
|
|
24
24
|
export interface ClientErrorResponse {
|
|
@@ -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: Context): void;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
/**
|
|
@@ -136,6 +136,10 @@ export interface EggAppConfig {
|
|
|
136
136
|
timeout?: number;
|
|
137
137
|
/** Default request args for httpclient */
|
|
138
138
|
request?: HttpClientRequestOptions;
|
|
139
|
+
/**
|
|
140
|
+
* @deprecated keep compatible with egg 3.x, no more used
|
|
141
|
+
*/
|
|
142
|
+
useHttpClientNext?: boolean;
|
|
139
143
|
};
|
|
140
144
|
|
|
141
145
|
development: {
|
|
@@ -327,3 +331,37 @@ export interface EggAppConfig {
|
|
|
327
331
|
|
|
328
332
|
[prop: string]: any;
|
|
329
333
|
}
|
|
334
|
+
|
|
335
|
+
export type RequestObjectBody = Record<string, any>;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* plugin config item interface
|
|
339
|
+
*/
|
|
340
|
+
export interface IEggPluginItem {
|
|
341
|
+
env?: EggEnvType[];
|
|
342
|
+
path?: string;
|
|
343
|
+
package?: string;
|
|
344
|
+
enable?: boolean;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export type EggPluginItem = IEggPluginItem | boolean;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* build-in plugin list
|
|
351
|
+
*/
|
|
352
|
+
export interface EggPlugin {
|
|
353
|
+
[key: string]: EggPluginItem | undefined;
|
|
354
|
+
onerror?: EggPluginItem;
|
|
355
|
+
session?: EggPluginItem;
|
|
356
|
+
i18n?: EggPluginItem;
|
|
357
|
+
watcher?: EggPluginItem;
|
|
358
|
+
multipart?: EggPluginItem;
|
|
359
|
+
security?: EggPluginItem;
|
|
360
|
+
development?: EggPluginItem;
|
|
361
|
+
logrotator?: EggPluginItem;
|
|
362
|
+
schedule?: EggPluginItem;
|
|
363
|
+
static?: EggPluginItem;
|
|
364
|
+
jsonp?: EggPluginItem;
|
|
365
|
+
view?: EggPluginItem;
|
|
366
|
+
}
|
|
367
|
+
|
package/dist/esm/lib/type.js
DELETED