Skip to content

Commit

Permalink
Merge pull request #79 from ensdomains/fix/remove-global-fetch
Browse files Browse the repository at this point in the history
remove global reference from getDNSOwner
  • Loading branch information
TateB authored Nov 17, 2022
2 parents e73d5d3 + cf3ce97 commit 861e217
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
7 changes: 0 additions & 7 deletions packages/ensjs/src/functions/getDNSOwner.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import nock from 'nock'
import fetch from 'node-fetch'
import getDNSOwner, { encodeURLParams } from './getDNSOwner'

function hexDecode(string: string) {
Expand All @@ -24,12 +23,6 @@ describe('encodeURLParams', () => {
})

describe('getDNSOwner', () => {
beforeAll(() => {
global.fetch = fetch as any
})
afterAll(() => {
global.fetch = undefined as any
})
it('should return the address from the result of a dnsQuery', async () => {
const dnsName = 'brantly.xyz'

Expand Down
18 changes: 10 additions & 8 deletions packages/ensjs/src/functions/getDNSOwner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as packet from 'dns-packet'
import { _fetchData } from 'ethers/lib/utils'

export function encodeURLParams(p: { [key: string]: string }): string {
return Object.entries(p)
Expand All @@ -7,15 +8,16 @@ export function encodeURLParams(p: { [key: string]: string }): string {
}

export const getDNS = async (q: packet.Packet): Promise<packet.Packet> => {
const response = await global.fetch(
`https://cloudflare-dns.com/dns-query?${encodeURLParams({
ct: 'application/dns-udpwireformat',
dns: packet.encode(q)?.toString('base64'),
ts: Date.now().toString(),
})}`,
const url = `https://cloudflare-dns.com/dns-query?${encodeURLParams({
ct: 'application/dns-udpwireformat',
dns: packet.encode(q)?.toString('base64'),
ts: Date.now().toString(),
})}`
const response = await _fetchData(url, undefined)
const arrayBuffer = response.buffer.slice(
response.byteOffset,
response.byteLength + response.byteOffset,
)
const arrayBuffer = await response.arrayBuffer()
// @ts-ignore:next-line
const fromArrayBuffer = Buffer.from(arrayBuffer)
return packet.decode(fromArrayBuffer)
}
Expand Down

0 comments on commit 861e217

Please sign in to comment.