Skip to content

ReadableStreamFrom pull until cannot on empty enqueu #4002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
29 changes: 17 additions & 12 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,20 +600,25 @@ function ReadableStreamFrom (iterable) {
async start () {
iterator = iterable[Symbol.asyncIterator]()
},
async pull (controller) {
const { done, value } = await iterator.next()
if (done) {
queueMicrotask(() => {
controller.close()
controller.byobRequest?.respond(0)
})
} else {
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
if (buf.byteLength) {
controller.enqueue(new Uint8Array(buf))
pull (controller) {
async function pull () {
const { done, value } = await iterator.next()
if (done) {
queueMicrotask(() => {
controller.close()
controller.byobRequest?.respond(0)
})
} else {
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
if (buf.byteLength) {
controller.enqueue(new Uint8Array(buf))
} else {
return await pull()
}
}
}
return controller.desiredSize > 0

return pull()
},
async cancel () {
await iterator.return()
Expand Down
30 changes: 30 additions & 0 deletions test/fetch/issue-node-56474.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { test } = require('node:test')
const { deepStrictEqual } = require('node:assert')
const { Response } = require('../..')

// https://github.com/nodejs/node/issues/56474
test('ReadableStream empty enqueue then other enqueued', async () => {
const iterable = {
async * [Symbol.asyncIterator] () {
yield ''
yield '3'
yield '4'
}
}

const response = new Response(iterable)
deepStrictEqual(await response.text(), '34')
})

test('ReadableStream empty enqueue', async () => {
const iterable = {
async * [Symbol.asyncIterator] () {
yield ''
}
}

const response = new Response(iterable)
deepStrictEqual(await response.text(), '')
})
Loading