@eggjs/core 6.5.0 → 6.6.0-beta.11
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 -5
- package/dist/base_context_class.d.ts +21 -0
- package/dist/base_context_class.js +40 -0
- package/dist/egg.d.ts +248 -0
- package/dist/egg.js +358 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/lifecycle.d.ts +84 -0
- package/dist/lifecycle.js +280 -0
- package/dist/loader/context_loader.d.ts +39 -0
- package/dist/loader/context_loader.js +79 -0
- package/dist/loader/egg_loader.d.ts +374 -0
- package/dist/loader/egg_loader.js +1184 -0
- package/dist/loader/file_loader.d.ts +105 -0
- package/dist/loader/file_loader.js +198 -0
- package/dist/singleton.d.ts +33 -0
- package/dist/singleton.js +107 -0
- package/{src/types.ts → dist/types.d.ts} +7 -7
- package/dist/utils/index.d.ts +19 -0
- package/dist/utils/index.js +103 -0
- package/dist/utils/sequencify.d.ts +16 -0
- package/dist/utils/sequencify.js +46 -0
- package/dist/utils/timing.d.ts +24 -0
- package/dist/utils/timing.js +85 -0
- package/package.json +39 -75
- package/dist/commonjs/base_context_class.d.ts +0 -16
- package/dist/commonjs/base_context_class.js +0 -41
- package/dist/commonjs/egg.d.ts +0 -246
- package/dist/commonjs/egg.js +0 -401
- package/dist/commonjs/index.d.ts +0 -12
- package/dist/commonjs/index.js +0 -32
- package/dist/commonjs/lifecycle.d.ts +0 -78
- package/dist/commonjs/lifecycle.js +0 -315
- package/dist/commonjs/loader/context_loader.d.ts +0 -35
- package/dist/commonjs/loader/context_loader.js +0 -110
- package/dist/commonjs/loader/egg_loader.d.ts +0 -369
- package/dist/commonjs/loader/egg_loader.js +0 -1558
- package/dist/commonjs/loader/file_loader.d.ts +0 -100
- package/dist/commonjs/loader/file_loader.js +0 -260
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/singleton.d.ts +0 -29
- package/dist/commonjs/singleton.js +0 -124
- package/dist/commonjs/types.d.ts +0 -53
- package/dist/commonjs/types.js +0 -3
- package/dist/commonjs/utils/index.d.ts +0 -17
- package/dist/commonjs/utils/index.js +0 -117
- package/dist/commonjs/utils/sequencify.d.ts +0 -13
- package/dist/commonjs/utils/sequencify.js +0 -64
- package/dist/commonjs/utils/timing.d.ts +0 -21
- package/dist/commonjs/utils/timing.js +0 -106
- package/dist/esm/base_context_class.d.ts +0 -16
- package/dist/esm/base_context_class.js +0 -37
- package/dist/esm/egg.d.ts +0 -246
- package/dist/esm/egg.js +0 -388
- package/dist/esm/index.d.ts +0 -12
- package/dist/esm/index.js +0 -12
- package/dist/esm/lifecycle.d.ts +0 -78
- package/dist/esm/lifecycle.js +0 -308
- package/dist/esm/loader/context_loader.d.ts +0 -35
- package/dist/esm/loader/context_loader.js +0 -102
- package/dist/esm/loader/egg_loader.d.ts +0 -369
- package/dist/esm/loader/egg_loader.js +0 -1551
- package/dist/esm/loader/file_loader.d.ts +0 -100
- package/dist/esm/loader/file_loader.js +0 -253
- package/dist/esm/package.json +0 -3
- package/dist/esm/singleton.d.ts +0 -29
- package/dist/esm/singleton.js +0 -117
- package/dist/esm/types.d.ts +0 -53
- package/dist/esm/types.js +0 -2
- package/dist/esm/utils/index.d.ts +0 -17
- package/dist/esm/utils/index.js +0 -112
- package/dist/esm/utils/sequencify.d.ts +0 -13
- package/dist/esm/utils/sequencify.js +0 -61
- package/dist/esm/utils/timing.d.ts +0 -21
- package/dist/esm/utils/timing.js +0 -99
- package/dist/package.json +0 -4
- package/src/base_context_class.ts +0 -39
- package/src/egg.ts +0 -617
- package/src/index.ts +0 -14
- package/src/lifecycle.ts +0 -438
- package/src/loader/context_loader.ts +0 -123
- package/src/loader/egg_loader.ts +0 -1984
- package/src/loader/file_loader.ts +0 -349
- package/src/singleton.ts +0 -187
- package/src/utils/index.ts +0 -132
- package/src/utils/sequencify.ts +0 -105
- package/src/utils/timing.ts +0 -128
package/README.md
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
# @eggjs/core
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-image]][npm-url]
|
|
4
|
-
[](https://github.com/eggjs/core/actions/workflows/nodejs.yml)
|
|
5
|
-
[![Test coverage][codecov-image]][codecov-url]
|
|
6
4
|
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
7
5
|
[![npm download][download-image]][download-url]
|
|
8
6
|
[](https://nodejs.org/en/download/)
|
|
9
7
|
|
|
10
8
|
[npm-image]: https://img.shields.io/npm/v/@eggjs/core.svg?style=flat-square
|
|
11
9
|
[npm-url]: https://npmjs.org/package/@eggjs/core
|
|
12
|
-
[codecov-image]: https://codecov.io/github/eggjs/core/coverage.svg?branch=master
|
|
13
|
-
[codecov-url]: https://codecov.io/github/eggjs/core?branch=master
|
|
14
10
|
[snyk-image]: https://snyk.io/test/npm/@eggjs/core/badge.svg?style=flat-square
|
|
15
11
|
[snyk-url]: https://snyk.io/test/npm/@eggjs/core
|
|
16
12
|
[download-image]: https://img.shields.io/npm/dm/@eggjs/core.svg?style=flat-square
|
|
@@ -285,7 +281,7 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
|
285
281
|
|
|
286
282
|
## Contributors
|
|
287
283
|
|
|
288
|
-
[](https://github.com/eggjs/egg/graphs/contributors)
|
|
289
285
|
|
|
290
286
|
Made with [contributors-img](https://contrib.rocks).
|
|
291
287
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Context, EggCore } from "./egg.js";
|
|
2
|
+
|
|
3
|
+
//#region src/base_context_class.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* BaseContextClass is a base class that can be extended,
|
|
7
|
+
* it's instantiated in context level,
|
|
8
|
+
* {@link Helper}, {@link Service} is extending it.
|
|
9
|
+
*/
|
|
10
|
+
declare class BaseContextClass {
|
|
11
|
+
ctx: Context;
|
|
12
|
+
app: EggCore;
|
|
13
|
+
config: Record<string, any>;
|
|
14
|
+
service: BaseContextClass;
|
|
15
|
+
/**
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
constructor(ctx: Context);
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { BaseContextClass };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/base_context_class.ts
|
|
2
|
+
/**
|
|
3
|
+
* BaseContextClass is a base class that can be extended,
|
|
4
|
+
* it's instantiated in context level,
|
|
5
|
+
* {@link Helper}, {@link Service} is extending it.
|
|
6
|
+
*/
|
|
7
|
+
var BaseContextClass = class {
|
|
8
|
+
ctx;
|
|
9
|
+
app;
|
|
10
|
+
config;
|
|
11
|
+
service;
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
constructor(ctx) {
|
|
16
|
+
/**
|
|
17
|
+
* @member {Context} BaseContextClass#ctx
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
this.ctx = ctx;
|
|
21
|
+
/**
|
|
22
|
+
* @member {Application} BaseContextClass#app
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
this.app = ctx.app;
|
|
26
|
+
/**
|
|
27
|
+
* @member {Config} BaseContextClass#config
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
this.config = ctx.app.config;
|
|
31
|
+
/**
|
|
32
|
+
* @member {Service} BaseContextClass#service
|
|
33
|
+
* @since 1.0.0
|
|
34
|
+
*/
|
|
35
|
+
this.service = ctx.service;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { BaseContextClass };
|
package/dist/egg.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Fun } from "./utils/index.js";
|
|
2
|
+
import { EggAppConfig, EggPluginInfo } from "./types.js";
|
|
3
|
+
import { BaseContextClass } from "./base_context_class.js";
|
|
4
|
+
import { Timing } from "./utils/timing.js";
|
|
5
|
+
import { Lifecycle } from "./lifecycle.js";
|
|
6
|
+
import { EggLoader } from "./loader/egg_loader.js";
|
|
7
|
+
import { SingletonCreateMethod } from "./singleton.js";
|
|
8
|
+
import { Application as KoaApplication, Context as KoaContext, MiddlewareFunc as KoaMiddlewareFunc, Next, Request as KoaRequest, Response as KoaResponse } from "@eggjs/koa";
|
|
9
|
+
import { EggConsoleLogger, Logger } from "egg-logger";
|
|
10
|
+
import { EggRouter as Router, RegisterOptions, ResourcesController } from "@eggjs/router";
|
|
11
|
+
import { ReadyFunctionArg } from "get-ready";
|
|
12
|
+
|
|
13
|
+
//#region src/egg.d.ts
|
|
14
|
+
declare const EGG_LOADER: unique symbol;
|
|
15
|
+
interface EggCoreOptions {
|
|
16
|
+
baseDir: string;
|
|
17
|
+
type: 'application' | 'agent';
|
|
18
|
+
plugins?: any;
|
|
19
|
+
serverScope?: string;
|
|
20
|
+
env?: string;
|
|
21
|
+
}
|
|
22
|
+
type EggCoreInitOptions = Partial<EggCoreOptions>;
|
|
23
|
+
declare class Request$1 extends KoaRequest {
|
|
24
|
+
app: EggCore;
|
|
25
|
+
response: Response$1;
|
|
26
|
+
}
|
|
27
|
+
declare class Response$1 extends KoaResponse {
|
|
28
|
+
app: EggCore;
|
|
29
|
+
request: Request$1;
|
|
30
|
+
}
|
|
31
|
+
declare class Context$1 extends KoaContext {
|
|
32
|
+
app: EggCore;
|
|
33
|
+
request: Request$1;
|
|
34
|
+
response: Response$1;
|
|
35
|
+
service: BaseContextClass;
|
|
36
|
+
/**
|
|
37
|
+
* Returns map of URL parameters for given `path` and `paramNames`.
|
|
38
|
+
* @example
|
|
39
|
+
* ##### ctx.params.id {string}
|
|
40
|
+
*
|
|
41
|
+
* `GET /api/users/1` => `'1'`
|
|
42
|
+
*
|
|
43
|
+
* ##### ctx.params.per_page {string}
|
|
44
|
+
*
|
|
45
|
+
* The number of every page, `GET /api/users?per_page=20` => `20`
|
|
46
|
+
*/
|
|
47
|
+
params?: Record<string, string>;
|
|
48
|
+
/**
|
|
49
|
+
* Returns array of router regexp url path captures.
|
|
50
|
+
*/
|
|
51
|
+
captures?: string[];
|
|
52
|
+
/**
|
|
53
|
+
* Returns the name of the matched router.
|
|
54
|
+
*/
|
|
55
|
+
routerName?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the path of the matched router.
|
|
58
|
+
*/
|
|
59
|
+
routerPath?: string | RegExp;
|
|
60
|
+
}
|
|
61
|
+
type MiddlewareFunc<T extends KoaContext = Context$1> = KoaMiddlewareFunc<T>;
|
|
62
|
+
declare class EggCore extends KoaApplication {
|
|
63
|
+
#private;
|
|
64
|
+
options: EggCoreOptions;
|
|
65
|
+
timing: Timing;
|
|
66
|
+
console: EggConsoleLogger;
|
|
67
|
+
BaseContextClass: typeof BaseContextClass;
|
|
68
|
+
Controller: typeof BaseContextClass;
|
|
69
|
+
Service: typeof BaseContextClass;
|
|
70
|
+
Helper?: typeof BaseContextClass;
|
|
71
|
+
lifecycle: Lifecycle;
|
|
72
|
+
loader: EggLoader;
|
|
73
|
+
/** auto inject on loadService() */
|
|
74
|
+
readonly serviceClasses: Record<string, any>;
|
|
75
|
+
/** auto inject on loadController() */
|
|
76
|
+
readonly controller: Record<string, any>;
|
|
77
|
+
/** auto inject on loadMiddleware() */
|
|
78
|
+
readonly middlewares: Record<string, (opt: unknown, app: EggCore) => MiddlewareFunc>;
|
|
79
|
+
/**
|
|
80
|
+
* @class
|
|
81
|
+
* @param {Object} options - options
|
|
82
|
+
* @param {String} [options.baseDir] - the directory of application
|
|
83
|
+
* @param {String} [options.type] - whether it's running in app worker or agent worker
|
|
84
|
+
* @param {Object} [options.plugins] - custom plugins
|
|
85
|
+
* @since 1.0.0
|
|
86
|
+
*/
|
|
87
|
+
constructor(options?: EggCoreInitOptions);
|
|
88
|
+
get logger(): Logger;
|
|
89
|
+
get coreLogger(): Logger;
|
|
90
|
+
/**
|
|
91
|
+
* create a singleton instance
|
|
92
|
+
* @param {String} name - unique name for singleton
|
|
93
|
+
* @param {Function|AsyncFunction} create - method will be invoked when singleton instance create
|
|
94
|
+
*/
|
|
95
|
+
addSingleton(name: string, create: SingletonCreateMethod): void;
|
|
96
|
+
/**
|
|
97
|
+
* override koa's app.use, support generator function
|
|
98
|
+
* @since 1.0.0
|
|
99
|
+
*/
|
|
100
|
+
use<T extends KoaContext = Context$1>(fn: MiddlewareFunc<T>): this;
|
|
101
|
+
/**
|
|
102
|
+
* Whether `application` or `agent`
|
|
103
|
+
* @member {String}
|
|
104
|
+
* @since 1.0.0
|
|
105
|
+
*/
|
|
106
|
+
get type(): "application" | "agent";
|
|
107
|
+
/**
|
|
108
|
+
* The current directory of application
|
|
109
|
+
* @member {String}
|
|
110
|
+
* @see {@link AppInfo#baseDir}
|
|
111
|
+
* @since 1.0.0
|
|
112
|
+
*/
|
|
113
|
+
get baseDir(): string;
|
|
114
|
+
/**
|
|
115
|
+
* Alias to {@link https://npmjs.com/package/depd}
|
|
116
|
+
* @member {Function}
|
|
117
|
+
* @since 1.0.0
|
|
118
|
+
*/
|
|
119
|
+
get deprecate(): (message: string) => void;
|
|
120
|
+
/**
|
|
121
|
+
* The name of application
|
|
122
|
+
* @member {String}
|
|
123
|
+
* @see {@link AppInfo#name}
|
|
124
|
+
* @since 1.0.0
|
|
125
|
+
*/
|
|
126
|
+
get name(): any;
|
|
127
|
+
/**
|
|
128
|
+
* Retrieve enabled plugins
|
|
129
|
+
* @member {Object}
|
|
130
|
+
* @since 1.0.0
|
|
131
|
+
*/
|
|
132
|
+
get plugins(): Record<string, EggPluginInfo>;
|
|
133
|
+
/**
|
|
134
|
+
* The configuration of application
|
|
135
|
+
* @member {Config}
|
|
136
|
+
* @since 1.0.0
|
|
137
|
+
*/
|
|
138
|
+
get config(): EggAppConfig;
|
|
139
|
+
/**
|
|
140
|
+
* Execute scope after loaded and before app start.
|
|
141
|
+
*
|
|
142
|
+
* Notice:
|
|
143
|
+
* This method is now NOT recommended and regarded as a deprecated one,
|
|
144
|
+
* For plugin development, we should use `didLoad` instead.
|
|
145
|
+
* For application development, we should use `willReady` instead.
|
|
146
|
+
*
|
|
147
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforestart
|
|
148
|
+
*
|
|
149
|
+
* @param {Function} scope function will execute before app start
|
|
150
|
+
* @param {string} [name] scope name, default is empty string
|
|
151
|
+
*/
|
|
152
|
+
beforeStart(scope: Fun, name?: string): void;
|
|
153
|
+
/**
|
|
154
|
+
* register an callback function that will be invoked when application is ready.
|
|
155
|
+
* @see https://github.com/node-modules/get-ready
|
|
156
|
+
* @since 1.0.0
|
|
157
|
+
* @example
|
|
158
|
+
* const app = new Application(...);
|
|
159
|
+
* app.ready(err => {
|
|
160
|
+
* if (err) throw err;
|
|
161
|
+
* console.log('done');
|
|
162
|
+
* });
|
|
163
|
+
*/
|
|
164
|
+
ready(): Promise<void>;
|
|
165
|
+
ready(flagOrFunction: ReadyFunctionArg): void;
|
|
166
|
+
/**
|
|
167
|
+
* If a client starts asynchronously, you can register `readyCallback`,
|
|
168
|
+
* then the application will wait for the callback to ready
|
|
169
|
+
*
|
|
170
|
+
* It will log when the callback is not invoked after 10s
|
|
171
|
+
*
|
|
172
|
+
* Recommend to use {@link EggCore#beforeStart}
|
|
173
|
+
* @since 1.0.0
|
|
174
|
+
*
|
|
175
|
+
* @param {String} name - readyCallback task name
|
|
176
|
+
* @param {object} opts -
|
|
177
|
+
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
|
|
178
|
+
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency
|
|
179
|
+
* @returns {Function} - a callback
|
|
180
|
+
* @example
|
|
181
|
+
* const done = app.readyCallback('mysql');
|
|
182
|
+
* mysql.ready(done);
|
|
183
|
+
*/
|
|
184
|
+
readyCallback(name: string, opts: object): (...args: unknown[]) => void;
|
|
185
|
+
/**
|
|
186
|
+
* Register a function that will be called when app close.
|
|
187
|
+
*
|
|
188
|
+
* Notice:
|
|
189
|
+
* This method is now NOT recommended directly used,
|
|
190
|
+
* Developers SHOULDN'T use app.beforeClose directly now,
|
|
191
|
+
* but in the form of class to implement beforeClose instead.
|
|
192
|
+
*
|
|
193
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforeclose
|
|
194
|
+
*
|
|
195
|
+
* @param {Function} fn - the function that can be generator function or async function.
|
|
196
|
+
*/
|
|
197
|
+
beforeClose(fn: Fun, name?: string): void;
|
|
198
|
+
/**
|
|
199
|
+
* Close all, it will close
|
|
200
|
+
* - callbacks registered by beforeClose
|
|
201
|
+
* - emit `close` event
|
|
202
|
+
* - remove add listeners
|
|
203
|
+
*
|
|
204
|
+
* If error is thrown when it's closing, the promise will reject.
|
|
205
|
+
* It will also reject after following call.
|
|
206
|
+
* @returns {Promise} promise
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
*/
|
|
209
|
+
close(): Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* get router
|
|
212
|
+
* @member {Router} EggCore#router
|
|
213
|
+
* @since 1.0.0
|
|
214
|
+
*/
|
|
215
|
+
get router(): Router;
|
|
216
|
+
/**
|
|
217
|
+
* Alias to {@link Router#url}
|
|
218
|
+
* @param {String} name - Router name
|
|
219
|
+
* @param {Object} params - more parameters
|
|
220
|
+
* @returns {String} url
|
|
221
|
+
*/
|
|
222
|
+
url(name: string, params?: Parameters<Router['url']>[1]): string;
|
|
223
|
+
head(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
224
|
+
head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
225
|
+
get(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
226
|
+
get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
227
|
+
put(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
228
|
+
put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
229
|
+
patch(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
230
|
+
patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
231
|
+
post(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
232
|
+
post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
233
|
+
delete(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
234
|
+
delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
235
|
+
del(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
236
|
+
del(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
237
|
+
all(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
238
|
+
all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
239
|
+
resources(prefix: string, controller: string | ResourcesController): EggCore;
|
|
240
|
+
resources(prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
|
|
241
|
+
resources(name: string, prefix: string, controller: string | ResourcesController): EggCore;
|
|
242
|
+
resources(name: string, prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
|
|
243
|
+
redirect(source: string, destination: string, status?: number): this;
|
|
244
|
+
register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | MiddlewareFunc[], opts?: RegisterOptions): this;
|
|
245
|
+
get [EGG_LOADER](): typeof EggLoader;
|
|
246
|
+
}
|
|
247
|
+
//#endregion
|
|
248
|
+
export { Context$1 as Context, EGG_LOADER, EggCore, EggCoreInitOptions, EggCoreOptions, KoaApplication, KoaContext, type KoaMiddlewareFunc, KoaRequest, KoaResponse, MiddlewareFunc, type Next, Request$1 as Request, Response$1 as Response, Router };
|
package/dist/egg.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import utils_default from "./utils/index.js";
|
|
2
|
+
import { BaseContextClass } from "./base_context_class.js";
|
|
3
|
+
import { Timing } from "./utils/timing.js";
|
|
4
|
+
import { Lifecycle } from "./lifecycle.js";
|
|
5
|
+
import { EggLoader } from "./loader/egg_loader.js";
|
|
6
|
+
import { Singleton } from "./singleton.js";
|
|
7
|
+
import { debuglog } from "node:util";
|
|
8
|
+
import assert from "node:assert";
|
|
9
|
+
import { Application as KoaApplication, Context as KoaContext, Request as KoaRequest, Response as KoaResponse } from "@eggjs/koa";
|
|
10
|
+
import { EggConsoleLogger } from "egg-logger";
|
|
11
|
+
import { EggRouter as Router } from "@eggjs/router";
|
|
12
|
+
|
|
13
|
+
//#region src/egg.ts
|
|
14
|
+
const debug = debuglog("egg/core/egg");
|
|
15
|
+
const EGG_LOADER = Symbol.for("egg#loader");
|
|
16
|
+
var Request$1 = class extends KoaRequest {};
|
|
17
|
+
var Response$1 = class extends KoaResponse {};
|
|
18
|
+
var Context$1 = class extends KoaContext {
|
|
19
|
+
/**
|
|
20
|
+
* Returns map of URL parameters for given `path` and `paramNames`.
|
|
21
|
+
* @example
|
|
22
|
+
* ##### ctx.params.id {string}
|
|
23
|
+
*
|
|
24
|
+
* `GET /api/users/1` => `'1'`
|
|
25
|
+
*
|
|
26
|
+
* ##### ctx.params.per_page {string}
|
|
27
|
+
*
|
|
28
|
+
* The number of every page, `GET /api/users?per_page=20` => `20`
|
|
29
|
+
*/
|
|
30
|
+
params;
|
|
31
|
+
/**
|
|
32
|
+
* Returns array of router regexp url path captures.
|
|
33
|
+
*/
|
|
34
|
+
captures;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the name of the matched router.
|
|
37
|
+
*/
|
|
38
|
+
routerName;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the path of the matched router.
|
|
41
|
+
*/
|
|
42
|
+
routerPath;
|
|
43
|
+
};
|
|
44
|
+
var EggCore = class EggCore extends KoaApplication {
|
|
45
|
+
options;
|
|
46
|
+
timing;
|
|
47
|
+
console;
|
|
48
|
+
BaseContextClass;
|
|
49
|
+
Controller;
|
|
50
|
+
Service;
|
|
51
|
+
Helper;
|
|
52
|
+
lifecycle;
|
|
53
|
+
loader;
|
|
54
|
+
#closePromise;
|
|
55
|
+
#router;
|
|
56
|
+
/** auto inject on loadService() */
|
|
57
|
+
serviceClasses = {};
|
|
58
|
+
/** auto inject on loadController() */
|
|
59
|
+
controller = {};
|
|
60
|
+
/** auto inject on loadMiddleware() */
|
|
61
|
+
middlewares = {};
|
|
62
|
+
/**
|
|
63
|
+
* @class
|
|
64
|
+
* @param {Object} options - options
|
|
65
|
+
* @param {String} [options.baseDir] - the directory of application
|
|
66
|
+
* @param {String} [options.type] - whether it's running in app worker or agent worker
|
|
67
|
+
* @param {Object} [options.plugins] - custom plugins
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
options.baseDir = options.baseDir ?? process.cwd();
|
|
72
|
+
options.type = options.type ?? "application";
|
|
73
|
+
assert(typeof options.baseDir === "string", "options.baseDir required, and must be a string");
|
|
74
|
+
assert(options.type === "application" || options.type === "agent", "options.type should be application or agent");
|
|
75
|
+
super();
|
|
76
|
+
this.timing = new Timing();
|
|
77
|
+
/**
|
|
78
|
+
* @member {Object} EggCore#options
|
|
79
|
+
* @private
|
|
80
|
+
* @since 1.0.0
|
|
81
|
+
*/
|
|
82
|
+
this.options = options;
|
|
83
|
+
/**
|
|
84
|
+
* logging for EggCore, avoid using console directly
|
|
85
|
+
* @member {Logger} EggCore#console
|
|
86
|
+
* @private
|
|
87
|
+
* @since 1.0.0
|
|
88
|
+
*/
|
|
89
|
+
this.console = new EggConsoleLogger();
|
|
90
|
+
/**
|
|
91
|
+
* @member {BaseContextClass} EggCore#BaseContextClass
|
|
92
|
+
* @since 1.0.0
|
|
93
|
+
*/
|
|
94
|
+
this.BaseContextClass = BaseContextClass;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieve base controller
|
|
97
|
+
* @member {Controller} EggCore#Controller
|
|
98
|
+
* @since 1.0.0
|
|
99
|
+
*/
|
|
100
|
+
this.Controller = this.BaseContextClass;
|
|
101
|
+
/**
|
|
102
|
+
* Retrieve base service
|
|
103
|
+
* @member {Service} EggCore#Service
|
|
104
|
+
* @since 1.0.0
|
|
105
|
+
*/
|
|
106
|
+
this.Service = this.BaseContextClass;
|
|
107
|
+
this.lifecycle = new Lifecycle({
|
|
108
|
+
baseDir: options.baseDir,
|
|
109
|
+
app: this,
|
|
110
|
+
logger: this.console
|
|
111
|
+
});
|
|
112
|
+
this.lifecycle.on("error", (err) => this.emit("error", err));
|
|
113
|
+
this.lifecycle.on("ready_timeout", (id) => this.emit("ready_timeout", id));
|
|
114
|
+
this.lifecycle.on("ready_stat", (data) => this.emit("ready_stat", data));
|
|
115
|
+
/**
|
|
116
|
+
* The loader instance, the default class is {@link EggLoader}.
|
|
117
|
+
* If you want define
|
|
118
|
+
* @member {EggLoader} EggCore#loader
|
|
119
|
+
* @since 1.0.0
|
|
120
|
+
*/
|
|
121
|
+
const Loader = this[EGG_LOADER];
|
|
122
|
+
assert(Loader, "Symbol.for('egg#loader') is required");
|
|
123
|
+
this.loader = new Loader({
|
|
124
|
+
baseDir: options.baseDir,
|
|
125
|
+
app: this,
|
|
126
|
+
plugins: options.plugins,
|
|
127
|
+
logger: this.console,
|
|
128
|
+
serverScope: options.serverScope,
|
|
129
|
+
env: options.env ?? "",
|
|
130
|
+
EggCoreClass: EggCore
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
get logger() {
|
|
134
|
+
return this.console;
|
|
135
|
+
}
|
|
136
|
+
get coreLogger() {
|
|
137
|
+
return this.console;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* create a singleton instance
|
|
141
|
+
* @param {String} name - unique name for singleton
|
|
142
|
+
* @param {Function|AsyncFunction} create - method will be invoked when singleton instance create
|
|
143
|
+
*/
|
|
144
|
+
addSingleton(name, create) {
|
|
145
|
+
const initPromise = new Singleton({
|
|
146
|
+
name,
|
|
147
|
+
create,
|
|
148
|
+
app: this
|
|
149
|
+
}).init();
|
|
150
|
+
if (initPromise) this.lifecycle.registerBeforeStart(async () => {
|
|
151
|
+
await initPromise;
|
|
152
|
+
}, `${name}-singleton-init`);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* override koa's app.use, support generator function
|
|
156
|
+
* @since 1.0.0
|
|
157
|
+
*/
|
|
158
|
+
use(fn) {
|
|
159
|
+
assert(typeof fn === "function", "app.use() requires a function");
|
|
160
|
+
debug("[use] add middleware: %o", fn._name || fn.name || "-");
|
|
161
|
+
this.middleware.push(fn);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Whether `application` or `agent`
|
|
166
|
+
* @member {String}
|
|
167
|
+
* @since 1.0.0
|
|
168
|
+
*/
|
|
169
|
+
get type() {
|
|
170
|
+
return this.options.type;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The current directory of application
|
|
174
|
+
* @member {String}
|
|
175
|
+
* @see {@link AppInfo#baseDir}
|
|
176
|
+
* @since 1.0.0
|
|
177
|
+
*/
|
|
178
|
+
get baseDir() {
|
|
179
|
+
return this.options.baseDir;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Alias to {@link https://npmjs.com/package/depd}
|
|
183
|
+
* @member {Function}
|
|
184
|
+
* @since 1.0.0
|
|
185
|
+
*/
|
|
186
|
+
get deprecate() {
|
|
187
|
+
return utils_default.deprecated;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The name of application
|
|
191
|
+
* @member {String}
|
|
192
|
+
* @see {@link AppInfo#name}
|
|
193
|
+
* @since 1.0.0
|
|
194
|
+
*/
|
|
195
|
+
get name() {
|
|
196
|
+
return this.loader ? this.loader.pkg.name : "";
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Retrieve enabled plugins
|
|
200
|
+
* @member {Object}
|
|
201
|
+
* @since 1.0.0
|
|
202
|
+
*/
|
|
203
|
+
get plugins() {
|
|
204
|
+
return this.loader ? this.loader.plugins : {};
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The configuration of application
|
|
208
|
+
* @member {Config}
|
|
209
|
+
* @since 1.0.0
|
|
210
|
+
*/
|
|
211
|
+
get config() {
|
|
212
|
+
return this.loader ? this.loader.config : {};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Execute scope after loaded and before app start.
|
|
216
|
+
*
|
|
217
|
+
* Notice:
|
|
218
|
+
* This method is now NOT recommended and regarded as a deprecated one,
|
|
219
|
+
* For plugin development, we should use `didLoad` instead.
|
|
220
|
+
* For application development, we should use `willReady` instead.
|
|
221
|
+
*
|
|
222
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforestart
|
|
223
|
+
*
|
|
224
|
+
* @param {Function} scope function will execute before app start
|
|
225
|
+
* @param {string} [name] scope name, default is empty string
|
|
226
|
+
*/
|
|
227
|
+
beforeStart(scope, name) {
|
|
228
|
+
this.deprecate("`beforeStart` was deprecated, please use \"Life Cycles\" instead, see https://www.eggjs.org/advanced/loader#life-cycles");
|
|
229
|
+
this.lifecycle.registerBeforeStart(scope, name ?? "");
|
|
230
|
+
}
|
|
231
|
+
ready(flagOrFunction) {
|
|
232
|
+
if (flagOrFunction === void 0) return this.lifecycle.ready();
|
|
233
|
+
return this.lifecycle.ready(flagOrFunction);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* If a client starts asynchronously, you can register `readyCallback`,
|
|
237
|
+
* then the application will wait for the callback to ready
|
|
238
|
+
*
|
|
239
|
+
* It will log when the callback is not invoked after 10s
|
|
240
|
+
*
|
|
241
|
+
* Recommend to use {@link EggCore#beforeStart}
|
|
242
|
+
* @since 1.0.0
|
|
243
|
+
*
|
|
244
|
+
* @param {String} name - readyCallback task name
|
|
245
|
+
* @param {object} opts -
|
|
246
|
+
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
|
|
247
|
+
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency
|
|
248
|
+
* @returns {Function} - a callback
|
|
249
|
+
* @example
|
|
250
|
+
* const done = app.readyCallback('mysql');
|
|
251
|
+
* mysql.ready(done);
|
|
252
|
+
*/
|
|
253
|
+
readyCallback(name, opts) {
|
|
254
|
+
this.deprecate("`readyCallback` was deprecated, please use \"Life Cycles\" instead, see https://www.eggjs.org/advanced/loader#life-cycles");
|
|
255
|
+
return this.lifecycle.legacyReadyCallback(name, opts);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Register a function that will be called when app close.
|
|
259
|
+
*
|
|
260
|
+
* Notice:
|
|
261
|
+
* This method is now NOT recommended directly used,
|
|
262
|
+
* Developers SHOULDN'T use app.beforeClose directly now,
|
|
263
|
+
* but in the form of class to implement beforeClose instead.
|
|
264
|
+
*
|
|
265
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforeclose
|
|
266
|
+
*
|
|
267
|
+
* @param {Function} fn - the function that can be generator function or async function.
|
|
268
|
+
*/
|
|
269
|
+
beforeClose(fn, name) {
|
|
270
|
+
this.deprecate("`beforeClose` was deprecated, please use \"Life Cycles\" instead, see https://www.eggjs.org/advanced/loader#life-cycles");
|
|
271
|
+
this.lifecycle.registerBeforeClose(fn, name);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Close all, it will close
|
|
275
|
+
* - callbacks registered by beforeClose
|
|
276
|
+
* - emit `close` event
|
|
277
|
+
* - remove add listeners
|
|
278
|
+
*
|
|
279
|
+
* If error is thrown when it's closing, the promise will reject.
|
|
280
|
+
* It will also reject after following call.
|
|
281
|
+
* @returns {Promise} promise
|
|
282
|
+
* @since 1.0.0
|
|
283
|
+
*/
|
|
284
|
+
async close() {
|
|
285
|
+
if (this.#closePromise) return this.#closePromise;
|
|
286
|
+
this.#closePromise = this.lifecycle.close();
|
|
287
|
+
return this.#closePromise;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* get router
|
|
291
|
+
* @member {Router} EggCore#router
|
|
292
|
+
* @since 1.0.0
|
|
293
|
+
*/
|
|
294
|
+
get router() {
|
|
295
|
+
if (this.#router) return this.#router;
|
|
296
|
+
this.#router = new Router({ sensitive: true }, this);
|
|
297
|
+
return this.#router;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Alias to {@link Router#url}
|
|
301
|
+
* @param {String} name - Router name
|
|
302
|
+
* @param {Object} params - more parameters
|
|
303
|
+
* @returns {String} url
|
|
304
|
+
*/
|
|
305
|
+
url(name, params) {
|
|
306
|
+
return this.router.url(name, params);
|
|
307
|
+
}
|
|
308
|
+
head(...args) {
|
|
309
|
+
this.router.head.apply(this.router, args);
|
|
310
|
+
return this;
|
|
311
|
+
}
|
|
312
|
+
get(...args) {
|
|
313
|
+
this.router.get.apply(this.router, args);
|
|
314
|
+
return this;
|
|
315
|
+
}
|
|
316
|
+
put(...args) {
|
|
317
|
+
this.router.put.apply(this.router, args);
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
patch(...args) {
|
|
321
|
+
this.router.patch.apply(this.router, args);
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
post(...args) {
|
|
325
|
+
this.router.post.apply(this.router, args);
|
|
326
|
+
return this;
|
|
327
|
+
}
|
|
328
|
+
delete(...args) {
|
|
329
|
+
this.router.delete.apply(this.router, args);
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
del(...args) {
|
|
333
|
+
this.router.del.apply(this.router, args);
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
all(...args) {
|
|
337
|
+
this.router.all.apply(this.router, args);
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
resources(...args) {
|
|
341
|
+
this.router.resources.apply(this.router, args);
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
redirect(source, destination, status = 301) {
|
|
345
|
+
this.router.redirect(source, destination, status);
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
register(path, methods, middleware, opts) {
|
|
349
|
+
this.router.register(path, methods, middleware, opts);
|
|
350
|
+
return this;
|
|
351
|
+
}
|
|
352
|
+
get [EGG_LOADER]() {
|
|
353
|
+
return EggLoader;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
//#endregion
|
|
358
|
+
export { Context$1 as Context, EGG_LOADER, EggCore, KoaApplication, KoaContext, KoaRequest, KoaResponse, Request$1 as Request, Response$1 as Response, Router };
|