Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor: gateway resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Sep 4, 2017
1 parent d05591a commit c31e013
Showing 1 changed file with 63 additions and 73 deletions.
136 changes: 63 additions & 73 deletions src/http/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,30 @@ const PathUtil = require('./utils/path')

const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ]

function noop () {}

const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
if (!callback) {
callback = noop
}
callback = callback || noop

mh.validate(mh.fromB58String(multihash))

ipfs
.object
.get(multihash, { enc: 'base58' })
.then((DAGNode) => {
const links = DAGNode.links
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => {
if (err) { return callback(err) }

// found index file in links
if (indexFiles.length > 0) {
return callback(null, indexFiles)
}
const links = dagNode.links
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)

return callback(null, html.build(path, links))
})
})
// found index file in links
if (indexFiles.length > 0) {
return callback(null, indexFiles)
}

const noop = function () {}
return callback(null, html.build(path, links))
})
})

const resolveMultihash = promisify((ipfs, path, callback) => {
if (!callback) {
callback = noop
}
callback = callback || noop

const parts = PathUtil.splitPath(path)
const partsLength = parts.length
Expand All @@ -53,62 +48,57 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
log('currentMultihash: ', currentMultihash)
log('currentIndex: ', currentIndex, '/', partsLength)

ipfs
.object
.get(currentMultihash, { enc: 'base58' })
.then((DAGNode) => {
// log('DAGNode: ', DAGNode)
if (currentIndex === partsLength - 1) {
// leaf node
log('leaf node: ', currentMultihash)
// log('DAGNode: ', DAGNode.links)

if (DAGNode.links &&
DAGNode.links.length > 0 &&
DAGNode.links[0].name.length > 0) {
// this is a directory.
let isDirErr = new Error('This dag node is a directory')
// add currentMultihash as a fileName so it can be used by resolveDirectory
isDirErr.fileName = currentMultihash
return next(isDirErr)
}

next()
} else {
// find multihash of requested named-file
// in current DAGNode's links
let multihashOfNextFile
const nextFileName = parts[currentIndex + 1]
const links = DAGNode.links

for (let link of links) {
if (link.name === nextFileName) {
// found multihash of requested named-file
multihashOfNextFile = mh.toB58String(link.multihash)
log('found multihash: ', multihashOfNextFile)
break
}
}

if (!multihashOfNextFile) {
log.error(`no link named "${nextFileName}" under ${currentMultihash}`)
throw new Error(`no link named "${nextFileName}" under ${currentMultihash}`)
}

currentMultihash = multihashOfNextFile
next()
}
})
ipfs.object.get(currentMultihash, { enc: 'base58' }, (err, dagNode) => {
if (err) { return next(err) }

if (currentIndex === partsLength - 1) {
// leaf node
log('leaf node: ', currentMultihash)

// TODO: Check if it is a directory by using Unixfs Type, right now
// it won't detect empty dirs
if (dagNode.links &&
dagNode.links.length > 0 &&
dagNode.links[0].name.length > 0) {
// this is a directory.

let isDirErr = new Error('This dag node is a directory')
// add currentMultihash as a fileName so it can be used by resolveDirectory
isDirErr.fileName = currentMultihash
return next(isDirErr)
}
return next()
}

// find multihash of requested named-file in current dagNode's links
let multihashOfNextFile
const nextFileName = parts[currentIndex + 1]
const links = dagNode.links

for (let link of links) {
if (link.name === nextFileName) {
// found multihash of requested named-file
multihashOfNextFile = mh.toB58String(link.multihash)
log('found multihash: ', multihashOfNextFile)
break
}
}

if (!multihashOfNextFile) {
log.error(`no link named "${nextFileName}" under ${currentMultihash}`)
return next(new Error(`no link named "${nextFileName}" under ${currentMultihash}`))
}

currentMultihash = multihashOfNextFile
next()
})
}, (err) => {
if (err) {
log.error(err)
return callback(err)
}
callback(null, {multihash: currentMultihash})
if (err) { return callback(err) }
callback(null, { multihash: currentMultihash })
})
})

module.exports = {
resolveDirectory,
resolveMultihash
resolveDirectory: resolveDirectory,
resolveMultihash: resolveMultihash
}

0 comments on commit c31e013

Please sign in to comment.