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

Commit

Permalink
refactor: move links retrieval from object to refs
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed May 9, 2019
1 parent b4f062a commit d852c8d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
41 changes: 34 additions & 7 deletions src/core/components/files-regular/refs-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const pullDefer = require('pull-defer')
const pullTraverse = require('pull-traverse')
const pullCat = require('pull-cat')
const isIpfs = require('is-ipfs')
const CID = require('cids')
const { normalizePath } = require('./utils')
const { Format } = require('./refs')

Expand Down Expand Up @@ -82,6 +83,14 @@ function refsStream (ipfs, path, options) {
return deferred
}

// Get formatted link
function formatLink (srcCid, dstCid, linkName, format) {
let out = format.replace(/<src>/g, srcCid.toString())
out = out.replace(/<dst>/g, dstCid.toString())
out = out.replace(/<linkname>/g, linkName)
return out
}

// Do a depth first search of the DAG, starting from the given root cid
function objectStream (ipfs, rootCid, maxDepth, isUnique) {
const uniques = new Set()
Expand Down Expand Up @@ -112,7 +121,7 @@ function objectStream (ipfs, rootCid, maxDepth, isUnique) {
const deferred = pullDefer.source()

// Get this object's links
ipfs.object.links(node.cid, (err, links) => {
getLinks(ipfs, node.cid, (err, links) => {
if (err) {
if (err.code === 'ERR_NOT_FOUND') {
err.message = `Could not find object with CID: ${node.cid}`
Expand All @@ -136,10 +145,28 @@ function objectStream (ipfs, rootCid, maxDepth, isUnique) {
return pullTraverse.depthFirst(root, traverseLevel)
}

// Get formatted link
function formatLink (srcCid, dstCid, linkName, format) {
let out = format.replace(/<src>/g, srcCid.toString())
out = out.replace(/<dst>/g, dstCid.toString())
out = out.replace(/<linkname>/g, linkName)
return out
// Fetch a node from IPLD then get all its links
function getLinks (ipfs, cid, callback) {
ipfs._ipld.get(new CID(cid), (err, node) => {
if (err) {
return callback(err)
}
callback(null, node.value.links || getNodeLinks(node.value))
})
}

// Recursively search the node for CIDs
function getNodeLinks (node, path = '') {
let links = []
for (const [name, value] of Object.entries(node)) {
if (CID.isCID(value)) {
links.push({
name: path + name,
cid: value
})
} else if (typeof value === 'object') {
links = links.concat(getNodeLinks(value, path + name + '/'))
}
}
return links
}
18 changes: 1 addition & 17 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,6 @@ function parseProtoBuffer (buf, callback) {
dagPB.util.deserialize(buf, callback)
}

// Recursively search the node for CIDs
function getNodeLinks (node, path = '') {
let links = []
for (const [name, value] of Object.entries(node)) {
if (CID.isCID(value)) {
links.push({
name: path + name,
cid: value
})
} else if (typeof value === 'object') {
links = links.concat(getNodeLinks(value, path + name + '/'))
}
}
return links
}

module.exports = function object (self) {
function editAndSave (edit) {
return (multihash, options, callback) => {
Expand Down Expand Up @@ -299,7 +283,7 @@ module.exports = function object (self) {
return callback(err)
}

callback(null, node.links || getNodeLinks(node))
callback(null, node.links)
})
}),

Expand Down

0 comments on commit d852c8d

Please sign in to comment.