From 6a2898599452f1afe119c86840b37c1af52d3a5f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 13 Aug 2021 18:53:18 +0200 Subject: [PATCH] fix: request body.body regression (#955) * fix: request body.body regression Refs: https://github.com/nodejs/undici/pull/928#discussion_r688506456 * fixup * fixup * fixup * fixup * fixup --- index.js | 3 ++- lib/api/readable.js | 3 ++- test/client-request.js | 27 ++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f218bd282c3..35e07fc4c69 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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() diff --git a/lib/api/readable.js b/lib/api/readable.js index d4efeec95b9..58716ba8580 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -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 @@ -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. diff --git a/test/client-request.js b/test/client-request.js index 111c472b3a7..7ffc0772560 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -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) => { @@ -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) + }) +})