11'use strict'
22
3- const { CID } = require ( 'multiformats/cid' )
4- const cborg = require ( 'cborg' )
5- const dagPb = require ( '@ipld/dag-pb' )
6- const dagCbor = require ( '@ipld/dag-cbor' )
73const log = require ( 'debug' ) ( 'ipfs:repo:utils:walk-dag' )
4+ const Block = require ( 'multiformats/block' )
85
96/**
7+ * @typedef {import('multiformats/cid').CID } CID
108 * @typedef {import('interface-blockstore').Blockstore } Blockstore
119 * @typedef {import('../types').loadCodec } loadCodec
1210 * @typedef {import('../types').AbortOptions } AbortOptions
@@ -21,20 +19,13 @@ const log = require('debug')('ipfs:repo:utils:walk-dag')
2119 */
2220async function * walkDag ( cid , blockstore , loadCodec , options ) {
2321 try {
24- const block = await blockstore . get ( cid , options )
22+ const bytes = await blockstore . get ( cid , options )
2523 const codec = await loadCodec ( cid . code )
26- const node = codec . decode ( block )
24+ const block = Block . createUnsafe ( { bytes , cid , codec } )
2725
28- if ( cid . code === dagPb . code ) {
29- for ( const link of node . Links ) {
30- yield link . Hash
31- yield * walkDag ( link . Hash , blockstore , loadCodec , options )
32- }
33- } else if ( cid . code === dagCbor . code ) {
34- for ( const [ , childCid ] of dagCborLinks ( node ) ) {
35- yield childCid
36- yield * walkDag ( childCid , blockstore , loadCodec , options )
37- }
26+ for ( const [ , childCid ] of block . links ( ) ) {
27+ yield childCid
28+ yield * walkDag ( childCid , blockstore , loadCodec , options )
3829 }
3930 } catch ( err ) {
4031 log ( 'Could not walk DAG for CID' , cid . toString ( ) , err )
@@ -43,44 +34,4 @@ async function * walkDag (cid, blockstore, loadCodec, options) {
4334 }
4435}
4536
46- // eslint-disable-next-line jsdoc/require-returns-check
47- /**
48- * @param {any } obj
49- * @param {string[] } path
50- * @param {boolean } parseBuffer
51- * @returns {Generator<[string, CID], void, undefined> }
52- */
53- function * dagCborLinks ( obj , path = [ ] , parseBuffer = true ) {
54- if ( parseBuffer && obj instanceof Uint8Array ) {
55- obj = cborg . decode ( obj )
56- }
57-
58- for ( const key of Object . keys ( obj ) ) {
59- const _path = path . slice ( )
60- _path . push ( key )
61- const val = obj [ key ]
62-
63- if ( val && typeof val === 'object' ) {
64- if ( Array . isArray ( val ) ) {
65- for ( let i = 0 ; i < val . length ; i ++ ) {
66- const __path = _path . slice ( )
67- __path . push ( i . toString ( ) )
68- const o = val [ i ]
69- if ( o instanceof CID ) { // eslint-disable-line max-depth
70- yield [ __path . join ( '/' ) , o ]
71- } else if ( typeof o === 'object' ) {
72- yield * dagCborLinks ( o , _path , false )
73- }
74- }
75- } else {
76- if ( val instanceof CID ) {
77- yield [ _path . join ( '/' ) , val ]
78- } else {
79- yield * dagCborLinks ( val , _path , false )
80- }
81- }
82- }
83- }
84- }
85-
8637module . exports = walkDag
0 commit comments