Skip to content

Commit a301838

Browse files
committed
🐛 Fix WebBlob uploads in browsers
1 parent 2405785 commit a301838

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

packages/hub/src/lib/commit.spec.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,26 @@ size ${lfsContent.length}
167167
const LFSSize = (await fileDownloadInfo({ repo, path: "mobilenet/group1-shard1of2" }))?.size;
168168

169169
assert.strictEqual(LFSSize, 4_194_304);
170+
171+
const pointerFile = await downloadFile({ repo, path: "mobilenet/group1-shard1of2", raw: true });
172+
173+
// Make sure SHA is computed properly as well
174+
assert.strictEqual(
175+
(await pointerFile?.text())?.trim(),
176+
`
177+
version https://git-lfs.github.com/spec/v1
178+
oid sha256:3fb621eb9b37478239504ee083042d5b18699e8b8618e569478b03b119a85a69
179+
size 4194304
180+
`.trim()
181+
);
170182
} finally {
171-
if (!Math.random()) {
172-
await deleteRepo({
173-
repo: {
174-
name: repoName,
175-
type: "model",
176-
},
177-
credentials: { accessToken: TEST_ACCESS_TOKEN },
178-
});
179-
}
183+
await deleteRepo({
184+
repo: {
185+
name: repoName,
186+
type: "model",
187+
},
188+
credentials: { accessToken: TEST_ACCESS_TOKEN },
189+
});
180190
}
181191
// https://huggingfacejs-push-model-from-web.hf.space/
182192
}, 60_000);

packages/hub/src/lib/commit.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,15 @@ async function* commitIter(params: CommitParams): AsyncGenerator<unknown, Commit
284284
})),
285285
};
286286

287-
await promisesQueue(
287+
await promisesQueueStreaming(
288288
parts.map((part) => async () => {
289289
const index = parseInt(part) - 1;
290+
const slice = content.slice(index * chunkSize, (index + 1) * chunkSize);
291+
290292
const res = await fetch(header[part], {
291293
method: "PUT",
292-
body: content.slice(index * chunkSize, (index + 1) * chunkSize),
294+
/** Unfortunately, browsers don't support our inherited version of Blob in fetch calls */
295+
body: slice instanceof WebBlob && isFrontend ? await slice.arrayBuffer() : slice,
293296
});
294297

295298
if (!res.ok) {
@@ -333,7 +336,8 @@ async function* commitIter(params: CommitParams): AsyncGenerator<unknown, Commit
333336
headers: {
334337
...(batchRequestId ? { "X-Request-Id": batchRequestId } : undefined),
335338
},
336-
body: content,
339+
/** Unfortunately, browsers don't support our inherited version of Blob in fetch calls */
340+
body: content instanceof WebBlob && isFrontend ? await content.arrayBuffer() : content,
337341
});
338342

339343
if (!res.ok) {

0 commit comments

Comments
 (0)