What event is triggered when EOF is reached while writing to a stream ? My code is as follows. and it is as per http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream
But surprisingly my 'end' event is never fired. When I checked http://nodejs.org/api/stream.html#stream_event_end, I see that writable stream does not have any event on 'end'
var x = a1.jpg; var options1 = {'url': url_of_an_image, 'encoding': null}; var r = request(options1).pipe(fs.createWriteStream('/tmp/imageresize/'+x)); r.on('end', function(){ console.log('file downloaded to ', '/tmp/imageresize/'+x); }
How do I capture the EOF event ?
The function fs. createWriteStream() creates a writable stream in a very simple manner. After a call to fs. createWriteStream() with the filepath, you have a writeable stream to work with. It turns out that the response (as well as the request) objects are streams.
The fs. read() method reads the file using the file descriptor and stores in the buffer.
createWriteStream() Method. The createWriteStream() method is an inbuilt application programming interface of fs module which allows to quickly make a writable stream for the purpose of writing data to a file.
TABLE OF CONTENTS. The easiest way to write to files in Node. js is to use the fs. writeFile() API.
Readable Steams emit close
event when the underlying resource done writing.
r.on('close', function(){ console.log('request finished downloading file'); });
But if you want to catch the moment when fs
finished writing data to the disc, you need Writeable Stream finish
event:
var w = fs.createWriteStream('/tmp/imageresize/'+x); request(options1).pipe(w); w.on('finish', function(){ console.log('file downloaded to ', '/tmp/imageresize/'+x); });
This is really old but this still saved me a lot of time.
Solution from leonid-beschastny is correct. However, there is a very subtle difference in the complete sequence of the calls being made. The solutions are all from the experts and a newbie like me gets lost - due to simple differences like - how a stream is being created or the objects missed in the description.
Also, this is a generic solution to a lot of other scenarios. When you are piping to a writableStream - and by the time async function that contains this operation - returns. The streaming is not complete - so the probable image/document/pdf that is being piped to - is not yet created. It will be of size 0 KB.
Sharing here in simple steps.
a. createPdfKitDocument with your definition as required. b. Create the writeStream the way you want as per your design. c. Pipe your prepared document to this stream. d. Add this finish event to your writeStream. e. Most important - without this it doesn't work.
Here
var pdfStream = printer.createPdfKitDocument(docDefinition); //a let writerStream = fs.createWriteStream(fullfileName); //b pdfStream.pipe(writerStream); //c writerStream.on('finish', function () { //This could be your callback function. callback()); }) //d pdfStream.end(); //e
I also referred to these links - they might help someone a. PDF download fails using PDFMake in NodeJS This gave me the clue to post.
Main issue is - createWriteStream is not finished [due to whatever reasn] - by the time the async function that contains it - returns.
Also leonid-beschastny is right in saying that race condition won't occur - I test my code by swapping the position of the pdfStream.pipe(writerStream);
and
writerStream.on('finish', function () { //This could be your callback function. callback()); })
Hope it helps few more people.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With