Skip to content
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

fix: readable body #2642

Merged
merged 1 commit into from
Jan 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 31 additions & 21 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = class BodyReadable extends Readable {
}

push (chunk) {
if (this[kConsume] && chunk !== null && this.readableLength === 0) {
if (this[kConsume] && chunk !== null) {
consumePush(this[kConsume], chunk)
return this[kReading] ? super.push(chunk) : true
}
Expand Down Expand Up @@ -215,26 +215,28 @@ async function consume (stream, type) {
reject(rState.errored ?? new TypeError('unusable'))
}
} else {
stream[kConsume] = {
type,
stream,
resolve,
reject,
length: 0,
body: []
}
queueMicrotask(() => {
stream[kConsume] = {
type,
stream,
resolve,
reject,
length: 0,
body: []
}

stream
.on('error', function (err) {
consumeFinish(this[kConsume], err)
})
.on('close', function () {
if (this[kConsume].body !== null) {
consumeFinish(this[kConsume], new RequestAbortedError())
}
})
stream
.on('error', function (err) {
consumeFinish(this[kConsume], err)
})
.on('close', function () {
if (this[kConsume].body !== null) {
consumeFinish(this[kConsume], new RequestAbortedError())
}
})

queueMicrotask(() => consumeStart(stream[kConsume]))
consumeStart(stream[kConsume])
})
}
})
}
Expand All @@ -246,8 +248,16 @@ function consumeStart (consume) {

const { _readableState: state } = consume.stream

for (const chunk of state.buffer) {
consumePush(consume, chunk)
if (state.bufferIndex) {
const start = state.bufferIndex
const end = state.buffer.length
for (let n = start; n < end; n++) {
consumePush(consume, state.buffer[n])
}
} else {
for (const chunk of state.buffer) {
consumePush(consume, chunk)
}
}

if (state.endEmitted) {
Expand Down
45 changes: 45 additions & 0 deletions test/node-test/large-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const { test } = require('node:test')
const { createServer } = require('http')
const { request } = require('../../')
const { strictEqual } = require('node:assert')

test('socket should not be reused unless body is consumed', async (t) => {
const LARGE_BODY = 'x'.repeat(10000000)

const server = createServer((req, res) => {
if (req.url === '/foo') {
res.end(LARGE_BODY)
return
}
if (req.url === '/bar') {
res.end('bar')
return
}
throw new Error('Unexpected request url: ' + req.url)
})

await new Promise((resolve) => { server.listen(0, resolve) })
t.after(() => { server.close() })

// Works fine
// const fooRes = await request('http://localhost:3000/foo')
// const fooBody = await fooRes.body.text()

// const barRes = await request('http://localhost:3000/bar')
// await barRes.body.text()

const port = server.address().port

// Fails with:
const fooRes = await request(`http://localhost:${port}/foo`)
const barRes = await request(`http://localhost:${port}/bar`)

const fooBody = await fooRes.body.text()
await barRes.body.text()

strictEqual(fooRes.headers['content-length'], String(LARGE_BODY.length))
strictEqual(fooBody.length, LARGE_BODY.length)
strictEqual(fooBody, LARGE_BODY)
})