Skip to content

Commit

Permalink
fix: request body.body regression (nodejs#955)
Browse files Browse the repository at this point in the history
* fix: request body.body regression

Refs: nodejs#928 (comment)

* fixup

* fixup

* fixup

* fixup

* fixup
  • Loading branch information
ronag authored and crysmags committed Feb 27, 2024
1 parent d106924 commit 6a28985
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MockPool = require('./lib/mock/mock-pool')
const mockErrors = require('./lib/mock/mock-errors')

const nodeMajor = Number(process.versions.node.split('.')[0])
const nodeMinor = Number(process.versions.node.split('.')[1])

Object.assign(Dispatcher.prototype, api)

Expand Down Expand Up @@ -86,7 +87,7 @@ function makeDispatcher (fn) {
module.exports.setGlobalDispatcher = setGlobalDispatcher
module.exports.getGlobalDispatcher = getGlobalDispatcher

if (nodeMajor >= 16) {
if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) {
const fetchImpl = require('./lib/fetch')
module.exports.fetch = async function fetch (resource, init) {
const dispatcher = getGlobalDispatcher()
Expand Down
3 changes: 2 additions & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError } = require('../core/errors')
const util = require('../core/util')
const { toWebReadable } = require('../fetch/util')

let Blob

Expand Down Expand Up @@ -130,7 +131,7 @@ module.exports = class BodyReadable extends Readable {
// https://fetch.spec.whatwg.org/#dom-body-body
get body () {
if (!this[kBody]) {
this[kBody] = util.toWeb(this)
this[kBody] = toWebReadable(this)
if (this[kConsume]) {
// TODO: Is this the best way to force a lock?
this[kBody].getReader() // Ensure stream is locked.
Expand Down
27 changes: 26 additions & 1 deletion test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { kConnect } = require('../lib/core/symbols')
const { Readable } = require('stream')
const net = require('net')
const { promisify } = require('util')

const nodeMajor = Number(process.versions.node.split('.')[0])

test('request abort before headers', (t) => {
Expand Down Expand Up @@ -275,3 +274,29 @@ test('request arrayBuffer', (t) => {
t.strictSame(Buffer.from(JSON.stringify(obj)), Buffer.from(await body.arrayBuffer()))
})
})

test('request body', { skip: nodeMajor < 16 }, (t) => {
t.plan(1)

const obj = { asd: true }
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.teardown(server.close.bind(server))

server.listen(0, async () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

const { body } = await client.request({
path: '/',
method: 'GET'
})

let x = ''
for await (const chunk of body.body) {
x += Buffer.from(chunk)
}
t.strictSame(JSON.stringify(obj), x)
})
})

0 comments on commit 6a28985

Please sign in to comment.