Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ function newWritableStreamFromStreamWritable(streamWritable) {
write(chunk) {
if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) {
backpressurePromise = PromiseWithResolvers();
if (!streamWritable.writableNeedDrain) {
backpressurePromise.resolve();
}
return SafePromisePrototypeFinally(
backpressurePromise.promise, () => {
backpressurePromise = undefined;
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,26 @@ class TestWritable extends Writable {
const writer = writableStream.getWriter();
writer.closed.then(common.mustCall());
}

{
// Test that the stream doesn't hang when the underlying Writable
// emits 'drain' synchronously during write().
// Fixes: https://github.com/nodejs/node/issues/61145
const writable = new Writable({
write(chunk, encoding, callback) {
callback();
},
});

// Force synchronous 'drain' emission during write()
// to simulate a stream that doesn't have Node.js's built-in kSync protection.
writable.write = function(chunk) {
this.emit('drain');
return false;
};

const writableStream = newWritableStreamFromStreamWritable(writable);
const writer = writableStream.getWriter();
writer.write(new Uint8Array([1, 2, 3])).then(common.mustCall());
writer.write(new Uint8Array([4, 5, 6])).then(common.mustCall());
}
Loading