@@ -73,7 +73,6 @@ const { |
73 | 73 | toUint8Array, |
74 | 74 | validateBatchEntry, |
75 | 75 | validateByteView, |
76 | | - yieldAbortable, |
77 | 76 | } = require('internal/streams/iter/utils'); |
78 | 77 | const { |
79 | 78 | converters, |
@@ -1737,6 +1736,11 @@ async function pipeTo(source, ...args) { |
1737 | 1736 |
|
1738 | 1737 | const hasWriteSync = typeof writer.writeSync === 'function'; |
1739 | 1738 | const normalized = from(source); |
| 1739 | + // The options passed to every write(), writev() and end(). |
| 1740 | + const writeOptions = signal ? { __proto__: null, signal } : undefined; |
| 1741 | + // Without transforms, whether `signal` has aborted the pipe: the batches |
| 1742 | + // are then no longer read or written (see raceSignal()). |
| 1743 | + let signalAborted = false; |
1740 | 1744 |
|
1741 | 1745 | let totalBytes = 0; |
1742 | 1746 | const hasWritev = typeof writer.writev === 'function'; |
@@ -1757,9 +1761,7 @@ async function pipeTo(source, ...args) { |
1757 | 1761 | } |
1758 | 1762 | validateByteView(view); |
1759 | 1763 | } |
1760 | | - const result = writer.write( |
1761 | | - validateByteView(view), |
1762 | | - signal ? { __proto__: null, signal } : undefined); |
| 1764 | + const result = writer.write(validateByteView(view), writeOptions); |
1763 | 1765 | if (result !== undefined) { |
1764 | 1766 | await result; |
1765 | 1767 | } |
@@ -1792,10 +1794,11 @@ async function pipeTo(source, ...args) { |
1792 | 1794 | // ignoring errors closing it. |
1793 | 1795 | async function pipeSyncSource(iterator) { |
1794 | 1796 | for (;;) { |
| 1797 | + if (signalAborted) return; |
1795 | 1798 | let batch = iterator[kNextSyncBatch](); |
1796 | 1799 | if (batch === undefined) { |
1797 | 1800 | const result = await iterator.next(); |
1798 | | - if (result.done) return; |
| 1801 | + if (result.done || signalAborted) return; |
1799 | 1802 | batch = result.value; |
1800 | 1803 | } else if (batch === null) { |
1801 | 1804 | return; |
@@ -1804,16 +1807,87 @@ async function pipeTo(source, ...args) { |
1804 | 1807 | const p = writeBatch(batch); |
1805 | 1808 | if (p) await p; |
1806 | 1809 | } catch (error) { |
1807 | | - try { |
1808 | | - await iterator.return(); |
1809 | | - } catch { |
1810 | | - // The error writing the batch is thrown. |
| 1810 | + if (!signalAborted) { |
| 1811 | + try { |
| 1812 | + await iterator.return(); |
| 1813 | + } catch { |
| 1814 | + // The error writing the batch is thrown. |
| 1815 | + } |
| 1816 | + } |
| 1817 | + throw error; |
| 1818 | + } |
| 1819 | + } |
| 1820 | + } |
| 1821 | + |
| 1822 | + // Write the batches of a source read with next(), as the for await...of |
| 1823 | + // loop below does, stopping once `signal` aborts. |
| 1824 | + async function pipeSourceWithSignal(iterator) { |
| 1825 | + for (;;) { |
| 1826 | + const result = await iterator.next(); |
| 1827 | + if (result.done || signalAborted) return; |
| 1828 | + try { |
| 1829 | + const p = writeBatch(result.value); |
| 1830 | + if (p) await p; |
| 1831 | + } catch (error) { |
| 1832 | + if (!signalAborted) { |
| 1833 | + try { |
| 1834 | + await iterator.return(); |
| 1835 | + } catch { |
| 1836 | + // The error writing the batch is thrown. |
| 1837 | + } |
1811 | 1838 | } |
1812 | 1839 | throw error; |
1813 | 1840 | } |
1814 | 1841 | } |
1815 | 1842 | } |
1816 | 1843 |
|
| 1844 | + // Wait for `pipe(iterator)` to write the batches of `iterator`, the |
| 1845 | + // normalized source, unless `signal` aborts first. On an abort, the source |
| 1846 | + // is closed, without waiting, and the abort reason is thrown at once, as a |
| 1847 | + // pull() pipeline with `signal` would reject its pending pull; `piped` |
| 1848 | + // then stops before reading or writing another batch. This takes one |
| 1849 | + // listener and one promise for the whole pipe, instead of a layer reading |
| 1850 | + // the source for every batch. The listener is added first, as the signal |
| 1851 | + // can abort while the source is read. |
| 1852 | + function raceSignal(iterator, pipe) { |
| 1853 | + const { promise, resolve, reject } = PromiseWithResolvers(); |
| 1854 | + let settled = false; |
| 1855 | + function closeAndReject() { |
| 1856 | + try { |
| 1857 | + const returnMethod = iterator.return; |
| 1858 | + if (typeof returnMethod === 'function') { |
| 1859 | + markPromiseAsHandled(PromiseResolve( |
| 1860 | + FunctionPrototypeCall(returnMethod, iterator))); |
| 1861 | + } |
| 1862 | + } catch { |
| 1863 | + // The abort takes precedence over errors closing the source. |
| 1864 | + } |
| 1865 | + reject(signal.reason); |
| 1866 | + } |
| 1867 | + |
| 1868 | + function onAbort() { |
| 1869 | + if (settled) return; |
| 1870 | + settled = true; |
| 1871 | + signalAborted = true; |
| 1872 | + // The signal can abort while the source is called: close it once that |
| 1873 | + // call has returned. |
| 1874 | + PromisePrototypeThen(kResolvedPromise, closeAndReject); |
| 1875 | + } |
| 1876 | + signal.addEventListener('abort', onAbort, kNullOnceOption); |
| 1877 | + PromisePrototypeThen(pipe(iterator), (value) => { |
| 1878 | + if (settled) return; |
| 1879 | + settled = true; |
| 1880 | + signal.removeEventListener('abort', onAbort); |
| 1881 | + resolve(value); |
| 1882 | + }, (error) => { |
| 1883 | + if (settled) return; |
| 1884 | + settled = true; |
| 1885 | + signal.removeEventListener('abort', onAbort); |
| 1886 | + reject(error); |
| 1887 | + }); |
| 1888 | + return promise; |
| 1889 | + } |
| 1890 | + |
1817 | 1891 | // Write the batches of an async source as the for await...of loop below |
1818 | 1892 | // does. Nothing cancels the normalization while a batch is read: return() |
1819 | 1893 | // is only called after an error writing one. |
@@ -1856,8 +1930,8 @@ async function pipeTo(source, ...args) { |
1856 | 1930 | if (!hasWritevSync || |
1857 | 1931 | !writer.writevSync(validateBatchEntry(entry))) { |
1858 | 1932 | validateBatchEntry(entry); |
1859 | | - const opts = signal ? { __proto__: null, signal } : undefined; |
1860 | | - const writevResult = writer.writev(validateBatchEntry(entry), opts); |
| 1933 | + const writevResult = writer.writev( |
| 1934 | + validateBatchEntry(entry), writeOptions); |
1861 | 1935 | if (writevResult === undefined) { |
1862 | 1936 | validateBatchEntry(entry); |
1863 | 1937 | totalBytes += entry.byteLength; |
@@ -1889,11 +1963,12 @@ async function pipeTo(source, ...args) { |
1889 | 1963 | if (transforms.length === 0) { |
1890 | 1964 | // Fast path: no transforms - iterate normalized source directly |
1891 | 1965 | if (signal) { |
1892 | | - for await (const batch of yieldAbortable(normalized, signal)) { |
1893 | | - signal.throwIfAborted(); |
1894 | | - const p = writeBatch(batch); |
1895 | | - if (p) await p; |
1896 | | - } |
| 1966 | + // Batches are read synchronously when possible, as without a signal. |
| 1967 | + // Async reads go through next(), which return() can cancel while it |
| 1968 | + // is pending, unlike kNextUncancellable. |
| 1969 | + const iterator = normalized[SymbolAsyncIterator](); |
| 1970 | + await raceSignal(iterator, iterator[kNextSyncBatch] !== undefined ? |
| 1971 | + pipeSyncSource : pipeSourceWithSignal); |
1897 | 1972 | } else if (normalized[kNextSyncBatch] !== undefined) { |
1898 | 1973 | await pipeSyncSource(normalized); |
1899 | 1974 | } else if (normalized[kNextUncancellable] !== undefined) { |
@@ -1909,8 +1984,9 @@ async function pipeTo(source, ...args) { |
1909 | 1984 | normalized, transforms, signal, signal !== undefined); |
1910 | 1985 |
|
1911 | 1986 | if (signal) { |
| 1987 | + // The pipeline rejects its pending pull when `signal` aborts. |
1912 | 1988 | for await (const batch of pipeline) { |
1913 | | - signal.throwIfAborted(); |
| 1989 | + if (signal.aborted) throw signal.reason; |
1914 | 1990 | const p = writeBatch(batch); |
1915 | 1991 | if (p) await p; |
1916 | 1992 | } |
@@ -1924,7 +2000,7 @@ async function pipeTo(source, ...args) { |
1924 | 2000 |
|
1925 | 2001 | if (!options.preventClose) { |
1926 | 2002 | if (!hasEndSync || writer.endSync() < 0) { |
1927 | | - await writer.end?.(signal ? { __proto__: null, signal } : undefined); |
| 2003 | + await writer.end?.(writeOptions); |
1928 | 2004 | } |
1929 | 2005 | } |
1930 | 2006 | } catch (error) { |
|