Skip to content

Commit daec055

Browse files
committed
can skip whole chunks at the start of the term
1 parent ca0feb9 commit daec055

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/hub/src/utils/XetBlob.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class XetBlob extends Blob {
8888
constructor(params: XetBlobCreateOptions) {
8989
super([]);
9090

91-
this.fetch = params.fetch ?? fetch;
91+
this.fetch = params.fetch ?? fetch.bind(globalThis);
9292
this.accessToken = checkCredentials(params);
9393
this.repoId = toRepoId(params.repo);
9494
this.hubUrl = params.hubUrl ?? HUB_URL;
@@ -163,7 +163,7 @@ export class XetBlob extends Blob {
163163

164164
async function* readData(reconstructionInfo: ReconstructionInfo, customFetch: typeof fetch, maxBytes: number) {
165165
let totalBytesRead = 0;
166-
let isFirstChunk = true;
166+
let readBytesToSkip = reconstructionInfo.offset_into_first_range;
167167

168168
for (const term of reconstructionInfo.terms) {
169169
if (totalBytesRead >= maxBytes) {
@@ -254,6 +254,14 @@ export class XetBlob extends Blob {
254254
continue;
255255
}
256256

257+
if (readBytesToSkip >= chunkHeader.uncompressed_length) {
258+
readBytesToSkip -= chunkHeader.uncompressed_length;
259+
leftoverBytes = result.value.slice(CHUNK_HEADER_BYTES);
260+
bytesToSkip = chunkHeader.compressed_length;
261+
chunksToRead--;
262+
continue;
263+
}
264+
257265
if (result.value.length < chunkHeader.compressed_length + CHUNK_HEADER_BYTES) {
258266
// We need more data to read the full chunk
259267
leftoverBytes = result.value;
@@ -270,16 +278,13 @@ export class XetBlob extends Blob {
270278
)
271279
: result.value.slice(0, chunkHeader.compressed_length);
272280

273-
if (isFirstChunk) {
281+
if (readBytesToSkip) {
274282
yield uncompressed.slice(
275-
reconstructionInfo.offset_into_first_range,
276-
Math.min(uncompressed.length, reconstructionInfo.offset_into_first_range + maxBytes - totalBytesRead)
277-
);
278-
totalBytesRead += Math.min(
279-
uncompressed.length,
280-
reconstructionInfo.offset_into_first_range + maxBytes - totalBytesRead
283+
readBytesToSkip,
284+
Math.min(uncompressed.length, readBytesToSkip + maxBytes - totalBytesRead)
281285
);
282-
isFirstChunk = false;
286+
totalBytesRead += Math.min(uncompressed.length, readBytesToSkip + maxBytes - totalBytesRead);
287+
readBytesToSkip = 0;
283288
} else {
284289
yield uncompressed.slice(0, Math.min(uncompressed.length, maxBytes - totalBytesRead));
285290
totalBytesRead += Math.min(uncompressed.length, maxBytes - totalBytesRead);

0 commit comments

Comments
 (0)