koa 2.14.0 → 3.0.0-alpha.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/lib/request.js CHANGED
@@ -1,23 +1,23 @@
1
1
 
2
- 'use strict';
2
+ 'use strict'
3
3
 
4
4
  /**
5
5
  * Module dependencies.
6
6
  */
7
7
 
8
- const URL = require('url').URL;
9
- const net = require('net');
10
- const accepts = require('accepts');
11
- const contentType = require('content-type');
12
- const stringify = require('url').format;
13
- const parse = require('parseurl');
14
- const qs = require('querystring');
15
- const typeis = require('type-is');
16
- const fresh = require('fresh');
17
- const only = require('only');
18
- const util = require('util');
8
+ const URL = require('url').URL
9
+ const net = require('net')
10
+ const accepts = require('accepts')
11
+ const contentType = require('content-type')
12
+ const stringify = require('url').format
13
+ const parse = require('parseurl')
14
+ const qs = require('querystring')
15
+ const typeis = require('type-is')
16
+ const fresh = require('fresh')
17
+ const only = require('only')
18
+ const util = require('util')
19
19
 
20
- const IP = Symbol('context#ip');
20
+ const IP = Symbol('context#ip')
21
21
 
22
22
  /**
23
23
  * Prototype.
@@ -32,8 +32,8 @@ module.exports = {
32
32
  * @api public
33
33
  */
34
34
 
