@@ -15,7 +15,9 @@ import { CustomProgressEvent } from 'progress-events'
15
15
async function walkDAG ( blockstore : ReadableStorage , node : dagPb . PBNode | Uint8Array , queue : Pushable < Uint8Array > , streamPosition : bigint , start : bigint , end : bigint , options : ExporterOptions ) : Promise < void > {
16
16
// a `raw` node
17
17
if ( node instanceof Uint8Array ) {
18
- queue . push ( extractDataFromBlock ( node , streamPosition , start , end ) )
18
+ const buf = extractDataFromBlock ( node , streamPosition , start , end )
19
+
20
+ queue . push ( buf )
19
21
20
22
return
21
23
}
@@ -123,6 +125,10 @@ async function walkDAG (blockstore: ReadableStorage, node: dagPb.PBNode | Uint8A
123
125
}
124
126
}
125
127
)
128
+
129
+ if ( streamPosition >= end ) {
130
+ queue . end ( )
131
+ }
126
132
}
127
133
128
134
const fileContent : UnixfsV1Resolver = ( cid , node , unixfs , path , resolve , depth , blockstore ) => {
@@ -134,34 +140,23 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
134
140
}
135
141
136
142
const {
137
- offset ,
138
- length
143
+ start ,
144
+ end
139
145
} = validateOffsetAndLength ( fileSize , options . offset , options . length )
140
146
141
- if ( length === 0n ) {
147
+ if ( end === 0n ) {
142
148
return
143
149
}
144
150
145
151
let read = 0n
146
- const wanted = length - offset
152
+ const wanted = end - start
147
153
const queue = pushable ( )
148
154
149
155
options . onProgress ?.( new CustomProgressEvent < ExportWalk > ( 'unixfs:exporter:walk:file' , {
150
156
cid
151
157
} ) )
152
158
153
- void walkDAG ( blockstore , node , queue , 0n , offset , offset + length , options )
154
- . then ( ( ) => {
155
- if ( read < wanted ) {
156
- throw errCode ( new Error ( 'Traversed entire DAG but did not read enough bytes' ) , 'ERR_UNDER_READ' )
157
- }
158
-
159
- if ( read > wanted ) {
160
- throw errCode ( new Error ( 'Read too many bytes - the file size reported by the UnixFS data in the root node may be incorrect' ) , 'ERR_OVER_READ' )
161
- }
162
-
163
- queue . end ( )
164
- } )
159
+ void walkDAG ( blockstore , node , queue , 0n , start , end , options )
165
160
. catch ( err => {
166
161
queue . end ( err )
167
162
} )
@@ -173,7 +168,12 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
173
168
174
169
read += BigInt ( buf . byteLength )
175
170
176
- if ( read === length ) {
171
+ if ( read > wanted ) {
172
+ queue . end ( )
173
+ throw errCode ( new Error ( 'Read too many bytes - the file size reported by the UnixFS data in the root node may be incorrect' ) , 'ERR_OVER_READ' )
174
+ }
175
+
176
+ if ( read === wanted ) {
177
177
queue . end ( )
178
178
}
179
179
@@ -185,6 +185,10 @@ const fileContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth,
185
185
186
186
yield buf
187
187
}
188
+
189
+ if ( read < wanted ) {
190
+ throw errCode ( new Error ( 'Traversed entire DAG but did not read enough bytes' ) , 'ERR_UNDER_READ' )
191
+ }
188
192
}
189
193
190
194
return yieldFileContent
0 commit comments