Skip to content

Commit 78d30f0

Browse files
committed
enable chunked response
1 parent 3a32ee1 commit 78d30f0

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

src/index.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,33 @@ function fastbootExpressMiddleware(distPath, options) {
3232
.then(success, failure);
3333

3434
function success(result) {
35-
result.html()
36-
.then(html => {
37-
let headers = result.headers;
38-
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
39-
40-
for (var pair of headers.entries()) {
41-
res.set(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-
res.send(html);
52-
})
53-
.catch(error => {
54-
res.status(500);
55-
next(error);
56-
});
35+
result.chunks()
36+
.then(chunks => {
37+
let headers = result.headers;
38+
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
39+
40+
for (var pair of headers.entries()) {
41+
res.set(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+
if (result.error) {
52+
res.send(chunks[0]);
53+
} else {
54+
chunks.forEach(chunk => res.write(chunk));
55+
res.end();
56+
}
57+
})
58+
.catch(error => {
59+
res.status(500);
60+
next(error);
61+
});
5762
}
5863

5964
function failure(error) {

test/middleware-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ describe("FastBoot", function() {
5151
});
5252
});
5353

54+
it("responds with a chunked response", function() {
55+
let middleware = fastbootMiddleware({
56+
distPath: fixture('basic-app')
57+
});
58+
server = new TestHTTPServer(middleware, { errorHandling: true });
59+
60+
return server.start()
61+
.then(() => server.request('/', { resolveWithFullResponse: true }))
62+
.then(({ body, _, headers }) => {
63+
expect(headers['transfer-encoding']).to.eq('chunked');
64+
expect(body).to.match(/Welcome to Ember/);
65+
});
66+
});
67+
5468
it("returns 404 when navigating to a URL that doesn't exist", function() {
5569
let middleware = fastbootMiddleware(fixture('basic-app'));
5670
server = new TestHTTPServer(middleware);

0 commit comments

Comments
 (0)