I'm writing a large file with node.js using a writable stream:
var fs = require('fs');
var stream = fs.createWriteStream('someFile.txt', { flags : 'w' });
var lines;
while (lines = getLines()) {
for (var i = 0; i < lines.length; i++) {
stream.write( lines[i] );
}
}
I'm wondering if this scheme is safe without using drain
event? If it is not (which I think is the case), what is the pattern for writing an arbitrary large data to a file?
[Edit] The updated Node.js
writable.write(...)
API docs say:[Original] From the
stream.write(...)
documentation (emphasis mine):I interpret this to mean that the "write" function returns
true
if the given string was immediately written to the underlying OS buffer orfalse
if it was not yet written but will be written by the write function (e.g. was presumably buffered for you by the WriteStream) so that you do not have to call "write" again.