Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,9 @@ async function httpNetworkFetch (
// into stream.
const buffer = new Uint8Array(bytes)
if (buffer.byteLength) {
fetchParams.controller.controller.enqueue(buffer)
try {
fetchParams.controller.controller.enqueue(buffer)
} catch {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhafraDev please review this. A wpt is failing without it: https://github.com/nodejs/undici/blob/main/test/wpt/tests/fetch/api/basic/stream-safe-creation.any.js

Uncaught exception in "D:\a\undici\undici\test\wpt\tests\fetch\api\basic\stream-safe-creation.any.js":
TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
    at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1160:13)
    at fetchParams.controller.resume (D:\a\undici\undici\lib\fetch\index.js:2024:43)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
================================================================================================
Uncaught exception in "D:\a\undici\undici\test\wpt\tests\fetch\api\basic\stream-safe-creation.any.js":
TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
    at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1160:13)
    at fetchParams.controller.resume (D:\a\undici\undici\lib\fetch\index.js:2024:43)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
================================================================================================

}

// 8. If stream is errored, then terminate the ongoing fetch.
Expand Down
11 changes: 7 additions & 4 deletions test/fetch/pull-dont-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const { setTimeout: sleep } = require('timers/promises')

const { closeServerAsPromise } = require('../utils/node-http')

test('Allow the usage of custom implementation of AbortController', async (t) => {
test('pull dont\'t push', async (t) => {
let count = 0
let socket
const max = 1_000_000
const server = createServer((req, res) => {
res.statusCode = 200
socket = res.socket
Expand All @@ -21,7 +22,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
const stream = new Readable({
read () {
this.push('a')
if (count++ > 1000000) {
if (count++ > max) {
this.push(null)
}
}
Expand All @@ -42,12 +43,14 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
// Some time is needed to fill the buffer
await sleep(1000)

assert.strictEqual(socket.bytesWritten < 1024 * 1024, true) // 1 MB
socket.destroy()
assert.strictEqual(count < max, true) // the stream should be closed before the max

// consume the stream
try {
/* eslint-disable-next-line no-empty, no-unused-vars */
for await (const chunk of res.body) {}
for await (const chunk of res.body) {
// process._rawDebug('chunk', chunk)
}
} catch {}
})