Skip to content

Commit 5085813

Browse files
RafaelGSSruyadorno
authored andcommitted
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 bd5d158 commit 5085813

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/api/stream.md

+22
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,28 @@ run().catch(console.error);
24632463
after the `callback` has been invoked. In the case of reuse of streams after
24642464
failure, this can cause event listener leaks and swallowed errors.
24652465

2466+
`stream.pipeline()` closes all the streams when an error is raised.
2467+
The `IncomingRequest` usage with `pipeline` could lead to an unexpected behavior
2468+
once it would destroy the socket without sending the expected response.
2469+
See the example below:
2470+
2471+
```js
2472+
const fs = require('fs');
2473+
const http = require('http');
2474+
const { pipeline } = require('stream');
2475+
2476+
const server = http.createServer((req, res) => {
2477+
const fileStream = fs.createReadStream('./fileNotExist.txt');
2478+
pipeline(fileStream, res, (err) => {
2479+
if (err) {
2480+
console.log(err); // No such file
2481+
// this message can't be sent once `pipeline` already destroyed the socket
2482+
return res.end('error!!!');
2483+
}
2484+
});
2485+
});
2486+
```
2487+
24662488
### `stream.compose(...streams)`
24672489

24682490
<!-- YAML

0 commit comments

Comments
 (0)