Skip to content

Commit

Permalink
Fix piping to process.stdout and process.stderr
Browse files Browse the repository at this point in the history
process.stdout and process.stderr are not closed or ended after piping
like other streams. So we must resolve them manually.
  • Loading branch information
esamattis committed Oct 15, 2013
1 parent 1e2b655 commit 7244db9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,14 @@ var Q = require("q");
function promiseFromStreams(streams) {
return Q.all(streams.map(function(stream) {
return Q.promise(function(resolve, reject) {

// process.stdout and process.stderr are not closed or ended
// after piping like other streams. So we must resolve them
// manually.
if (stream === process.stdout || stream === process.stderr) {
return resolve();
}

stream.on("error", function(streamErr) {
var err = new Error(streamErr.message);
err.source = stream;
Expand Down
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -53,6 +53,15 @@ describe("promisePipe", function() {
});
});

["stdout", "stderr"].forEach(function(stdio) {
it("can pipe to " + stdio, function(done) {
var input = fs.createReadStream(INPUT);
promisePipe(input, process[stdio]).done(function() {
done();
});
});
});

it("can handle errors from source", function(done) {
var input = fs.createReadStream("bad");
var output = fs.createWriteStream(OUTPUT);
Expand Down

0 comments on commit 7244db9

Please sign in to comment.