Skip to content

Commit daf989f

Browse files
committed
extract data from git lfs resp
1 parent 4590434 commit daf989f

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

packages/hub/src/lib/commit.ts

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
350350
if (useXet && json.transfer !== "xet") {
351351
useXet = false;
352352
}
353+
let xetRefreshWriteTokenUrl: string | undefined;
354+
let xetSessionId: string | undefined;
353355

354356
if (useXet) {
355357
// First get all the files that are already uploaded out of the way
@@ -374,6 +376,17 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
374376
progress: 1,
375377
state: "uploading",
376378
};
379+
} else {
380+
xetRefreshWriteTokenUrl = obj.actions.upload.href;
381+
// Also, obj.actions.upload.header: {
382+
// X-Xet-Cas-Url: string;
383+
// X-Xet-Access-Token: string;
384+
// X-Xet-Token-Expiration: string;
385+
// X-Xet-Session-Id: string;
386+
// }
387+
const headers = new Headers(obj.actions.upload.header);
388+
xetSessionId = headers.get("X-Xet-Session-Id") ?? undefined;
389+
// todo: use other data, like x-xet-cas-url, ...
377390
}
378391
}
379392
const source = (async function* () {
@@ -383,44 +396,49 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
383396
continue;
384397
}
385398
abortSignal?.throwIfAborted();
386-
387-
// todo: load writeTokenUrl from obj.actions.upload.header or obj.actions.upload.href
388399
yield { content: op.content, path: op.path, sha256: obj.oid };
389400
}
390401
})();
391-
const sources = splitAsyncGenerator(source, 5);
392-
yield* eventToGenerator((yieldCallback, returnCallback, rejectCallback) =>
393-
Promise.all(
394-
sources.map(async function (source) {
395-
for await (const event of uploadShards(source, {
396-
fetch: params.fetch,
397-
accessToken,
398-
hubUrl: params.hubUrl ?? HUB_URL,
399-
repo: repoId,
400-
// todo: maybe leave empty if PR?
401-
rev: params.branch ?? "main",
402-
isPullRequest: params.isPullRequest,
403-
yieldCallback: (event) => yieldCallback({ ...event, state: "uploading" }),
404-
})) {
405-
if (event.event === "file") {
406-
yieldCallback({
407-
event: "fileProgress" as const,
408-
path: event.path,
409-
progress: 1,
410-
state: "uploading" as const,
411-
});
412-
} else if (event.event === "fileProgress") {
413-
yieldCallback({
414-
event: "fileProgress" as const,
415-
path: event.path,
416-
progress: event.progress,
417-
state: "uploading" as const,
418-
});
402+
if (xetRefreshWriteTokenUrl) {
403+
const xetRefreshWriteTokenUrlFixed = xetRefreshWriteTokenUrl;
404+
const sources = splitAsyncGenerator(source, 5);
405+
yield* eventToGenerator((yieldCallback, returnCallback, rejectCallback) =>
406+
Promise.all(
407+
sources.map(async function (source) {
408+
for await (const event of uploadShards(source, {
409+
fetch: params.fetch,
410+
accessToken,
411+
hubUrl: params.hubUrl ?? HUB_URL,
412+
repo: repoId,
413+
xetRefreshWriteTokenUrl: xetRefreshWriteTokenUrlFixed,
414+
xetSessionId,
415+
// todo: maybe leave empty if PR?
416+
rev: params.branch ?? "main",
417+
isPullRequest: params.isPullRequest,
418+
yieldCallback: (event) => yieldCallback({ ...event, state: "uploading" }),
419+
})) {
420+
if (event.event === "file") {
421+
yieldCallback({
422+
event: "fileProgress" as const,
423+
path: event.path,
424+
progress: 1,
425+
state: "uploading" as const,
426+
});
427+
} else if (event.event === "fileProgress") {
428+
yieldCallback({
429+
event: "fileProgress" as const,
430+
path: event.path,
431+
progress: event.progress,
432+
state: "uploading" as const,
433+
});
434+
}
419435
}
420-
}
421-
})
422-
).then(() => returnCallback(undefined), rejectCallback)
423-
);
436+
})
437+
).then(() => returnCallback(undefined), rejectCallback)
438+
);
439+
} else {
440+
// No LFS file to upload
441+
}
424442
} else {
425443
yield* eventToGenerator<CommitProgressEvent, void>((yieldCallback, returnCallback, rejectCallback) => {
426444
return promisesQueueStreaming(

packages/hub/src/utils/uploadShards.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export const SHARD_MAGIC_TAG = new Uint8Array([
5454
interface UploadShardsParams {
5555
accessToken: string | undefined;
5656
hubUrl: string;
57+
xetRefreshWriteTokenUrl: string;
58+
xetSessionId: string | undefined;
5759
fetch?: typeof fetch;
5860
repo: RepoId;
5961
rev: string;
@@ -365,6 +367,7 @@ async function uploadXorb(
365367
body: xorb.xorb,
366368
headers: {
367369
Authorization: `Bearer ${token.accessToken}`,
370+
...(params.xetSessionId ? { "X-Xet-Session-Id": params.xetSessionId } : {}),
368371
},
369372
...{
370373
progressHint: {
@@ -394,6 +397,7 @@ async function uploadShard(shard: Uint8Array, params: UploadShardsParams) {
394397
body: shard,
395398
headers: {
396399
Authorization: `Bearer ${token.accessToken}`,
400+
...(params.xetSessionId ? { "X-Xet-Session-Id": params.xetSessionId } : {}),
397401
},
398402
});
399403

packages/hub/src/utils/xetWriteToken.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface XetWriteTokenParams {
88
repo: RepoId;
99
rev: string;
1010
isPullRequest?: boolean;
11+
xetRefreshWriteTokenUrl: string | undefined;
1112
}
1213

1314
const JWT_SAFETY_PERIOD = 60_000;
@@ -47,9 +48,10 @@ export async function xetWriteToken(params: XetWriteTokenParams): Promise<{ acce
4748

4849
const promise = (async () => {
4950
const resp = await (params.fetch ?? fetch)(
50-
`${params.hubUrl}/api/${params.repo.type}s/${params.repo.name}/xet-write-token/${encodeURIComponent(
51-
params.rev
52-
)}` + (params.isPullRequest ? "?create_pr=1" : ""),
51+
params.xetRefreshWriteTokenUrl ??
52+
`${params.hubUrl}/api/${params.repo.type}s/${params.repo.name}/xet-write-token/${encodeURIComponent(
53+
params.rev
54+
)}` + (params.isPullRequest ? "?create_pr=1" : ""),
5355
{
5456
headers: params.accessToken
5557
? {

0 commit comments

Comments
 (0)