Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function dohBinary (key, callback) {
const cid = new Cid(key.slice(ipns.namespaceLength))
const buf = dnsPacket.encode({
type: 'query',
id: getRandomInt(1, 65534),
flags: dnsPacket.RECURSION_DESIRED,
questions: [{
type: 'TXT',
name: `${cid.toV1().toString()}.dns.ipns.dev`
}]
})
// https://dns.google.com/experimental
// https://cloudflare-dns.com/dns-query
// https://mozilla.cloudflare-dns.com/dns-query
ky
.get('https://cloudflare-dns.com/dns-query', {
searchParams: {
dns: buf.toString('base64')
},
headers: {
accept: 'application/dns-message'
}
})
.arrayBuffer()
.then(data => {
data = dnsPacket.decode(Buffer.from(data))
console.log('TCL: dohBinary -> data', data)
if (!data && data.answers.length < 1) {
throw errcode(new Error('Record not found'), 'ERR_NOT_FOUND')
}
return httpQueue.add(async () => {
const res = await ky.get(url, { signal: options.signal })
const reader = res.body.getReader()
try {
while (true) {
const { done } = await reader.read()
if (done) return
// Read to completion but do not cache
}
} finally {
reader.releaseLock()
}
})
}
module.exports = async function preload (url, options) {
log(url)
options = options || {}
const res = await ky.get(url, { signal: options.signal })
for await (const _ of res.body) { // eslint-disable-line no-unused-vars
// Read to completion but do not cache
}
}
function dohJson (key, callback) {
const cid = new Cid(key.slice(ipns.namespaceLength))
// https://dns.google.com/resolve
// https://cloudflare-dns.com/dns-query
// https://mozilla.cloudflare-dns.com/dns-query
ky
.get('https://cloudflare-dns.com/dns-query', {
searchParams: {
name: `${cid.toV1().toString()}.dns.ipns.dev`,
type: 'TXT',
cd: 1,
ad: 0,
ct: 'application/dns-json'
}
})
.json()
.then(data => {
if (!data && !data.Answer && data.Answer.length < 1) {
throw errcode(new Error('Record not found'), 'ERR_NOT_FOUND')
}
const record = new Record(key, Buffer.from(data.Answer[0].data, 'base64'))
setImmediate(() => callback(null, record.value))
const getPreloadCids = addr => ky.get(`${toUri(addr || defaultAddr)}/cids`).json()
async function dohBinary (url, domain, key) {
const start = Date.now()
const keyStr = keyToBase32(key)
const buf = dnsPacket.encode({
type: 'query',
questions: [{
type: 'TXT',
name: `${keyStr}.${domain}`
}]
})
const result = await ky
.get(url, {
searchParams: {
dns: buf.toString('base64')
},
headers: {
accept: 'application/dns-message'
}
})
.arrayBuffer()
const data = dnsPacket.decode(Buffer.from(result))
if (!data || data.answers.length < 1) {
throw errcode(new Error('Record not found'), 'ERR_NOT_FOUND')
}
const record = new Record(key, Buffer.from(Buffer.concat(data.answers[0].data).toString(), 'base64'))
console.log(`Resolved ${keyStr}.${domain} in ${(Date.now() - start)}ms`)
return async function * addFromURL (url, options) {
options = options || {}
const { body } = await kyDefault.get(url)
const input = {
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
content: toIterable(body)
}
yield * add(input, options)
}
}
async get (key) {
const start = Date.now()
if (!Buffer.isBuffer(key)) {
throw errcode(new Error(`Workers datastore key must be a buffer`), 'ERR_INVALID_KEY')
}
const keyStr = keyToBase32(key)
const data = await ky
.get('https://workers.ipns.dev', {
searchParams: {
key: keyStr
}
})
.text()
const record = new Record(key, Buffer.from(data, 'base64'))
console.log(`Resolved ${keyStr} with workers in: ${(Date.now() - start)}ms`)
return record.value
}
}