GeezerVison is showing github.com — stream: ensure pipeToSync requires synchronous close · nodejs/node@c475ec3
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/pull.js +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ const { | 22 | 22 | codes: { | 23 | 23 | ERR_INVALID_ARG_TYPE, | 24 | 24 | ERR_INVALID_ARG_VALUE, | | 25 | + ERR_INVALID_STATE, | 25 | 26 | ERR_OUT_OF_RANGE, | 26 | 27 | }, | 27 | 28 | } = require('internal/errors'); | @@ -969,6 +970,13 @@ function pullWithConsumerCleanup(source, transforms, signal) { | 969 | 970 | */ | 970 | 971 | function pipeToSync(source, ...args) { | 971 | 972 | const { transforms, writer, options } = parsePipeToArgs(args, 'writeSync'); | | 973 | + const hasWritevSync = typeof writer.writevSync === 'function'; | | 974 | + const endSync = writer.endSync; | | 975 | + | | 976 | + if (!options?.preventClose && typeof endSync !== 'function') { | | 977 | + throw new ERR_INVALID_ARG_TYPE( | | 978 | + 'writer.endSync', 'Function', endSync); | | 979 | + } | 972 | 980 |
| 973 | 981 | // Normalize source and create pipeline | 974 | 982 | const normalized = fromSync(source); | @@ -977,8 +985,6 @@ function pipeToSync(source, ...args) { | 977 | 985 | normalized; | 978 | 986 |
| 979 | 987 | let totalBytes = 0; | 980 | | - const hasWritevSync = typeof writer.writevSync === 'function'; | 981 | | - const hasEndSync = typeof writer.endSync === 'function'; | 982 | 988 |
| 983 | 989 | try { | 984 | 990 | for (const batch of pipeline) { | @@ -1007,8 +1013,9 @@ function pipeToSync(source, ...args) { | 1007 | 1013 | } | 1008 | 1014 |
| 1009 | 1015 | if (!options?.preventClose) { | 1010 | | - if (!hasEndSync || writer.endSync() < 0) { | 1011 | | - writer.end?.(); | | 1016 | + if (FunctionPrototypeCall(endSync, writer) < 0) { | | 1017 | + throw new ERR_INVALID_STATE( | | 1018 | + 'Writer could not be closed synchronously'); | 1012 | 1019 | } | 1013 | 1020 | } | 1014 | 1021 | } catch (error) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pipeto-edge.js +19-11Lines changed: 19 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Flags: --experimental-stream-iter | 2 | 2 | 'use strict'; | 3 | 3 |
| 4 | | -// Edge case tests for pipeToSync: endSync fallback, preventFail. | | 4 | +// Edge case tests for pipeToSync close and failure behavior. | 5 | 5 |
| 6 | 6 | const common = require('../common'); | 7 | 7 | const assert = require('assert'); | 8 | 8 | const { pipeToSync, fromSync } = require('stream/iter'); | 9 | 9 |
| 10 | | -// pipeToSync endSync returns negative → falls back to end() | 11 | | -async function testPipeToSyncEndSyncFallback() { | | 10 | +// pipeToSync cannot complete when endSync() requires async fallback. | | 11 | +async function testPipeToSyncEndSyncFailure() { | 12 | 12 | let endCalled = false; | 13 | 13 | const writer = { | 14 | 14 | writeSync() { return true; }, | 15 | | - endSync() { return -1; }, // Negative → triggers end() fallback | | 15 | + endSync() { return -1; }, | 16 | 16 | end() { endCalled = true; }, | 17 | 17 | }; | 18 | | - pipeToSync(fromSync('data'), writer); | 19 | | - assert.strictEqual(endCalled, true); | | 18 | + assert.throws( | | 19 | + () => pipeToSync(fromSync('data'), writer, { preventFail: true }), | | 20 | + { code: 'ERR_INVALID_STATE' }, | | 21 | + ); | | 22 | + assert.strictEqual(endCalled, false); | 20 | 23 | } | 21 | 24 |
| 22 | | -// pipeToSync endSync missing → falls back to end() | | 25 | +// pipeToSync requires endSync() when closing is enabled. | 23 | 26 | async function testPipeToSyncNoEndSync() { | | 27 | + let writeCalled = false; | 24 | 28 | let endCalled = false; | 25 | 29 | const writer = { | 26 | | - writeSync() { return true; }, | | 30 | + writeSync() { writeCalled = true; return true; }, | 27 | 31 | end() { endCalled = true; }, | 28 | 32 | }; | 29 | | - pipeToSync(fromSync('data'), writer); | 30 | | - assert.strictEqual(endCalled, true); | | 33 | + assert.throws( | | 34 | + () => pipeToSync(fromSync('data'), writer), | | 35 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 36 | + ); | | 37 | + assert.strictEqual(writeCalled, false); | | 38 | + assert.strictEqual(endCalled, false); | 31 | 39 | } | 32 | 40 |
| 33 | 41 | // pipeToSync with preventFail: true — source error does NOT call fail() | @@ -61,7 +69,7 @@ async function testPipeToSyncPreventClose() { | 61 | 69 | } | 62 | 70 |
| 63 | 71 | Promise.all([ | 64 | | - testPipeToSyncEndSyncFallback(), | | 72 | + testPipeToSyncEndSyncFailure(), | 65 | 73 | testPipeToSyncNoEndSync(), | 66 | 74 | testPipeToSyncPreventFail(), | 67 | 75 | testPipeToSyncPreventClose(), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pipeto.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,7 @@ async function testPipeToSyncSourceError() { | 71 | 71 | let failCalled = false; | 72 | 72 | const writer = { | 73 | 73 | writeSync() { return true; }, | | 74 | + endSync: common.mustNotCall(), | 74 | 75 | fail(reason) { failCalled = true; }, | 75 | 76 | }; | 76 | 77 | function* failingSource() { | @@ -127,6 +128,7 @@ async function testPipeToSyncWithTransforms() { | 127 | 128 | const chunks = []; | 128 | 129 | const writer = { | 129 | 130 | writeSync(chunk) { chunks.push(new TextDecoder().decode(chunk)); return true; }, | | 131 | + endSync() { return 0; }, | 130 | 132 | }; | 131 | 133 | const upper = (batch) => { | 132 | 134 | if (batch === null) return null; | @@ -160,6 +162,7 @@ async function testPipeToSyncWriterTransformMethodIgnored() { | 160 | 162 | chunks.push(new TextDecoder().decode(chunk)); | 161 | 163 | return true; | 162 | 164 | }, | | 165 | + endSync() { return 0; }, | 163 | 166 | }; | 164 | 167 |
| 165 | 168 | pipeToSync(fromSync('hello'), writer); | @@ -240,7 +243,7 @@ async function testPipeToSyncMinimalWriter() { | 240 | 243 | }, | 241 | 244 | }; | 242 | 245 |
| 243 | | - pipeToSync(fromSync('minimal-sync'), minimalWriter); | | 246 | + pipeToSync(fromSync('minimal-sync'), minimalWriter, { preventClose: true }); | 244 | 247 | assert.strictEqual(chunks.length > 0, true); | 245 | 248 | } | 246 | 249 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-resizable-buffers.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,7 +161,8 @@ async function testPipeRejectsWriterResize() { | 161 | 161 | fail: common.mustCall(), | 162 | 162 | }; | 163 | 163 | assert.throws( | 164 | | - () => pipeToSync([new Uint8Array(syncBuffer)], syncWriter), | | 164 | + () => pipeToSync( | | 165 | + [new Uint8Array(syncBuffer)], syncWriter, { preventClose: true }), | 165 | 166 | kResizeError, | 166 | 167 | ); | 167 | 168 | } | |
|