Skip to content

Commit d7a504c

Browse files
authored
Merge pull request #70 from rwjblue/update-fastboot
Update to [email protected]
2 parents 8199cb4 + f50f3b1 commit d7a504c

3 files changed

Lines changed: 342 additions & 90 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"dependencies": {
2727
"chalk": "^2.0.1",
28-
"fastboot": "^2.0.1"
28+
"fastboot": "^3.1.1"
2929
},
3030
"devDependencies": {
3131
"chai": "^4.1.0",

src/index.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,37 @@ function fastbootExpressMiddleware(distPath, options) {
2525
});
2626
}
2727

28-
return function(req, res, next) {
28+
return async function(req, res, next) {
2929
let path = req.url;
30-
fastboot.visit(path, { request: req, response: res }).then(success, failure);
31-
32-
function success(result) {
33-
let responseBody = opts.chunkedResponse ? result.chunks() : result.html();
34-
35-
responseBody
36-
.then(body => {
37-
let headers = result.headers;
38-
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
39-
40-
for (var pair of headers.entries()) {
41-
res.append(pair[0], pair[1]);
42-
}
43-
44-
if (result.error) {
45-
log('RESILIENT MODE CAUGHT:', result.error.stack);
46-
next(result.error);
47-
}
48-
49-
log(result.statusCode, statusMessage + path);
50-
res.status(result.statusCode);
51-
52-
if (typeof body === 'string') {
53-
res.send(body);
54-
} else if (result.error) {
55-
res.send(body[0]);
56-
} else {
57-
body.forEach(chunk => res.write(chunk));
58-
res.end();
59-
}
60-
})
61-
.catch(error => {
62-
res.status(500);
63-
next(error);
64-
});
65-
}
6630

67-
function failure(error) {
31+
try {
32+
let result = await fastboot.visit(path, { request: req, response: res });
33+
let body = opts.chunkedResponse ? await result.chunks() : await result.html();
34+
35+
if (result.error) {
36+
log('RESILIENT MODE CAUGHT:', result.error.stack);
37+
next(result.error);
38+
}
39+
40+
let headers = result.headers;
41+
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
42+
43+
for (var pair of headers.entries()) {
44+
res.append(pair[0], pair[1]);
45+
}
46+
47+
log(result.statusCode, statusMessage + path);
48+
res.status(result.statusCode);
49+
50+
if (typeof body === 'string') {
51+
res.send(body);
52+
} else if (result.error) {
53+
res.send(body[0]);
54+
} else {
55+
body.forEach(chunk => res.write(chunk));
56+
res.end();
57+
}
58+
} catch (error) {
6859
if (error.name === 'UnrecognizedURLError') {
6960
next();
7061
} else {

0 commit comments

Comments
 (0)