Skip to content

Commit bb63049

Browse files
authored
Merge pull request #141 from bekzod/es6-classes
es6 classes
2 parents 241fc28 + e16d16a commit bb63049

File tree

7 files changed

+247
-229
lines changed

7 files changed

+247
-229
lines changed

src/fastboot-headers.js

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,95 @@
22

33
// Partially implements Headers from the Fetch API
44
// https://developer.mozilla.org/en-US/docs/Web/API/Headers
5-
function FastBootHeaders(headers) {
6-
headers = headers || {};
7-
this.headers = {};
8-
9-
for (var header in headers) {
10-
let value = headers[header];
11-
12-
// Express gives us either a string
13-
// or an array of strings if there are multiple values.
14-
// We want to support the Header spec
15-
// so we will coerce to an array always.
16-
if (typeof value === 'string') {
17-
value = [value];
5+
class FastBootHeaders {
6+
constructor(headers) {
7+
headers = headers || {};
8+
this.headers = {};
9+
10+
for (var header in headers) {
11+
let value = headers[header];
12+
13+
// Express gives us either a string
14+
// or an array of strings if there are multiple values.
15+
// We want to support the Header spec
16+
// so we will coerce to an array always.
17+
if (typeof value === 'string') {
18+
value = [value];
19+
}
20+
21+
this.headers[header] = value;
1822
}
19-
20-
this.headers[header] = value;
2123
}
22-
}
2324

24-
FastBootHeaders.prototype.append = function(header, value) {
25-
header = header.toLowerCase();
26-
if (!this.has(header)) {
27-
this.headers[header] = [];
28-
}
25+
append(header, value) {
26+
let _header = header.toLowerCase();
2927

30-
this.headers[header].push(value);
31-
};
28+
if (!this.has(_header)) {
29+
this.headers[_header] = [];
30+
}
3231

33-
FastBootHeaders.prototype.delete = function(header) {
34-
delete this.headers[header.toLowerCase()];
35-
};
32+
this.headers[_header].push(value);
33+
}
3634

37-
FastBootHeaders.prototype.entries = function() {
38-
var entries = [];
35+
delete(header) {
36+
delete this.headers[header.toLowerCase()];
37+
}
38+
39+
entries() {
40+
var entries = [];
3941

40-
for(var key in this.headers) {
41-
var values = this.headers[key];
42-
for(var index = 0; index < values.length; ++index ) {
43-
entries.push([key, values[index]]);
42+
for(var key in this.headers) {
43+
var values = this.headers[key];
44+
for(var index = 0; index < values.length; ++index ) {
45+
entries.push([key, values[index]]);
46+
}
4447
}
45-
}
4648

47-
return entries[Symbol.iterator]();
48-
};
49+
return entries[Symbol.iterator]();
50+
}
4951

50-
FastBootHeaders.prototype.get = function(header) {
51-
return this.getAll(header)[0] || null;
52-
};
52+
get(header) {
53+
return this.getAll(header)[0] || null;
54+
}
5355

54-
FastBootHeaders.prototype.getAll = function(header) {
55-
return this.headers[header.toLowerCase()] || [];
56-
};
56+
getAll(header) {
57+
return this.headers[header.toLowerCase()] || [];
58+
}
5759

58-
FastBootHeaders.prototype.has = function(header) {
59-
return this.headers[header.toLowerCase()] !== undefined;
60-
};
60+
has(header) {
61+
return this.headers[header.toLowerCase()] !== undefined;
62+
}
6163

62-
FastBootHeaders.prototype.keys = function() {
63-
var entries = [];
64+
keys() {
65+
var entries = [];
6466

65-
for(var key in this.headers) {
66-
var values = this.headers[key];
67-
for(var index = 0; index < values.length; ++index ) {
68-
entries.push(key);
67+
for(var key in this.headers) {
68+
var values = this.headers[key];
69+
for(var index = 0; index < values.length; ++index ) {
70+
entries.push(key);
71+
}
6972
}
70-
}
7173

72-
return entries[Symbol.iterator]();
73-
};
74+
return entries[Symbol.iterator]();
75+
}
7476

75-
FastBootHeaders.prototype.set = function(header, value) {
76-
header = header.toLowerCase();
77-
this.headers[header] = [value];
78-
};
77+
set(header, value) {
78+
header = header.toLowerCase();
79+
this.headers[header] = [value];
80+
}
7981

80-
FastBootHeaders.prototype.values = function() {
81-
var entries = [];
82+
values() {
83+
var entries = [];
8284

83-
for(var key in this.headers) {
84-
var values = this.headers[key];
85-
for(var index = 0; index < values.length; ++index ) {
86-
entries.push(values[index]);
85+
for(var key in this.headers) {
86+
var values = this.headers[key];
87+
for(var index = 0; index < values.length; ++index ) {
88+
entries.push(values[index]);
89+
}
8790
}
88-
}
8991

90-
return entries[Symbol.iterator]();
91-
};
92+
return entries[Symbol.iterator]();
93+
}
94+
}
9295

9396
module.exports = FastBootHeaders;

src/fastboot-info.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,35 @@ var FastBootResponse = require('./fastboot-response');
1515
* @param {Array} [options.hostWhitelist] expected hosts in your application
1616
* @param {Object} [options.metaData] per request meta data
1717
*/
18-
function FastBootInfo(request, response, options) {
18+
class FastBootInfo {
19+
constructor(request, response, options) {
20+
this.deferredPromise = RSVP.resolve();
21+
let hostWhitelist = options.hostWhitelist;
22+
let metadata = options.metadata;
23+
if (request) {
24+
this.request = new FastBootRequest(request, hostWhitelist);
25+
}
1926

20-
this.deferredPromise = RSVP.resolve();
21-
let hostWhitelist = options.hostWhitelist;
22-
let metadata = options.metadata;
23-
if (request) {
24-
this.request = new FastBootRequest(request, hostWhitelist);
27+
this.response = new FastBootResponse(response || {});
28+
this.metadata = metadata;
2529
}
2630

27-
this.response = new FastBootResponse(response || {});
28-
this.metadata = metadata;
29-
}
31+
deferRendering(promise) {
32+
this.deferredPromise = this.deferredPromise.then(function() {
33+
return promise;
34+
});
35+
}
3036

31-
FastBootInfo.prototype.deferRendering = function(promise) {
32-
this.deferredPromise = this.deferredPromise.then(function() {
33-
return promise;
34-
});
35-
};
37+
/*
38+
* Registers this FastBootInfo instance in the registry of an Ember
39+
* ApplicationInstance. It is configured to be injected into the FastBoot
40+
* service, ensuring it is available inside instance initializers.
41+
*/
42+
register(instance) {
43+
instance.register('info:-fastboot', this, { instantiate: false });
44+
instance.inject('service:fastboot', '_fastbootInfo', 'info:-fastboot');
45+
}
46+
}
3647

37-
/*
38-
* Registers this FastBootInfo instance in the registry of an Ember
39-
* ApplicationInstance. It is configured to be injected into the FastBoot
40-
* service, ensuring it is available inside instance initializers.
41-
*/
42-
FastBootInfo.prototype.register = function(instance) {
43-
instance.register('info:-fastboot', this, { instantiate: false });
44-
instance.inject('service:fastboot', '_fastbootInfo', 'info:-fastboot');
45-
};
4648

4749
module.exports = FastBootInfo;

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;

src/fastboot-response.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
'use strict';
2+
13
var FastBootHeaders = require('./fastboot-headers');
24

3-
function FastbootResponse(response) {
4-
this.headers = new FastBootHeaders(response._headers);
5-
this.statusCode = 200;
5+
class FastbootResponse {
6+
constructor(response) {
7+
this.headers = new FastBootHeaders(response._headers);
8+
this.statusCode = 200;
9+
}
610
}
711

812
module.exports = FastbootResponse;

0 commit comments

Comments
 (0)