@eggjs/koa 2.14.2 → 2.15.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/Readme.md +8 -100
- package/lib/application.d.ts +110 -0
- package/lib/application.js +250 -304
- package/lib/context.d.ts +104 -0
- package/lib/context.js +212 -242
- package/lib/request.d.ts +317 -0
- package/lib/request.js +525 -724
- package/lib/response.d.ts +210 -0
- package/lib/response.js +441 -586
- package/package.json +26 -43
- package/dist/koa.mjs +0 -4
package/Readme.md
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
@eggjs/koa is forked from [Koa v2.x](https://github.com/koajs/koa/tree/v2.x) for LTS and drop Node.js < 16.13.0 support.
|
|
4
4
|
|
|
5
|
-
<img src="/docs/logo.png" alt="Koa middleware framework for nodejs"/>
|
|
5
|
+
<img height="240px" src="/docs/logo.png" alt="Koa middleware framework for nodejs"/>
|
|
6
6
|
|
|
7
7
|
[](https://npmjs.org/package/@eggjs/koa)
|
|
8
8
|
[](http://packagequality.com/#?package=@eggjs/koa)
|
|
9
9
|
[](https://npmjs.org/package/@eggjs/koa)
|
|
10
10
|
[](https://app.fossa.com/projects/git%2Bgithub.com%2Feggjs%2Fkoa?ref=badge_shield)
|
|
11
|
-
[](https://github.com/eggjs/koa/actions/workflows/node.js.yml)
|
|
12
12
|
[](https://codecov.io/gh/eggjs/koa)
|
|
13
13
|
[](https://snyk.io/test/npm/@eggjs/koa)
|
|
14
14
|
|
|
@@ -29,7 +29,7 @@ npm install @eggjs/koa
|
|
|
29
29
|
|
|
30
30
|
## Hello Koa
|
|
31
31
|
|
|
32
|
-
```
|
|
32
|
+
```ts
|
|
33
33
|
const Koa = require('@eggjs/koa');
|
|
34
34
|
const app = new Koa();
|
|
35
35
|
|
|
@@ -56,9 +56,9 @@ Koa is a middleware framework that can take two different kinds of functions as
|
|
|
56
56
|
|
|
57
57
|
Here is an example of logger middleware with each of the different functions:
|
|
58
58
|
|
|
59
|
-
### ___async___ functions
|
|
59
|
+
### ___async___ functions
|
|
60
60
|
|
|
61
|
-
```
|
|
61
|
+
```ts
|
|
62
62
|
app.use(async (ctx, next) => {
|
|
63
63
|
const start = Date.now();
|
|
64
64
|
await next();
|
|
@@ -69,7 +69,7 @@ app.use(async (ctx, next) => {
|
|
|
69
69
|
|
|
70
70
|
### Common function
|
|
71
71
|
|
|
72
|
-
```
|
|
72
|
+
```ts
|
|
73
73
|
// Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
|
|
74
74
|
// next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
|
|
75
75
|
|
|
@@ -82,15 +82,6 @@ app.use((ctx, next) => {
|
|
|
82
82
|
});
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
### Koa v1.x Middleware Signature
|
|
86
|
-
|
|
87
|
-
The middleware signature changed between v1.x and v2.x. The older signature is deprecated.
|
|
88
|
-
|
|
89
|
-
__Old signature middleware support will be removed in v3__
|
|
90
|
-
|
|
91
|
-
Please see the [Migration Guide](docs/migration.md) for more information on upgrading from v1.x and
|
|
92
|
-
using v1.x middleware with v2.x.
|
|
93
|
-
|
|
94
85
|
## Context, Request and Response
|
|
95
86
|
|
|
96
87
|
Each middleware receives a Koa `Context` object that encapsulates an incoming
|
|
@@ -110,7 +101,7 @@ from the node `http` module.
|
|
|
110
101
|
|
|
111
102
|
Here is an example of checking that a requesting client supports xml.
|
|
112
103
|
|
|
113
|
-
```
|
|
104
|
+
```ts
|
|
114
105
|
app.use(async (ctx, next) => {
|
|
115
106
|
ctx.assert(ctx.request.accepts('xml'), 406);
|
|
116
107
|
// equivalent to:
|
|
@@ -132,7 +123,7 @@ accessed as the `res` property on the `Context`.
|
|
|
132
123
|
|
|
133
124
|
Here is an example using Koa's `Response` object to stream a file as the response body.
|
|
134
125
|
|
|
135
|
-
```
|
|
126
|
+
```ts
|
|
136
127
|
app.use(async (ctx, next) => {
|
|
137
128
|
await next();
|
|
138
129
|
ctx.response.type = 'xml';
|
|
@@ -186,93 +177,10 @@ See [AUTHORS](AUTHORS).
|
|
|
186
177
|
|
|
187
178
|
## Community
|
|
188
179
|
|
|
189
|
-
- [Badgeboard](https://koajs.github.io/badgeboard) and list of official modules
|
|
190
180
|
- [Examples](https://github.com/koajs/examples)
|
|
191
181
|
- [Middleware](https://github.com/koajs/koa/wiki) list
|
|
192
182
|
- [Wiki](https://github.com/koajs/koa/wiki)
|
|
193
|
-
- [Reddit Community](https://www.reddit.com/r/koajs)
|
|
194
|
-
- [Mailing list](https://groups.google.com/forum/#!forum/koajs)
|
|
195
|
-
- [中文文档 v1.x](https://github.com/guo-yu/koa-guide)
|
|
196
183
|
- [中文文档 v2.x](https://github.com/demopark/koa-docs-Zh-CN)
|
|
197
|
-
- __[#koajs]__ on freenode
|
|
198
|
-
|
|
199
|
-
## Job Board
|
|
200
|
-
|
|
201
|
-
Looking for a career upgrade?
|
|
202
|
-
|
|
203
|
-
<a href="https://astro.netlify.com/automattic"><img src="https://astro.netlify.com/static/automattic.png"></a>
|
|
204
|
-
<a href="https://astro.netlify.com/segment"><img src="https://astro.netlify.com/static/segment.png"></a>
|
|
205
|
-
<a href="https://astro.netlify.com/auth0"><img src="https://astro.netlify.com/static/auth0.png"/></a>
|
|
206
|
-
|
|
207
|
-
## Backers
|
|
208
|
-
|
|
209
|
-
Support us with a monthly donation and help us continue our activities.
|
|
210
|
-
|
|
211
|
-
<a href="https://opencollective.com/koajs/backer/0/website" target="_blank"><img src="https://opencollective.com/koajs/backer/0/avatar.svg"></a>
|
|
212
|
-
<a href="https://opencollective.com/koajs/backer/1/website" target="_blank"><img src="https://opencollective.com/koajs/backer/1/avatar.svg"></a>
|
|
213
|
-
<a href="https://opencollective.com/koajs/backer/2/website" target="_blank"><img src="https://opencollective.com/koajs/backer/2/avatar.svg"></a>
|
|
214
|
-
<a href="https://opencollective.com/koajs/backer/3/website" target="_blank"><img src="https://opencollective.com/koajs/backer/3/avatar.svg"></a>
|
|
215
|
-
<a href="https://opencollective.com/koajs/backer/4/website" target="_blank"><img src="https://opencollective.com/koajs/backer/4/avatar.svg"></a>
|
|
216
|
-
<a href="https://opencollective.com/koajs/backer/5/website" target="_blank"><img src="https://opencollective.com/koajs/backer/5/avatar.svg"></a>
|
|
217
|
-
<a href="https://opencollective.com/koajs/backer/6/website" target="_blank"><img src="https://opencollective.com/koajs/backer/6/avatar.svg"></a>
|
|
218
|
-
<a href="https://opencollective.com/koajs/backer/7/website" target="_blank"><img src="https://opencollective.com/koajs/backer/7/avatar.svg"></a>
|
|
219
|
-
<a href="https://opencollective.com/koajs/backer/8/website" target="_blank"><img src="https://opencollective.com/koajs/backer/8/avatar.svg"></a>
|
|
220
|
-
<a href="https://opencollective.com/koajs/backer/9/website" target="_blank"><img src="https://opencollective.com/koajs/backer/9/avatar.svg"></a>
|
|
221
|
-
<a href="https://opencollective.com/koajs/backer/10/website" target="_blank"><img src="https://opencollective.com/koajs/backer/10/avatar.svg"></a>
|
|
222
|
-
<a href="https://opencollective.com/koajs/backer/11/website" target="_blank"><img src="https://opencollective.com/koajs/backer/11/avatar.svg"></a>
|
|
223
|
-
<a href="https://opencollective.com/koajs/backer/12/website" target="_blank"><img src="https://opencollective.com/koajs/backer/12/avatar.svg"></a>
|
|
224
|
-
<a href="https://opencollective.com/koajs/backer/13/website" target="_blank"><img src="https://opencollective.com/koajs/backer/13/avatar.svg"></a>
|
|
225
|
-
<a href="https://opencollective.com/koajs/backer/14/website" target="_blank"><img src="https://opencollective.com/koajs/backer/14/avatar.svg"></a>
|
|
226
|
-
<a href="https://opencollective.com/koajs/backer/15/website" target="_blank"><img src="https://opencollective.com/koajs/backer/15/avatar.svg"></a>
|
|
227
|
-
<a href="https://opencollective.com/koajs/backer/16/website" target="_blank"><img src="https://opencollective.com/koajs/backer/16/avatar.svg"></a>
|
|
228
|
-
<a href="https://opencollective.com/koajs/backer/17/website" target="_blank"><img src="https://opencollective.com/koajs/backer/17/avatar.svg"></a>
|
|
229
|
-
<a href="https://opencollective.com/koajs/backer/18/website" target="_blank"><img src="https://opencollective.com/koajs/backer/18/avatar.svg"></a>
|
|
230
|
-
<a href="https://opencollective.com/koajs/backer/19/website" target="_blank"><img src="https://opencollective.com/koajs/backer/19/avatar.svg"></a>
|
|
231
|
-
<a href="https://opencollective.com/koajs/backer/20/website" target="_blank"><img src="https://opencollective.com/koajs/backer/20/avatar.svg"></a>
|
|
232
|
-
<a href="https://opencollective.com/koajs/backer/21/website" target="_blank"><img src="https://opencollective.com/koajs/backer/21/avatar.svg"></a>
|
|
233
|
-
<a href="https://opencollective.com/koajs/backer/22/website" target="_blank"><img src="https://opencollective.com/koajs/backer/22/avatar.svg"></a>
|
|
234
|
-
<a href="https://opencollective.com/koajs/backer/23/website" target="_blank"><img src="https://opencollective.com/koajs/backer/23/avatar.svg"></a>
|
|
235
|
-
<a href="https://opencollective.com/koajs/backer/24/website" target="_blank"><img src="https://opencollective.com/koajs/backer/24/avatar.svg"></a>
|
|
236
|
-
<a href="https://opencollective.com/koajs/backer/25/website" target="_blank"><img src="https://opencollective.com/koajs/backer/25/avatar.svg"></a>
|
|
237
|
-
<a href="https://opencollective.com/koajs/backer/26/website" target="_blank"><img src="https://opencollective.com/koajs/backer/26/avatar.svg"></a>
|
|
238
|
-
<a href="https://opencollective.com/koajs/backer/27/website" target="_blank"><img src="https://opencollective.com/koajs/backer/27/avatar.svg"></a>
|
|
239
|
-
<a href="https://opencollective.com/koajs/backer/28/website" target="_blank"><img src="https://opencollective.com/koajs/backer/28/avatar.svg"></a>
|
|
240
|
-
<a href="https://opencollective.com/koajs/backer/29/website" target="_blank"><img src="https://opencollective.com/koajs/backer/29/avatar.svg"></a>
|
|
241
|
-
|
|
242
|
-
## Sponsors
|
|
243
|
-
|
|
244
|
-
Become a sponsor and get your logo on our README on Github with a link to your site.
|
|
245
|
-
|
|
246
|
-
<a href="https://opencollective.com/koajs/sponsor/0/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/0/avatar.svg"></a>
|
|
247
|
-
<a href="https://opencollective.com/koajs/sponsor/1/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/1/avatar.svg"></a>
|
|
248
|
-
<a href="https://opencollective.com/koajs/sponsor/2/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/2/avatar.svg"></a>
|
|
249
|
-
<a href="https://opencollective.com/koajs/sponsor/3/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/3/avatar.svg"></a>
|
|
250
|
-
<a href="https://opencollective.com/koajs/sponsor/4/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/4/avatar.svg"></a>
|
|
251
|
-
<a href="https://opencollective.com/koajs/sponsor/5/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/5/avatar.svg"></a>
|
|
252
|
-
<a href="https://opencollective.com/koajs/sponsor/6/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/6/avatar.svg"></a>
|
|
253
|
-
<a href="https://opencollective.com/koajs/sponsor/7/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/7/avatar.svg"></a>
|
|
254
|
-
<a href="https://opencollective.com/koajs/sponsor/8/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/8/avatar.svg"></a>
|
|
255
|
-
<a href="https://opencollective.com/koajs/sponsor/9/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/9/avatar.svg"></a>
|
|
256
|
-
<a href="https://opencollective.com/koajs/sponsor/10/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/10/avatar.svg"></a>
|
|
257
|
-
<a href="https://opencollective.com/koajs/sponsor/11/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/11/avatar.svg"></a>
|
|
258
|
-
<a href="https://opencollective.com/koajs/sponsor/12/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/12/avatar.svg"></a>
|
|
259
|
-
<a href="https://opencollective.com/koajs/sponsor/13/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/13/avatar.svg"></a>
|
|
260
|
-
<a href="https://opencollective.com/koajs/sponsor/14/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/14/avatar.svg"></a>
|
|
261
|
-
<a href="https://opencollective.com/koajs/sponsor/15/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/15/avatar.svg"></a>
|
|
262
|
-
<a href="https://opencollective.com/koajs/sponsor/16/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/16/avatar.svg"></a>
|
|
263
|
-
<a href="https://opencollective.com/koajs/sponsor/17/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/17/avatar.svg"></a>
|
|
264
|
-
<a href="https://opencollective.com/koajs/sponsor/18/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/18/avatar.svg"></a>
|
|
265
|
-
<a href="https://opencollective.com/koajs/sponsor/19/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/19/avatar.svg"></a>
|
|
266
|
-
<a href="https://opencollective.com/koajs/sponsor/20/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/20/avatar.svg"></a>
|
|
267
|
-
<a href="https://opencollective.com/koajs/sponsor/21/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/21/avatar.svg"></a>
|
|
268
|
-
<a href="https://opencollective.com/koajs/sponsor/22/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/22/avatar.svg"></a>
|
|
269
|
-
<a href="https://opencollective.com/koajs/sponsor/23/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/23/avatar.svg"></a>
|
|
270
|
-
<a href="https://opencollective.com/koajs/sponsor/24/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/24/avatar.svg"></a>
|
|
271
|
-
<a href="https://opencollective.com/koajs/sponsor/25/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/25/avatar.svg"></a>
|
|
272
|
-
<a href="https://opencollective.com/koajs/sponsor/26/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/26/avatar.svg"></a>
|
|
273
|
-
<a href="https://opencollective.com/koajs/sponsor/27/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/27/avatar.svg"></a>
|
|
274
|
-
<a href="https://opencollective.com/koajs/sponsor/28/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/28/avatar.svg"></a>
|
|
275
|
-
<a href="https://opencollective.com/koajs/sponsor/29/website" target="_blank"><img src="https://opencollective.com/koajs/sponsor/29/avatar.svg"></a>
|
|
276
184
|
|
|
277
185
|
# License
|
|
278
186
|
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
import Emitter from 'node:events';
|
|
6
|
+
import util from 'node:util';
|
|
7
|
+
import http from 'node:http';
|
|
8
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
9
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
10
|
+
import Context from './context';
|
|
11
|
+
import Request from './request';
|
|
12
|
+
import Response from './response';
|
|
13
|
+
import type { ContextDelegation } from './context';
|
|
14
|
+
import type { CustomError, ProtoImplClass, AnyProto } from './types';
|
|
15
|
+
export type Next = () => Promise<void>;
|
|
16
|
+
export type MiddlewareFunc = (ctx: ContextDelegation, next: Next) => Promise<void> | void;
|
|
17
|
+
export type { ContextDelegation as Context } from './context';
|
|
18
|
+
export type { CustomError, ProtoImplClass } from './types';
|
|
19
|
+
/**
|
|
20
|
+
* Expose `Application` class.
|
|
21
|
+
* Inherits from `Emitter.prototype`.
|
|
22
|
+
*/
|
|
23
|
+
export default class Application extends Emitter {
|
|
24
|
+
#private;
|
|
25
|
+
/**
|
|
26
|
+
* Make HttpError available to consumers of the library so that consumers don't
|
|
27
|
+
* have a direct dependency upon `http-errors`
|
|
28
|
+
*/
|
|
29
|
+
static HttpError: any;
|
|
30
|
+
proxy: boolean;
|
|
31
|
+
subdomainOffset: number;
|
|
32
|
+
proxyIpHeader: string;
|
|
33
|
+
maxIpsCount: number;
|
|
34
|
+
env: string;
|
|
35
|
+
keys?: string[];
|
|
36
|
+
middleware: MiddlewareFunc[];
|
|
37
|
+
ctxStorage: AsyncLocalStorage<ContextDelegation>;
|
|
38
|
+
silent: boolean;
|
|
39
|
+
ContextClass: ProtoImplClass<Context>;
|
|
40
|
+
context: AnyProto;
|
|
41
|
+
RequestClass: ProtoImplClass<Request>;
|
|
42
|
+
request: AnyProto;
|
|
43
|
+
ResponseClass: ProtoImplClass<Response>;
|
|
44
|
+
response: AnyProto;
|
|
45
|
+
/**
|
|
46
|
+
* Initialize a new `Application`.
|
|
47
|
+
*
|
|
48
|
+
* @param {object} [options] Application options
|
|
49
|
+
* @param {string} [options.env='development'] Environment
|
|
50
|
+
* @param {string[]} [options.keys] Signed cookie keys
|
|
51
|
+
* @param {boolean} [options.proxy] Trust proxy headers
|
|
52
|
+
* @param {number} [options.subdomainOffset] Subdomain offset
|
|
53
|
+
* @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
|
|
54
|
+
* @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
|
|
55
|
+
*/
|
|
56
|
+
constructor(options?: {
|
|
57
|
+
proxy?: boolean;
|
|
58
|
+
subdomainOffset?: number;
|
|
59
|
+
proxyIpHeader?: string;
|
|
60
|
+
maxIpsCount?: number;
|
|
61
|
+
env?: string;
|
|
62
|
+
keys?: string[];
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* Shorthand for:
|
|
66
|
+
*
|
|
67
|
+
* http.createServer(app.callback()).listen(...)
|
|
68
|
+
*/
|
|
69
|
+
listen(...args: any[]): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
70
|
+
/**
|
|
71
|
+
* Return JSON representation.
|
|
72
|
+
* We only bother showing settings.
|
|
73
|
+
*/
|
|
74
|
+
toJSON(): any;
|
|
75
|
+
/**
|
|
76
|
+
* Inspect implementation.
|
|
77
|
+
*/
|
|
78
|
+
inspect(): any;
|
|
79
|
+
[util.inspect.custom](): any;
|
|
80
|
+
/**
|
|
81
|
+
* Use the given middleware `fn`.
|
|
82
|
+
*
|
|
83
|
+
* Old-style middleware will be converted.
|
|
84
|
+
*/
|
|
85
|
+
use(fn: MiddlewareFunc): this;
|
|
86
|
+
/**
|
|
87
|
+
* Return a request handler callback
|
|
88
|
+
* for node's native http server.
|
|
89
|
+
*/
|
|
90
|
+
callback(): (req: IncomingMessage, res: ServerResponse) => Promise<void | http.ServerResponse<http.IncomingMessage>>;
|
|
91
|
+
/**
|
|
92
|
+
* return currnect contenxt from async local storage
|
|
93
|
+
*/
|
|
94
|
+
get currentContext(): ContextDelegation | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Initialize a new context.
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
protected createContext(req: IncomingMessage, res: ServerResponse): ContextDelegation;
|
|
100
|
+
/**
|
|
101
|
+
* Default error handler.
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
protected onerror(err: CustomError): void;
|
|
105
|
+
createAsyncCtxStorageMiddleware(): (ctx: ContextDelegation, next: Next) => Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Response helper.
|
|
108
|
+
*/
|
|
109
|
+
protected _respond(ctx: ContextDelegation): http.ServerResponse<http.IncomingMessage> | undefined;
|
|
110
|
+
}
|