@@ -5,6 +5,7 @@ const pullDefer = require('pull-defer')
5
5
const pullTraverse = require ( 'pull-traverse' )
6
6
const pullCat = require ( 'pull-cat' )
7
7
const isIpfs = require ( 'is-ipfs' )
8
+ const CID = require ( 'cids' )
8
9
const { normalizePath } = require ( './utils' )
9
10
const { Format } = require ( './refs' )
10
11
@@ -82,6 +83,14 @@ function refsStream (ipfs, path, options) {
82
83
return deferred
83
84
}
84
85
86
+ // Get formatted link
87
+ function formatLink ( srcCid , dstCid , linkName , format ) {
88
+ let out = format . replace ( / < s r c > / g, srcCid . toString ( ) )
89
+ out = out . replace ( / < d s t > / g, dstCid . toString ( ) )
90
+ out = out . replace ( / < l i n k n a m e > / g, linkName )
91
+ return out
92
+ }
93
+
85
94
// Do a depth first search of the DAG, starting from the given root cid
86
95
function objectStream ( ipfs , rootCid , maxDepth , isUnique ) {
87
96
const uniques = new Set ( )
@@ -112,7 +121,7 @@ function objectStream (ipfs, rootCid, maxDepth, isUnique) {
112
121
const deferred = pullDefer . source ( )
113
122
114
123
// Get this object's links
115
- ipfs . object . links ( node . cid , ( err , links ) => {
124
+ getLinks ( ipfs , node . cid , ( err , links ) => {
116
125
if ( err ) {
117
126
if ( err . code === 'ERR_NOT_FOUND' ) {
118
127
err . message = `Could not find object with CID: ${ node . cid } `
@@ -136,10 +145,28 @@ function objectStream (ipfs, rootCid, maxDepth, isUnique) {
136
145
return pullTraverse . depthFirst ( root , traverseLevel )
137
146
}
138
147
139
- // Get formatted link
140
- function formatLink ( srcCid , dstCid , linkName , format ) {
141
- let out = format . replace ( / < s r c > / g, srcCid . toString ( ) )
142
- out = out . replace ( / < d s t > / g, dstCid . toString ( ) )
143
- out = out . replace ( / < l i n k n a m e > / 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
145
172
}
0 commit comments