-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Use custom implementation of fs.WriteStream that supports flush (f…
…ixes #189)
- Loading branch information
Showing
5 changed files
with
328 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,54 @@ | ||
'use strict'; | ||
|
||
var fs = require('graceful-fs'); | ||
|
||
var fo = require('../../file-operations'); | ||
var readStream = require('../../src/get-contents/read-stream'); | ||
|
||
function writeStream(file, onWritten) { | ||
var opt = { | ||
mode: file.stat.mode, | ||
// TODO: need to test this (node calls this `flags` property) | ||
flag: file.flag, | ||
}; | ||
|
||
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); | ||
// TODO: is this the best API? | ||
var outStream = fo.createWriteStream(file.path, opt, onFlush); | ||
|
||
// 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 }); | ||
file.contents.once('error', onComplete); | ||
outStream.once('error', onComplete); | ||
outStream.once('finish', onComplete); | ||
|
||
// Obtain the file descriptor from the "open" event. | ||
function onOpen(openFd) { | ||
fd = openFd; | ||
} | ||
// TODO: should this use a clone? | ||
file.contents.pipe(outStream); | ||
|
||
function readStreamEnd() { | ||
readStream(file, complete); | ||
function cleanup() { | ||
file.contents.removeListener('error', onComplete); | ||
outStream.removeListener('error', onComplete); | ||
outStream.removeListener('finish', onComplete); | ||
} | ||
|
||
function end(propagatedErr) { | ||
outStream.end(onEnd); | ||
|
||
function onEnd(endErr) { | ||
onWritten(propagatedErr || endErr); | ||
} | ||
function onComplete(streamErr) { | ||
cleanup(); | ||
onWritten(streamErr); | ||
} | ||
|
||
// Cleanup | ||
function complete(streamErr) { | ||
file.contents.removeListener('error', complete); | ||
file.contents.removeListener('end', readStreamEnd); | ||
outStream.removeListener('error', complete); | ||
outStream.removeListener('finish', complete); | ||
outStream.removeListener('open', onOpen); | ||
function onFlush(fd, callback) { | ||
// TODO: removing this before readStream because it replaces the stream | ||
file.contents.removeListener('error', onComplete); | ||
|
||
if (streamErr) { | ||
return end(streamErr); | ||
} | ||
// TODO: this is doing sync stuff & the callback seems unnecessary | ||
// TODO: do we really want to replace the contents stream or should we use a clone | ||
readStream(file, complete); | ||
|
||
if (typeof fd !== 'number') { | ||
return end(); | ||
} | ||
function complete() { | ||
if (typeof fd !== 'number') { | ||
return callback(); | ||
} | ||
|
||
fo.updateMetadata(fd, file, end); | ||
fo.updateMetadata(fd, file, callback); | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = writeStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.