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 @@ -541,6 +541,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
streamReadable.pause();

streamReadable.on('data', 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 @@ -42,3 +42,25 @@ const { setTimeout: delay } = require('timers/promises');
assert.deepStrictEqual(closeResult, { value: undefined, done: true });
})().then(common.mustCall());
}

// Cancelling a web ReadableStream while the underlying Readable is actively
// producing data should not throw ERR_INVALID_STATE. The 'data' 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 () => {
await reader.read();
await reader.read();
reader.releaseLock();
await webStream.cancel();
})().then(common.mustCall());
}