@@ -139,10 +139,13 @@ async function traverseBlock (block, get, car, concurrency = 1, seen = new Set()
139
139
const cid = await block . cid ( )
140
140
await car . put ( cid , block . encodeUnsafe ( ) )
141
141
seen . add ( cid . toString ( 'base58btc' ) )
142
- if ( cid . codec === 'raw' ) return
142
+ if ( cid . codec === 'raw' ) {
143
+ return
144
+ }
143
145
const reader = block . reader ( )
144
- const missing = link => ! seen . has ( link . toString ( 'base58btc' ) )
146
+ const missing = ( link ) => ! seen . has ( link . toString ( 'base58btc' ) )
145
147
const links = Array . from ( reader . links ( ) ) . filter ( missing ) . map ( ( [ , link ] ) => link )
148
+
146
149
while ( links . length ) {
147
150
const chunk = links . splice ( 0 , concurrency )
148
151
const blocks = chunk . map ( get )
@@ -156,6 +159,25 @@ async function traverseBlock (block, get, car, concurrency = 1, seen = new Set()
156
159
}
157
160
}
158
161
162
+ /**
163
+ * @name CarDatastore.completeGraph
164
+ * @description
165
+ * Read a complete IPLD graph from a provided datastore and store the blocks in
166
+ * a CAR file.
167
+ * @function
168
+ * @memberof CarDatastore
169
+ * @static
170
+ * @async
171
+ * @param {Block } root the root of the graph to start at, this block will be
172
+ * included in the CAR and its CID will be set as the single root.
173
+ * @param {AsyncFunction } get an `async` function that takes a CID and returns
174
+ * a `Block`. Can be used to attach to an arbitrary data store.
175
+ * @param {CarDatastore } car a writable `CarDatastore` that has not yet been
176
+ * written to (`setRoots()` will be called on it which requires that no data
177
+ * has been written).
178
+ * @param {number } [concurrency=1] how many asynchronous `get` operations to
179
+ * perform at once.
180
+ */
159
181
async function completeGraph ( root , get , car , concurrency ) {
160
182
await car . setRoots ( [ root ] )
161
183
await traverseBlock ( await get ( root ) , get , car , concurrency )
0 commit comments