From 15816bd0ddb0f1dcde21dcff3e412271d4464d10 Mon Sep 17 00:00:00 2001 From: Austin Wright Date: Fri, 26 Jul 2024 01:09:23 -0700 Subject: [PATCH] stream: expose DuplexPair API PR-URL: https://github.com/nodejs/node/pull/34111 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Matteo Collina --- doc/api/stream.md | 32 +++++++- lib/internal/streams/duplexpair.js | 62 ++++++++++++++++ lib/stream.js | 1 + test/common/README.md | 9 --- test/common/duplexpair.js | 48 ------------ test/parallel/test-bootstrap-modules.js | 1 + test/parallel/test-gc-tls-external-memory.js | 4 +- .../test-http-agent-domain-reused-gc.js | 4 +- test/parallel/test-http-generic-streams.js | 12 +-- .../test-http-insecure-parser-per-stream.js | 10 +-- .../test-http-max-header-size-per-stream.js | 10 +-- ...t-http-sync-write-error-during-continue.js | 4 +- test/parallel/test-http2-backpressure.js | 4 +- .../test-http2-generic-streams-sendfile.js | 4 +- test/parallel/test-http2-generic-streams.js | 4 +- test/parallel/test-http2-padding-aligned.js | 4 +- .../test-http2-perform-server-handshake.js | 4 +- test/parallel/test-http2-sensitive-headers.js | 4 +- ...-http2-session-gc-while-write-scheduled.js | 4 +- test/parallel/test-http2-session-unref.js | 4 +- ...tp2-write-finishes-after-stream-destroy.js | 4 +- .../test-https-insecure-parse-per-stream.js | 7 +- .../test-https-max-header-size-per-stream.js | 7 +- test/parallel/test-stream-duplexpair.js | 74 +++++++++++++++++++ test/parallel/test-tls-destroy-stream.js | 4 +- test/parallel/test-tls-error-servername.js | 4 +- test/parallel/test-tls-generic-stream.js | 4 +- ...t-tls-socket-snicallback-without-server.js | 4 +- .../test-tls-streamwrap-buffersize.js | 4 +- ...test-tls-transport-destroy-after-own-gc.js | 4 +- .../test-worker-http2-stream-terminate.js | 4 +- ...orker-terminate-http2-respond-with-file.js | 4 +- 32 files changed, 230 insertions(+), 123 deletions(-) create mode 100644 lib/internal/streams/duplexpair.js delete mode 100644 test/common/duplexpair.js create mode 100644 test/parallel/test-stream-duplexpair.js diff --git a/doc/api/stream.md b/doc/api/stream.md index c189df15c42957..1cf07d00093a26 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -45,8 +45,11 @@ There are four fundamental stream types within Node.js: is written and read (for example, [`zlib.createDeflate()`][]). Additionally, this module includes the utility functions -[`stream.pipeline()`][], [`stream.finished()`][], [`stream.Readable.from()`][] -and [`stream.addAbortSignal()`][]. +[`stream.duplexPair()`][], +[`stream.pipeline()`][], +[`stream.finished()`][] +[`stream.Readable.from()`][], and +[`stream.addAbortSignal()`][]. ### Streams Promises API @@ -2675,6 +2678,30 @@ unless `emitClose` is set in false. Once `destroy()` has been called, any further calls will be a no-op and no further errors except from `_destroy()` may be emitted as `'error'`. +#### `stream.duplexPair([options])` + + + +* `options` {Object} A value to pass to both [`Duplex`][] constructors, + to set options such as buffering. +* Returns: {Array} of two [`Duplex`][] instances. + +The utility function `duplexPair` returns an Array with two items, +each being a `Duplex` stream connected to the other side: + +```js +const [ sideA, sideB ] = duplexPair(); +``` + +Whatever is written to one stream is made readable on the other. It provides +behavior analogous to a network connection, where the data written by the client +becomes readable by the server, and vice-versa. + +The Duplex streams are symmetrical; one or the other may be used without any +difference in behavior. + ### `stream.finished(stream[, options], callback)`