-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
index.js
680 lines (634 loc) · 13.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
'use strict'
/*
* node-req
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const parseurl = require('parseurl')
const qs = require('qs')
const fresh = require('fresh')
const proxyaddr = require('proxy-addr')
const isIP = require('net').isIP
const accepts = require('accepts')
const is = require('type-is')
const compileTrust = function (value) {
/**
* If value is a function, return it right away.
*/
if (typeof (value) === 'function') {
return value
}
/**
* Wrap a boolean true inside a function
* and return true
*/
if (value === true) {
return function () {
return true
}
}
/**
* Wrap number inside a function and perform
* required operations.
*/
if (typeof (value) === 'number') {
return function (a, i) {
return i < value
}
}
/**
* Support comma-separated values
*/
if (typeof value === 'string') {
value = value.split(/ *, */)
}
/**
* Finally let proxyaddr understand
* and compile the input.
*/
return proxyaddr.compile(value || [])
}
/**
* Request class is used for reading request
* information from native HTTP request.
*
* @class Request
* @static
*/
let Request = exports = module.exports = {}
/**
* Parses query string from url an returns
* an object.
*
* @method get
*
* @param {Object} request
* @param {Object} [options] Options are passed to https://www.npmjs.com/package/qs
*
* @return {Object}
*
* @example
* ```js
* const queryString = nodeReq.get(req)
* ```
*/
Request.get = function (request, options = {}) {
return qs.parse(parseurl(request).query, options)
}
/**
* Returns the exact copy of `request.method`. Defined
* [here](https://nodejs.org/api/http.html#http_message_method)
*
* @method method
*
* @param {Object} request
*
* @return {String}
*
* @example
* ```js
* const method = nodeReq.method(req)
* ```
*/
Request.method = function (request) {
return request.method
}
/**
* Returns an object of headers for a given
* request.
*
* @method headers
*
* @param {Object} request
*
* @return {Object}
*
* @example
* ```js
* const headers = nodeReq.headers(req)
* ```
*/
Request.headers = function (request) {
return request.headers
}
/**
* Returns header value for a given key. Also
* it will handle the inconsistencies between
* `referer` and `referrer` header.
*
* @method header
*
* @param {Object} request
* @param {String} key
* @return {String}
*
* @example
* ```js
* const authHeader = nodeReq.header(req, 'Authorization')
* ```
*/
Request.header = function (request, key) {
key = key.toLowerCase()
const headers = Request.headers(request)
switch (key) {
case 'referer':
case 'referrer':
return headers.referrer || headers.referer || ''
default:
return headers[key] || ''
}
}
/**
* Returns the freshness of a response inside the client
* cache. If client cache has the latest response, this
* method will return `true`, otherwise it will return
* `false`.
*
* Also when HTTP header `Cache-Control: no-cache` is present
* this method will return false everytime.
*
* @method fresh
*
* @param {Object} request
* @param {Object} response
*
* @return {Boolean}
*
* @example
* ```js
* if (nodeReq.fresh(req, res)) {
* res.writeHead(304)
* }
* ```
*/
Request.fresh = function (request, response) {
const method = Request.method(request)
const status = response.statusCode
/**
* only for GET and HEAD
*/
if (method !== 'GET' && method !== 'HEAD') {
return false
}
/**
* 2xx or 304 as per rfc2616 14.26
*/
if ((status >= 200 && status < 300) || status === 304) {
return fresh(request.headers, (response.headers || {}))
}
return false
}
/**
* This method is the opposite of the {{#crossLink "Request/fresh"}}{{/crossLink}} method
*
* @method stale
*
* @param {Object} request
*
* @return {Boolean}
*
* @example
* ```js
* if (!nodeReq.stale(req, res)) {
* res.writeHead(304)
* }
* ```
*/
Request.stale = function (request, response) {
return !Request.fresh(request, response)
}
/**
* Returns the most trusted ip address for the HTTP
* request. It will handle the use cases where your
* server is behind a proxy.
*
* Make sure to check [proxy-addr](https://www.npmjs.com/package/proxy-addr)
* for the available options for `trust`.
*
* @method ip
*
* @param {Object} request
* @param {Mixed} [trust]
*
* @return {String}
*
* @example
* ```js
* nodeReq.ip(req, '127.0.0.1')
* nodeReq.ip(req, ['::1/128', 'fe80::/10'])
* ```
*/
Request.ip = function (request, trust) {
return proxyaddr(request, compileTrust(trust))
}
/**
* Returns list of all remote addresses ordered with
* most trusted on the top of the list.
*
* Make sure to check [proxy-addr](https://www.npmjs.com/package/proxy-addr)
* for the available options for `trust`.
*
* @method ips
*
* @param {Object} request
* @param {Mixed} [trust]
*
* @return {Array}
*
* @example
* ```
* nodeReq.ips(req, '127.0.0.1')
* nodeReq.ips(req, ['::1/128', 'fe80::/10'])
* ```
*/
Request.ips = function (request, trust) {
const addresses = proxyaddr.all(request, compileTrust(trust))
return addresses.slice(1).reverse()
}
/**
* Returns request protocol based upon encrypted
* connection or X-Forwaded-Proto header.
*
* Make sure to check [proxy-addr](https://www.npmjs.com/package/proxy-addr)
* for the available options for `trust`.
*
* @method protocol
*
* @param {Object} request
* @param {Mixed} [trust]
*
* @return {String}
*
* @example
* ```
* const protocol = nodeReq.protocol(req)
* ```
*/
Request.protocol = function (request, trust) {
let proto = request.connection.encrypted ? 'https' : 'http'
trust = compileTrust(trust)
if (!trust(request.connection.remoteAddress, 0)) {
return proto
}
proto = Request.header(request, 'X-Forwarded-Proto') || proto
return proto.split(/\s*,\s*/)[0]
}
/**
* Looks for request protocol to check for
* https existence or returns false.
*
* @method secure
*
* @param {Object} request
*
* @return {Boolean}
*
* @example
* ```
* const isHttps = nodeReq.secure(req)
* ```
*/
Request.secure = function (request) {
return Request.protocol(request) === 'https'
}
/**
* Returns the request subdomains as an array. Also
* it will make sure to exclude `www` from the
* subdomains list.
*
* Make sure to check [proxy-addr](https://www.npmjs.com/package/proxy-addr)
* for the available options for `trust`.
*
* @method subdomains
*
* @param {Object} request
* @param {Mixed} [trust]
* @param {Number} [offset = 2] subdomain offset
*
* @return {Array}
*
* @example
* ```js
* const subdomains = nodeReq.subdomains(req)
* ```
*/
Request.subdomains = function (request, trust, offset = 2) {
const hostname = Request.hostname(request, trust)
if (!hostname || isIP(hostname)) {
return []
}
const subdomains = hostname.split('.').reverse().slice(offset)
/**
* remove www if is the last subdomain
* after reverse
*/
if (subdomains[subdomains.length - 1] === 'www') {
subdomains.splice(subdomains.length - 1, 1)
}
return subdomains
}
/**
* Determines whether request is an ajax request
* or not, based on X-Requested-With header.
*
* @method ajax
*
* @param {Object} request
*
* @return {Boolean}
*
* @example
* ```js
* if (nodeReq.ajax(req)) {
* res.writeHead(200, {"Content-type": "application/json"})
* } else {
* res.writeHead(200, {"Content-type": "text/html"})
* }
* ```
*/
Request.ajax = function (request) {
const xhr = Request.header(request, 'X-Requested-With') || ''
return xhr.toLowerCase() === 'xmlhttprequest'
}
/**
* Tells whether request has X-Pjax
* header or not.
*
* @method pjax
*
* @param {Object} request
*
* @return {Boolean}
*
* @example
* ```js
* if (nodeReq.pjax(req)) {
* // return partial content
* } else {
* // full page refresh
* }
* ```
*/
Request.pjax = function (request) {
return !!Request.header(request, 'X-Pjax')
}
/**
* Returns the hostname of HTTP request.
*
* Make sure to check [proxy-addr](https://www.npmjs.com/package/proxy-addr)
* for the available options for `trust`.
*
* @method hostname
*
* @param {Object} request
* @param {Mixed} [trust]
*
* @return {String}
*
* @example
* ```js
* const hostname = nodeReq.hostname(request)
* ```
*/
Request.hostname = function (request, trust) {
trust = compileTrust(trust)
let host = Request.header(request, 'X-Forwarded-Host')
/**
* grabbing host header if trust proxy is disabled or host
* does not exists on forwared headers
*/
if (!host || !trust(request.connection.remoteAddress, 0)) {
host = Request.header(request, 'Host')
}
if (!host) {
return null
}
/**
* Support for IPv6
*/
const offset = host[0] === '[' ? host.indexOf(']') + 1 : 0
const index = host.indexOf(':', offset)
return index !== -1 ? host.substring(0, index) : host
}
/**
* Returns request url after removing the query
* string.
*
* @method url
*
* @param {Object} request
*
* @return {String}
*
* @example
* ```js
* const url = nodeReq.url(request)
* ```
*/
Request.url = function (request) {
return parseurl(request).pathname
}
/**
* Returns the untouched url.
*
* @method originalUrl
*
* @param {Object} request
*
* @return {String}
*
* @example
* ```js
* const url = nodeReq.originalUrl(request)
* ```
*/
Request.originalUrl = function (request) {
return parseurl(request).href
}
/**
* Tells whether request accept content of a given
* type or not (based on **Content-type**) header.
*
* @method is
*
* @param {Object} request
* @param {Mixed} keys
*
* @return {String}
*
* @example
* ```js
* // req.headers.content-type = 'application/json'
*
* nodeReq.is(req, ['json']) // json
* nodeReq.is(req, ['json', 'html']) // json
* nodeReq.is(req, ['application/*']) // application/json
*
* nodeReq.is(req, ['html']) // '<empty string>'
* ```
*/
Request.is = function (request, keys) {
return is.is(request, keys) || ''
}
/**
* Return the best possible response accepted by the
* client. This is based on the `Accept` header.
* [Learn more about it](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept)
*
* @method accepts
*
* @param {Object} request
* @param {Mixed} keys
*
* @return {String}
*
* @example
* ```js
* const type = nodeReq.accepts(req, ['json', 'html'])
*
* switch(type) {
* case 'json':
* res.setHeader('Content-Type', 'application/json')
* res.write('{"hello":"world!"}')
* break
*
* case 'html':
* res.setHeader('Content-Type', 'text/html')
* res.write('<b>hello, world!</b>')
* break
*
* default:
* res.setHeader('Content-Type', 'text/plain')
* res.write('hello, world!')
* }
* ```
*/
Request.accepts = function (request, keys) {
const accept = accepts(request)
return accept.type(keys)
}
/**
* This method is similar to {{#crossLink "Request/accepts"}}{{/crossLink}},
* instead it will return an array of types from most to least preferred
* one.
*
* @method types
*
* @param {Object} request
*
* @return {Array}
*/
Request.types = function (request) {
const accept = accepts(request)
return accept.types()
}
/**
* Returns one of the most preferrable language.
*
* @method language
*
* @param {Object} request
* @param {Array} accepted
*
* @return {String}
*/
Request.language = function (request, accepted) {
const accept = accepts(request)
const acceptedLangs = accept.language(accepted)
return (acceptedLangs instanceof Array) ? acceptedLangs[0] : acceptedLangs
}
/**
* Returns list of all accepted languages from most
* to least preferred one.
*
* @method languages
*
* @param {Object} request
*
* @return {Array}
*/
Request.languages = function (request) {
const accept = accepts(request)
return accept.languages()
}
/**
* Returns the best maching encoding
*
* @method encoding
*
* @param {Object} request
* @param {Array} accepted
*
* @return {String}
*/
Request.encoding = function (request, accepted) {
const accept = accepts(request)
const acceptedEncoding = accept.encoding(accepted)
return (acceptedEncoding instanceof Array) ? acceptedEncoding[0] : acceptedEncoding
}
/**
* Returns list of all encodings from most
* to least preferred one.
*
* @method encodings
*
* @param {Object} request
*
* @return {Array}
*/
Request.encodings = function (request) {
const accept = accepts(request)
return accept.encodings()
}
/**
* Returns the best maching charset based upon
* `Accept-Charset` header.
*
* @method charset
*
* @param {Object} request
* @param {Array} accepted
*
* @return {String}
*/
Request.charset = function (request, accepted) {
const accept = accepts(request)
const acceptedCharsets = accept.charset(accepted)
return (acceptedCharsets instanceof Array) ? acceptedCharsets[0] : acceptedCharsets
}
/**
* Returns a list of all charsets from most
* to least preferred one based upon
* `Accept-Charset` header.
*
* @method charsets
*
* @param {Object} request
* @return {Array}
*/
Request.charsets = function (request) {
const accept = accepts(request)
return accept.charsets()
}
/**
* Tells whether request has body or
* not to be read by any body parser.
*
* @method hasBody
*
* @param {Object} request
* @return {Boolean}
*
* @example
* ```js
* if (nodeReq.hasBody(request)) {
* // use body parser
* }
* ```
*/
Request.hasBody = function (request) {
return is.hasBody(request)
}