Skip to content

Commit

Permalink
fix: fix request with readable mode is object (#2279)
Browse files Browse the repository at this point in the history
In the stream Readable.from(buf) state, the length
of 1 refers to the number of objects, not the length of
buf. Therefore, when the stream mode is set to 'object,'
should not use the length from the state.
  • Loading branch information
killagu authored Sep 22, 2023
1 parent a732603 commit ba95ff6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function bodyLength (body) {
return 0
} else if (isStream(body)) {
const state = body._readableState
return state && state.ended === true && Number.isFinite(state.length)
return state && state.objectMode === false && state.ended === true && Number.isFinite(state.length)
? state.length
: null
} else if (isBlobLike(body)) {
Expand Down
29 changes: 29 additions & 0 deletions test/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,32 @@ test('response invalid content length with close', (t) => {
})
})
})

test('request streaming with Readable.from(buf)', (t) => {
const server = createServer((req, res) => {
req.pipe(res)
})
t.teardown(server.close.bind(server))
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

client.request({
path: '/',
method: 'PUT',
body: Readable.from(Buffer.from('hello'))
}, (err, data) => {
const chunks = []
t.error(err)
data.body
.on('data', (chunk) => {
chunks.push(chunk)
})
.on('end', () => {
t.equal(Buffer.concat(chunks).toString(), 'hello')
t.pass()
t.end()
})
})
})
})

0 comments on commit ba95ff6

Please sign in to comment.