mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
stream: reject writes during WritableStream close
WritableStreamDefaultControllerProcessClose clears the controller algorithms while the stream is still 'writable', before the sink close settles. A write in that window, such as writer.write() after writer.close() while the sink's close() or a TransformStream's flush() is pending, reached the cleared size algorithm and failed an internal assertion instead of being rejected as closing. Accept that state in writableStreamDefaultControllerGetChunkSize so the write is rejected with a TypeError, as it is once the stream has closed. Refs: https://github.com/nodejs/node/issues/57272 Refs: https://github.com/whatwg/streams/issues/1341 Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/67058 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
ffddf27ec1
commit
5d541dcd26
2 files changed
+42
-3
No files matched your search
@@ -1264,9 +1264,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
|
||||
sizeAlgorithm,
|
||||
} = controller[kState];
|
||||
if (sizeAlgorithm === undefined) {
|
||||
assert(stream[kState].state === 'closed' ||
|
||||
stream[kState].state === 'errored' ||
|
||||
stream[kState].state === 'erroring');
|
||||
// The algorithms are also cleared while the close request is in
|
||||
// flight, before the stream leaves the 'writable' state; the caller
|
||||
// then rejects the write as closing.
|
||||
assert(stream[kState].state !== 'writable' ||
|
||||
stream[kState].closeQueuedOrInFlight);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,3 +27,40 @@ test('should throw error when writing after close', async (t) => {
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test('should reject writes while the close is in flight', async () => {
|
||||
const { promise, resolve } = Promise.withResolvers();
|
||||
const writable = new WritableStream({
|
||||
close() {
|
||||
return promise;
|
||||
},
|
||||
});
|
||||
|
||||
const writer = writable.getWriter();
|
||||
const closed = writer.close();
|
||||
// Let the close request reach the sink.
|
||||
await new Promise(setImmediate);
|
||||
|
||||
await assert.rejects(writer.write('World'), { name: 'TypeError' });
|
||||
|
||||
resolve();
|
||||
await closed;
|
||||
});
|
||||
|
||||
test('should reject writes to a TransformStream while flush() is pending', async () => {
|
||||
const { promise, resolve } = Promise.withResolvers();
|
||||
const transform = new TransformStream({
|
||||
flush() {
|
||||
return promise;
|
||||
},
|
||||
});
|
||||
|
||||
const writer = transform.writable.getWriter();
|
||||
const closed = writer.close();
|
||||
await new Promise(setImmediate);
|
||||
|
||||
await assert.rejects(writer.write('World'), { name: 'TypeError' });
|
||||
|
||||
resolve();
|
||||
await closed;
|
||||
});
|
||||
Reference in new issue
Block a user