Skip to content

Commit

Permalink
fix(#3901): migrate dns interceptor to new hooks (#3903)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Nov 29, 2024
1 parent 4b85f4e commit fb3138d
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { isIP } = require('node:net')
const { lookup } = require('node:dns')
const DecoratorHandler = require('../handler/decorator-handler')
const WrapHandler = require('../handler/wrap-handler')
const { InvalidArgumentError, InformationalError } = require('../core/errors')
const maxInt = Math.pow(2, 31) - 1

Expand Down Expand Up @@ -224,25 +225,47 @@ class DNSDispatchHandler extends DecoratorHandler {
#dispatch = null
#handler = null
#origin = null
#controller = null

constructor (state, { origin, handler, dispatch }, opts) {
super(handler)
this.#origin = origin
this.#handler = handler
this.#handler = WrapHandler.wrap(handler)
this.#opts = { ...opts }
this.#state = state
this.#dispatch = dispatch
}

onError (err) {
onRequestStart (controller, context) {
this.#handler.onRequestStart?.(controller, context)
}

onRequestUpgrade (controller, statusCode, headers, socket) {
this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
}

onResponseStart (controller, statusCode, headers, statusMessage) {
this.#handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
}

onResponseData (controller, data) {
this.#handler.onResponseData?.(controller, data)
}

onResponseEnd (controller, trailers) {
this.#handler.onResponseEnd?.(controller, trailers)
}

onResponseError (controller, err) {
switch (err.code) {
case 'ETIMEDOUT':
case 'ECONNREFUSED': {
if (this.#state.dualStack) {
// We delete the record and retry
this.#state.runLookup(this.#origin, this.#opts, (err, newOrigin) => {
if (err) {
return this.#handler.onError(err)
this.#handler.onResponseError(controller, err)
return
}

const dispatchOpts = {
Expand All @@ -253,18 +276,18 @@ class DNSDispatchHandler extends DecoratorHandler {
this.#dispatch(dispatchOpts, this)
})

// if dual-stack disabled, we error out
return
}

this.#handler.onError(err)
return
// if dual-stack disabled, we error out
this.#handler.onResponseError(controller, err)
break
}
case 'ENOTFOUND':
this.#state.deleteRecord(this.#origin)
// eslint-disable-next-line no-fallthrough
default:
this.#handler.onError(err)
this.#handler.onResponseError(controller, err)
break
}
}
Expand Down

0 comments on commit fb3138d

Please sign in to comment.