|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | var cookie = require('cookie'); |
2 | 4 | var FastBootHeaders = require('./fastboot-headers'); |
3 | 5 |
|
4 | | -function FastBootRequest(request, hostWhitelist) { |
5 | | - this.hostWhitelist = hostWhitelist; |
6 | | - |
7 | | - this.protocol = request.protocol; |
8 | | - this.headers = new FastBootHeaders(request.headers); |
9 | | - this.queryParams = request.query; |
10 | | - this.path = request.url; |
11 | | - this.method = request.method; |
12 | | - this.body = request.body; |
| 6 | +class FastBootRequest { |
| 7 | + constructor(request, hostWhitelist) { |
| 8 | + this.hostWhitelist = hostWhitelist; |
13 | 9 |
|
14 | | - this.cookies = this.extractCookies(request); |
15 | | -} |
| 10 | + this.protocol = request.protocol; |
| 11 | + this.headers = new FastBootHeaders(request.headers); |
| 12 | + this.queryParams = request.query; |
| 13 | + this.path = request.url; |
| 14 | + this.method = request.method; |
| 15 | + this.body = request.body; |
16 | 16 |
|
17 | | -FastBootRequest.prototype.host = function() { |
18 | | - if (!this.hostWhitelist) { |
19 | | - throw new Error('You must provide a hostWhitelist to retrieve the host'); |
| 17 | + this.cookies = this.extractCookies(request); |
20 | 18 | } |
21 | 19 |
|
22 | | - var host = this.headers.get('host'); |
| 20 | + host() { |
| 21 | + if (!this.hostWhitelist) { |
| 22 | + throw new Error('You must provide a hostWhitelist to retrieve the host'); |
| 23 | + } |
| 24 | + |
| 25 | + var host = this.headers.get('host'); |
| 26 | + |
| 27 | + var matchFound = this.hostWhitelist.reduce(function(previous, currentEntry) { |
| 28 | + if (currentEntry[0] === '/' && |
| 29 | + currentEntry.slice(-1) === '/') { |
| 30 | + // RegExp as string |
| 31 | + var regexp = new RegExp(currentEntry.slice(1, -1)); |
23 | 32 |
|
24 | | - var matchFound = this.hostWhitelist.reduce(function(previous, currentEntry) { |
25 | | - if (currentEntry[0] === '/' && |
26 | | - currentEntry.slice(-1) === '/') { |
27 | | - // RegExp as string |
28 | | - var regexp = new RegExp(currentEntry.slice(1, -1)); |
| 33 | + return previous || regexp.test(host); |
| 34 | + } else { |
| 35 | + return previous || currentEntry === host; |
| 36 | + } |
| 37 | + }, false); |
29 | 38 |
|
30 | | - return previous || regexp.test(host); |
31 | | - } else { |
32 | | - return previous || currentEntry === host; |
| 39 | + if (!matchFound) { |
| 40 | + throw new Error(`The host header did not match a hostWhitelist entry. Host header: ${host}`); |
33 | 41 | } |
34 | | - }, false); |
35 | 42 |
|
36 | | - if (!matchFound) { |
37 | | - throw new Error(`The host header did not match a hostWhitelist entry. Host header: ${host}`); |
| 43 | + return host; |
38 | 44 | } |
39 | 45 |
|
40 | | - return host; |
41 | | -}; |
| 46 | + extractCookies(request) { |
| 47 | + // If cookie-parser middleware has already parsed the cookies, |
| 48 | + // just use that. |
| 49 | + if (request.cookies) { |
| 50 | + return request.cookies; |
| 51 | + } |
42 | 52 |
|
43 | | -FastBootRequest.prototype.extractCookies = function(request) { |
44 | | - // If cookie-parser middleware has already parsed the cookies, |
45 | | - // just use that. |
46 | | - if (request.cookies) { |
47 | | - return request.cookies; |
48 | | - } |
| 53 | + // Otherwise, try to parse the cookies ourselves, if they exist. |
| 54 | + var cookies = request.headers.cookie; |
| 55 | + if (cookies) { |
| 56 | + return cookie.parse(cookies); |
| 57 | + } |
49 | 58 |
|
50 | | - // Otherwise, try to parse the cookies ourselves, if they exist. |
51 | | - var cookies = request.headers.cookie; |
52 | | - if (cookies) { |
53 | | - return cookie.parse(cookies); |
| 59 | + // Return an empty object instead of undefined if no cookies are present. |
| 60 | + return {}; |
54 | 61 | } |
55 | 62 |
|
56 | | - // Return an empty object instead of undefined if no cookies are present. |
57 | | - return {}; |
58 | | -}; |
| 63 | +} |
| 64 | + |
59 | 65 |
|
60 | 66 | module.exports = FastBootRequest; |
0 commit comments