Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
End asyncStream later in the process (before piping)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Amirov committed Sep 21, 2017
1 parent 2eba01e commit a1fb334
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ module.exports = function processRequest(options, request, response) {
} = options;

const asyncStream = new AsyncStream();
asyncStream.once('plugged', () => {
asyncStream.end();
});

const contextPromise = fetchContext(request).catch(err => {
this.emit('context:error', request, err);
return {};
Expand Down Expand Up @@ -195,9 +199,6 @@ module.exports = function processRequest(options, request, response) {
});

resultStream.once('finish', () => {
// Flush the async stream only after stringifer stream is finished
// This guarentees the async fragments are flushed to the client at last
asyncStream.end();
const statusCode = response.statusCode || 200;
if (shouldWriteHead) {
shouldWriteHead = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/streams/stringifier-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = class StringifierStream extends stream.Transform {
}
let st = this.queue.shift();
if (st instanceof stream) {
st.emit('plugged');

st.setMaxListeners(st.getMaxListeners() + 1);
this.isBusy = true;

Expand Down

0 comments on commit a1fb334

Please sign in to comment.