@@ -183,10 +183,10 @@ export class XetBlob extends Blob {
183183 let readBytesToSkip = reconstructionInfo . offset_into_first_range ;
184184
185185 for ( const term of reconstructionInfo . terms ) {
186- // console.log("term", term, readBytesToSkip, totalBytesRead);
187186 if ( totalBytesRead >= maxBytes ) {
188187 break ;
189188 }
189+
190190 const fetchInfo = reconstructionInfo . fetch_info [ term . hash ] . find (
191191 ( info ) => info . range . start <= term . range . start && info . range . end >= term . range . end
192192 ) ;
@@ -240,6 +240,7 @@ export class XetBlob extends Blob {
240240 continue readChunks;
241241 }
242242 result . value = result . value . slice ( bytesToSkip ) ;
243+ bytesToSkip = 0 ;
243244 }
244245 if ( leftoverBytes ) {
245246 result . value = new Uint8Array ( [ ...leftoverBytes , ...result . value ] ) ;
@@ -314,21 +315,26 @@ export class XetBlob extends Blob {
314315 )
315316 : result . value . slice ( 0 , chunkHeader . compressed_length ) ;
316317
318+ let bytesToYield : Uint8Array ;
317319 if ( readBytesToSkip ) {
318- yield uncompressed . slice (
319- readBytesToSkip ,
320- Math . min ( uncompressed . length , readBytesToSkip + maxBytes - totalBytesRead )
321- ) ;
322- totalBytesRead += Math . min ( uncompressed . length , readBytesToSkip + maxBytes - totalBytesRead ) ;
320+ const remainingBytes = Math . min ( uncompressed . length - readBytesToSkip , maxBytes - totalBytesRead ) ;
321+ bytesToYield = uncompressed . slice ( readBytesToSkip , readBytesToSkip + remainingBytes ) ;
323322 readBytesToSkip = 0 ;
324323 } else {
325- yield uncompressed . slice ( 0 , Math . min ( uncompressed . length , maxBytes - totalBytesRead ) ) ;
326- totalBytesRead += Math . min ( uncompressed . length , maxBytes - totalBytesRead ) ;
324+ bytesToYield = uncompressed . slice ( 0 , Math . min ( uncompressed . length , maxBytes - totalBytesRead ) ) ;
327325 }
326+
327+ totalBytesRead += bytesToYield . length ;
328+ yield bytesToYield ;
328329 chunksToRead -- ;
330+
331+ result . value = result . value . slice ( chunkHeader . compressed_length ) ;
329332 }
330333 }
331334 }
335+
336+ // Release the reader
337+ await reader . cancel ( ) ;
332338 }
333339 }
334340
0 commit comments