Skip to content

Commit af45a1d

Browse files
Renegade334aduh95
authored andcommitted
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 5bfdac4 commit af45a1d

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
@@ -4320,6 +4320,21 @@ import { opendir } from 'node:fs/promises';
43204320
}
43214321
```
43224322
4323+
### DEP0201: Passing `options.type` to `Duplex.toWeb()`
4324+
4325+
<!-- YAML
4326+
changes:
4327+
- version: REPLACEME
4328+
pr-url: https://github.com/nodejs/node/pull/61632
4329+
description: Documentation-only deprecation.
4330+
-->
4331+
4332+
Type: Documentation-only
4333+
4334+
Passing the `type` option to [`Duplex.toWeb()`][] is deprecated. To specify the
4335+
type of the readable half of the constructed readable-writable pair, use the
4336+
`readableType` option instead.
4337+
43234338
[DEP0142]: #dep0142-repl_builtinlibs
43244339
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
43254340
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
@@ -4338,6 +4353,7 @@ import { opendir } from 'node:fs/promises';
43384353
[`Buffer.isBuffer()`]: buffer.md#static-method-bufferisbufferobj
43394354
[`Cipheriv`]: crypto.md#class-cipheriv
43404355
[`Decipheriv`]: crypto.md#class-decipheriv
4356+
[`Duplex.toWeb()`]: stream.md#streamduplextowebstreamduplex-options
43414357
[`Error.isError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
43424358
[`REPLServer.clearBufferedCommand()`]: repl.md#replserverclearbufferedcommand
43434359
[`ReadStream.open()`]: fs.md#class-fsreadstream

doc/api/stream.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,8 @@ changes:
32033203
If no value is provided, the size will be `1` for all the chunks.
32043204
* `chunk` {any}
32053205
* Returns: {number}
3206-
* `type` {string} Must be 'bytes' or undefined.
3206+
* `type` {string} Specifies the type of the created `ReadableStream`. Must be
3207+
`'bytes'` or undefined.
32073208
* Returns: {ReadableStream}
32083209

32093210
### `stream.Writable.fromWeb(writableStream[, options])`
@@ -3376,17 +3377,23 @@ duplex.once('readable', () => console.log('readable', duplex.read()));
33763377
<!-- YAML
33773378
added: v17.0.0
33783379
changes:
3380+
- version: REPLACEME
3381+
pr-url: https://github.com/nodejs/node/pull/61632
3382+
description: Added the 'readableType' option to specify the ReadableStream
3383+
type. The 'type' option is deprecated.
33793384
- version: v24.14.0
33803385
pr-url: https://github.com/nodejs/node/pull/58664
3381-
description: Add 'type' option to specify 'bytes'.
3386+
description: Added the 'type' option to specify the ReadableStream type.
33823387
- version: v24.0.0
33833388
pr-url: https://github.com/nodejs/node/pull/57513
33843389
description: Marking the API stable.
33853390
-->
33863391

33873392
* `streamDuplex` {stream.Duplex}
33883393
* `options` {Object}
3389-
* `type` {string} Must be 'bytes' or undefined.
3394+
* `readableType` {string} Specifies the type of the `ReadableStream` half of
3395+
the created readable-writable pair. Must be `'bytes'` or undefined.
3396+
(`options.type` is a deprecated alias for this option.)
33903397
* Returns: {Object}
33913398
* `readable` {ReadableStream}
33923399
* `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)