Skip to content

Commit

Permalink
refactor: remove duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Sep 13, 2024
1 parent 763a283 commit 454c1f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DNSInstance {
!newOpts.dualStack && newOpts.affinity
)

return cb(
cb(
null,
`${origin.protocol}//${
ip.family === 6 ? `[${ip.address}]` : ip.address
Expand All @@ -75,10 +75,9 @@ class DNSInstance {
})
} else {
// If there's IPs we pick
const records = this.#records.get(origin.hostname)
const ip = this.pick(
origin,
records,
ips,
// Only set affinity if dual stack is disabled
// otherwise let it go through normal flow
!newOpts.dualStack && newOpts.affinity
Expand Down Expand Up @@ -109,22 +108,21 @@ class DNSInstance {
return cb(err)
}

const results = []
const results = new Map()

for (const addr of addresses) {
const record = {
address: addr.address,
ttl:
addr.ttl != null
? Math.min(addr.ttl * 1000, this.#maxTTL)
: this.#maxTTL,
ttl: opts.maxTTL,
family: addr.family
}

results.push(record)
// On linux we found duplicates, we attempt to remove them with
// the latest record
results.set(`${record.address}:${record.family}`, record)
}

cb(null, results)
cb(null, results.values())
}
)
}
Expand Down
6 changes: 3 additions & 3 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ test('Should we handle TTL (4)', async t => {
return cb(err)
}

const results = []
const results = new Map()

for (const addr of addresses) {
const record = {
Expand All @@ -552,10 +552,10 @@ test('Should we handle TTL (4)', async t => {
family: addr.family
}

results.push(record)
results.set(`${record.address}:${record.family}`, record)
}

cb(null, results)
cb(null, results.values())
}
)
}
Expand Down

0 comments on commit 454c1f0

Please sign in to comment.