Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -3219,7 +3219,8 @@ changes:
If no value is provided, the size will be `1` for all the chunks.
* `chunk` {any}
* Returns: {number}
* `type` {string} Must be 'bytes' or undefined.
* `type` {string} Specifies the type of the created `ReadableStream`. Must be
`'bytes'` or undefined.
* Returns: {ReadableStream}

### `stream.Writable.fromWeb(writableStream[, options])`
Expand Down Expand Up @@ -3398,9 +3399,12 @@ duplex.once('readable', () => console.log('readable', duplex.read()));
<!-- YAML
added: v17.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/61632
description: The 'type' option is renamed to 'readableType'.
- version: v25.4.0
pr-url: https://github.com/nodejs/node/pull/58664
description: Add 'type' option to specify 'bytes'.
description: Added the 'type' option to specify the ReadableStream type.
- version:
- v24.0.0
- v22.17.0
Expand All @@ -3410,7 +3414,8 @@ changes:

* `streamDuplex` {stream.Duplex}
* `options` {Object}
* `type` {string} Must be 'bytes' or undefined.
* `readableType` {string} Specifies the type of the `ReadableStream` half of
the created readable-writable pair. Must be `'bytes'` or undefined.
* Returns: {Object}
* `readable` {ReadableStream}
* `writable` {WritableStream}
Expand Down
14 changes: 10 additions & 4 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ function newStreamReadableFromReadableStream(readableStream, options = kEmptyObj

/**
* @param {Duplex} duplex
* @param {{ type?: 'bytes' }} [options]
* @param {{ readableType?: 'bytes' }} [options]
* @returns {ReadableWritablePair}
*/
function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {
Expand All @@ -641,9 +641,15 @@ function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {

validateObject(options, 'options');

const readableOptions = {
__proto__: null,
// 'options.type' is a legacy alias for 'options.readableType'
type: options.readableType ?? options.type,
};

if (isDestroyed(duplex)) {
const writable = new WritableStream();
const readable = new ReadableStream({ type: options.type });
const readable = new ReadableStream({ type: readableOptions.type });
writable.close();
readable.cancel();
return { readable, writable };
Expand All @@ -659,8 +665,8 @@ function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {

const readable =
isReadable(duplex) ?
newReadableStreamFromStreamReadable(duplex, options) :
new ReadableStream({ type: options.type });
newReadableStreamFromStreamReadable(duplex, readableOptions) :
new ReadableStream({ type: readableOptions.type });

if (!isReadable(duplex))
readable.cancel();
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-stream-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ process.on('exit', () => {
})
});

const { writable, readable } = Duplex.toWeb(duplex, { type: 'bytes' });
const { writable, readable } = Duplex.toWeb(duplex, { readableType: 'bytes' });
writable.getWriter().write(dataToWrite);
const data = new Uint8Array(dataToRead.length);
readable.getReader({ mode: 'byob' }).read(data).then(common.mustCall((result) => {
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
}));

// Ensure that the originally-named `options.type` still works as an alias for `options.readableType`
// `getReader({ mode: 'byob' })` throws if the underlying ReadableStream is not a byte stream
Duplex.toWeb(duplex, { type: 'bytes' }).readable.getReader({ mode: 'byob' });
}
Loading