Skip to content

Commit b524a03

Browse files
committed
convert FastBootRequest to es6 class
1 parent f6b886f commit b524a03

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

src/fastboot-request.js

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,66 @@
1+
'use strict';
2+
13
var cookie = require('cookie');
24
var FastBootHeaders = require('./fastboot-headers');
35

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;
139

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;
1616

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);
2018
}
2119

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));
2332

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);
2938

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}`);
3341
}
34-
}, false);
3542

36-
if (!matchFound) {
37-
throw new Error(`The host header did not match a hostWhitelist entry. Host header: ${host}`);
43+
return host;
3844
}
3945

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+
}
4252

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+
}
4958

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 {};
5461
}
5562

56-
// Return an empty object instead of undefined if no cookies are present.
57-
return {};
58-
};
63+
}
64+
5965

6066
module.exports = FastBootRequest;

0 commit comments

Comments
 (0)