Cloud storage: how to cancel/destroy a stream created with createReadStream()

Viewed 97

How can I cancel a stream created with const stream = file.createReadStream()?

I'm using node.js and sending files via fastify to the client. If the client closes the connection, I'd like also to cancel the reading from the stream, so to close the connection to cloud storage and stop transfering data.

Currently my server continues reading from the stream but the content is never used since the outgoing stream to the client is already destroyed. fastify calls stream.destroy() when the receiving end is prematurely closed. But the read stream happily receives data from the cloud storage.

From reading the source code, this is a PassThrough stream, that gets its content only via events and has no connection to the underlying receiving stream.

Is there a method to stop this cloud stream?

Christian

1 Answers
Related