@eggjs/koa 2.20.1 → 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 +76 -2
- package/dist/commonjs/context.js +188 -58
- package/dist/commonjs/request.d.ts +11 -10
- 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 +76 -2
- package/dist/esm/context.js +188 -58
- package/dist/esm/request.d.ts +11 -10
- 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 +264 -68
- package/src/request.ts +11 -7
- package/src/response.ts +17 -9
package/src/application.ts
CHANGED
|
@@ -11,7 +11,7 @@ import onFinished from 'on-finished';
|
|
|
11
11
|
import statuses from 'statuses';
|
|
12
12
|
import compose from 'koa-compose';
|
|
13
13
|
import { HttpError } from 'http-errors';
|
|
14
|
-
import { Context
|
|
14
|
+
import { Context } from './context.js';
|
|
15
15
|
import { Request } from './request.js';
|
|
16
16
|
import { Response } from './response.js';
|
|
17
17
|
import type { CustomError, AnyProto } from './types.js';
|
|
@@ -21,13 +21,13 @@ const debug = debuglog('@eggjs/koa/application');
|
|
|
21
21
|
export type ProtoImplClass<T = object> = new(...args: any[]) => T;
|
|
22
22
|
export type Next = () => Promise<void>;
|
|
23
23
|
type _MiddlewareFunc<T> = (ctx: T, next: Next) => Promise<void> | void;
|
|
24
|
-
export type MiddlewareFunc<T =
|
|
24
|
+
export type MiddlewareFunc<T extends Context = Context> = _MiddlewareFunc<T> & { _name?: string };
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Expose `Application` class.
|
|
28
28
|
* Inherits from `Emitter.prototype`.
|
|
29
29
|
*/
|
|
30
|
-
export class Application extends Emitter {
|
|
30
|
+
export class Application<T extends Context = Context> extends Emitter {
|
|
31
31
|
[key: symbol]: unknown;
|
|
32
32
|
/**
|
|
33
33
|
* Make HttpError available to consumers of the library so that consumers don't
|
|
@@ -41,14 +41,14 @@ export class Application extends Emitter {
|
|
|
41
41
|
proxyIpHeader: string;
|
|
42
42
|
maxIpsCount: number;
|
|
43
43
|
protected _keys?: string[];
|
|
44
|
-
middleware: MiddlewareFunc[];
|
|
45
|
-
ctxStorage: AsyncLocalStorage<
|
|
44
|
+
middleware: MiddlewareFunc<T>[];
|
|
45
|
+
ctxStorage: AsyncLocalStorage<T>;
|
|
46
46
|
silent: boolean;
|
|
47
|
-
ContextClass: ProtoImplClass<
|
|
47
|
+
ContextClass: ProtoImplClass<T>;
|
|
48
48
|
context: AnyProto;
|
|
49
|
-
RequestClass: ProtoImplClass<Request
|
|
49
|
+
RequestClass: ProtoImplClass<Request<T>>;
|
|
50
50
|
request: AnyProto;
|
|
51
|
-
ResponseClass: ProtoImplClass<Response
|
|
51
|
+
ResponseClass: ProtoImplClass<Response<T>>;
|
|
52
52
|
response: AnyProto;
|
|
53
53
|
|
|
54
54
|
/**
|
|
@@ -84,11 +84,11 @@ export class Application extends Emitter {
|
|
|
84
84
|
this.middleware = [];
|
|
85
85
|
this.ctxStorage = getAsyncLocalStorage();
|
|
86
86
|
this.silent = false;
|
|
87
|
-
this.ContextClass = class ApplicationContext extends Context {} as
|
|
87
|
+
this.ContextClass = class ApplicationContext extends Context {} as ProtoImplClass<T>;
|
|
88
88
|
this.context = this.ContextClass.prototype;
|
|
89
|
-
this.RequestClass = class ApplicationRequest extends Request {}
|
|
89
|
+
this.RequestClass = class ApplicationRequest extends Request {} as ProtoImplClass<Request<T>>;
|
|
90
90
|
this.request = this.RequestClass.prototype;
|
|
91
|
-
this.ResponseClass = class ApplicationResponse extends Response {}
|
|
91
|
+
this.ResponseClass = class ApplicationResponse extends Response {} as ProtoImplClass<Response<T>>;
|
|
92
92
|
this.response = this.ResponseClass.prototype;
|
|
93
93
|
}
|
|
94
94
|
|
|
@@ -151,7 +151,7 @@ export class Application extends Emitter {
|
|
|
151
151
|
/**
|
|
152
152
|
* Use the given middleware `fn`.
|
|
153
153
|
*/
|
|
154
|
-
use(fn: MiddlewareFunc) {
|
|
154
|
+
use(fn: MiddlewareFunc<T>) {
|
|
155
155
|
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
|
|
156
156
|
const name = fn._name || fn.name || '-';
|
|
157
157
|
if (isGeneratorFunction(fn)) {
|
|
@@ -196,7 +196,7 @@ export class Application extends Emitter {
|
|
|
196
196
|
* Handle request in callback.
|
|
197
197
|
* @private
|
|
198
198
|
*/
|
|
199
|
-
protected async handleRequest(ctx:
|
|
199
|
+
protected async handleRequest(ctx: T, fnMiddleware: (ctx: T) => Promise<void>) {
|
|
200
200
|
this.emit('request', ctx);
|
|
201
201
|
const res = ctx.res;
|
|
202
202
|
res.statusCode = 404;
|
|
@@ -219,7 +219,7 @@ export class Application extends Emitter {
|
|
|
219
219
|
* Initialize a new context.
|
|
220
220
|
* @private
|
|
221
221
|
*/
|
|
222
|
-
|
|
222
|
+
createContext(req: IncomingMessage, res: ServerResponse) {
|
|
223
223
|
const context = new this.ContextClass(this, req, res);
|
|
224
224
|
return context;
|
|
225
225
|
}
|
|
@@ -246,7 +246,7 @@ export class Application extends Emitter {
|
|
|
246
246
|
/**
|
|
247
247
|
* Response helper.
|
|
248
248
|
*/
|
|
249
|
-
protected _respond(ctx:
|
|
249
|
+
protected _respond(ctx: T) {
|
|
250
250
|
// allow bypassing koa
|
|
251
251
|
if (ctx.respond === false) return;
|
|
252
252
|
|
|
@@ -303,5 +303,3 @@ export class Application extends Emitter {
|
|
|
303
303
|
res.end(body);
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
|
-
|
|
307
|
-
export default Application;
|
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';
|
|
@@ -17,15 +18,14 @@ export class Context {
|
|
|
17
18
|
res: ServerResponse;
|
|
18
19
|
request: Request & AnyProto;
|
|
19
20
|
response: Response & AnyProto;
|
|
20
|
-
state: Record<string, any>;
|
|
21
21
|
originalUrl: string;
|
|
22
22
|
respond?: boolean;
|
|
23
|
+
#state: Record<string, any> = {};
|
|
23
24
|
|
|
24
25
|
constructor(app: Application, req: IncomingMessage, res: ServerResponse) {
|
|
25
26
|
this.app = app;
|
|
26
27
|
this.req = req;
|
|
27
28
|
this.res = res;
|
|
28
|
-
this.state = {};
|
|
29
29
|
this.request = new app.RequestClass(app, this, req, res);
|
|
30
30
|
this.response = new app.ResponseClass(app, this as any, req, res);
|
|
31
31
|
this.request.response = this.response;
|
|
@@ -219,69 +219,265 @@ export class Context {
|
|
|
219
219
|
set cookies(cookies: Cookies) {
|
|
220
220
|
this._cookies = cookies;
|
|
221
221
|
}
|
|
222
|
-
}
|
|
223
222
|
|
|
224
|
-
|
|
225
|
-
|
|
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
|
-
|
|
223
|
+
get state() {
|
|
224
|
+
return this.#state;
|
|
225
|
+
}
|
|
226
|
+
|
|
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,19 +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
|
-
export
|
|
15
|
+
export interface RequestSocket extends Socket {
|
|
16
|
+
encrypted: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class Request<T extends Context = Context> {
|
|
16
20
|
[key: symbol]: unknown;
|
|
17
21
|
app: Application;
|
|
18
22
|
req: IncomingMessage;
|
|
19
23
|
res: ServerResponse;
|
|
20
|
-
ctx:
|
|
24
|
+
ctx: T;
|
|
21
25
|
response: Response;
|
|
22
26
|
originalUrl: string;
|
|
23
27
|
|
|
24
|
-
constructor(app: Application, ctx:
|
|
28
|
+
constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) {
|
|
25
29
|
this.app = app;
|
|
26
30
|
this.req = req;
|
|
27
31
|
this.res = res;
|
|
@@ -138,7 +142,7 @@ export class Request {
|
|
|
138
142
|
/**
|
|
139
143
|
* Get parsed query string.
|
|
140
144
|
*/
|
|
141
|
-
get query() {
|
|
145
|
+
get query(): ParsedUrlQuery {
|
|
142
146
|
const str = this.querystring;
|
|
143
147
|
if (!this._parsedUrlQueryCache) {
|
|
144
148
|
this._parsedUrlQueryCache = {};
|
|
@@ -154,7 +158,7 @@ export class Request {
|
|
|
154
158
|
* Set query string as an object.
|
|
155
159
|
*/
|
|
156
160
|
|
|
157
|
-
set query(obj) {
|
|
161
|
+
set query(obj: ParsedUrlQuery) {
|
|
158
162
|
this.querystring = qs.stringify(obj);
|
|
159
163
|
}
|
|
160
164
|
|
|
@@ -299,7 +303,7 @@ export class Request {
|
|
|
299
303
|
* Return the request socket.
|
|
300
304
|
*/
|
|
301
305
|
get socket() {
|
|
302
|
-
return this.req.socket as
|
|
306
|
+
return this.req.socket as RequestSocket;
|
|
303
307
|
}
|
|
304
308
|
|
|
305
309
|
/**
|
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];
|