Skip to content

Commit

Permalink
refactor: alternative solution
Browse files Browse the repository at this point in the history
  • Loading branch information
luddd3 committed Dec 18, 2024
1 parent 171f6fc commit 46c6d06
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
10 changes: 10 additions & 0 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ class Client extends DispatcherBase {
this[kResume]()
})
}

request (opts, callback) {
if (!opts?.origin) {
const origin = this[kUrl]?.origin
if (origin) {
opts = { ...opts, origin }
}
}
return super.request(opts, callback)
}
}

function onError (client, err) {
Expand Down
3 changes: 1 addition & 2 deletions lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const EventEmitter = require('node:events')
const { kUrl } = require('../core/symbols')
const WrapHandler = require('../handler/wrap-handler')

const wrapInterceptor = (dispatch) => (opts, handler) => dispatch(opts, WrapHandler.wrap(handler))
Expand Down Expand Up @@ -32,7 +31,7 @@ class Dispatcher extends EventEmitter {
throw new TypeError(`invalid interceptor, expected function received ${typeof interceptor}`)
}

dispatch = interceptor(dispatch, { url: this[kUrl] })
dispatch = interceptor(dispatch)
dispatch = wrapInterceptor(dispatch)

if (dispatch == null || typeof dispatch !== 'function' || dispatch.length !== 2) {
Expand Down
10 changes: 10 additions & 0 deletions lib/dispatcher/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class Pool extends PoolBase {
return dispatcher
}
}

request (opts, callback) {
if (!opts?.origin) {
const origin = this[kUrl]?.origin
if (origin) {
opts = { ...opts, origin }
}
}
return super.request(opts, callback)
}
}

module.exports = Pool
15 changes: 6 additions & 9 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,14 @@ module.exports = interceptorOpts => {

return (dispatch, opts) => {
return function dnsInterceptor (origDispatchOpts, handler) {
let origin
if (origDispatchOpts.origin) {
origin = origDispatchOpts.origin.constructor === URL
? origDispatchOpts.origin
: new URL(origDispatchOpts.origin)
} else if (opts.url) {
origin = opts.url
} else {
return handler.onError(new InvalidArgumentError('Origin missing'))
if (!origDispatchOpts.origin) {
return handler.onResponseError(null, new InvalidArgumentError('Origin Missing'))
}

const origin = origDispatchOpts.origin.constructor === URL
? origDispatchOpts.origin
: new URL(origDispatchOpts.origin)

if (isIP(origin.hostname) !== 0) {
return dispatch(origDispatchOpts, handler)
}
Expand Down

0 comments on commit 46c6d06

Please sign in to comment.