Skip to content

Commit

Permalink
ReadableStreamFrom pull until cannot on empty enqueu (#4002)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jan 15, 2025
1 parent 0907792 commit 90a5d60
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
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(), '')
})

0 comments on commit 90a5d60

Please sign in to comment.