I know native Node streams also do not exhibit this behaviour either but they provide pipeline (AKA pump) to ensure proper cleanup. I wondered what the equivalent pattern is for highland.
My particular use case is piping a readable stream to a Node.ServerResponse, e.g.
const express = require('express');
const highland = require('highland');
const { pipeline } = require('stream');
const app = express();
app.get('/highland', (req, res) => {
const readable = createNodeReadableStream();
_(readable).pipe(res);
res.destroy();
setInterval(() => {
console.log(aNodeReadableStream.destroyed) // Always false, even when `res` is closed 😔
}, 1000);
});
app.get('/node-stream', (req, res) => {
const readable = createNodeReadableStream();
pipeline(readable, res, (ex) => {
ex && console.error(ex);
});
res.destroy();
setInterval(() => {
console.log(aNodeReadableStream.destroyed) // Becomes true when `res` closes 🙂
}, 1000);
});
If res is destroyed, either explicitly as above or for example by the browser cancelling the request, readable is not destroyed and therefore any cleanup code is not run -- it will continue to write until it's buffer reaches the highWaterMark.
I know native Node streams also do not exhibit this behaviour either but they provide
pipeline(AKApump) to ensure proper cleanup. I wondered what the equivalent pattern is for highland.My particular use case is piping a readable stream to a
Node.ServerResponse, e.g.If
resis destroyed, either explicitly as above or for example by the browser cancelling the request,readableis not destroyed and therefore any cleanup code is not run -- it will continue to write until it's buffer reaches thehighWaterMark.