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

Commit d852c8d

Browse files
committed
refactor: move links retrieval from object to refs
1 parent b4f062a commit d852c8d

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/core/components/files-regular/refs-pull-stream.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const pullDefer = require('pull-defer')
55
const pullTraverse = require('pull-traverse')
66
const pullCat = require('pull-cat')
77
const isIpfs = require('is-ipfs')
8+
const CID = require('cids')
89
const { normalizePath } = require('./utils')
910
const { Format } = require('./refs')
1011

@@ -82,6 +83,14 @@ function refsStream (ipfs, path, options) {
8283
return deferred
8384
}
8485

86+
// Get formatted link
87+
function formatLink (srcCid, dstCid, linkName, format) {
88+
let out = format.replace(/<src>/g, srcCid.toString())
89+
out = out.replace(/<dst>/g, dstCid.toString())
90+
out = out.replace(/<linkname>/g, linkName)
91+
return out
92+
}
93+
8594
// Do a depth first search of the DAG, starting from the given root cid
8695
function objectStream (ipfs, rootCid, maxDepth, isUnique) {
8796
const uniques = new Set()
@@ -112,7 +121,7 @@ function objectStream (ipfs, rootCid, maxDepth, isUnique) {
112121
const deferred = pullDefer.source()
113122

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

139-
// Get formatted link
140-
function formatLink (srcCid, dstCid, linkName, format) {
141-
let out = format.replace(/<src>/g, srcCid.toString())
142-
out = out.replace(/<dst>/g, dstCid.toString())
143-
out = out.replace(/<linkname>/g, linkName)
144-
return out
148+
// Fetch a node from IPLD then get all its links
149+
function getLinks (ipfs, cid, callback) {
150+
ipfs._ipld.get(new CID(cid), (err, node) => {
151+
if (err) {
152+
return callback(err)
153+
}
154+
callback(null, node.value.links || getNodeLinks(node.value))
155+
})
156+
}
157+
158+
// Recursively search the node for CIDs
159+
function getNodeLinks (node, path = '') {
160+
let links = []
161+
for (const [name, value] of Object.entries(node)) {
162+
if (CID.isCID(value)) {
163+
links.push({
164+
name: path + name,
165+
cid: value
166+
})
167+
} else if (typeof value === 'object') {
168+
links = links.concat(getNodeLinks(value, path + name + '/'))
169+
}
170+
}
171+
return links
145172
}

src/core/components/object.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ function parseProtoBuffer (buf, callback) {
6565
dagPB.util.deserialize(buf, callback)
6666
}
6767

68-
// Recursively search the node for CIDs
69-
function getNodeLinks (node, path = '') {
70-
let links = []
71-
for (const [name, value] of Object.entries(node)) {
72-
if (CID.isCID(value)) {
73-
links.push({
74-
name: path + name,
75-
cid: value
76-
})
77-
} else if (typeof value === 'object') {
78-
links = links.concat(getNodeLinks(value, path + name + '/'))
79-
}
80-
}
81-
return links
82-
}
83-
8468
module.exports = function object (self) {
8569
function editAndSave (edit) {
8670
return (multihash, options, callback) => {
@@ -299,7 +283,7 @@ module.exports = function object (self) {
299283
return callback(err)
300284
}
301285

302-
callback(null, node.links || getNodeLinks(node))
286+
callback(null, node.links)
303287
})
304288
}),
305289

0 commit comments

Comments
 (0)