Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
apiProvider = 'https://ipfs.io/'
}
// js-ipfs-api does not provide method for fetching this
// TODO: revisit after https://github.com/ipfs/js-ipfs-api/issues/501 is addressed
// TODO: consider worst-case-scenario fallback to https://developers.google.com/speed/public-dns/docs/dns-over-https
const apiCall = `${apiProvider}api/v0/dns/${fqdn}?r=true`
const xhr = new XMLHttpRequest() // older XHR API us used because window.fetch appends Origin which causes error 403 in go-ipfs
// synchronous mode with small timeout
// (it is okay, because we do it only once, then it is cached and read via readAndCacheDnslink)
xhr.open('GET', apiCall, false)
xhr.setRequestHeader('Accept', 'application/json')
xhr.send(null)
if (xhr.status === 200) {
const dnslink = JSON.parse(xhr.responseText).Path
// console.log('readDnslinkFromTxtRecord', readDnslinkFromTxtRecord)
if (!IsIpfs.path(dnslink)) {
throw new Error(`dnslink for '${fqdn}' is not a valid IPFS path: '${dnslink}'`)
}
return dnslink
} else if (xhr.status === 500) {
// go-ipfs returns 500 if host has no dnslink or an error occurred
// TODO: find/fill an upstream bug to make this more intuitive
return false
} else {
throw new Error(xhr.statusText)
}
},
function normalizedUnhandledIpfsProtocol (request, pubGwUrl) {
let path = unhandledIpfsPath(request.url)
path = fixupDnslinkPath(path) // /ipfs/example.com → /ipns/example.com
if (IsIpfs.path(path)) {
// replace search query with a request to a public gateway
// (will be redirected later, if needed)
return { redirectUrl: pathAtHttpGateway(path, pubGwUrl) }
}
}
function normalizedRedirectingProtocolRequest (request, pubGwUrl) {
const oldPath = decodeURIComponent(new URL(request.url).hash)
let path = oldPath
// prefixed (Firefox < 59)
path = path.replace(/^#web\+dweb:\//i, '/') // web+dweb:/ipfs/Qm → /ipfs/Qm
path = path.replace(/^#web\+ipfs:\/\//i, '/ipfs/') // web+ipfs://Qm → /ipfs/Qm
path = path.replace(/^#web\+ipns:\/\//i, '/ipns/') // web+ipns://Qm → /ipns/Qm
// without prefix (Firefox >= 59)
path = path.replace(/^#dweb:\//i, '/') // dweb:/ipfs/Qm → /ipfs/Qm
path = path.replace(/^#ipfs:\/\//i, '/ipfs/') // ipfs://Qm → /ipfs/Qm
path = path.replace(/^#ipns:\/\//i, '/ipns/') // ipns://Qm → /ipns/Qm
// additional fixups of the final path
path = fixupDnslinkPath(path) // /ipfs/example.com → /ipns/example.com
if (oldPath !== path && IsIpfs.path(path)) {
return { redirectUrl: pathAtHttpGateway(path, pubGwUrl) }
}
return null
}
function normalizedIpfsPath (urlOrPath) {
let result = urlOrPath
// Convert CID-in-subdomain URL to /ipns// path
if (IsIpfs.subdomain(urlOrPath)) {
result = subdomainToIpfsPath(urlOrPath)
}
// Drop everything before the IPFS path
result = result.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1')
// Remove Unescape special characters
// https://github.com/ipfs/ipfs-companion/issues/303
result = decodeURIComponent(result)
// Return a valid IPFS path or null otherwise
return IsIpfs.path(result) ? result : null
}
exports.normalizedIpfsPath = normalizedIpfsPath
function normalizeKey (key) {
let res
const isMhash = isIPFS.multihash(key)
const isPath = isIPFS.path(key)
if (!isMhash && !isPath) {
return null
}
if (isMhash) {
res = key
} else if (isPath) {
res = key.replace('/ipfs/', '')
}
if (typeof res === 'string') {
return mh.fromB58String(res)
}
return res
function unhandledIpfsPath (requestUrl) {
const unhandled = requestUrl.match(unhandledIpfsRE)
if (unhandled && unhandled.length > 1) {
const unhandledProtocol = decodeURIComponent(unhandled[1])
const unhandledPath = `/${decodeURIComponent(unhandled[2])}`
return IsIpfs.path(unhandledPath) ? unhandledPath : `/${unhandledProtocol}${unhandledPath}`
}
return null
}
get isValid () {
return this.path !== '' && (isIPFS.cid(this.path) || isIPFS.path(this.path))
}