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: request body.body regression #955

Merged
merged 6 commits into from
Aug 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})