Frage

I'm reading from a file stream returned from fs.createReadStream() and I'm piping it to a gzip stream created using zlib.createGzip() and then I'm piping the gzip stream to a HTTP response.

I'm not sure how to deal with the 'error' event on these streams. I just want to make sure that all the the streams get closed, that the error is logged, and that no resources are leaked (note that the file stream has autoClose set to true).

If an error happens on the fs read stream for example, how will that affect the gzip stream and then the response stream? Will the 'error' event be propagated automatically or will it just be unhandled and crash my app? Should I listen for the 'error' event on each of the streams or just on the last stream in the chain? What would happen if I listen to the 'error' on the fs stream - Will the gzip stream still detect that an error has occurred?

War es hilfreich?

Lösung

The errors from one stream to another is not propagated on piping, so you should be attaching the error listeners to both streams.

If the fs read stream has an error, if autoClose is true then it will be destroyed(it will clean up and close the file descriptor). But the gzip stream won't be closed so you have to close it manually.

If the gzip has an error, it will just emit it. It won't be closed, nor the readable stream will be.

Looking at other streams, like fs write stream, if an error happens on the writing, then it will close the writable stream, but the readable stream will stay open.

So my recommendation is that, set error handler on all your streams and don't rely on themselves closing on error, so call .close or .destroy on all errors.

To make sure you are listening on all error handlers, use domains.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top