Skip to content

Commit 23c707d

Browse files
committed
cleanup
1 parent 05132ac commit 23c707d

2 files changed

Lines changed: 86 additions & 109 deletions

File tree

src/index.js

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,58 +32,37 @@ function fastbootExpressMiddleware(distPath, options) {
3232
.then(success, failure);
3333

3434
function success(result) {
35-
if (opts.chunkedResponse) {
36-
result.chunks()
37-
.then(chunks => {
38-
let headers = result.headers;
39-
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
40-
41-
for (var pair of headers.entries()) {
42-
res.set(pair[0], pair[1]);
43-
}
44-
45-
if (result.error) {
46-
log("RESILIENT MODE CAUGHT:", result.error.stack);
47-
next(result.error);
48-
}
49-
50-
log(result.statusCode, statusMessage + path);
51-
res.status(result.statusCode);
52-
if (result.error) {
53-
res.send(chunks[0]);
54-
} else {
55-
chunks.forEach(chunk => res.write(chunk));
56-
res.end();
57-
}
58-
})
59-
.catch(error => {
60-
res.status(500);
61-
next(error);
62-
});
63-
} else {
64-
result.html()
65-
.then(html => {
66-
let headers = result.headers;
67-
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
68-
69-
for (var pair of headers.entries()) {
70-
res.set(pair[0], pair[1]);
71-
}
72-
73-
if (result.error) {
74-
log("RESILIENT MODE CAUGHT:", result.error.stack);
75-
next(result.error);
76-
}
77-
78-
log(result.statusCode, statusMessage + path);
79-
res.status(result.statusCode);
80-
res.send(html);
81-
})
82-
.catch(error => {
83-
res.status(500);
84-
next(error);
85-
});
86-
}
35+
let responseBody = opts.chunkedResponse ? result.chunks() : result.html();
36+
37+
responseBody.then(body => {
38+
let headers = result.headers;
39+
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
40+
41+
for (var pair of headers.entries()) {
42+
res.set(pair[0], pair[1]);
43+
}
44+
45+
if (result.error) {
46+
log("RESILIENT MODE CAUGHT:", result.error.stack);
47+
next(result.error);
48+
}
49+
50+
log(result.statusCode, statusMessage + path);
51+
res.status(result.statusCode);
52+
53+
if (typeof body === 'string') {
54+
res.send(body);
55+
} else if (result.error) {
56+
res.send(body[0]);
57+
} else {
58+
body.forEach(chunk => res.write(chunk));
59+
res.end();
60+
}
61+
})
62+
.catch(error => {
63+
res.status(500);
64+
next(error);
65+
});
8766
}
8867

8968
function failure(error) {

test/middleware-test.js

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

54+
it("can be provided with a custom FastBoot instance", function() {
55+
let fastboot = new FastBoot({
56+
distPath: fixture('basic-app')
57+
});
58+
59+
let middleware = fastbootMiddleware({
60+
fastboot: fastboot
61+
});
62+
63+
server = new TestHTTPServer(middleware);
64+
65+
return server.start()
66+
.then(() => server.request('/'))
67+
.then(html => {
68+
expect(html).to.match(/Welcome to Ember/);
69+
});
70+
});
71+
72+
it("can reload the FastBoot instance", function() {
73+
let fastboot = new FastBoot({
74+
distPath: fixture('basic-app')
75+
});
76+
77+
let middleware = fastbootMiddleware({
78+
fastboot: fastboot
79+
});
80+
81+
server = new TestHTTPServer(middleware);
82+
83+
return server.start()
84+
.then(requestFirstApp)
85+
.then(hotReloadApp)
86+
.then(requestSecondApp);
87+
88+
function requestFirstApp(info) {
89+
return server.request('/')
90+
.then(function(html) {
91+
expect(html).to.match(/Welcome to Ember/);
92+
});
93+
}
94+
95+
function hotReloadApp() {
96+
fastboot.reload({
97+
distPath: fixture('hot-swap-app')
98+
});
99+
}
100+
101+
function requestSecondApp(info) {
102+
return server.request('/')
103+
.then(function(html) {
104+
expect(html).to.match(/Goodbye from Ember/);
105+
});
106+
}
107+
});
108+
54109
[true, false].forEach((chunkedResponse) => {
55110
describe(`when chunked response is ${chunkedResponse ? 'enabled' : 'disabled'}`, function() {
56111
if (chunkedResponse) {
@@ -98,63 +153,6 @@ describe("FastBoot", function() {
98153
});
99154
});
100155

101-
it("can be provided with a custom FastBoot instance", function() {
102-
let fastboot = new FastBoot({
103-
distPath: fixture('basic-app')
104-
});
105-
106-
let middleware = fastbootMiddleware({
107-
fastboot: fastboot,
108-
chunkedResponse
109-
});
110-
111-
server = new TestHTTPServer(middleware);
112-
113-
return server.start()
114-
.then(() => server.request('/'))
115-
.then(html => {
116-
expect(html).to.match(/Welcome to Ember/);
117-
});
118-
});
119-
120-
it("can reload the FastBoot instance", function() {
121-
let fastboot = new FastBoot({
122-
distPath: fixture('basic-app')
123-
});
124-
125-
let middleware = fastbootMiddleware({
126-
fastboot: fastboot,
127-
chunkedResponse
128-
});
129-
130-
server = new TestHTTPServer(middleware);
131-
132-
return server.start()
133-
.then(requestFirstApp)
134-
.then(hotReloadApp)
135-
.then(requestSecondApp);
136-
137-
function requestFirstApp(info) {
138-
return server.request('/')
139-
.then(function(html) {
140-
expect(html).to.match(/Welcome to Ember/);
141-
});
142-
}
143-
144-
function hotReloadApp() {
145-
fastboot.reload({
146-
distPath: fixture('hot-swap-app')
147-
});
148-
}
149-
150-
function requestSecondApp(info) {
151-
return server.request('/')
152-
.then(function(html) {
153-
expect(html).to.match(/Goodbye from Ember/);
154-
});
155-
}
156-
});
157-
158156
describe('when reslient mode is enabled', function () {
159157
it("renders no FastBoot markup", function() {
160158
let middleware = fastbootMiddleware({

0 commit comments

Comments
 (0)