@eggjs/koa 2.19.1 → 2.19.2

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/src/request.ts CHANGED
@@ -1,11 +1,9 @@
1
- import net from 'node:net';
2
- import type { Socket } from 'node:net';
1
+ import net, { type Socket } from 'node:net';
3
2
  import { format as stringify } from 'node:url';
4
- import qs from 'node:querystring';
3
+ import qs, { type ParsedUrlQuery } from 'node:querystring';
5
4
  import util from 'node:util';
6
- import type { ParsedUrlQuery } from 'node:querystring';
7
5
  import type { IncomingMessage, ServerResponse } from 'node:http';
8
- import accepts from 'accepts';
6
+ import accepts, { type Accepts } from 'accepts';
9
7
  import contentType from 'content-type';
10
8
  import parse from 'parseurl';
11
9
  import typeis from 'type-is';
@@ -134,19 +132,19 @@ export class Request {
134
132
  this.url = stringify(url);
135
133
  }
136
134
 
137
- #parsedUrlQueryCache: Record<string, ParsedUrlQuery>;
135
+ protected _parsedUrlQueryCache: Record<string, ParsedUrlQuery> | undefined;
138
136
 
139
137
  /**
140
138
  * Get parsed query string.
141
139
  */
142
140
  get query() {
143
141
  const str = this.querystring;
144
- if (!this.#parsedUrlQueryCache) {
145
- this.#parsedUrlQueryCache = {};
142
+ if (!this._parsedUrlQueryCache) {
143
+ this._parsedUrlQueryCache = {};
146
144
  }
147
- let parsedUrlQuery = this.#parsedUrlQueryCache[str];
145
+ let parsedUrlQuery = this._parsedUrlQueryCache[str];
148
146
  if (!parsedUrlQuery) {
149
- parsedUrlQuery = this.#parsedUrlQueryCache[str] = qs.parse(str);
147
+ parsedUrlQuery = this._parsedUrlQueryCache[str] = qs.parse(str);
150
148
  }
151
149
  return parsedUrlQuery;
152
150
  }
@@ -210,10 +208,16 @@ export class Request {
210
208
  const proxy = this.app.proxy;
211
209
  let host = proxy ? this.get<string>('X-Forwarded-Host') : '';
212
210
  if (!host) {
213
- if (this.req.httpVersionMajor >= 2) host = this.get(':authority');
214
- if (!host) host = this.get('Host');
211
+ if (this.req.httpVersionMajor >= 2) {
212
+ host = this.get(':authority');
213
+ }
214
+ if (!host) {
215
+ host = this.get('Host');
216
+ }
217
+ }
218
+ if (!host) {
219
+ return '';
215
220
  }
216
- if (!host) return '';
217
221
  return host.split(/\s*,\s*/, 1)[0];
218
222
  }
219
223
 
@@ -224,27 +228,31 @@ export class Request {
224
228
  */
225
229
  get hostname() {
226
230
  const host = this.host;
227
- if (!host) return '';
228
- if (host[0] === '[') return this.URL.hostname || ''; // IPv6
231
+ if (!host) {
232
+ return '';
233
+ }
234
+ if (host[0] === '[') {
235
+ return this.URL.hostname || ''; // IPv6
236
+ }
229
237
  return host.split(':', 1)[0];
230
238
  }
231
239
 
232
- #memoizedURL: URL;
240
+ protected _memoizedURL: URL | undefined;
233
241
 
234
242
  /**
235
243
  * Get WHATWG parsed URL.
236
244
  * Lazily memoized.
237
245
  */
238
246
  get URL() {
239
- if (!this.#memoizedURL) {
247
+ if (!this._memoizedURL) {
240
248
  const originalUrl = this.originalUrl || ''; // avoid undefined in template string
241
249
  try {
242
- this.#memoizedURL = new URL(`${this.origin}${originalUrl}`);
250
+ this._memoizedURL = new URL(`${this.origin}${originalUrl}`);
243
251
  } catch {
244
- this.#memoizedURL = Object.create(null);
252
+ this._memoizedURL = Object.create(null);
245
253
  }
246
254
  }
247
- return this.#memoizedURL;
255
+ return this._memoizedURL!;
248
256
  }
249
257
 
250
258
  /**
@@ -257,7 +265,9 @@ export class Request {
257
265
  const status = this.response.status;
258
266
 
259
267
  // GET or HEAD for weak freshness validation only
260
- if (method !== 'GET' && method !== 'HEAD') return false;
268
+ if (method !== 'GET' && method !== 'HEAD') {
269
+ return false;
270
+ }
261
271
 
262
272
  // 2xx or 304 as per rfc2616 14.26
263
273
  if ((status >= 200 && status < 300) || status === 304) {
@@ -308,7 +318,9 @@ export class Request {
308
318
  */
309
319
  get length() {
310
320
  const len = this.get<string>('Content-Length');
311
- if (len === '') return;
321
+ if (len === '') {
322
+ return;
323
+ }
312
324
  return parseInt(len);
313
325
  }
314
326
 
@@ -321,8 +333,12 @@ export class Request {
321
333
  * may be enabled.
322
334
  */
323
335
  get protocol() {
324
- if (this.socket.encrypted) return 'https';
325
- if (!this.app.proxy) return 'http';
336
+ if (this.socket.encrypted) {
337
+ return 'https';
338
+ }
339
+ if (!this.app.proxy) {
340
+ return 'http';
341
+ }
326
342
  const proto = this.get<string>('X-Forwarded-Proto');
327
343
  return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
328
344
  }
@@ -356,21 +372,21 @@ export class Request {
356
372
  return ips;
357
373
  }
358
374
 
359
- #ip: string;
375
+ protected _ip: string;
360
376
  /**
361
377
  * Return request's remote address
362
378
  * When `app.proxy` is `true`, parse
363
379
  * the "X-Forwarded-For" ip address list and return the first one
364
380
  */
365
381
  get ip() {
366
- if (!this.#ip) {
367
- this.#ip = this.ips[0] || this.socket.remoteAddress || '';
382
+ if (!this._ip) {
383
+ this._ip = this.ips[0] || this.socket.remoteAddress || '';
368
384
  }
369
- return this.#ip;
385
+ return this._ip;
370
386
  }
371
387
 
372
388
  set ip(ip: string) {
373
- this.#ip = ip;
389
+ this._ip = ip;
374
390
  }
375
391
 
376
392
  /**
@@ -395,20 +411,20 @@ export class Request {
395
411
  .slice(offset);
396
412
  }
397
413
 
398
- #accept: any;
414
+ protected _accept: Accepts;
399
415
  /**
400
416
  * Get accept object.
401
417
  * Lazily memoized.
402
418
  */
403
419
  get accept() {
404
- return this.#accept || (this.#accept = accepts(this.req));
420
+ return this._accept || (this._accept = accepts(this.req));
405
421
  }
406
422
 
407
423
  /**
408
424
  * Set accept object.
409
425
  */
410
- set accept(obj) {
411
- this.#accept = obj;
426
+ set accept(obj: Accepts) {
427
+ this._accept = obj;
412
428
  }
413
429
 
414
430
  /**
@@ -459,8 +475,19 @@ export class Request {
459
475
  *
460
476
  * ['gzip', 'deflate']
461
477
  */
462
- acceptsEncodings(...args: any[]): string | string[] {
463
- return this.accept.encodings(...args);
478
+ acceptsEncodings(): string[];
479
+ acceptsEncodings(encodings: string[]): string | false;
480
+ acceptsEncodings(...encodings: string[]): string | false;
481
+ acceptsEncodings(encodings?: string | string[], ...others: string[]): string[] | string | false {
482
+ if (!encodings) {
483
+ return this.accept.encodings();
484
+ }
485
+ if (Array.isArray(encodings)) {
486
+ encodings = [ ...encodings, ...others ];
487
+ } else {
488
+ encodings = [ encodings, ...others ];
489
+ }
490
+ return this.accept.encodings(...encodings);
464
491
  }
465
492
 
466
493
  /**
@@ -471,8 +498,19 @@ export class Request {
471
498
  *
472
499
  * ['utf-8', 'utf-7', 'iso-8859-1']
473
500
  */
474
- acceptsCharsets(...args: any[]): string | string[] {
475
- return this.accept.charsets(...args);
501
+ acceptsCharsets(): string[];
502
+ acceptsCharsets(charsets: string[]): string | false;
503
+ acceptsCharsets(...charsets: string[]): string | false;
504
+ acceptsCharsets(charsets?: string | string[], ...others: string[]): string[] | string | false {
505
+ if (!charsets) {
506
+ return this.accept.charsets();
507
+ }
508
+ if (Array.isArray(charsets)) {
509
+ charsets = [ ...charsets, ...others ];
510
+ } else {
511
+ charsets = [ charsets, ...others ];
512
+ }
513
+ return this.accept.charsets(...charsets);
476
514
  }
477
515
 
478
516
  /**
@@ -483,8 +521,19 @@ export class Request {
483
521
  *
484
522
  * ['es', 'pt', 'en']
485
523
  */
486
- acceptsLanguages(...args: any[]): string | string[] {
487
- return this.accept.languages(...args);
524
+ acceptsLanguages(): string[];
525
+ acceptsLanguages(languages: string[]): string | false;
526
+ acceptsLanguages(...languages: string[]): string | false;
527
+ acceptsLanguages(languages?: string | string[], ...others: string[]): string | string[] | false {
528
+ if (!languages) {
529
+ return this.accept.languages();
530
+ }
531
+ if (Array.isArray(languages)) {
532
+ languages = [ ...languages, ...others ];
533
+ } else {
534
+ languages = [ languages, ...others ];
535
+ }
536
+ return this.accept.languages(...languages);
488
537
  }
489
538
 
490
539
  /**
package/src/response.ts CHANGED
@@ -4,7 +4,7 @@ import util from 'node:util';
4
4
  import Stream from 'node:stream';
5
5
  import type { IncomingMessage, ServerResponse } from 'node:http';
6
6
  import contentDisposition from 'content-disposition';
7
- import getType from 'cache-content-type';
7
+ import { getType } from 'cache-content-type';
8
8
  import onFinish from 'on-finished';
9
9
  import escape from 'escape-html';
10
10
  import { is as typeis } from 'type-is';