Skip to content

Commit

Permalink
Fix: Don't rely on undocumented Stream.fd property (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent a8ab60b commit c7798bb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/dest/write-contents/write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ function writeStream(file, written) {
};

var outStream = fs.createWriteStream(file.path, opt);
var fd = null;

file.contents.once('error', complete);
file.contents.once('end', readStreamEnd);
outStream.once('error', complete);
outStream.once('finish', complete);
outStream.once('open', onOpen);

// Streams are piped with end disabled, this prevents the
// WriteStream from closing the file descriptor after all
// data is written.
file.contents.pipe(outStream, { end: false });

// Obtain the file descriptor from the "open" event.
function onOpen(openFd) {
fd = openFd;
}

function readStreamEnd() {
readStream(file, complete);
}
Expand All @@ -41,16 +48,17 @@ function writeStream(file, written) {
file.contents.removeListener('end', readStreamEnd);
outStream.removeListener('error', complete);
outStream.removeListener('finish', complete);
outStream.removeListener('open', onOpen);

if (streamErr) {
return end(streamErr);
}

if (typeof outStream.fd !== 'number') {
if (typeof fd !== 'number') {
return end();
}

fo.updateMetadata(outStream.fd, file, end);
fo.updateMetadata(fd, file, end);
}
}

Expand Down

0 comments on commit c7798bb

Please sign in to comment.