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

version cleanup #2483

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion benchmarks/benchmark-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const path = require('path')
const { readFileSync } = require('fs')
const { table } = require('table')
const { Writable } = require('stream')
const { WritableStream } = require('stream/web')
const { isMainThread } = require('worker_threads')

const { Pool, Client, fetch, Agent, setGlobalDispatcher } = require('..')
Expand Down
1 change: 0 additions & 1 deletion benchmarks/benchmark-https.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const path = require('path')
const { readFileSync } = require('fs')
const { table } = require('table')
const { Writable } = require('stream')
const { WritableStream } = require('stream/web')
const { isMainThread } = require('worker_threads')

const { Pool, Client, fetch, Agent, setGlobalDispatcher } = require('..')
Expand Down
1 change: 0 additions & 1 deletion benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const os = require('os')
const path = require('path')
const { table } = require('table')
const { Writable } = require('stream')
const { WritableStream } = require('stream/web')
const { isMainThread } = require('worker_threads')

const { Pool, Client, fetch, Agent, setGlobalDispatcher } = require('..')
Expand Down
20 changes: 5 additions & 15 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,9 @@ function getSocketInfo (socket) {
}
}

async function * convertIterableToBuffer (iterable) {
for await (const chunk of iterable) {
yield Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
}
}

