@@ -9,7 +9,8 @@ import parallel from 'it-parallel'
9
9
import { pipe } from 'it-pipe'
10
10
import map from 'it-map'
11
11
import 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'
13
14
14
15
async function walkDAG ( blockstore : ReadableStorage , node : dagPb . PBNode | Uint8Array , queue : Pushable < Uint8Array > , streamPosition : bigint , start : bigint , end : bigint , options : ExporterOptions ) : Promise < void > {
15
16
// a `raw` node
@@ -110,6 +111,10 @@ async function walkDAG (blockstore: ReadableStorage, node: dagPb.PBNode | Uint8A
110
111
111
112
// if the job rejects the 'error' event will be emitted on the child queue
112
113
void childQueue . add ( async ( ) => {
114
+ options . onProgress ?.( new CustomProgressEvent < ExportWalk > ( 'unixfs:exporter:walk:file' , {
115
+ cid : link . Hash
116
+ } ) )
117
+
113
118
await walkDAG ( blockstore , child , queue , blockStart , start , end , options )
114
119
} )
115
120
@@ -138,12 +143,15 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
138
143
}
139
144
140
145
let read = 0n
146
+ const wanted = length - offset
141
147
const queue = pushable ( )
142
148
149
+ options . onProgress ?.( new CustomProgressEvent < ExportWalk > ( 'unixfs:exporter:walk:file' , {
150
+ cid
151
+ } ) )
152
+
143
153
void walkDAG ( blockstore , node , queue , 0n , offset , offset + length , options )
144
154
. then ( ( ) => {
145
- const wanted = length - offset
146
-
147
155
if ( read < wanted ) {
148
156
throw errCode ( new Error ( 'Traversed entire DAG but did not read enough bytes' ) , 'ERR_UNDER_READ' )
149
157
}
@@ -169,6 +177,12 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
169
177
queue . end ( )
170
178
}
171
179
180
+ options . onProgress ?.( new CustomProgressEvent < ExportProgress > ( 'unixfs:exporter:progress:unixfs:file' , {
181
+ bytesRead : read ,
182
+ totalBytes : wanted ,
183
+ fileSize
184
+ } ) )
185
+
172
186
yield buf
173
187
}
174
188
}
0 commit comments