mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
doc: add stream pipelining note on Http usage
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
70f6554403
commit
77685d5ab0
1 file changed
+22
@@ -2107,6 +2107,28 @@ run().catch(console.error);
|
||||
after the `callback` has been invoked. In the case of reuse of streams after
|
||||
failure, this can cause event listener leaks and swallowed errors.
|
||||
|
||||
`stream.pipeline()` closes all the streams when an error is raised.
|
||||
The `IncomingRequest` usage with `pipeline` could lead to an unexpected behavior
|
||||
once it would destroy the socket without sending the expected response.
|
||||
See the example below:
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
const http = require('http');
|
||||
const { pipeline } = require('stream');
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
const fileStream = fs.createReadStream('./fileNotExist.txt');
|
||||
pipeline(fileStream, res, (err) => {
|
||||
if (err) {
|
||||
console.log(err); // No such file
|
||||
// this message can't be sent once `pipeline` already destroyed the socket
|
||||
return res.end('error!!!');
|
||||
}
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### `stream.compose(...streams)`
|
||||
|
||||
<!-- YAML
|
||||
|
||||
Reference in new issue
Block a user