Skip to content

Commit c4fdc85

Browse files
committed
stream: rename Duplex.toWeb() type option to readableType
1 parent 6c44d31 commit c4fdc85

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

doc/api/stream.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,7 +3219,8 @@ changes:
32193219
If no value is provided, the size will be `1` for all the chunks.
32203220
* `chunk` {any}
32213221
* Returns: {number}
3222-
* `type` {string} Must be 'bytes' or undefined.
3222+
* `type` {string} Specifies the type of the created `ReadableStream`. Must be
3223+
`'bytes'` or undefined.
32233224
* Returns: {ReadableStream}
32243225

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

34113415
* `streamDuplex` {stream.Duplex}
34123416
* `options` {Object}
3413-
* `type` {string} Must be 'bytes' or undefined.
3417+
* `readableType` {string} Specifies the type of the `ReadableStream` half of
3418+
the created readable-writable pair. Must be `'bytes'` or undefined.
34143419
* Returns: {Object}
34153420
* `readable` {ReadableStream}
34163421
* `writable` {WritableStream}

lib/internal/webstreams/adapters.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ function newStreamReadableFromReadableStream(readableStream, options = kEmptyObj
624624

625625
/**
626626
* @param {Duplex} duplex
627-
* @param {{ type?: 'bytes' }} [options]
627+
* @param {{ readableType?: 'bytes' }} [options]
628628
* @returns {ReadableWritablePair}
629629
*/
630630
function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {
@@ -641,9 +641,15 @@ function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {
641641

642642
validateObject(options, 'options');
643643

644+
const readableOptions = {
645+
__proto__: null,
646+
// 'options.type' is a legacy alias for 'options.readableType'
647+
type: options.readableType ?? options.type,
648+
};
649+
644650
if (isDestroyed(duplex)) {
645651
const writable = new WritableStream();
646-
const readable = new ReadableStream({ type: options.type });
652+
const readable = new ReadableStream({ type: readableOptions.type });
647653
writable.close();
648654
readable.cancel();
649655
return { readable, writable };
@@ -659,8 +665,8 @@ function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {
659665

660666
const readable =
661667
isReadable(duplex) ?
662-
newReadableStreamFromStreamReadable(duplex, options) :
663-
new ReadableStream({ type: options.type });
668+
newReadableStreamFromStreamReadable(duplex, readableOptions) :
669+
new ReadableStream({ type: readableOptions.type });
664670

665671
if (!isReadable(duplex))
666672
readable.cancel();

test/parallel/test-stream-duplex.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ process.on('exit', () => {
147147
})
148148
});
149149

150-
const { writable, readable } = Duplex.toWeb(duplex, { type: 'bytes' });
150+
const { writable, readable } = Duplex.toWeb(duplex, { readableType: 'bytes' });
151151
writable.getWriter().write(dataToWrite);
152152
const data = new Uint8Array(dataToRead.length);
153153
readable.getReader({ mode: 'byob' }).read(data).then(common.mustCall((result) => {
154154
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
155155
}));
156+
157+
// Ensure that the originally-named `options.type` still works as an alias for `options.readableType`
158+
// `getReader({ mode: 'byob' })` throws if the underlying ReadableStream is not a byte stream
159+
Duplex.toWeb(duplex, { type: 'bytes' }).readable.getReader({ mode: 'byob' });
156160
}

0 commit comments

Comments
 (0)