Skip to content

Commit 1c7a74e

Browse files
authored
doc: add stream pipelining note on Http usage
PR-URL: #41796 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mary Marchini <oss@mmarchini.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 365e5f5 commit 1c7a74e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/api/stream.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,28 @@ run().catch(console.error);
25222522
after the `callback` has been invoked. In the case of reuse of streams after
25232523
failure, this can cause event listener leaks and swallowed errors.
25242524

2525+
`stream.pipeline()` closes all the streams when an error is raised.
2526+
The `IncomingRequest` usage with `pipeline` could lead to an unexpected behavior
2527+
once it would destroy the socket without sending the expected response.
2528+
See the example below:
2529+
2530+
```js
2531+
const fs = require('fs');
2532+
const http = require('http');
2533+
const { pipeline } = require('stream');
2534+
2535+
const server = http.createServer((req, res) => {
2536+
const fileStream = fs.createReadStream('./fileNotExist.txt');
2537+
pipeline(fileStream, res, (err) => {
2538+
if (err) {
2539+
console.log(err); // No such file
2540+
// this message can't be sent once `pipeline` already destroyed the socket
2541+
return res.end('error!!!');
2542+
}
2543+
});
2544+
});
2545+
```
2546+
25252547
### `stream.compose(...streams)`
25262548

25272549
<!-- YAML

0 commit comments

Comments
 (0)