35
- get header() {
36
- return this.req.headers;
35
+ get header () {
36
+ return this.req.headers
37
37
  },
38
38
 
39
39
  /**
@@ -42,8 +42,8 @@ module.exports = {
42
42
  * @api public
43
43
  */
44
44
 
45
- set header(val) {
46
- this.req.headers = val;
45
+ set header (val) {
46
+ this.req.headers = val
47
47
  },
48
48
 
49
49
  /**
@@ -53,8 +53,8 @@ module.exports = {
53
53
  * @api public
54
54
  */
55
55
 
56
- get headers() {
57
- return this.req.headers;
56
+ get headers () {
57
+ return this.req.headers
58
58
  },
59
59
 
60
60
  /**
@@ -63,8 +63,8 @@ module.exports = {
63
63
  * @api public
64
64
  */
65
65
 
66
- set headers(val) {
67
- this.req.headers = val;
66
+ set headers (val) {
67
+ this.req.headers = val
68
68
  },
69
69
 
70
70
  /**
@@ -74,8 +74,8 @@ module.exports = {
74
74
  * @api public
75
75
  */
76
76
 
77
- get url() {
78
- return this.req.url;
77
+ get url () {
78
+ return this.req.url
79
79
  },
80
80
 
81
81
  /**
@@ -84,8 +84,8 @@ module.exports = {
84
84
  * @api public
85
85
  */
86
86
 
87
- set url(val) {
88
- this.req.url = val;
87
+ set url (val) {
88
+ this.req.url = val
89
89
  },
90
90
 
91
91
  /**
@@ -95,8 +95,8 @@ module.exports = {
95
95
  * @api public
96
96
  */
97
97
 
98
- get origin() {
99
- return `${this.protocol}://${this.host}`;
98
+ get origin () {
99
+ return `${this.protocol}://${this.host}`
100
100
  },
101
101
 
102
102
  /**
@@ -106,10 +106,10 @@ module.exports = {
106
106
  * @api public
107
107
  */
108
108
 
109
- get href() {
109
+ get href () {
110
110
  // support: `GET http://example.com/foo`
111
- if (/^https?:\/\//i.test(this.originalUrl)) return this.originalUrl;
112
- return this.origin + this.originalUrl;
111
+ if (/^https?:\/\//i.test(this.originalUrl)) return this.originalUrl
112
+ return this.origin + this.originalUrl
113
113
  },
114
114
 
115
115
  /**
@@ -119,8 +119,8 @@ module.exports = {
119
119
  * @api public
120
120
  */
121
121
 
122
- get method() {
123
- return this.req.method;
122
+ get method () {
123
+ return this.req.method
124
124
  },
125
125
 
126
126
  /**
@@ -130,8 +130,8 @@ module.exports = {
130
130
  * @api public
131
131
  */
132
132
 
133
- set method(val) {
134
- this.req.method = val;
133
+ set method (val) {
134
+ this.req.method = val
135
135
  },
136
136
 
137
137
  /**
@@ -141,8 +141,8 @@ module.exports = {
141
141
  * @api public
142
142
  */
143
143
 
144
- get path() {
145
- return parse(this.req).pathname;
144
+ get path () {
145
+ return parse(this.req).pathname
146
146
  },
147
147
 
148
148
  /**
@@ -152,14 +152,14 @@ module.exports = {
152
152
  * @api public
153
153
  */
154
154
 
155
- set path(path) {
156
- const url = parse(this.req);
157
- if (url.pathname === path) return;
155
+ set path (path) {
156
+ const url = parse(this.req)
157
+ if (url.pathname === path) return
158
158
 
159
- url.pathname = path;
160
- url.path = null;
159
+ url.pathname = path
160
+ url.path = null
161
161
 
162
- this.url = stringify(url);
162
+ this.url = stringify(url)
163
163
  },
164
164
 
165
165
  /**
@@ -169,10 +169,10 @@ module.exports = {
169
169
  * @api public
170
170
  */
171
171
 
172
- get query() {
173
- const str = this.querystring;
174
- const c = this._querycache = this._querycache || {};
175
- return c[str] || (c[str] = qs.parse(str));
172
+ get query () {
173
+ const str = this.querystring
174
+ const c = this._querycache = this._querycache || {}
175
+ return c[str] || (c[str] = qs.parse(str))
176
176
  },
177
177
 
178
178
  /**
@@ -182,8 +182,8 @@ module.exports = {
182
182
  * @api public
183
183
  */
184
184
 
185
- set query(obj) {
186
- this.querystring = qs.stringify(obj);
185
+ set query (obj) {
186
+ this.querystring = qs.stringify(obj)
187
187
  },
188
188
 
189
189
  /**
@@ -193,9 +193,9 @@ module.exports = {
193
193
  * @api public
194
194
  */
195
195
 
196
- get querystring() {
197
- if (!this.req) return '';
198
- return parse(this.req).query || '';
196
+ get querystring () {
197
+ if (!this.req) return ''
198
+ return parse(this.req).query || ''
199
199
  },
200
200
 
201
201
  /**
@@ -205,14 +205,14 @@ module.exports = {
205
205
  * @api public
206
206
  */
207
207
 
208
- set querystring(str) {
209
- const url = parse(this.req);
210
- if (url.search === `?${str}`) return;
208
+ set querystring (str) {
209
+ const url = parse(this.req)
210
+ if (url.search === `?${str}`) return
211
211
 
212
- url.search = str;
213
- url.path = null;
212
+ url.search = str
213
+ url.path = null
214
214
 
215
- this.url = stringify(url);
215
+ this.url = stringify(url)
216
216
  },
217
217
 
218
218
  /**
@@ -223,9 +223,9 @@ module.exports = {
223
223
  * @api public
224
224
  */
225
225
 
226
- get search() {
227
- if (!this.querystring) return '';
228
- return `?${this.querystring}`;
226
+ get search () {
227
+ if (!this.querystring) return ''
228
+ return `?${this.querystring}`
229
229
  },
230
230
 
231
231
  /**
@@ -236,8 +236,8 @@ module.exports = {
236
236
  * @api public
237
237
  */
238
238
 
239
- set search(str) {
240
- this.querystring = str;
239
+ set search (str) {
240
+ this.querystring = str
241
241
  },
242
242
 
243
243
  /**
@@ -249,15 +249,15 @@ module.exports = {
249
249
  * @api public
250
250
  */
251
251
 
252
- get host() {
253
- const proxy = this.app.proxy;
254
- let host = proxy && this.get('X-Forwarded-Host');
252
+ get host () {
253
+ const proxy = this.app.proxy
254
+ let host = proxy && this.get('X-Forwarded-Host')
255
255
  if (!host) {
256
- if (this.req.httpVersionMajor >= 2) host = this.get(':authority');
257
- if (!host) host = this.get('Host');
256
+ if (this.req.httpVersionMajor >= 2) host = this.get(':authority')
257
+ if (!host) host = this.get('Host')
258
258
  }
259
- if (!host) return '';
260
- return host.split(/\s*,\s*/, 1)[0];
259
+ if (!host) return ''
260
+ return host.split(/\s*,\s*/, 1)[0]
261
261
  },
262
262
 
263
263
  /**
@@ -269,11 +269,11 @@ module.exports = {
269
269
  * @api public
270
270
  */
271
271
 
272
- get hostname() {
273
- const host = this.host;
274
- if (!host) return '';
275
- if ('[' === host[0]) return this.URL.hostname || ''; // IPv6
276
- return host.split(':', 1)[0];
272
+ get hostname () {
273
+ const host = this.host
274
+ if (!host) return ''
275
+ if (host[0] === '[') return this.URL.hostname || '' // IPv6
276
+ return host.split(':', 1)[0]
277
277
  },
278
278
 
279
279
  /**
@@ -284,17 +284,17 @@ module.exports = {
284
284
  * @api public
285
285
  */
286
286
 
287
- get URL() {
287
+ get URL () {
288
288
  /* istanbul ignore else */
289
289
  if (!this.memoizedURL) {
290
- const originalUrl = this.originalUrl || ''; // avoid undefined in template string
290
+ const originalUrl = this.originalUrl || '' // avoid undefined in template string
291
291
  try {
292
- this.memoizedURL = new URL(`${this.origin}${originalUrl}`);
292
+ this.memoizedURL = new URL(`${this.origin}${originalUrl}`)
293
293
  } catch (err) {
294
- this.memoizedURL = Object.create(null);
294
+ this.memoizedURL = Object.create(null)
295
295
  }
296
296
  }
297
- return this.memoizedURL;
297
+ return this.memoizedURL
298
298
  },
299
299
 
300
300
  /**
@@ -306,19 +306,19 @@ module.exports = {
306
306
  * @api public
307
307
  */
308
308
 
309
- get fresh() {
310
- const method = this.method;
311
- const s = this.ctx.status;
309
+ get fresh () {
310
+ const method = this.method
311
+ const s = this.ctx.status
312
312
 
313
313
  // GET or HEAD for weak freshness validation only
314
- if ('GET' !== method && 'HEAD' !== method) return false;
314
+ if (method !== 'GET' && method !== 'HEAD') return false
315
315
 
316
316
  // 2xx or 304 as per rfc2616 14.26
317
- if ((s >= 200 && s < 300) || 304 === s) {
318
- return fresh(this.header, this.response.header);
317
+ if ((s >= 200 && s < 300) || s === 304) {
318
+ return fresh(this.header, this.response.header)
319
319
  }
320
320
 
321
- return false;
321
+ return false
322
322
  },
323
323
 
324
324
  /**
@@ -330,8 +330,8 @@ module.exports = {
330
330
  * @api public
331
331
  */
332
332
 
333
- get stale() {
334
- return !this.fresh;
333
+ get stale () {
334
+ return !this.fresh
335
335
  },
336
336
 
337
337
  /**
@@ -341,9 +341,9 @@ module.exports = {
341
341
  * @api public
342
342
  */
343
343
 
344
- get idempotent() {
345
- const methods = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'];
346
- return !!~methods.indexOf(this.method);
344
+ get idempotent () {
345
+ const methods = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE']
346
+ return !!~methods.indexOf(this.method)
347
347
  },
348
348
 
349
349
  /**
@@ -353,8 +353,8 @@ module.exports = {
353
353
  * @api public
354
354
  */
355
355
 
356
- get socket() {
357
- return this.req.socket;
356
+ get socket () {
357
+ return this.req.socket
358
358
  },
359
359
 
360
360
  /**
@@ -364,12 +364,12 @@ module.exports = {
364
364
  * @api public
365
365
  */
366
366
 
367
- get charset() {
367
+ get charset () {
368
368
  try {
369
- const { parameters } = contentType.parse(this.req);
370
- return parameters.charset || '';
369
+ const { parameters } = contentType.parse(this.req)
370
+ return parameters.charset || ''
371
371
  } catch (e) {
372
- return '';
372
+ return ''
373
373
  }
374
374
  },
375
375
 
@@ -380,10 +380,10 @@ module.exports = {
380
380
  * @api public
381
381
  */
382
382
 
383
- get length() {
384
- const len = this.get('Content-Length');
385
- if (len === '') return;
386
- return ~~len;
383
+ get length () {
384
+ const len = this.get('Content-Length')
385
+ if (len === '') return
386
+ return ~~len
387
387
  },
388
388
 
389
389
  /**
@@ -398,11 +398,11 @@ module.exports = {
398
398
  * @api public
399
399
  */
400
400
 
401
- get protocol() {
402
- if (this.socket.encrypted) return 'https';
403
- if (!this.app.proxy) return 'http';
404
- const proto = this.get('X-Forwarded-Proto');
405
- return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
401
+ get protocol () {
402
+ if (this.socket.encrypted) return 'https'
403
+ if (!this.app.proxy) return 'http'
404
+ const proto = this.get('X-Forwarded-Proto')
405
+ return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http'
406
406
  },
407
407
 
408
408
  /**
@@ -414,8 +414,8 @@ module.exports = {
414
414
  * @api public
415
415
  */
416
416
 
417
- get secure() {
418
- return 'https' === this.protocol;
417
+ get secure () {
418
+ return this.protocol === 'https'
419
419
  },
420
420
 
421
421
  /**
@@ -430,16 +430,16 @@ module.exports = {
430
430
  * @api public
431
431
  */
432
432
 
433
- get ips() {
434
- const proxy = this.app.proxy;
435
- const val = this.get(this.app.proxyIpHeader);
433
+ get ips () {
434
+ const proxy = this.app.proxy
435
+ const val = this.get(this.app.proxyIpHeader)
436
436
  let ips = proxy && val
437
437
  ? val.split(/\s*,\s*/)
438
- : [];
438
+ : []
439
439
  if (this.app.maxIpsCount > 0) {
440
- ips = ips.slice(-this.app.maxIpsCount);
440
+ ips = ips.slice(-this.app.maxIpsCount)
441
441
  }
442
- return ips;
442
+ return ips
443
443
  },
444
444
 
445
445
  /**
@@ -451,15 +451,15 @@ module.exports = {
451
451
  * @api public
452
452
  */
453
453
 
454
- get ip() {
454
+ get ip () {
455
455
  if (!this[IP]) {
456
- this[IP] = this.ips[0] || this.socket.remoteAddress || '';
456
+ this[IP] = this.ips[0] || this.socket.remoteAddress || ''
457
457
  }
458
- return this[IP];
458
+ return this[IP]
459
459
  },
460
460
 
461
- set ip(_ip) {
462
- this[IP] = _ip;
461
+ set ip (_ip) {
462
+ this[IP] = _ip
463
463
  },
464
464
 
465
465
  /**
@@ -478,14 +478,14 @@ module.exports = {
478
478
  * @api public
479
479
  */
480
480
 
481
- get subdomains() {
482
- const offset = this.app.subdomainOffset;
483
- const hostname = this.hostname;
484
- if (net.isIP(hostname)) return [];
481
+ get subdomains () {
482
+ const offset = this.app.subdomainOffset
483
+ const hostname = this.hostname
484
+ if (net.isIP(hostname)) return []
485
485
  return hostname
486
486
  .split('.')
487
487
  .reverse()
488
- .slice(offset);
488
+ .slice(offset)
489
489
  },
490
490
 
491
491
  /**
@@ -496,8 +496,8 @@ module.exports = {
496
496
  * @api private
497
497
  */
498
498
 
499
- get accept() {
500
- return this._accept || (this._accept = accepts(this.req));
499
+ get accept () {
500
+ return this._accept || (this._accept = accepts(this.req))
501
501
  },
502
502
 
503
503
  /**
@@ -507,8 +507,8 @@ module.exports = {
507
507
  * @api private
508
508
  */
509
509
 
510
- set accept(obj) {
511
- this._accept = obj;
510
+ set accept (obj) {
511
+ this._accept = obj
512
512
  },
513
513
 
514
514
  /**
@@ -552,8 +552,8 @@ module.exports = {
552
552
  * @api public
553
553
  */
554
554
 
555
- accepts(...args) {
556
- return this.accept.types(...args);
555
+ accepts (...args) {
556
+ return this.accept.types(...args)
557
557
  },
558
558
 
559
559
  /**
@@ -569,8 +569,8 @@ module.exports = {
569
569
  * @api public
570
570
  */
571
571
 
572
- acceptsEncodings(...args) {
573
- return this.accept.encodings(...args);
572
+ acceptsEncodings (...args) {
573
+ return this.accept.encodings(...args)
574
574
  },
575
575
 
576
576
  /**
@@ -586,8 +586,8 @@ module.exports = {
586
586
  * @api public
587
587
  */
588
588
 
589
- acceptsCharsets(...args) {
590
- return this.accept.charsets(...args);
589
+ acceptsCharsets (...args) {
590
+ return this.accept.charsets(...args)
591
591
  },
592
592
 
593
593
  /**
@@ -603,8 +603,8 @@ module.exports = {
603
603
  * @api public
604
604
  */
605
605
 
606
- acceptsLanguages(...args) {
607
- return this.accept.languages(...args);
606
+ acceptsLanguages (...args) {
607
+ return this.accept.languages(...args)
608
608
  },
609
609
 
610
610
  /**
@@ -634,8 +634,8 @@ module.exports = {
634
634
  * @api public
635
635
  */
636
636
 
637
- is(type, ...types) {
638
- return typeis(this.req, type, ...types);
637
+ is (type, ...types) {
638
+ return typeis(this.req, type, ...types)
639
639
  },
640
640
 
641
641
  /**
@@ -646,10 +646,10 @@ module.exports = {
646
646
  * @api public
647
647
  */
648
648
 
649
- get type() {
650
- const type = this.get('Content-Type');
651
- if (!type) return '';
652
- return type.split(';')[0];
649
+ get type () {
650
+ const type = this.get('Content-Type')
651
+ if (!type) return ''
652
+ return type.split(';')[0]
653
653
  },
654
654
 
655
655
  /**
@@ -674,14 +674,14 @@ module.exports = {
674
674
  * @api public
675
675
  */
676
676
 
677
- get(field) {
678
- const req = this.req;
677
+ get (field) {
678
+ const req = this.req
679
679
  switch (field = field.toLowerCase()) {
680
680
  case 'referer':
681
681
  case 'referrer':
682
- return req.headers.referrer || req.headers.referer || '';
682
+ return req.headers.referrer || req.headers.referer || ''
683
683
  default:
684
- return req.headers[field] || '';
684
+ return req.headers[field] || ''
685
685
  }
686
686
  },
687
687
 
@@ -692,9 +692,9 @@ module.exports = {
692
692
  * @api public
693
693
  */
694
694
 
695
- inspect() {
696
- if (!this.req) return;
697
- return this.toJSON();
695
+ inspect () {
696
+ if (!this.req) return
697
+ return this.toJSON()
698
698
  },
699
699
 
700
700
  /**
@@ -704,14 +704,14 @@ module.exports = {
704
704
  * @api public
705
705
  */
706
706
 
707
- toJSON() {
707
+ toJSON () {
708
708
  return only(this, [
709
709
  'method',
710
710
  'url',
711
711
  'header'
712
- ]);
712
+ ])
713
713
  }
714
- };
714
+ }
715
715
 
716
716
  /**
717
717
  * Custom inspection implementation for newer Node.js versions.
@@ -722,5 +722,5 @@ module.exports = {
722
722
 
723
723
  /* istanbul ignore else */
724
724
  if (util.inspect.custom) {
725
- module.exports[util.inspect.custom] = module.exports.inspect;
725
+ module.exports[util.inspect.custom] = module.exports.inspect
726
726
  }