/** @type {globalThis['ReadableStream']} */
let ReadableStream
function ReadableStreamFrom (iterable) {
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

if (ReadableStream.from) {
return ReadableStream.from(convertIterableToBuffer(iterable))
}
// We cannot use ReadableStream.from here because it does not return a byte stream.

let iterator
return new ReadableStream(
Expand All @@ -387,10 +374,13 @@ function ReadableStreamFrom (iterable) {
if (done) {
queueMicrotask(() => {
controller.close()
controller.byobRequest?.respond(0)
})
} else {
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
controller.enqueue(new Uint8Array(buf))
if (buf.byteLength) {
controller.enqueue(new Uint8Array(buf))
}
}
return controller.desiredSize > 0
},
Expand Down
11 changes: 0 additions & 11 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ const { isUint8Array, isArrayBuffer } = require('util/types')
const { File: UndiciFile } = require('./file')
const { parseMIMEType, serializeAMimeType } = require('./dataURL')

let ReadableStream = globalThis.ReadableStream

/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile
const textEncoder = new TextEncoder()
const textDecoder = new TextDecoder()

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
function extractBody (object, keepalive = false) {
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

// 1. Let stream be null.
let stream = null

Expand Down Expand Up @@ -258,11 +252,6 @@ function extractBody (object, keepalive = false) {

// https://fetch.spec.whatwg.org/#bodyinit-safely-extract
function safelyExtractBody (object, keepalive = false) {
if (!ReadableStream) {
// istanbul ignore next
ReadableStream = require('stream/web').ReadableStream
}

// To safely extract a body and a `Content-Type` value from
// a byte sequence or BodyInit object object, run these steps:

Expand Down
6 changes: 0 additions & 6 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ const EE = require('events')
const { Readable, pipeline } = require('stream')
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util')
const { dataURLProcessor, serializeAMimeType, parseMIMEType } = require('./dataURL')
const { TransformStream } = require('stream/web')
const { getGlobalDispatcher } = require('../global')
const { webidl } = require('./webidl')
const { STATUS_CODES } = require('http')
const GET_OR_HEAD = ['GET', 'HEAD']

/** @type {import('buffer').resolveObjectURL} */
let resolveObjectURL
let ReadableStream = globalThis.ReadableStream

class Fetch extends EE {
constructor (dispatcher) {
Expand Down Expand Up @@ -1934,10 +1932,6 @@ async function httpNetworkFetch (
// 15. Let stream be a new ReadableStream.
// 16. Set up stream with byte reading support with pullAlgorithm set to pullAlgorithm,
// cancelAlgorithm set to cancelAlgorithm.
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

const stream = new ReadableStream(
{
async start (controller) {
Expand Down
6 changes: 0 additions & 6 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const { kHeadersList, kConstruct } = require('../core/symbols')
const assert = require('assert')
const { getMaxListeners, setMaxListeners, getEventListeners, defaultMaxListeners } = require('events')

let TransformStream = globalThis.TransformStream

const kAbortController = Symbol('abortController')

const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
Expand Down Expand Up @@ -516,10 +514,6 @@ class Request {
}

// 2. Set finalBody to the result of creating a proxy for inputBody.
if (!TransformStream) {
TransformStream = require('stream/web').TransformStream
}

// https://streams.spec.whatwg.org/#readablestream-create-a-proxy
const identityTransform = new TransformStream()
inputBody.stream.pipeThrough(identityTransform)
Expand Down
1 change: 0 additions & 1 deletion lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const { kHeadersList, kConstruct } = require('../core/symbols')
const assert = require('assert')
const { types } = require('util')

const ReadableStream = globalThis.ReadableStream || require('stream/web').ReadableStream
const textEncoder = new TextEncoder('utf-8')

// https://fetch.spec.whatwg.org/#response-class
Expand Down
7 changes: 0 additions & 7 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,14 +890,7 @@ async function fullyReadBody (body, processBody, processBodyError) {
}
}

/** @type {ReadableStream} */
let ReadableStream = globalThis.ReadableStream

function isReadableStreamLike (stream) {
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

return stream instanceof ReadableStream || (
stream[Symbol.toStringTag] === 'ReadableStream' &&
typeof stream.tee === 'function'
Expand Down
8 changes: 1 addition & 7 deletions test/client-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
Writable,
PassThrough
} = require('stream')
const { nodeMajor } = require('../lib/core/util')

test('pipeline get', (t) => {
t.plan(17)
Expand Down Expand Up @@ -535,12 +534,7 @@ test('pipeline abort piped res', (t) => {
return pipeline(body, pt, () => {})
})
.on('error', (err) => {
// Node < 13 doesn't always detect premature close.
if (nodeMajor < 13) {
t.ok(err)
} else {
t.equal(err.code, 'UND_ERR_ABORTED')
}
t.equal(err.code, 'UND_ERR_ABORTED')
})
.end()
})
Expand Down
31 changes: 6 additions & 25 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const { Readable } = require('stream')
const net = require('net')
const { promisify } = require('util')
const { NotSupportedError } = require('../lib/core/errors')
const { nodeMajor } = require('../lib/core/util')
const { parseFormDataString } = require('./utils/formdata')

test('request dump', (t) => {
Expand Down Expand Up @@ -387,7 +386,7 @@ test('request long multibyte text', (t) => {
})
})

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

const obj = { asd: true }
Expand Down Expand Up @@ -436,7 +435,7 @@ test('request arrayBuffer', (t) => {
})
})

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

const obj = { asd: true }
Expand All @@ -462,7 +461,7 @@ test('request body', { skip: nodeMajor < 16 }, (t) => {
})
})

test('request post body no missing data', { skip: nodeMajor < 16 }, (t) => {
test('request post body no missing data', (t) => {
t.plan(2)

const server = createServer(async (req, res) => {
Expand Down Expand Up @@ -495,7 +494,7 @@ test('request post body no missing data', { skip: nodeMajor < 16 }, (t) => {
})
})

test('request post body no extra data handler', { skip: nodeMajor < 16 }, (t) => {
test('request post body no extra data handler', (t) => {
t.plan(3)

const server = createServer(async (req, res) => {
Expand Down Expand Up @@ -663,7 +662,7 @@ test('request raw responseHeaders', async (t) => {
t.pass()
})

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

const obj = { asd: true }
Expand Down Expand Up @@ -718,7 +717,7 @@ test('request text2', (t) => {
})
})

test('request with FormData body', { skip: nodeMajor < 16 }, (t) => {
test('request with FormData body', (t) => {
const { FormData } = require('../')
const { Blob } = require('buffer')

Expand Down Expand Up @@ -769,24 +768,6 @@ test('request with FormData body', { skip: nodeMajor < 16 }, (t) => {
})
})

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

// a FormData polyfill, for example
class FormData {}

const fd = new FormData()

const client = new Client('http://localhost:3000')
t.teardown(client.destroy.bind(client))

await t.rejects(client.request({
path: '/',
method: 'POST',
body: fd
}), errors.InvalidArgumentError)
})

test('request post body Buffer from string', (t) => {
t.plan(2)
const requestBody = Buffer.from('abcdefghijklmnopqrstuvwxyz')
Expand Down
27 changes: 11 additions & 16 deletions test/esm-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
'use strict'
const { nodeMajor, nodeMinor } = require('../lib/core/util')

if (!((nodeMajor > 14 || (nodeMajor === 14 && nodeMajor > 13)) || (nodeMajor === 12 && nodeMinor > 20))) {
require('tap') // shows skipped
} else {
;(async () => {
try {
await import('./utils/esm-wrapper.mjs')
} catch (e) {
if (e.message === 'Not supported') {
require('tap') // shows skipped
return
}
console.error(e.stack)
process.exitCode = 1
;(async () => {
try {
await import('./utils/esm-wrapper.mjs')
} catch (e) {
if (e.message === 'Not supported') {
require('tap') // shows skipped
return
}
})()
}
console.error(e.stack)
process.exitCode = 1
}
})()
3 changes: 1 addition & 2 deletions test/fetch/abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { DOMException } = require('../../lib/fetch/constants')
const { nodeMajor } = require('../../lib/core/util')

const { AbortController: NPMAbortController } = require('abort-controller')

Expand Down Expand Up @@ -37,7 +36,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
}
})

test('allows aborting with custom errors', { skip: nodeMajor === 16 }, async (t) => {
test('allows aborting with custom errors', async (t) => {
const server = createServer().listen(0)

t.teardown(server.close.bind(server))
Expand Down
8 changes: 1 addition & 7 deletions test/fetch/bundle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
'use strict'

const { test, skip } = require('tap')
const { nodeMajor } = require('../../lib/core/util')

if (nodeMajor === 16) {
skip('esbuild uses static blocks with --keep-names which node 16.8 does not have')
process.exit()
}
const { test } = require('tap')

const { Response, Request, FormData, Headers } = require('../../undici-fetch')

Expand Down
1 change: 0 additions & 1 deletion test/fetch/client-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const { test, teardown } = require('tap')
const { createServer } = require('http')
const { ReadableStream } = require('stream/web')
const { Blob } = require('buffer')
const { fetch, Response, Request, FormData, File } = require('../..')
const { Client, setGlobalDispatcher, Agent } = require('../..')
Expand Down
8 changes: 1 addition & 7 deletions test/fetch/client-node-max-header-size.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
'use strict'

const { execSync } = require('node:child_process')
const { test, skip } = require('tap')
const { nodeMajor } = require('../../lib/core/util')

if (nodeMajor === 16) {
skip('esbuild uses static blocks with --keep-names which node 16.8 does not have')
process.exit()
}
const { test } = require('tap')

const command = 'node -e "require(\'./undici-fetch.js\').fetch(\'https://httpbin.org/get\')"'

Expand Down
8 changes: 1 addition & 7 deletions test/fetch/issue-1447.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
'use strict'

const { test, skip } = require('tap')
const { nodeMajor } = require('../../lib/core/util')

if (nodeMajor === 16) {
skip('esbuild uses static blocks with --keep-names which node 16.8 does not have')
process.exit()
}
const { test } = require('tap')

const undici = require('../..')
const { fetch: theoreticalGlobalFetch } = require('../../undici-fetch')
Expand Down
2 changes: 1 addition & 1 deletion test/fetch/resource-timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
performance
} = require('perf_hooks')

const skip = nodeMajor < 18 || (nodeMajor === 18 && nodeMinor < 2)
const skip = nodeMajor === 18 && nodeMinor < 2

test('should create a PerformanceResourceTiming after each fetch request', { skip }, (t) => {
t.plan(8)
Expand Down
1 change: 0 additions & 1 deletion test/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { test } = require('tap')
const {
Response
} = require('../../')
const { ReadableStream } = require('stream/web')
const {
Blob: ThirdPartyBlob,
FormData: ThirdPartyFormData
Expand Down
Loading
Loading