MCPcopy Create free account
hub / github.com/nodejs/node / newReadableWritablePairFromDuplex

Function newReadableWritablePairFromDuplex

lib/internal/webstreams/adapters.js:672–728  ·  view source on GitHub ↗

* @param {Duplex} duplex * @param {{ readableType?: 'bytes' }} [options] * @returns {ReadableWritablePair}

(duplex, options = kEmptyObject)

Source from the content-addressed store, hash-verified

670 * @returns {ReadableWritablePair}
671 */
672function newReadableWritablePairFromDuplex(duplex, options = kEmptyObject) {
673 // Not using the internal/streams/utils isWritableNodeStream and
674 // isReadableNodeStream utilities here because they will return false
675 // if the duplex was created with writable or readable options set to
676 // false. Instead, we'll check the readable and writable state after
677 // and return closed WritableStream or closed ReadableStream as
678 // necessary.
679 if (typeof duplex?._writableState !== 'object' ||
680 typeof duplex?._readableState !== 'object') {
681 throw new ERR_INVALID_ARG_TYPE('duplex', 'stream.Duplex', duplex);
682 }
683
684 validateObject(options, 'options');
685
686 const readableOptions = {
687 __proto__: null,
688 type: options.readableType,
689 };
690
691 if (options.readableType == null && options.type != null) {
692 // 'options.type' is a deprecated alias for 'options.readableType'
693 emitDEP0201();
694 readableOptions.type = options.type;
695 }
696
697 if (isDestroyed(duplex)) {
698 const writable = new WritableStream();
699 const readable = new ReadableStream({ type: readableOptions.type });
700 writable.close();
701 readable.cancel();
702 return { readable, writable };
703 }
704
705 const writableOptions = {
706 __proto__: null,
707 [kValidateChunk]: options[kValidateChunk],
708 [kDestroyOnSyncError]: options[kDestroyOnSyncError],
709 };
710
711 const writable =
712 isWritable(duplex) ?
713 newWritableStreamFromStreamWritable(duplex, writableOptions) :
714 new WritableStream();
715
716 if (!isWritable(duplex))
717 writable.close();
718
719 const readable =
720 isReadable(duplex) ?
721 newReadableStreamFromStreamReadable(duplex, readableOptions) :
722 new ReadableStream({ type: readableOptions.type });
723
724 if (!isReadable(duplex))
725 readable.cancel();
726
727 return { writable, readable };
728}
729

Calls 7

closeMethod · 0.95
cancelMethod · 0.95
isReadableFunction · 0.85
isDestroyedFunction · 0.50
isWritableFunction · 0.50

Tested by

no test coverage detected