Skip to content

Commit 04946a7

Browse files
authored
stream: rename Duplex.toWeb() type option to readableType
PR-URL: #61632 Refs: #58664 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com>
1 parent 42b261b commit 04946a7

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

doc/api/deprecations.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,6 +4415,21 @@ import { opendir } from 'node:fs/promises';
44154415
}
44164416
```
44174417
4418+
### DEP0201: Passing `options.type` to `Duplex.toWeb()`
4419+
4420+
<!-- YAML
4421+
changes:
4422+
- version: REPLACEME
4423+
pr-url: https://github.com/nodejs/node/pull/61632
4424+
description: Documentation-only deprecation.
4425+
-->
4426+
4427+
Type: Documentation-only
4428+
4429+
Passing the `type` option to [`Duplex.toWeb()`][] is deprecated. To specify the
4430+
type of the readable half of the constructed readable-writable pair, use the
4431+
`readableType` option instead.
4432+
44184433
[DEP0142]: #dep0142-repl_builtinlibs
44194434
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
44204435
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
@@ -4433,6 +4448,7 @@ import { opendir } from 'node:fs/promises';
44334448
[`Buffer.isBuffer()`]: buffer.md#static-method-bufferisbufferobj
44344449
[`Cipheriv`]: crypto.md#class-cipheriv
44354450
[`Decipheriv`]: crypto.md#class-decipheriv
4451+
[`Duplex.toWeb()`]: stream.md#streamduplextowebstreamduplex-options
44364452
[`Error.isError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
44374453
[`REPLServer.clearBufferedCommand()`]: repl.md#replserverclearbufferedcommand
44384454
[`ReadStream.open()`]: fs.md#class-fsreadstream

doc/api/stream.md

Lines changed: 10 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,13 @@ 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: Added the 'readableType' option to specify the ReadableStream
3405+
type. The 'type' option is deprecated.
34013406
- version: v25.4.0
34023407
pr-url: https://github.com/nodejs/node/pull/58664
3403-
description: Add 'type' option to specify 'bytes'.
3408+
description: Added the 'type' option to specify the ReadableStream type.
34043409
- version:
34053410
- v24.0.0
34063411
- v22.17.0
@@ -3410,7 +3415,9 @@ changes:
34103415

34113416
* `streamDuplex` {stream.Duplex}
34123417
* `options` {Object}
3413-
* `type` {string} Must be 'bytes' or undefined.
3418+
* `readableType` {string} Specifies the type of the `ReadableStream` half of
3419+
the created readable-writable pair. Must be `'bytes'` or undefined.
3420+
(`options.type` is a deprecated alias for this option.)
34143421
* Returns: {Object}
34153422
* `readable` {ReadableStream}
34163423
* `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+
// DEP0201: 'options.type' is a deprecated 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)