MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / ReadableStreamPipeTo

Function ReadableStreamPipeTo

examples/io/streams/modules/streams.js:771–979  ·  view source on GitHub ↗
(source, dest, preventClose, preventAbort, preventCancel, signal)

Source from the content-addressed store, hash-verified

769 return stream.reader !== undefined;
770}
771function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
772 assert(ReadableStreamBrand.has(source.self));
773 assert(WritableStreamBrand.has(dest.self));
774 assert(typeof preventClose === 'boolean');
775 assert(typeof preventAbort === 'boolean');
776 assert(typeof preventCancel === 'boolean');
777 if (signal !== undefined)
778 signal = AbortSignalBrand.get(signal);
779 assert(IsReadableStreamLocked(source) === false);
780 assert(IsWritableStreamLocked(dest) === false);
781 const reader = AcquireReadableStreamDefaultReader(source);
782 const writer = AcquireWritableStreamDefaultWriter(dest);
783 source.disturbed = true;
784 let shuttingDown = false;
785 // This is used to keep track of the spec's requirement that we wait for ongoing writes during shutdown.
786 let currentWrite = createResolvedPromise(undefined);
787 return new Promise((resolve, reject) => {
788 let abortAlgorithm;
789 if (signal !== undefined) {
790 abortAlgorithm = () => {
791 const error = signal.reason;
792 const actions = [];
793 if (preventAbort === false) {
794 actions.push(() => {
795 if (dest.state === 'writable') {
796 return WritableStreamAbort(dest, error);
797 }
798 return Promise.resolve(undefined);
799 });
800 }
801 if (preventCancel === false) {
802 actions.push(() => {
803 if (source.state === 'readable') {
804 return ReadableStreamCancel(source, error);
805 }
806 return Promise.resolve(undefined);
807 });
808 }
809 shutdownWithAction(() => Promise.all(actions.map(action => action())), true, error);
810 };
811 if (signal.aborted === true) {
812 abortAlgorithm();
813 return;
814 }
815 signal.self.addEventListener('abort', abortAlgorithm);
816 }
817
818 // Using reader and writer, read all chunks from this and write them to dest
819 // - Backpressure must be enforced
820 // - Shutdown must stop all activity
821 function pipeLoop() {
822 return new Promise((resolveLoop, rejectLoop) => {
823 function next(done) {
824 if (done) {
825 resolveLoop();
826 } else {
827 pipeStep().then(next, rejectLoop);
828 }

Callers 2

pipeThroughMethod · 0.85
pipeToMethod · 0.85

Calls 15

IsReadableStreamLockedFunction · 0.85
IsWritableStreamLockedFunction · 0.85
createResolvedPromiseFunction · 0.85
WritableStreamAbortFunction · 0.85
ReadableStreamCancelFunction · 0.85
shutdownWithActionFunction · 0.85
abortAlgorithmFunction · 0.85
isOrBecomesErroredFunction · 0.85
shutdownFunction · 0.85
isOrBecomesClosedFunction · 0.85

Tested by

no test coverage detected