diff --git a/doc/api/stream.md b/doc/api/stream.md index cd8e7e6e7f5c80..2b38f44d6a895b 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2869,6 +2869,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)`