@@ -9,7 +9,8 @@ import parallel from 'it-parallel'
99import { pipe } from 'it-pipe'
1010import map from 'it-map'
1111import PQueue from 'p-queue'
12- import type { ExporterOptions , UnixfsV1FileContent , UnixfsV1Resolver , ReadableStorage } from '../../../index.js'
12+ import type { ExporterOptions , UnixfsV1FileContent , UnixfsV1Resolver , ReadableStorage , ExportProgress , ExportWalk } from '../../../index.js'
13+ import { CustomProgressEvent } from 'progress-events'
1314
1415async function walkDAG ( blockstore : ReadableStorage , node : dagPb . PBNode | Uint8Array , queue : Pushable < Uint8Array > , streamPosition : bigint , start : bigint , end : bigint , options : ExporterOptions ) : Promise < void > {
1516 // a `raw` node
@@ -110,6 +111,10 @@ async function walkDAG (blockstore: ReadableStorage, node: dagPb.PBNode | Uint8A
110111
111112 // if the job rejects the 'error' event will be emitted on the child queue
112113 void childQueue . add ( async ( ) => {
114+ options . onProgress ?.( new CustomProgressEvent < ExportWalk > ( 'unixfs:exporter:walk:file' , {
115+ cid : link . Hash
116+ } ) )
117+
113118 await walkDAG ( blockstore , child , queue , blockStart , start , end , options )
114119 } )
115120
@@ -138,12 +143,15 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
138143 }
139144
140145 let read = 0n
146+ const wanted = length - offset
141147 const queue = pushable ( )
142148
149+ options . onProgress ?.( new CustomProgressEvent < ExportWalk > ( 'unixfs:exporter:walk:file' , {
150+ cid
151+ } ) )
152+
143153 void walkDAG ( blockstore , node , queue , 0n , offset , offset + length , options )
144154 . then ( ( ) => {
145- const wanted = length - offset
146-
147155 if ( read < wanted ) {
148156 throw errCode ( new Error ( 'Traversed entire DAG but did not read enough bytes' ) , 'ERR_UNDER_READ' )
149157 }
@@ -169,6 +177,12 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
169177 queue . end ( )
170178 }
171179
180+ options . onProgress ?.( new CustomProgressEvent < ExportProgress > ( 'unixfs:exporter:progress:unixfs:file' , {
181+ bytesRead : read ,
182+ totalBytes : wanted ,
183+ fileSize
184+ } ) )
185+
172186 yield buf
173187 }
174188 }
0 commit comments