@eggjs/koa 2.20.2 → 2.20.3
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/application.d.ts +14 -14
- package/dist/commonjs/application.js +1 -2
- package/dist/commonjs/context.d.ts +74 -1
- package/dist/commonjs/context.js +184 -56
- package/dist/commonjs/request.d.ts +7 -7
- package/dist/commonjs/request.js +1 -1
- package/dist/commonjs/response.d.ts +8 -6
- package/dist/commonjs/response.js +9 -4
- package/dist/esm/application.d.ts +14 -14
- package/dist/esm/application.js +1 -2
- package/dist/esm/context.d.ts +74 -1
- package/dist/esm/context.js +184 -56
- package/dist/esm/request.d.ts +7 -7
- package/dist/esm/request.js +1 -1
- package/dist/esm/response.d.ts +8 -6
- package/dist/esm/response.js +9 -4
- package/dist/package.json +1 -1
- package/package.json +9 -8
- package/src/application.ts +15 -17
- package/src/context.ts +259 -66
- package/src/request.ts +6 -6
- package/src/response.ts +17 -9
package/src/context.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import util from 'node:util';
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
+
import { ParsedUrlQuery } from 'node:querystring';
|
|
3
4
|
import createError from 'http-errors';
|
|
4
5
|
import httpAssert from 'http-assert';
|
|
5
|
-
import delegate from 'delegates';
|
|
6
6
|
import statuses from 'statuses';
|
|
7
7
|
import Cookies from 'cookies';
|
|
8
|
+
import { type Accepts } from 'accepts';
|
|
8
9
|
import type { Application } from './application.js';
|
|
9
10
|
import type { Request } from './request.js';
|
|
10
11
|
import type { Response } from './response.js';
|
|
@@ -222,69 +223,261 @@ export class Context {
|
|
|
222
223
|
get state() {
|
|
223
224
|
return this.#state;
|
|
224
225
|
}
|
|
225
|
-
}
|
|
226
226
|
|
|
227
|
-
/**
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Request delegation.
|
|
229
|
+
*/
|
|
230
|
+
|
|
231
|
+
acceptsLanguages(): string[];
|
|
232
|
+
acceptsLanguages(languages: string[]): string | false;
|
|
233
|
+
acceptsLanguages(...languages: string[]): string | false;
|
|
234
|
+
acceptsLanguages(languages?: string | string[], ...others: string[]): string | string[] | false {
|
|
235
|
+
return this.request.acceptsLanguages(languages as any, ...others);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
acceptsEncodings(): string[];
|
|
239
|
+
acceptsEncodings(encodings: string[]): string | false;
|
|
240
|
+
acceptsEncodings(...encodings: string[]): string | false;
|
|
241
|
+
acceptsEncodings(encodings?: string | string[], ...others: string[]): string[] | string | false {
|
|
242
|
+
return this.request.acceptsEncodings(encodings as any, ...others);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
acceptsCharsets(): string[];
|
|
246
|
+
acceptsCharsets(charsets: string[]): string | false;
|
|
247
|
+
acceptsCharsets(...charsets: string[]): string | false;
|
|
248
|
+
acceptsCharsets(charsets?: string | string[], ...others: string[]): string[] | string | false {
|
|
249
|
+
return this.request.acceptsCharsets(charsets as any, ...others);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
accepts(...args: Parameters<Request['accepts']>): string | string[] | false {
|
|
253
|
+
return this.request.accepts(...args);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
get<T = string | string []>(field: string): T {
|
|
257
|
+
return this.request.get(field);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
is(type?: string | string[], ...types: string[]): string | false | null {
|
|
261
|
+
return this.request.is(type, ...types);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
get querystring(): string {
|
|
265
|
+
return this.request.querystring;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
set querystring(str: string) {
|
|
269
|
+
this.request.querystring = str;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
get idempotent(): boolean {
|
|
273
|
+
return this.request.idempotent;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
get socket() {
|
|
277
|
+
return this.request.socket;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
get search(): string {
|
|
281
|
+
return this.request.search;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
set search(str: string) {
|
|
285
|
+
this.request.search = str;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
get method(): string {
|
|
289
|
+
return this.request.method;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
set method(method: string) {
|
|
293
|
+
this.request.method = method;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
get query(): ParsedUrlQuery {
|
|
297
|
+
return this.request.query;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
set query(obj: ParsedUrlQuery) {
|
|
301
|
+
this.request.query = obj;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
get path(): string {
|
|
305
|
+
return this.request.path;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
set path(path: string) {
|
|
309
|
+
this.request.path = path;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
get url(): string {
|
|
313
|
+
return this.request.url;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
set url(url: string) {
|
|
317
|
+
this.request.url = url;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
get accept(): Accepts {
|
|
321
|
+
return this.request.accept;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
set accept(accept: Accepts) {
|
|
325
|
+
this.request.accept = accept;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
get origin(): string {
|
|
329
|
+
return this.request.origin;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
get href(): string {
|
|
333
|
+
return this.request.href;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
get subdomains(): string[] {
|
|
337
|
+
return this.request.subdomains;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
get protocol(): string {
|
|
341
|
+
return this.request.protocol;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
get host(): string {
|
|
345
|
+
return this.request.host;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
get hostname(): string {
|
|
349
|
+
return this.request.hostname;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
get URL(): URL {
|
|
353
|
+
return this.request.URL;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
get header() {
|
|
357
|
+
return this.request.header;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
get headers() {
|
|
361
|
+
return this.request.headers;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
get secure(): boolean {
|
|
365
|
+
return this.request.secure;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
get stale(): boolean {
|
|
369
|
+
return this.request.stale;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
get fresh(): boolean {
|
|
373
|
+
return this.request.fresh;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
get ips(): string[] {
|
|
377
|
+
return this.request.ips;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
get ip(): string {
|
|
381
|
+
return this.request.ip;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Response delegation.
|
|
386
|
+
*/
|
|
387
|
+
|
|
388
|
+
attachment(...args: Parameters<Response['attachment']>) {
|
|
389
|
+
return this.response.attachment(...args);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
redirect(...args: Parameters<Response['redirect']>) {
|
|
393
|
+
return this.response.redirect(...args);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
remove(...args: Parameters<Response['remove']>) {
|
|
397
|
+
return this.response.remove(...args);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
vary(...args: Parameters<Response['vary']>) {
|
|
401
|
+
return this.response.vary(...args);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
has(...args: Parameters<Response['has']>) {
|
|
405
|
+
return this.response.has(...args);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
set(...args: Parameters<Response['set']>) {
|
|
409
|
+
return this.response.set(...args);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
append(...args: Parameters<Response['append']>) {
|
|
413
|
+
return this.response.append(...args);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
flushHeaders(...args: Parameters<Response['flushHeaders']>) {
|
|
417
|
+
return this.response.flushHeaders(...args);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
get status() {
|
|
421
|
+
return this.response.status;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
set status(status: number) {
|
|
425
|
+
this.response.status = status;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
get message() {
|
|
429
|
+
return this.response.message;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
set message(msg: string) {
|
|
433
|
+
this.response.message = msg;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
get body(): any {
|
|
437
|
+
return this.response.body;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
set body(val: any) {
|
|
441
|
+
this.response.body = val;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
get length(): number | undefined {
|
|
445
|
+
return this.response.length;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
set length(n: number | string | undefined) {
|
|
449
|
+
this.response.length = n;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
get type(): string {
|
|
453
|
+
return this.response.type;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
set type(type: string | null | undefined) {
|
|
457
|
+
this.response.type = type;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
get lastModified() {
|
|
461
|
+
return this.response.lastModified;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
set lastModified(val: string | Date | undefined) {
|
|
465
|
+
this.response.lastModified = val;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
get etag() {
|
|
469
|
+
return this.response.etag;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
set etag(val: string) {
|
|
473
|
+
this.response.etag = val;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
get headerSent() {
|
|
477
|
+
return this.response.headerSent;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
get writable() {
|
|
481
|
+
return this.response.writable;
|
|
482
|
+
}
|
|
483
|
+
}
|
package/src/request.ts
CHANGED
|
@@ -9,23 +9,23 @@ import parse from 'parseurl';
|
|
|
9
9
|
import typeis from 'type-is';
|
|
10
10
|
import fresh from 'fresh';
|
|
11
11
|
import type { Application } from './application.js';
|
|
12
|
-
import type {
|
|
12
|
+
import type { Context } from './context.js';
|
|
13
13
|
import type { Response } from './response.js';
|
|
14
14
|
|
|
15
15
|
export interface RequestSocket extends Socket {
|
|
16
16
|
encrypted: boolean;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class Request {
|
|
19
|
+
export class Request<T extends Context = Context> {
|
|
20
20
|
[key: symbol]: unknown;
|
|
21
21
|
app: Application;
|
|
22
22
|
req: IncomingMessage;
|
|
23
23
|
res: ServerResponse;
|
|
24
|
-
ctx:
|
|
24
|
+
ctx: T;
|
|
25
25
|
response: Response;
|
|
26
26
|
originalUrl: string;
|
|
27
27
|
|
|
28
|
-
constructor(app: Application, ctx:
|
|
28
|
+
constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) {
|
|
29
29
|
this.app = app;
|
|
30
30
|
this.req = req;
|
|
31
31
|
this.res = res;
|
|
@@ -142,7 +142,7 @@ export class Request {
|
|
|
142
142
|
/**
|
|
143
143
|
* Get parsed query string.
|
|
144
144
|
*/
|
|
145
|
-
get query() {
|
|
145
|
+
get query(): ParsedUrlQuery {
|
|
146
146
|
const str = this.querystring;
|
|
147
147
|
if (!this._parsedUrlQueryCache) {
|
|
148
148
|
this._parsedUrlQueryCache = {};
|
|
@@ -158,7 +158,7 @@ export class Request {
|
|
|
158
158
|
* Set query string as an object.
|
|
159
159
|
*/
|
|
160
160
|
|
|
161
|
-
set query(obj) {
|
|
161
|
+
set query(obj: ParsedUrlQuery) {
|
|
162
162
|
this.querystring = qs.stringify(obj);
|
|
163
163
|
}
|
|
164
164
|
|
package/src/response.ts
CHANGED
|
@@ -13,18 +13,18 @@ import destroy from 'destroy';
|
|
|
13
13
|
import vary from 'vary';
|
|
14
14
|
import encodeUrl from 'encodeurl';
|
|
15
15
|
import type { Application } from './application.js';
|
|
16
|
-
import type {
|
|
16
|
+
import type { Context } from './context.js';
|
|
17
17
|
import type { Request } from './request.js';
|
|
18
18
|
|
|
19
|
-
export class Response {
|
|
19
|
+
export class Response<T extends Context = Context> {
|
|
20
20
|
[key: symbol]: unknown;
|
|
21
21
|
app: Application;
|
|
22
22
|
req: IncomingMessage;
|
|
23
23
|
res: ServerResponse;
|
|
24
|
-
ctx:
|
|
24
|
+
ctx: T;
|
|
25
25
|
request: Request;
|
|
26
26
|
|
|
27
|
-
constructor(app: Application, ctx:
|
|
27
|
+
constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) {
|
|
28
28
|
this.app = app;
|
|
29
29
|
this.req = req;
|
|
30
30
|
this.res = res;
|
|
@@ -168,16 +168,24 @@ export class Response {
|
|
|
168
168
|
|
|
169
169
|
/**
|
|
170
170
|
* Return parsed response Content-Length when present.
|
|
171
|
+
*
|
|
172
|
+
* When Content-Length is not defined it will return `undefined`.
|
|
171
173
|
*/
|
|
172
|
-
get length() {
|
|
174
|
+
get length(): number | undefined {
|
|
173
175
|
if (this.has('Content-Length')) {
|
|
174
176
|
return parseInt(this.get('Content-Length'), 10) || 0;
|
|
175
177
|
}
|
|
176
178
|
|
|
177
179
|
const { body } = this;
|
|
178
|
-
if (!body || body instanceof Stream)
|
|
179
|
-
|
|
180
|
-
|
|
180
|
+
if (!body || body instanceof Stream) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
if (typeof body === 'string') {
|
|
184
|
+
return Buffer.byteLength(body);
|
|
185
|
+
}
|
|
186
|
+
if (Buffer.isBuffer(body)) {
|
|
187
|
+
return body.length;
|
|
188
|
+
}
|
|
181
189
|
return Buffer.byteLength(JSON.stringify(body));
|
|
182
190
|
}
|
|
183
191
|
|
|
@@ -270,7 +278,7 @@ export class Response {
|
|
|
270
278
|
* Return the response mime type void of
|
|
271
279
|
* parameters such as "charset".
|
|
272
280
|
*/
|
|
273
|
-
get type() {
|
|
281
|
+
get type(): string {
|
|
274
282
|
const type = this.get<string>('Content-Type');
|
|
275
283
|
if (!type) return '';
|
|
276
284
|
return type.split(';', 1)[0];
|