diff --git a/doc/api/stream.md b/doc/api/stream.md index d59cd6b51629e4..0526e696f54c13 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2874,6 +2874,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)`