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

cleanup index #2598

Merged
merged 1 commit into from
Jan 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ const DecoratorHandler = require('./lib/handler/DecoratorHandler')
const RedirectHandler = require('./lib/handler/RedirectHandler')
const createRedirectInterceptor = require('./lib/interceptor/redirectInterceptor')

let hasCrypto
try {
require('crypto')
hasCrypto = true
} catch {
hasCrypto = false
}

Object.assign(Dispatcher.prototype, api)

module.exports.Dispatcher = Dispatcher
Expand Down Expand Up @@ -102,14 +94,10 @@ function makeDispatcher (fn) {
module.exports.setGlobalDispatcher = setGlobalDispatcher
module.exports.getGlobalDispatcher = getGlobalDispatcher

let fetchImpl = null
module.exports.fetch = async function fetch (resource) {
if (!fetchImpl) {
fetchImpl = require('./lib/fetch').fetch
}

const fetchImpl = require('./lib/fetch').fetch
module.exports.fetch = async function fetch (init, options = undefined) {
try {
return await fetchImpl(...arguments)
return await fetchImpl(init, options)
} catch (err) {
if (typeof err === 'object') {
Error.captureStackTrace(err, this)
Expand Down Expand Up @@ -149,11 +137,7 @@ const { parseMIMEType, serializeAMimeType } = require('./lib/fetch/dataURL')
module.exports.parseMIMEType = parseMIMEType
module.exports.serializeAMimeType = serializeAMimeType

if (hasCrypto) {
const { WebSocket } = require('./lib/websocket/websocket')

module.exports.WebSocket = WebSocket
}
module.exports.WebSocket = require('./lib/websocket/websocket').WebSocket

module.exports.request = makeDispatcher(api.request)
module.exports.stream = makeDispatcher(api.stream)
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Fetch extends EE {
}

// https://fetch.spec.whatwg.org/#fetch-method
function fetch (input, init = {}) {
function fetch (input, init = undefined) {
webidl.argumentLengthCheck(arguments, 1, { header: 'globalThis.fetch' })

// 1. Let p be a new promise.
Expand Down Expand Up @@ -248,7 +248,7 @@ function fetch (input, init = {}) {
request,
processResponseEndOfBody: handleFetchDone,
processResponse,
dispatcher: init.dispatcher ?? getGlobalDispatcher() // undici
dispatcher: init?.dispatcher ?? getGlobalDispatcher() // undici
})

// 14. Return p.
Expand Down
Loading