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
1 change: 1 addition & 0 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
let wasCanceled = false;

function onData(chunk) {
if (wasCanceled) return;
// Copy the Buffer to detach it from the pool.
if (Buffer.isBuffer(chunk) && !objectMode)
chunk = new Uint8Array(chunk);
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-stream-readable-to-web-termination.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,25 @@
const reader = Readable.toWeb(r).getReader();
reader.read();
}

// Cancelling a web ReadableStream while the underlying Readable is actively
// producing data should not throw ERR_INVALID_STATE. The onData handler in
// newReadableStreamFromStreamReadable must check wasCanceled before calling
// controller.enqueue(). See: https://github.com/nodejs/node/issues/54205
{
const readable = new Readable({
read() {
this.push(Buffer.alloc(1024));
},
});

const webStream = Readable.toWeb(readable);
const reader = webStream.getReader();

(async () => {

Check failure on line 28 in test/parallel/test-stream-readable-to-web-termination.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

The result of an immediately-invoked async function needs to be used (e.g. with `.then(common.mustCall())`)
await reader.read();
await reader.read();
reader.releaseLock();
await webStream.cancel();
})();
}
Loading