Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const dirCid = err.cid
console.log('resolver.directory', dirCid.toBaseEncodedString())
const data = await resolver.directory(ipfs, url, dirCid)
console.log('resolver.directory', Array.isArray(data) ? data : `returned '${typeof data}'`)
// TODO: redirect so directory always end with `/`
if (typeof data === 'string') {
// return HTML with directory listing
return {
content: textBuffer(data),
contentType: 'text/html',
contentEncoding: 'utf-8'
}
} else if (Array.isArray(data)) {
console.log('resolver.directory.indexes', data)
// return first index file
path = PathUtils.joinURLParts(path, data[0].name)
return getResponse(ipfs, url, path)
}
throw new Error('Invalid output of resolver.directory')
} else if (err.parentDagNode && err.missingLinkName) {
// It may be legitimate error, but it could also be a part of hamt-sharded-directory
// (example: ipns://tr.wikipedia-on-ipfs.org/wiki/Anasayfa.html)
// which is not supported by resolver.cid from ipfs-http-response at this time.
// Until ipfs.resolve support with sharding is added upstream, we use fallback below.
// TODO remove this after ipfs-http-response switch to ipfs.resolve
// or sharding is supported by some other means
try {
const matchingLink = (await ipfs.ls(err.parentDagNode, { resolveType: false })).find(item => item.name === err.missingLinkName)
if (matchingLink) {
console.log('resolver.cid.err.matchingLink', matchingLink)
path = path.replace(matchingLink.path, matchingLink.hash)
console.log('resolver.cid.err.path.after.matchingLink', path)
async function getDirectoryListingOrIndexData (ipfs, path) {
const listing = await ipfs.ls(path)
const index = listing.find((l) => ['index', 'index.html', 'index.htm'].includes(l.name))
if (index) {
return getDataAndGuessMimeType(ipfs, PathUtils.joinURLParts(path, index.name))
}
return {
mimeType: 'text/html',
data: dirView.render(path.replace(/^\/ipfs\//, 'ipfs://'), listing),
charset: 'utf8'
}
}