Sharp: [question] handling stream errors

Created on 19 Feb 2016  Â·  3Comments  Â·  Source: lovell/sharp

Hi, I was wondering what's the proper way of handling stream errors when using sharp as a duplex stream when we have a done callback waiting for either err or the resulting path

const readStream = fs.createReadStream(...);
const writeStream = fs.createWriteStream(...);

const transform = sharp()
 .resize(width, height)
 .toFormat(format);

if (background) {
  transform.background(background).embed();
}

readStream
  .on('error', done) // do we need to only call done(err) ? 
  .pipe(transform)
  .on('error', done)
  .pipe(writeStream)
  .on('error', done)
  .on('finish', () => {
      // do smth and return done(null, res)
  });

Since errors do not propagate and an error in the source would cause 'unpipe' on the destination - do I need to explicitly close transform or any other stream? Couldn't find anything about that in the docs, but I'm sure others will find this questions resolved being helpful, too

question

Most helpful comment

Hi Vitaly,

As you've probably seen, using pipe ends/closes all the streams for you on the "happy path".

If there's an error, only the Stream from which that error originated will emit an error event. The other streams will be unpiped but will be left open.

Any Streams backed by the filesystem will need to be closed to prevent a file handle leak. I guess this is more a feature of Node's Streams than it is specific to sharp itself.

Perhaps try the pump module to help with this.

Any stray Buffer objects piped into an instance of sharp (transform in your example) will become elegible for GC when that instance falls out of scope.

Hope this helps!

All 3 comments

Hi Vitaly,

As you've probably seen, using pipe ends/closes all the streams for you on the "happy path".

If there's an error, only the Stream from which that error originated will emit an error event. The other streams will be unpiped but will be left open.

Any Streams backed by the filesystem will need to be closed to prevent a file handle leak. I guess this is more a feature of Node's Streams than it is specific to sharp itself.

Perhaps try the pump module to help with this.

Any stray Buffer objects piped into an instance of sharp (transform in your example) will become elegible for GC when that instance falls out of scope.

Hope this helps!

Thanks a lot, thats exactly what I was looking for.

/cc @substack - any chance you can add section about this to streams handbook? In my experience that's what people usually struggle with when working with them

On 20 Feb 2016, at 13:20, Lovell Fuller [email protected] wrote:

Hi Vitaly,

As you've probably seen, using pipe ends/closes all the streams for you on the "happy path".

If there's an error, only the Stream from which that error originated will emit an error event. The other streams will be unpiped but will be left open.

Any Streams backed by the filesystem will need to be closed to prevent a file handle leak. I guess this is more a feature of Node's Streams than it is specific to sharp itself.

Perhaps try the pump module to help with this.

Any stray Buffer objects piped into an instance of sharp (transform in your example) will become elegible for GC when that instance falls out of scope.

Hope this helps!

—
Reply to this email directly or view it on GitHub.

@AVVS Great, thanks for confirming.

Was this page helpful?
0 / 5 - 0 ratings