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

refactor: version cleanup #2605

Merged
merged 4 commits into from
Jan 8, 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
10 changes: 2 additions & 8 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,8 @@ function validateHandler (handler, method, upgrade) {
// A body is disturbed if it has been read from and it cannot
// be re-used without losing state or data.
function isDisturbed (body) {
return !!(body && (
stream.isDisturbed
? stream.isDisturbed(body) || body[kBodyUsed] // TODO (fix): Why is body[kBodyUsed] needed?
: body[kBodyUsed] ||
body.readableDidRead ||
(body._readableState && body._readableState.dataEmitted) ||
isReadableAborted(body)
))
// TODO (fix): Why is body[kBodyUsed] needed?
return !!(body && (stream.isDisturbed(body) || body[kBodyUsed]))
Copy link
Member Author

@tsctx tsctx Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

function isErrored (body) {
Expand Down
7 changes: 0 additions & 7 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ function markResourceTiming (timingInfo, originalURL, initiatorType, globalThis,

// https://fetch.spec.whatwg.org/#abort-fetch
function abortFetch (p, request, responseObject, error) {
// Note: AbortSignal.reason was added in node v17.2.0
// which would give us an undefined error to reject with.
// Remove this once node v16 is no longer supported.
if (!error) {
error = new DOMException('The operation was aborted.', 'AbortError')
}

// 1. Reject promise with error.
p.reject(error)

Expand Down
3 changes: 1 addition & 2 deletions test/client-node-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const command = 'node -e "require(\'.\').request(\'https://httpbin.org/get\')"'

test("respect Node.js' --max-http-header-size", async (t) => {
t.throws(
// TODO: Drop the `--unhandled-rejections=throw` once we drop Node.js 14
() => execSync(`${command} --max-http-header-size=1 --unhandled-rejections=throw`),
() => execSync(`${command} --max-http-header-size=1`),
/UND_ERR_HEADERS_OVERFLOW/,
'max-http-header-size=1 should throw'
)
Expand Down
3 changes: 1 addition & 2 deletions test/fetch/client-node-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const command = 'node -e "require(\'./undici-fetch.js\').fetch(\'https://httpbin

test("respect Node.js' --max-http-header-size", async () => {
assert.throws(
// TODO: Drop the `--unhandled-rejections=throw` once we drop Node.js 14
() => execSync(`${command} --max-http-header-size=1 --unhandled-rejections=throw`),
() => execSync(`${command} --max-http-header-size=1`),
/UND_ERR_HEADERS_OVERFLOW/,
'max-http-header-size=1 should throw'
)
Expand Down
10 changes: 4 additions & 6 deletions test/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ test('Modifying headers using Headers.prototype.set', () => {
})

// https://github.com/nodejs/node/issues/43838
test('constructing a Response with a ReadableStream body', { skip: process.version.startsWith('v16.') }, async (t) => {
test('constructing a Response with a ReadableStream body', async (t) => {
const text = '{"foo":"bar"}'
const uint8 = new TextEncoder().encode(text)

Expand Down Expand Up @@ -199,7 +199,7 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi
await assert.rejects(response.text(), TypeError)
})

await t.test('Readable with ArrayBuffer chunk still throws', { skip: process.version.startsWith('v16.') }, async () => {
await t.test('Readable with ArrayBuffer chunk still throws', async () => {
const readable = new ReadableStream({
start (controller) {
controller.enqueue(uint8.buffer)
Expand All @@ -210,14 +210,12 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi
const response1 = new Response(readable)
const response2 = response1.clone()
const response3 = response1.clone()
// const response4 = response1.clone()
const response4 = response1.clone()

await assert.rejects(response1.arrayBuffer(), TypeError)
await assert.rejects(response2.text(), TypeError)
await assert.rejects(response3.json(), TypeError)
// TODO: on Node v16.8.0, this throws a TypeError
// because the body is detected as disturbed.
// await t.rejects(response4.blob(), TypeError)
await assert.rejects(response4.blob(), TypeError)
})
})

Expand Down
2 changes: 1 addition & 1 deletion types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ declare namespace Dispatcher {
* @link https://fetch.spec.whatwg.org/#body-mixin
*/
interface BodyMixin {
readonly body?: never; // throws on node v16.6.0
readonly body?: never;
readonly bodyUsed: boolean;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
Expand Down
5 changes: 2 additions & 3 deletions types/readable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ declare class BodyReadable extends Readable {
*/
readonly bodyUsed: boolean

/** Throws on node 16.6.0
*
* If body is null, it should return null as the body
/**
* If body is null, it should return null as the body
*
* If body is not null, should return the body as a ReadableStream
*
Expand Down