Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js WriteStream synchronous

I'm writing a purely synchronous, single threaded command line program in node.js, which needs to write a single binary file, for which I'm using WriteStream. My usage pattern is along the lines of:

var stream = fs.createWriteStream(file)
stream.write(buf1)
stream.write(buf2)

This seems to work, but the documentation says it's asynchronous and I want to make sure I'm not writing code that works 99% of the time. I don't care exactly when the data gets written as long as it's written in the specified order and no later than when the program exits, and the quantity of data is small so speed and memory consumption are not issues.

I've seen mention of stream.end() but it seems to work without it and I've also seen suggestions that calling it may actually be a bad idea if you're not using callbacks because it might end up getting called before all the data is written.

Is my approach correct (given that I want purely synchronous) or is there anything I need to watch out for?

like image 867
rwallace Avatar asked Jun 28 '14 16:06

rwallace


People also ask

Is there a synchronous write stream in Node JS?

By the way, there is a synchronous fs write stream implementation in node: fs.SyncWriteStream. It's kind of private and requires fd as an argument, but if you really want it... Show activity on this post.

What are writestreams in Node JS?

Since Node.js WriteStreams are descendants of the Writable object, we will also listen to events to it. Streams are collections of data that may not be available all at once and don’t have to fit in memory. This makes stream handy for processing large amounts of data.

How to listen to events from a read stream in NodeJS?

By using the fs.createWriteStream function, we created read streams to read a file’s data sequentially and listen to events from a read stream. Since Node.js WriteStreams are descendants of the Writable object, we will also listen to events to it.

What is the use of the file system module in Node JS?

The file system module or fs module is an inbuilt module in Node js which is used to work with files on the computer. The functions of the module can be used by importing the fs module. The fs module can be included in the program by using the fs.writeSync () function of the filesystem module is the synchronous version of the write () method.


1 Answers

I'm working on a timing-critical API, where a new file has to have been written and its stream completely handled before the next action can be performed. The solution, in my case (and, quite possibly, that of the OP's question) was to use:

writer.on('finish', () => {
 console.error('All writes are now complete.');
});

as per the fs Event: 'finish' documentation

like image 131
Jay Edwards Avatar answered Oct 03 '22 01:10

Jay Edwards