caolan / caolan/highland

_(readable).pipe(writable) does not destroy readable when writable closes

Open
#691 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.4k
Forks
145
PR merge metrics
No merged PRs in 30d

Description

I know native Node streams also do not exhibit this behaviour either but they provide [`pipeline`](https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback) (AKA [`pump`](https://github.com/mafintosh/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`](https://nodejs.org/api/http.html#http_class_http_serverresponse), e.g.

```js
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`.

Contributor guide

Open the contributing guide

Research direction

Start at Highland's readable.pipe(writable) entry point and inspect how writable close or destroy events are handled. Reproduce the Node.ServerResponse case from the issue, then verify that closing the writable destroys the readable and runs its cleanup before the stream continues buffering.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.