@@ -5,13 +5,23 @@ import { createUnsafe } from 'multiformats/block'
55import { InvalidTraversalError } from '../errors.ts'
66import type { TraversalStrategy } from '../index.js'
77import type { CodecLoader } from '@helia/interface'
8- import type { GraphWalker } from '@helia/utils'
8+ import type { GraphWalker , GraphWalkerComponents } from '@helia/utils'
99import type { AbortOptions } from '@libp2p/interface'
1010import type { Blockstore } from 'interface-blockstore'
1111import type { BlockView } from 'multiformats'
1212import type { CID } from 'multiformats/cid'
1313
1414export interface GraphSearchOptions {
15+ /**
16+ * Graph traversal strategy, defaults to breadth-first
17+ */
18+ walker ?( components : GraphWalkerComponents ) : GraphWalker
19+
20+ /**
21+ * How to search the graph
22+ *
23+ * @deprecated use `walker` instead - this will be removed in a future release
24+ */
1525 strategy ?: 'depth-first' | 'breadth-first'
1626}
1727
@@ -27,6 +37,7 @@ export class GraphSearch implements TraversalStrategy {
2737 private haystack ?: CID
2838 private readonly needle : CID
2939 private readonly options ?: GraphSearchOptions
40+ private walker ?: ( components : GraphWalkerComponents ) => GraphWalker
3041
3142 constructor ( needle : CID , options ?: GraphSearchOptions )
3243 constructor ( haystack : CID , needle : CID , options ?: GraphSearchOptions )
@@ -43,22 +54,24 @@ export class GraphSearch implements TraversalStrategy {
4354 } else {
4455 throw new InvalidParametersError ( 'needle must be specified' )
4556 }
57+
58+ this . walker = this . options ?. walker
4659 }
4760
4861 async * traverse ( root : CID , blockstore : Blockstore , getCodec : CodecLoader , options ?: AbortOptions ) : AsyncGenerator < BlockView < unknown , number , number , 0 | 1 > , void , undefined > {
4962 const start = this . haystack ?? root
5063 let walker : GraphWalker
64+ const components = {
65+ blockstore,
66+ getCodec
67+ }
5168
52- if ( this . options ?. strategy === 'breadth-first' ) {
53- walker = breadthFirstWalker ( {
54- blockstore,
55- getCodec
56- } )
69+ if ( this . walker != null ) {
70+ walker = this . walker ( components )
71+ } else if ( this . options ?. strategy === 'breadth-first' ) {
72+ walker = breadthFirstWalker ( ) ( components )
5773 } else {
58- walker = depthFirstWalker ( {
59- blockstore,
60- getCodec
61- } )
74+ walker = depthFirstWalker ( ) ( components )
6275 }
6376
6477 for await ( const node of walker . walk ( start , options ) ) {
0 commit comments