Skip to content

Commit

Permalink
fix: stop resolving on signal abort (#2)
Browse files Browse the repository at this point in the history
If the passed signal has been aborted, stop trying to resolve records.
  • Loading branch information
achingbrain authored Mar 13, 2024
1 parent 7580a09 commit ea39509
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export class DNS implements DNSInterface {
const errors: Error[] = []

for (const resolver of resolvers) {
// skip further resolutions if the user aborted the signal
if (options.signal?.aborted === true) {
break
}

try {
const result = await resolver(domain, {
...options,
Expand Down
29 changes: 29 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,33 @@ describe('dns', () => {

expect(defaultResolver.callCount).to.equal(1)
})

it('should stop resolving if the user aborts the request', async () => {
const error = new Error('Aborted!')
const controller = new AbortController()
const fake = (): void => {
controller.abort()

throw error
}
const resolvers = [
Sinon.stub().callsFake(fake),
Sinon.stub().callsFake(fake),
Sinon.stub().callsFake(fake)
]

const resolver = dns({
resolvers: {
'.': resolvers
}
})
const result = resolver.query('test-abort-stops-resolving.com', {
signal: controller.signal
})

await expect(result).to.eventually.be.rejectedWith(error)

// only one resolver should have been called
expect(resolvers.reduce((acc, curr) => acc + curr.callCount, 0)).to.equal(1)
})
})

0 comments on commit ea39509

Please sign in to comment.