From bc47eb38491e65cf5730bba1fcb7de19c74dbdf9 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Thu, 28 Apr 2022 04:53:52 -0300 Subject: [PATCH] doc,test: add tests and docs for duplex.fromWeb and duplex.toWeb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/42738 Reviewed-By: Luigi Pinca Reviewed-By: Mestery Reviewed-By: James M Snell Reviewed-By: Juan José Arboleda Reviewed-By: Antoine du Hamel --- doc/api/stream.md | 106 ++++++++++++++++++++++++++++ test/parallel/test-stream-duplex.js | 80 ++++++++++++++++++++- 2 files changed, 185 insertions(+), 1 deletion(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index a696ffd543a403..c78f802d4a9ac2 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2887,6 +2887,67 @@ added: v17.0.0 * `signal` {AbortSignal} * Returns: {stream.Duplex} +```mjs +import { Duplex } from 'node:stream'; +import { + ReadableStream, + WritableStream +} from 'node:stream/web'; + +const readable = new ReadableStream({ + start(controller) { + controller.enqueue('world'); + }, +}); + +const writable = new WritableStream({ + write(chunk) { + console.log('writable', chunk); + } +}); + +const pair = { + readable, + writable +}; +const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); + +duplex.write('hello'); + +for await (const chunk of duplex) { + console.log('readable', chunk); +} +``` + +```cjs +const { Duplex } = require('node:stream'); +const { + ReadableStream, + WritableStream +} = require('node:stream/web'); + +const readable = new ReadableStream({ + start(controller) { + controller.enqueue('world'); + }, +}); + +const writable = new WritableStream({ + write(chunk) { + console.log('writable', chunk); + } +}); + +const pair = { + readable, + writable +}; +const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); + +duplex.write('hello'); +duplex.once('readable', () => console.log('readable', duplex.read())); +``` + ### `stream.Duplex.toWeb(streamDuplex)`