Tuesday, 4 April 2017

Should I close ReadStream after using .pipe() method

I'm reading image file, then piping it to image processing transformer using #pipe() method. Like this:

const imgStream = fs.createReadStream(...);
const transformedImage = imgStream.pipe(imageTransformer).pipe(anotherTransfomer);
// do something with transformedImage

Should I close readable and writable streams manually using the try-finally block?

As I understand #createReadStream method closes streams automatically. Is it true?

But from other side, I found this in documentation of #pipe() method:

One important caveat is that if the Readable stream emits an error during processing, the Writable destination is not closed automatically. If an error occurs, it will be necessary to manually close each stream in order to prevent memory leaks.

So I'm a bit confused, should I close streams in Node JS at all? Or I need it only when using #pipe() method?



via MyTitle

No comments:

Post a Comment