Skip to content

Commit 90a5d60

Browse files
authored
ReadableStreamFrom pull until cannot on empty enqueu (nodejs#4002)
1 parent 0907792 commit 90a5d60

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

lib/core/util.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -600,20 +600,25 @@ function ReadableStreamFrom (iterable) {
600600
async start () {
601601
iterator = iterable[Symbol.asyncIterator]()
602602
},
603-
async pull (controller) {
604-
const { done, value } = await iterator.next()
605-
if (done) {
606-
queueMicrotask(() => {
607-
controller.close()
608-
controller.byobRequest?.respond(0)
609-
})
610-
} else {
611-
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
612-
if (buf.byteLength) {
613-
controller.enqueue(new Uint8Array(buf))
603+
pull (controller) {
604+
async function pull () {
605+
const { done, value } = await iterator.next()
606+
if (done) {
607+
queueMicrotask(() => {
608+
controller.close()
609+
controller.byobRequest?.respond(0)
610+
})
611+
} else {
612+
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
613+
if (buf.byteLength) {
614+
controller.enqueue(new Uint8Array(buf))
615+
} else {
616+
return await pull()
617+
}
614618
}
615619
}
616-
return controller.desiredSize > 0
620+
621+
return pull()
617622
},
618623
async cancel () {
619624
await iterator.return()

test/fetch/issue-node-56474.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const { deepStrictEqual } = require('node:assert')
5+
const { Response } = require('../..')
6+
7+
// https://github.com/nodejs/node/issues/56474
8+
test('ReadableStream empty enqueue then other enqueued', async () => {
9+
const iterable = {
10+
async * [Symbol.asyncIterator] () {
11+
yield ''
12+
yield '3'
13+
yield '4'
14+
}
15+
}
16+
17+
const response = new Response(iterable)
18+
deepStrictEqual(await response.text(), '34')
19+
})
20+
21+
test('ReadableStream empty enqueue', async () => {
22+
const iterable = {
23+
async * [Symbol.asyncIterator] () {
24+
yield ''
25+
}
26+
}
27+
28+
const response = new Response(iterable)
29+
deepStrictEqual(await response.text(), '')
30+
})

0 commit comments

Comments
 (0)