MCPcopy Index your code
hub / github.com/nodejs/node / readableStreamDefaultTee

Function readableStreamDefaultTee

lib/internal/webstreams/readablestream.js:1755–1847  ·  view source on GitHub ↗
(stream, cloneForBranch2)

Source from the content-addressed store, hash-verified

1753}
1754
1755function readableStreamDefaultTee(stream, cloneForBranch2) {
1756 const reader = new ReadableStreamDefaultReader(stream);
1757 let reading = false;
1758 let canceled1 = false;
1759 let canceled2 = false;
1760 let reason1;
1761 let reason2;
1762 let branch1;
1763 let branch2;
1764 const cancelPromise = PromiseWithResolvers();
1765
1766 async function pullAlgorithm() {
1767 if (reading) return;
1768 reading = true;
1769 const readRequest = {
1770 [kChunk](value) {
1771 queueMicrotask(() => {
1772 reading = false;
1773 const value1 = value;
1774 let value2 = value;
1775 if (!canceled2 && cloneForBranch2) {
1776 value2 = structuredClone(value2);
1777 }
1778 if (!canceled1) {
1779 readableStreamDefaultControllerEnqueue(
1780 branch1[kState].controller,
1781 value1);
1782 }
1783 if (!canceled2) {
1784 readableStreamDefaultControllerEnqueue(
1785 branch2[kState].controller,
1786 value2);
1787 }
1788 });
1789 },
1790 [kClose]() {
1791 // The `process.nextTick()` is not part of the spec.
1792 // This approach was needed to avoid a race condition working with esm
1793 // Further information, see: https://github.com/nodejs/node/issues/39758
1794 process.nextTick(() => {
1795 reading = false;
1796 if (!canceled1)
1797 readableStreamDefaultControllerClose(branch1[kState].controller);
1798 if (!canceled2)
1799 readableStreamDefaultControllerClose(branch2[kState].controller);
1800 if (!canceled1 || !canceled2)
1801 cancelPromise.resolve();
1802 });
1803 },
1804 [kError]() {
1805 reading = false;
1806 },
1807 };
1808 readableStreamDefaultReaderRead(reader, readRequest);
1809 }
1810
1811 function cancel1Algorithm(reason) {
1812 canceled1 = true;

Callers 1

readableStreamTeeFunction · 0.85

Calls 4

createReadableStreamFunction · 0.85
readerClosedPromiseFunction · 0.85
resolveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…