@eggjs/core 6.0.0-beta.0
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/LICENSE +21 -0
- package/README.md +296 -0
- package/dist/commonjs/base_context_class.d.ts +16 -0
- package/dist/commonjs/base_context_class.js +41 -0
- package/dist/commonjs/egg.d.ts +204 -0
- package/dist/commonjs/egg.js +346 -0
- package/dist/commonjs/index.d.ts +5 -0
- package/dist/commonjs/index.js +26 -0
- package/dist/commonjs/lifecycle.d.ts +75 -0
- package/dist/commonjs/lifecycle.js +306 -0
- package/dist/commonjs/loader/context_loader.d.ts +24 -0
- package/dist/commonjs/loader/context_loader.js +109 -0
- package/dist/commonjs/loader/egg_loader.d.ts +405 -0
- package/dist/commonjs/loader/egg_loader.js +1497 -0
- package/dist/commonjs/loader/file_loader.d.ts +96 -0
- package/dist/commonjs/loader/file_loader.js +248 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/types.d.ts +1 -0
- package/dist/commonjs/types.js +403 -0
- package/dist/commonjs/utils/index.d.ts +14 -0
- package/dist/commonjs/utils/index.js +146 -0
- package/dist/commonjs/utils/sequencify.d.ts +13 -0
- package/dist/commonjs/utils/sequencify.js +59 -0
- package/dist/commonjs/utils/timing.d.ts +22 -0
- package/dist/commonjs/utils/timing.js +100 -0
- package/dist/esm/base_context_class.d.ts +16 -0
- package/dist/esm/base_context_class.js +37 -0
- package/dist/esm/egg.d.ts +204 -0
- package/dist/esm/egg.js +339 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/lifecycle.d.ts +75 -0
- package/dist/esm/lifecycle.js +276 -0
- package/dist/esm/loader/context_loader.d.ts +24 -0
- package/dist/esm/loader/context_loader.js +102 -0
- package/dist/esm/loader/egg_loader.d.ts +405 -0
- package/dist/esm/loader/egg_loader.js +1490 -0
- package/dist/esm/loader/file_loader.d.ts +96 -0
- package/dist/esm/loader/file_loader.js +241 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/types.js +402 -0
- package/dist/esm/utils/index.d.ts +14 -0
- package/dist/esm/utils/index.js +141 -0
- package/dist/esm/utils/sequencify.d.ts +13 -0
- package/dist/esm/utils/sequencify.js +56 -0
- package/dist/esm/utils/timing.d.ts +22 -0
- package/dist/esm/utils/timing.js +93 -0
- package/package.json +103 -0
- package/src/base_context_class.ts +39 -0
- package/src/egg.ts +430 -0
- package/src/index.ts +6 -0
- package/src/lifecycle.ts +363 -0
- package/src/loader/context_loader.ts +121 -0
- package/src/loader/egg_loader.ts +1703 -0
- package/src/loader/file_loader.ts +295 -0
- package/src/types.ts +447 -0
- package/src/utils/index.ts +154 -0
- package/src/utils/sequencify.ts +70 -0
- package/src/utils/timing.ts +114 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# @eggjs/core
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[](https://github.com/eggjs/egg-core/actions/workflows/nodejs.yml)
|
|
5
|
+
[![Test coverage][codecov-image]][codecov-url]
|
|
6
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
7
|
+
[![npm download][download-image]][download-url]
|
|
8
|
+
|
|
9
|
+
[npm-image]: https://img.shields.io/npm/v/@eggjs/core.svg?style=flat-square
|
|
10
|
+
[npm-url]: https://npmjs.org/package/@eggjs/core
|
|
11
|
+
[codecov-image]: https://codecov.io/github/eggjs/egg-core/coverage.svg?branch=master
|
|
12
|
+
[codecov-url]: https://codecov.io/github/eggjs/egg-core?branch=master
|
|
13
|
+
[snyk-image]: https://snyk.io/test/npm/@eggjs/core/badge.svg?style=flat-square
|
|
14
|
+
[snyk-url]: https://snyk.io/test/npm/@eggjs/core
|
|
15
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/core.svg?style=flat-square
|
|
16
|
+
[download-url]: https://npmjs.org/package/@eggjs/core
|
|
17
|
+
|
|
18
|
+
A core plugin framework based on [koa](https://github.com/koajs/koa).
|
|
19
|
+
|
|
20
|
+
**Don't use it directly, see [egg].**
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Directory structure
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
├── package.json
|
|
28
|
+
├── app.js (optional)
|
|
29
|
+
├── agent.js (optional)
|
|
30
|
+
├── app
|
|
31
|
+
| ├── router.js
|
|
32
|
+
│ ├── controller
|
|
33
|
+
│ │ └── home.js
|
|
34
|
+
| ├── extend (optional)
|
|
35
|
+
│ | ├── helper.js (optional)
|
|
36
|
+
│ | ├── filter.js (optional)
|
|
37
|
+
│ | ├── request.js (optional)
|
|
38
|
+
│ | ├── response.js (optional)
|
|
39
|
+
│ | ├── context.js (optional)
|
|
40
|
+
│ | ├── application.js (optional)
|
|
41
|
+
│ | └── agent.js (optional)
|
|
42
|
+
│ ├── service (optional)
|
|
43
|
+
│ ├── middleware (optional)
|
|
44
|
+
│ │ └── response_time.js
|
|
45
|
+
│ └── view (optional)
|
|
46
|
+
| ├── layout.html
|
|
47
|
+
│ └── home.html
|
|
48
|
+
├── config
|
|
49
|
+
| ├── config.default.js
|
|
50
|
+
│ ├── config.prod.js
|
|
51
|
+
| ├── config.test.js (optional)
|
|
52
|
+
| ├── config.local.js (optional)
|
|
53
|
+
| ├── config.unittest.js (optional)
|
|
54
|
+
│ └── plugin.js
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Then you can start with code below
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { EggCore as Application } from '@eggjs/core';
|
|
61
|
+
|
|
62
|
+
const app = new Application({
|
|
63
|
+
baseDir: '/path/to/app'
|
|
64
|
+
});
|
|
65
|
+
app.ready(() => {
|
|
66
|
+
app.listen(3000);
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## EggLoader
|
|
71
|
+
|
|
72
|
+
EggLoader can easily load files or directories in your [egg] project. In addition, you can customize the loader with low level APIs.
|
|
73
|
+
|
|
74
|
+
### constructor
|
|
75
|
+
|
|
76
|
+
- {String} baseDir - current directory of application
|
|
77
|
+
- {Object} app - instance of egg application
|
|
78
|
+
- {Object} plugins - merge plugins for test
|
|
79
|
+
- {Logger} logger - logger instance,default is console
|
|
80
|
+
|
|
81
|
+
### High Level APIs
|
|
82
|
+
|
|
83
|
+
#### loadPlugin
|
|
84
|
+
|
|
85
|
+
Load config/plugin.ts
|
|
86
|
+
|
|
87
|
+
#### loadConfig
|
|
88
|
+
|
|
89
|
+
Load config/config.ts and config/{serverEnv}.ts
|
|
90
|
+
|
|
91
|
+
If `process.env.EGG_APP_CONFIG` is exists, then it will be parse and override config.
|
|
92
|
+
|
|
93
|
+
#### loadController
|
|
94
|
+
|
|
95
|
+
Load app/controller
|
|
96
|
+
|
|
97
|
+
#### loadMiddleware
|
|
98
|
+
|
|
99
|
+
Load app/middleware
|
|
100
|
+
|
|
101
|
+
#### loadApplicationExtend
|
|
102
|
+
|
|
103
|
+
Load app/extend/application.ts
|
|
104
|
+
|
|
105
|
+
#### loadContextExtend
|
|
106
|
+
|
|
107
|
+
Load app/extend/context.ts
|
|
108
|
+
|
|
109
|
+
#### loadRequestExtend
|
|
110
|
+
|
|
111
|
+
Load app/extend/request.ts
|
|
112
|
+
|
|
113
|
+
#### loadResponseExtend
|
|
114
|
+
|
|
115
|
+
Load app/extend/response.ts
|
|
116
|
+
|
|
117
|
+
#### loadHelperExtend
|
|
118
|
+
|
|
119
|
+
Load app/extend/helper.ts
|
|
120
|
+
|
|
121
|
+
#### loadCustomApp
|
|
122
|
+
|
|
123
|
+
Load app.ts, if app.ts export boot class, then trigger configDidLoad
|
|
124
|
+
|
|
125
|
+
#### loadCustomAgent
|
|
126
|
+
|
|
127
|
+
Load agent.ts, if agent.ts export boot class, then trigger configDidLoad
|
|
128
|
+
|
|
129
|
+
#### loadService
|
|
130
|
+
|
|
131
|
+
Load app/service
|
|
132
|
+
|
|
133
|
+
### Low Level APIs
|
|
134
|
+
|
|
135
|
+
#### getServerEnv()
|
|
136
|
+
|
|
137
|
+
Retrieve application environment variable values via `serverEnv`. You can access directly by calling `this.serverEnv` after instantiation.
|
|
138
|
+
|
|
139
|
+
serverEnv | description
|
|
140
|
+
--- | ---
|
|
141
|
+
default | default environment
|
|
142
|
+
test | system integration testing environment
|
|
143
|
+
prod | production environment
|
|
144
|
+
local | local environment on your own computer
|
|
145
|
+
unittest | unit test environment
|
|
146
|
+
|
|
147
|
+
#### getEggPaths()
|
|
148
|
+
|
|
149
|
+
To get directories of the frameworks. A new framework is created by extending egg, then you can use this function to get all frameworks.
|
|
150
|
+
|
|
151
|
+
#### getLoadUnits()
|
|
152
|
+
|
|
153
|
+
A loadUnit is a directory that can be loaded by EggLoader, cause it has the same structure.
|
|
154
|
+
|
|
155
|
+
This function will get add loadUnits follow the order:
|
|
156
|
+
|
|
157
|
+
1. plugin
|
|
158
|
+
2. framework
|
|
159
|
+
3. app
|
|
160
|
+
|
|
161
|
+
loadUnit has a path and a type. Type must be one of those values: *app*, *framework*, *plugin*.
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
{
|
|
165
|
+
path: 'path/to/application',
|
|
166
|
+
type: 'app'
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### getAppname()
|
|
171
|
+
|
|
172
|
+
To get application name from *package.json*
|
|
173
|
+
|
|
174
|
+
#### appInfo
|
|
175
|
+
|
|
176
|
+
Get the infomation of the application
|
|
177
|
+
|
|
178
|
+
- pkg: `package.json`
|
|
179
|
+
- name: the application name from `package.json`
|
|
180
|
+
- baseDir: current directory of application
|
|
181
|
+
- env: equals to serverEnv
|
|
182
|
+
- HOME: home directory of the OS
|
|
183
|
+
- root: baseDir when local and unittest, HOME when other environment
|
|
184
|
+
|
|
185
|
+
#### loadFile(filepath)
|
|
186
|
+
|
|
187
|
+
To load a single file. **Note:** The file must export as a function.
|
|
188
|
+
|
|
189
|
+
#### loadToApp(directory, property, LoaderOptions)
|
|
190
|
+
|
|
191
|
+
To load files from directory in the application.
|
|
192
|
+
|
|
193
|
+
Invoke `this.loadToApp('$baseDir/app/controller', 'controller')`, then you can use it by `app.controller`.
|
|
194
|
+
|
|
195
|
+
#### loadToContext(directory, property, LoaderOptions)
|
|
196
|
+
|
|
197
|
+
To load files from directory, and it will be bound the context.
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
// define service in app/service/query.ts
|
|
201
|
+
export default class Query {
|
|
202
|
+
constructor(ctx: Context) {
|
|
203
|
+
super(ctx);
|
|
204
|
+
// get the ctx
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async get() {}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// use the service in app/controller/home.ts
|
|
211
|
+
export default async (ctx: Context) => {
|
|
212
|
+
ctx.body = await ctx.service.query.get();
|
|
213
|
+
};
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### loadExtend(name, target)
|
|
217
|
+
|
|
218
|
+
Loader app/extend/xx.ts to target, For example,
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
this.loadExtend('application', app);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### LoaderOptions
|
|
225
|
+
|
|
226
|
+
Param | Type | Description
|
|
227
|
+
-------------- | -------------- | ------------------------
|
|
228
|
+
directory | `String/Array` | directories to be loaded
|
|
229
|
+
target | `Object` | attach the target object from loaded files
|
|
230
|
+
match | `String/Array` | match the files when load, default to `**/*.js`(if process.env.EGG_TYPESCRIPT was true, default to `[ '**/*.(js|ts)', '!**/*.d.ts' ]`)
|
|
231
|
+
ignore | `String/Array` | ignore the files when load
|
|
232
|
+
initializer | `Function` | custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path`
|
|
233
|
+
caseStyle | `String/Function` | set property's case when converting a filepath to property list.
|
|
234
|
+
override | `Boolean` | determine whether override the property when get the same name
|
|
235
|
+
call | `Boolean` | determine whether invoke when exports is function
|
|
236
|
+
inject | `Object` | an object that be the argument when invoke the function
|
|
237
|
+
filter | `Function` | a function that filter the exports which can be loaded
|
|
238
|
+
|
|
239
|
+
## Timing
|
|
240
|
+
|
|
241
|
+
EggCore record boot progress with `Timing`, include:
|
|
242
|
+
|
|
243
|
+
- Process start time
|
|
244
|
+
- Script start time(node don't implement an interface like `process.uptime` to record the script start running time, framework can implement a prestart file used with node `--require` options to set `process.scriptTime`)
|
|
245
|
+
- Application start time
|
|
246
|
+
- Load duration
|
|
247
|
+
- `require` duration
|
|
248
|
+
|
|
249
|
+
### start
|
|
250
|
+
|
|
251
|
+
Start record a item. If the item exits, end the old one and start a new one.
|
|
252
|
+
|
|
253
|
+
- {String} name - record item name
|
|
254
|
+
- {Number} [start] - record item start time, default is Date.now()
|
|
255
|
+
|
|
256
|
+
### end
|
|
257
|
+
|
|
258
|
+
End a item.
|
|
259
|
+
|
|
260
|
+
- {String} name - end item name
|
|
261
|
+
|
|
262
|
+
### toJSON
|
|
263
|
+
|
|
264
|
+
Generate all record items to json
|
|
265
|
+
|
|
266
|
+
- {String} name - record item name
|
|
267
|
+
- {Number} start - item start time
|
|
268
|
+
- {Number} end - item end time
|
|
269
|
+
- {Number} duration - item duration
|
|
270
|
+
- {Number} pid - pid
|
|
271
|
+
- {Number} index - item index
|
|
272
|
+
|
|
273
|
+
## Questions & Suggestions
|
|
274
|
+
|
|
275
|
+
Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
276
|
+
|
|
277
|
+
## License
|
|
278
|
+
|
|
279
|
+
[MIT](LICENSE)
|
|
280
|
+
|
|
281
|
+
[egg]: https://github.com/eggjs/egg
|
|
282
|
+
|
|
283
|
+
<!-- GITCONTRIBUTOR_START -->
|
|
284
|
+
|
|
285
|
+
## Contributors
|
|
286
|
+
|
|
287
|
+
|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|
|
|
288
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
289
|
+
|[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/2170848?v=4" width="100px;"/><br/><sub><b>iyuq</b></sub>](https://github.com/iyuq)<br/>|[<img src="https://avatars.githubusercontent.com/u/5243774?v=4" width="100px;"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|[<img src="https://avatars.githubusercontent.com/u/17722900?v=4" width="100px;"/><br/><sub><b>initial-wu</b></sub>](https://github.com/initial-wu)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7315743?v=4" width="100px;"/><br/><sub><b>AnzerWall</b></sub>](https://github.com/AnzerWall)<br/>|
|
|
290
|
+
|[<img src="https://avatars.githubusercontent.com/u/174904?v=4" width="100px;"/><br/><sub><b>army8735</b></sub>](https://github.com/army8735)<br/>|[<img src="https://avatars.githubusercontent.com/u/5938871?v=4" width="100px;"/><br/><sub><b>njugray</b></sub>](https://github.com/njugray)<br/>|[<img src="https://avatars.githubusercontent.com/u/327019?v=4" width="100px;"/><br/><sub><b>JacksonTian</b></sub>](https://github.com/JacksonTian)<br/>|[<img src="https://avatars.githubusercontent.com/u/16460813?v=4" width="100px;"/><br/><sub><b>JimmyDaddy</b></sub>](https://github.com/JimmyDaddy)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/6913898?v=4" width="100px;"/><br/><sub><b>monkindey</b></sub>](https://github.com/monkindey)<br/>|
|
|
291
|
+
|[<img src="https://avatars.githubusercontent.com/u/1148428?v=4" width="100px;"/><br/><sub><b>mattma</b></sub>](https://github.com/mattma)<br/>|[<img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub>](https://github.com/shaoshuai0102)<br/>|[<img src="https://avatars.githubusercontent.com/u/7530656?v=4" width="100px;"/><br/><sub><b>zhang740</b></sub>](https://github.com/zhang740)<br/>|[<img src="https://avatars.githubusercontent.com/u/457552?v=4" width="100px;"/><br/><sub><b>dsonet</b></sub>](https://github.com/dsonet)<br/>|[<img src="https://avatars.githubusercontent.com/u/3995814?v=4" width="100px;"/><br/><sub><b>chenbin92</b></sub>](https://github.com/chenbin92)<br/>|[<img src="https://avatars.githubusercontent.com/u/19908330?v=4" width="100px;"/><br/><sub><b>hyj1991</b></sub>](https://github.com/hyj1991)<br/>|
|
|
292
|
+
[<img src="https://avatars.githubusercontent.com/u/8816730?v=4" width="100px;"/><br/><sub><b>maxming2333</b></sub>](https://github.com/maxming2333)<br/>|[<img src="https://avatars.githubusercontent.com/u/26317926?v=4" width="100px;"/><br/><sub><b>supperchong</b></sub>](https://github.com/supperchong)<br/>|[<img src="https://avatars.githubusercontent.com/u/18463189?v=4" width="100px;"/><br/><sub><b>ZhangDianPeng</b></sub>](https://github.com/ZhangDianPeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/15242708?v=4" width="100px;"/><br/><sub><b>mosaic101</b></sub>](https://github.com/mosaic101)<br/>
|
|
293
|
+
|
|
294
|
+
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed Nov 24 2021 22:24:39 GMT+0800`.
|
|
295
|
+
|
|
296
|
+
<!-- GITCONTRIBUTOR_END -->
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EggCore, EggCoreContext } from './egg.js';
|
|
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
|
+
export declare class BaseContextClass {
|
|
8
|
+
ctx: EggCoreContext;
|
|
9
|
+
app: EggCore;
|
|
10
|
+
config: Record<string, any>;
|
|
11
|
+
service: BaseContextClass;
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
constructor(ctx: EggCoreContext);
|
|
16
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseContextClass = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* BaseContextClass is a base class that can be extended,
|
|
6
|
+
* it's instantiated in context level,
|
|
7
|
+
* {@link Helper}, {@link Service} is extending it.
|
|
8
|
+
*/
|
|
9
|
+
class BaseContextClass {
|
|
10
|
+
ctx;
|
|
11
|
+
app;
|
|
12
|
+
config;
|
|
13
|
+
service;
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
constructor(ctx) {
|
|
18
|
+
/**
|
|
19
|
+
* @member {Context} BaseContextClass#ctx
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
this.ctx = ctx;
|
|
23
|
+
/**
|
|
24
|
+
* @member {Application} BaseContextClass#app
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
this.app = ctx.app;
|
|
28
|
+
/**
|
|
29
|
+
* @member {Config} BaseContextClass#config
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
*/
|
|
32
|
+
this.config = ctx.app.config;
|
|
33
|
+
/**
|
|
34
|
+
* @member {Service} BaseContextClass#service
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
this.service = ctx.service;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.BaseContextClass = BaseContextClass;
|
|
41
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZV9jb250ZXh0X2NsYXNzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2Jhc2VfY29udGV4dF9jbGFzcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFFQTs7OztHQUlHO0FBQ0gsTUFBYSxnQkFBZ0I7SUFDM0IsR0FBRyxDQUFpQjtJQUNwQixHQUFHLENBQVU7SUFDYixNQUFNLENBQXNCO0lBQzVCLE9BQU8sQ0FBbUI7SUFFMUI7O09BRUc7SUFDSCxZQUFZLEdBQW1CO1FBQzdCOzs7V0FHRztRQUNILElBQUksQ0FBQyxHQUFHLEdBQUcsR0FBRyxDQUFDO1FBQ2Y7OztXQUdHO1FBQ0gsSUFBSSxDQUFDLEdBQUcsR0FBRyxHQUFHLENBQUMsR0FBRyxDQUFDO1FBQ25COzs7V0FHRztRQUNILElBQUksQ0FBQyxNQUFNLEdBQUcsR0FBRyxDQUFDLEdBQUcsQ0FBQyxNQUFNLENBQUM7UUFDN0I7OztXQUdHO1FBQ0gsSUFBSSxDQUFDLE9BQU8sR0FBRyxHQUFHLENBQUMsT0FBTyxDQUFDO0lBQzdCLENBQUM7Q0FDRjtBQS9CRCw0Q0ErQkMifQ==
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import KoaApplication from '@eggjs/koa';
|
|
2
|
+
import type { ContextDelegation, Next } from '@eggjs/koa';
|
|
3
|
+
import { EggConsoleLogger } from 'egg-logger';
|
|
4
|
+
import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
|
|
5
|
+
import type { ReadyFunctionArg } from 'get-ready';
|
|
6
|
+
import { BaseContextClass } from './base_context_class.js';
|
|
7
|
+
import { Timing } from './utils/timing.js';
|
|
8
|
+
import type { Fun } from './utils/index.js';
|
|
9
|
+
import { Lifecycle } from './lifecycle.js';
|
|
10
|
+
import { EggLoader } from './loader/egg_loader.js';
|
|
11
|
+
declare const EGG_LOADER: unique symbol;
|
|
12
|
+
export interface EggCoreOptions {
|
|
13
|
+
baseDir: string;
|
|
14
|
+
type: 'application' | 'agent';
|
|
15
|
+
plugins?: any;
|
|
16
|
+
serverScope?: string;
|
|
17
|
+
env?: string;
|
|
18
|
+
}
|
|
19
|
+
export type EggCoreInitOptions = Partial<EggCoreOptions>;
|
|
20
|
+
type Middleware = (ctx: EggCoreContext, next: Next) => Promise<void> | void;
|
|
21
|
+
export type MiddlewareFunc = Middleware & {
|
|
22
|
+
_name?: string;
|
|
23
|
+
};
|
|
24
|
+
export interface EggCoreContext extends ContextDelegation {
|
|
25
|
+
app: EggCore;
|
|
26
|
+
}
|
|
27
|
+
export declare class EggCore extends KoaApplication {
|
|
28
|
+
#private;
|
|
29
|
+
options: EggCoreOptions;
|
|
30
|
+
timing: Timing;
|
|
31
|
+
console: EggConsoleLogger;
|
|
32
|
+
BaseContextClass: typeof BaseContextClass;
|
|
33
|
+
Controller: typeof BaseContextClass;
|
|
34
|
+
Service: typeof BaseContextClass;
|
|
35
|
+
Helper?: typeof BaseContextClass;
|
|
36
|
+
lifecycle: Lifecycle;
|
|
37
|
+
loader: EggLoader;
|
|
38
|
+
/** auto inject on loadService() */
|
|
39
|
+
readonly serviceClasses: Record<string, any>;
|
|
40
|
+
/** auto inject on loadController() */
|
|
41
|
+
readonly controller: Record<string, any>;
|
|
42
|
+
/** auto inject on loadMiddleware() */
|
|
43
|
+
readonly middlewares: Record<string, (opt: any, app: EggCore) => MiddlewareFunc>;
|
|
44
|
+
middleware: MiddlewareFunc[];
|
|
45
|
+
/**
|
|
46
|
+
* @class
|
|
47
|
+
* @param {Object} options - options
|
|
48
|
+
* @param {String} [options.baseDir=process.cwd()] - the directory of application
|
|
49
|
+
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
|
|
50
|
+
* @param {Object} [options.plugins] - custom plugins
|
|
51
|
+
* @since 1.0.0
|
|
52
|
+
*/
|
|
53
|
+
constructor(options?: EggCoreInitOptions);
|
|
54
|
+
/**
|
|
55
|
+
* override koa's app.use, support generator function
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
*/
|
|
58
|
+
use(fn: MiddlewareFunc): this;
|
|
59
|
+
/**
|
|
60
|
+
* Whether `application` or `agent`
|
|
61
|
+
* @member {String}
|
|
62
|
+
* @since 1.0.0
|
|
63
|
+
*/
|
|
64
|
+
get type(): "application" | "agent";
|
|
65
|
+
/**
|
|
66
|
+
* The current directory of application
|
|
67
|
+
* @member {String}
|
|
68
|
+
* @see {@link AppInfo#baseDir}
|
|
69
|
+
* @since 1.0.0
|
|
70
|
+
*/
|
|
71
|
+
get baseDir(): string;
|
|
72
|
+
/**
|
|
73
|
+
* Alias to {@link https://npmjs.com/package/depd}
|
|
74
|
+
* @member {Function}
|
|
75
|
+
* @since 1.0.0
|
|
76
|
+
*/
|
|
77
|
+
get deprecate(): (message: string) => void;
|
|
78
|
+
/**
|
|
79
|
+
* The name of application
|
|
80
|
+
* @member {String}
|
|
81
|
+
* @see {@link AppInfo#name}
|
|
82
|
+
* @since 1.0.0
|
|
83
|
+
*/
|
|
84
|
+
get name(): any;
|
|
85
|
+
/**
|
|
86
|
+
* Retrieve enabled plugins
|
|
87
|
+
* @member {Object}
|
|
88
|
+
* @since 1.0.0
|
|
89
|
+
*/
|
|
90
|
+
get plugins(): Record<string, import("./loader/egg_loader.js").EggPluginInfo>;
|
|
91
|
+
/**
|
|
92
|
+
* The configuration of application
|
|
93
|
+
* @member {Config}
|
|
94
|
+
* @since 1.0.0
|
|
95
|
+
*/
|
|
96
|
+
get config(): Record<string, any>;
|
|
97
|
+
/**
|
|
98
|
+
* Execute scope after loaded and before app start.
|
|
99
|
+
*
|
|
100
|
+
* Notice:
|
|
101
|
+
* This method is now NOT recommanded and reguarded as a deprecated one,
|
|
102
|
+
* For plugin development, we should use `didLoad` instead.
|
|
103
|
+
* For application development, we should use `willReady` instead.
|
|
104
|
+
*
|
|
105
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforestart
|
|
106
|
+
*
|
|
107
|
+
* @param {Function|AsyncFunction} scope function will execute before app start
|
|
108
|
+
* @param {string} [name] scope name, default is empty string
|
|
109
|
+
*/
|
|
110
|
+
beforeStart(scope: Fun, name?: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* register an callback function that will be invoked when application is ready.
|
|
113
|
+
* @see https://github.com/node-modules/get-ready
|
|
114
|
+
* @since 1.0.0
|
|
115
|
+
* @example
|
|
116
|
+
* const app = new Application(...);
|
|
117
|
+
* app.ready(err => {
|
|
118
|
+
* if (err) throw err;
|
|
119
|
+
* console.log('done');
|
|
120
|
+
* });
|
|
121
|
+
*/
|
|
122
|
+
ready(flagOrFunction?: ReadyFunctionArg): Promise<void> | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* If a client starts asynchronously, you can register `readyCallback`,
|
|
125
|
+
* then the application will wait for the callback to ready
|
|
126
|
+
*
|
|
127
|
+
* It will log when the callback is not invoked after 10s
|
|
128
|
+
*
|
|
129
|
+
* Recommend to use {@link EggCore#beforeStart}
|
|
130
|
+
* @since 1.0.0
|
|
131
|
+
*
|
|
132
|
+
* @param {String} name - readyCallback task name
|
|
133
|
+
* @param {object} opts -
|
|
134
|
+
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
|
|
135
|
+
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency
|
|
136
|
+
* @return {Function} - a callback
|
|
137
|
+
* @example
|
|
138
|
+
* const done = app.readyCallback('mysql');
|
|
139
|
+
* mysql.ready(done);
|
|
140
|
+
*/
|
|
141
|
+
readyCallback(name: string, opts: object): (...args: any[]) => void;
|
|
142
|
+
/**
|
|
143
|
+
* Register a function that will be called when app close.
|
|
144
|
+
*
|
|
145
|
+
* Notice:
|
|
146
|
+
* This method is now NOT recommanded directly used,
|
|
147
|
+
* Developers SHOULDN'T use app.beforeClose directly now,
|
|
148
|
+
* but in the form of class to implement beforeClose instead.
|
|
149
|
+
*
|
|
150
|
+
* @see https://eggjs.org/en/advanced/loader.html#beforeclose
|
|
151
|
+
*
|
|
152
|
+
* @param {Function} fn - the function that can be generator function or async function.
|
|
153
|
+
*/
|
|
154
|
+
beforeClose(fn: Fun): void;
|
|
155
|
+
/**
|
|
156
|
+
* Close all, it will close
|
|
157
|
+
* - callbacks registered by beforeClose
|
|
158
|
+
* - emit `close` event
|
|
159
|
+
* - remove add listeners
|
|
160
|
+
*
|
|
161
|
+
* If error is thrown when it's closing, the promise will reject.
|
|
162
|
+
* It will also reject after following call.
|
|
163
|
+
* @return {Promise} promise
|
|
164
|
+
* @since 1.0.0
|
|
165
|
+
*/
|
|
166
|
+
close(): Promise<void>;
|
|
167
|
+
/**
|
|
168
|
+
* get router
|
|
169
|
+
* @member {Router} EggCore#router
|
|
170
|
+
* @since 1.0.0
|
|
171
|
+
*/
|
|
172
|
+
get router(): Router;
|
|
173
|
+
/**
|
|
174
|
+
* Alias to {@link Router#url}
|
|
175
|
+
* @param {String} name - Router name
|
|
176
|
+
* @param {Object} params - more parameters
|
|
177
|
+
* @return {String} url
|
|
178
|
+
*/
|
|
179
|
+
url(name: string, params?: any): string;
|
|
180
|
+
head(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
181
|
+
head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
182
|
+
get(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
183
|
+
get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
184
|
+
put(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
185
|
+
put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
186
|
+
patch(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
187
|
+
patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
188
|
+
post(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
189
|
+
post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
190
|
+
delete(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
191
|
+
delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
192
|
+
del(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
193
|
+
del(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
194
|
+
all(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
195
|
+
all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
|
|
196
|
+
resources(prefix: string, controller: string | ResourcesController): EggCore;
|
|
197
|
+
resources(prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
|
|
198
|
+
resources(name: string, prefix: string, controller: string | ResourcesController): EggCore;
|
|
199
|
+
resources(name: string, prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
|
|
200
|
+
redirect(source: string, destination: string, status?: number): this;
|
|
201
|
+
register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | MiddlewareFunc[], opts?: RegisterOptions): this;
|
|
202
|
+
get [EGG_LOADER](): typeof EggLoader;
|
|
203
|
+
}
|
|
204
|
+
export {};
|