Skip to content

Commit 699b51a

Browse files
VivianHublovvnsrzn
authored andcommitted
feat: add combineUint8Arrays utility and corresponding tests
1 parent a30d3db commit 699b51a

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

packages/hub/src/utils/XetBlob.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApiError } from "../error";
22
import type { CredentialsParams } from "../types/public";
33
import { checkCredentials } from "./checkCredentials";
4+
import { combineUint8Arrays } from "./combineUint8Arrays";
45
import { decompress as lz4_decompress } from "../vendor/lz4js";
56
import { RangeList } from "./RangeList";
67

@@ -201,7 +202,7 @@ export class XetBlob extends Blob {
201202
rangeList.add(term.range.start, term.range.end);
202203
}
203204
const listener = this.listener;
204-
const log = this.internalLogging ? (...args: unknown[]) => console.log(...args) : () => {};
205+
const log = this.internalLogging ? (...args: unknown[]) => console.log(...args) : () => { };
205206

206207
async function* readData(
207208
reconstructionInfo: ReconstructionInfo,
@@ -327,14 +328,11 @@ export class XetBlob extends Blob {
327328
totalFetchBytes += result.value.byteLength;
328329

329330
if (leftoverBytes) {
330-
const leftoverBytesLength: number = leftoverBytes.length;
331-
const combinedBytes = new Uint8Array(leftoverBytesLength + result.value.length);
332-
combinedBytes.set(leftoverBytes);
333-
combinedBytes.set(result.value, leftoverBytesLength);
334-
result.value = combinedBytes;
331+
result.value = combineUint8Arrays(leftoverBytes, result.value);
332+
leftoverBytes = undefined;
335333
}
336334

337-
while (totalBytesRead < maxBytes && result.value.byteLength) {
335+
while (totalBytesRead < maxBytes && result.value?.byteLength) {
338336
if (result.value.byteLength < 8) {
339337
// We need 8 bytes to parse the chunk header
340338
leftoverBytes = result.value;
@@ -361,8 +359,7 @@ export class XetBlob extends Blob {
361359
chunkHeader.compression_scheme !== XetChunkCompressionScheme.ByteGroupingLZ4
362360
) {
363361
throw new Error(
364-
`Unsupported compression scheme ${
365-
compressionSchemeLabels[chunkHeader.compression_scheme] ?? chunkHeader.compression_scheme
362+
`Unsupported compression scheme ${compressionSchemeLabels[chunkHeader.compression_scheme] ?? chunkHeader.compression_scheme
366363
}`
367364
);
368365
}
@@ -379,13 +376,13 @@ export class XetBlob extends Blob {
379376
chunkHeader.compression_scheme === XetChunkCompressionScheme.LZ4
380377
? lz4_decompress(result.value.slice(0, chunkHeader.compressed_length), chunkHeader.uncompressed_length)
381378
: chunkHeader.compression_scheme === XetChunkCompressionScheme.ByteGroupingLZ4
382-
? bg4_regroup_bytes(
383-
lz4_decompress(
384-
result.value.slice(0, chunkHeader.compressed_length),
385-
chunkHeader.uncompressed_length
386-
)
387-
)
388-
: result.value.slice(0, chunkHeader.compressed_length);
379+
? bg4_regroup_bytes(
380+
lz4_decompress(
381+
result.value.slice(0, chunkHeader.compressed_length),
382+
chunkHeader.uncompressed_length
383+
)
384+
)
385+
: result.value.slice(0, chunkHeader.compressed_length);
389386

390387
const range = ranges.find((range) => chunkIndex >= range.start && chunkIndex < range.end);
391388
const shouldYield = chunkIndex >= term.range.start && chunkIndex < term.range.end;
@@ -439,8 +436,7 @@ export class XetBlob extends Blob {
439436
log("done", done, "total read", totalBytesRead, maxBytes, totalFetchBytes);
440437
log("failed to fetch all data for term", term.hash);
441438
throw new Error(
442-
`Failed to fetch all data for term ${term.hash}, fetched ${totalFetchBytes} bytes out of ${
443-
fetchInfo.url_range.end - fetchInfo.url_range.start + 1
439+
`Failed to fetch all data for term ${term.hash}, fetched ${totalFetchBytes} bytes out of ${fetchInfo.url_range.end - fetchInfo.url_range.start + 1
444440
}`
445441
);
446442
}
@@ -651,8 +647,8 @@ async function getAccessToken(
651647
headers: {
652648
...(initialAccessToken
653649
? {
654-
Authorization: `Bearer ${initialAccessToken}`,
655-
}
650+
Authorization: `Bearer ${initialAccessToken}`,
651+
}
656652
: {}),
657653
},
658654
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, it, expect } from "vitest";
2+
import { combineUint8Arrays } from "./combineUint8Arrays";
3+
4+
describe("combineUint8Arrays", () => {
5+
it.each([
6+
{ a: [], b: [], expected: [] },
7+
{ a: [], b: [1, 2, 3], expected: [1, 2, 3] },
8+
{ a: [4, 5, 6], b: [], expected: [4, 5, 6] },
9+
{ a: [7, 8], b: [9, 10], expected: [7, 8, 9, 10] },
10+
{ a: [1], b: [2, 3, 4], expected: [1, 2, 3, 4] },
11+
])("combines $a and $b to $expected", ({ a, b, expected }) => {
12+
const result = combineUint8Arrays(new Uint8Array(a), new Uint8Array(b));
13+
expect(result).toEqual(new Uint8Array(expected));
14+
});
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function combineUint8Arrays(
2+
a: Uint8Array<ArrayBufferLike>,
3+
b: Uint8Array<ArrayBufferLike>
4+
): Uint8Array<ArrayBuffer> {
5+
const aLength = a.length;
6+
const combinedBytes = new Uint8Array(aLength + b.length);
7+
combinedBytes.set(a);
8+
combinedBytes.set(b, aLength);
9+
return combinedBytes;
10+
}

0 commit comments

Comments
 (0)