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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
import { HUB_URL } from "../consts";
import { HubApiError, createApiError, InvalidApiResponseFormatError } from "../error";
import type {
	ApiBucketBatchResponse,
	ApiCommitHeader,
	ApiCommitLfsFile,
	ApiCommitOperation,
	ApiLfsBatchRequest,
	ApiLfsBatchResponse,
	ApiLfsCompleteMultipartRequest,
	ApiPreuploadRequest,
	ApiPreuploadResponse,
} from "../types/api/api-commit";
import type { CredentialsParams, RepoDesignation } from "../types/public";
import { checkCredentials } from "../utils/checkCredentials";
import { chunk } from "../utils/chunk";
import { promisesQueue } from "../utils/promisesQueue";
import { promisesQueueStreaming } from "../utils/promisesQueueStreaming";
import { sha256 } from "../utils/sha256";
import { toRepoId } from "../utils/toRepoId";
import { WebBlob } from "../utils/WebBlob";
import { eventToGenerator } from "../utils/eventToGenerator";
import { base64FromBytes } from "../utils/base64FromBytes";
import { isFrontend } from "../utils/isFrontend";
import { createBlobs } from "../utils/createBlobs";
import type { XetTokenParams } from "../utils/uploadShards";
import { uploadShards } from "../utils/uploadShards";
import { splitAsyncGenerator } from "../utils/splitAsyncGenerator";
import { SplicedBlob } from "../utils/SplicedBlob";

const CONCURRENT_SHAS = 5;
const CONCURRENT_LFS_UPLOADS = 5;
const MULTIPART_PARALLEL_UPLOAD = 5;

export interface CommitDeletedEntry {
	operation: "delete";
	path: string;
}

export type ContentSource = Blob | URL;

export interface CommitFile {
	operation: "addOrUpdate";
	path: string;
	content: ContentSource;
	// forceLfs?: boolean
}

/**
 * Opitmized when only the beginning or the end of the file is replaced
 *
 * todo: handle other cases
 */
export interface CommitEditFile {
	operation: "edit";
	path: string;
	/** Later, will be ContentSource. For now simpler to just handle blobs */
	originalContent: Blob;
	edits: Array<{
		/**
		 * Later, will be ContentSource. For now simpler to just handle blobs
		 *
		 * originalContent from [start, end) will be replaced by this
		 */
		content: Blob;
		/**
		 * The start position of the edit in the original content
		 */
		start: number;
		/**
		 * The end position of the edit in the original content
		 *
		 * originalContent from [start, end) will be replaced by the edit
		 */
		end: number;
	}>;
}

type CommitBlob = Omit<CommitFile, "content"> & { content: Blob };

// TODO: find a nice way to handle LFS & non-LFS files in an uniform manner, see https://github.com/huggingface/moon-landing/issues/4370
// export type CommitRenameFile = {
// 	operation: "rename";
// 	path:      string;
// 	oldPath:   string;
// 	content?:  ContentSource;
// };

export type CommitOperation = CommitDeletedEntry | CommitFile | CommitEditFile /* | CommitRenameFile */;
type CommitBlobOperation = Exclude<CommitOperation, CommitFile> | CommitBlob;

export type CommitParams = {
	title: string;
	description?: string;
	repo: RepoDesignation;
	operations: CommitOperation[];
	/** @default "main" */
	branch?: string;
	/**
	 * Parent commit. Optional
	 *
	 * - When opening a PR: will use parentCommit as the parent commit
	 * - When committing on a branch: Will make sure that there were no intermediate commits
	 */
	parentCommit?: string;
	isPullRequest?: boolean;
	hubUrl?: string;
	/**
	 * Whether to use web workers to compute SHA256 hashes.
	 *
	 * @default false
	 */
	useWebWorkers?: boolean | { minSize?: number; poolSize?: number };
	/**
	 * Maximum depth of folders to upload. Files deeper than this will be ignored
	 *
	 * @default 5
	 */
	maxFolderDepth?: number;
	/**
	 * Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
	 */
	fetch?: typeof fetch;
	abortSignal?: AbortSignal;
	/**
	 * @default true
	 *
	 * Use xet protocol: https://huggingface.co/blog/xet-on-the-hub to upload, rather than a basic S3 PUT
	 */
	useXet?: boolean;
	// Credentials are optional due to custom fetch functions or cookie auth
} & Partial<CredentialsParams>;

export interface CommitOutput {
	pullRequestUrl?: string;
	commit: {
		oid: string;
		url: string;
	};
	hookOutput: string;
}

function isFileOperation(op: CommitOperation): op is CommitBlob {
	const ret = op.operation === "addOrUpdate";

	if (ret && !(op.content instanceof Blob)) {
		throw new TypeError("Precondition failed: op.content should be a Blob");
	}

	return ret;
}

export type CommitProgressEvent =
	| {
			event: "phase";
			phase: "preuploading" | "uploadingLargeFiles" | "committing";
	  }
	| {
			event: "fileProgress";
			path: string;
			progress: number;
			state: "hashing" | "uploading" | "error";
	  };

/**
 * Internal function for now, used by commit.
 *
 * Can be exposed later to offer fine-tuned progress info
 *
 * CommitOutput is not present for bucket commits
 */
export async function* commitIter(params: CommitParams): AsyncGenerator<CommitProgressEvent, CommitOutput | undefined> {
	const accessToken = checkCredentials(params);
	const repoId = toRepoId(params.repo);

	if (repoId.type === "bucket") {
		return yield* commitIterBucket(params);
	}

	yield { event: "phase", phase: "preuploading" };

	let useXet = params.useXet ?? true;

	const lfsShas = new Map<string, string | null>();

	const abortController = new AbortController();
	const abortSignal = abortController.signal;

	// Polyfill see https://discuss.huggingface.co/t/why-cant-i-upload-a-parquet-file-to-my-dataset-error-o-throwifaborted-is-not-a-function/62245
	if (!abortSignal.throwIfAborted) {
		abortSignal.throwIfAborted = () => {
			if (abortSignal.aborted) {
				throw new DOMException("Aborted", "AbortError");
			}
		};
	}

	if (params.abortSignal) {
		params.abortSignal.addEventListener("abort", () => abortController.abort());
	}

	try {
		const allOperations = (
			await Promise.all(
				params.operations.map(async (operation) => {
					if (operation.operation === "edit") {
						// Convert EditFile operation to a file operation with SplicedBlob
						const splicedBlob = SplicedBlob.create(
							operation.originalContent,
							operation.edits.map((splice) => ({ insert: splice.content, start: splice.start, end: splice.end })),
						);
						return {
							operation: "addOrUpdate" as const,
							path: operation.path,
							content: splicedBlob,
						};
					}

					if (operation.operation !== "addOrUpdate") {
						return operation;
					}

					if (!(operation.content instanceof URL)) {
						/** TS trick to enforce `content` to be a `Blob` */
						return { ...operation, content: operation.content };
					}

					const lazyBlobs = await createBlobs(operation.content, operation.path, {
						fetch: params.fetch,
						maxFolderDepth: params.maxFolderDepth,
					});

					abortSignal?.throwIfAborted();

					return lazyBlobs.map((blob) => ({
						...operation,
						content: blob.blob,
						path: blob.path,
					}));
				}),
			)
		).flat(1);

		const gitAttributes = allOperations.filter(isFileOperation).find((op) => op.path === ".gitattributes")?.content;

		for (const operations of chunk(allOperations.filter(isFileOperation), 100)) {
			const payload: ApiPreuploadRequest = {
				gitAttributes: gitAttributes && (await gitAttributes.text()),
				files: await Promise.all(
					operations.map(async (operation) => ({
						path: operation.path,
						size: operation.content.size,
						sample: base64FromBytes(new Uint8Array(await operation.content.slice(0, 512).arrayBuffer())),
					})),
				),
			};

			abortSignal?.throwIfAborted();

			const res = await (params.fetch ?? fetch)(
				`${params.hubUrl ?? HUB_URL}/api/${repoId.type}s/${repoId.name}/preupload/${encodeURIComponent(
					params.branch ?? "main",
				)}` + (params.isPullRequest ? "?create_pr=1" : ""),
				{
					method: "POST",
					headers: {
						...(accessToken && { Authorization: `Bearer ${accessToken}` }),
						"Content-Type": "application/json",
					},
					body: JSON.stringify(payload),
					signal: abortSignal,
				},
			);

			if (!res.ok) {
				throw await createApiError(res);
			}

			const json: ApiPreuploadResponse = await res.json();

			for (const file of json.files) {
				if (file.uploadMode === "lfs") {
					lfsShas.set(file.path, null);
				}
			}
		}

		yield { event: "phase", phase: "uploadingLargeFiles" };

		for (const operations of chunk(
			allOperations.filter(isFileOperation).filter((op) => lfsShas.has(op.path)),
			100,
		)) {
			const shas = yield* eventToGenerator<
				{ event: "fileProgress"; state: "hashing"; path: string; progress: number },
				string[]
			>((yieldCallback, returnCallback, rejectCallack) => {
				return promisesQueue(
					operations.map((op) => async () => {
						const iterator = sha256(op.content, { useWebWorker: params.useWebWorkers, abortSignal: abortSignal });
						let res: IteratorResult<number, string>;
						do {
							res = await iterator.next();
							if (!res.done) {
								yieldCallback({ event: "fileProgress", path: op.path, progress: res.value, state: "hashing" });
							}
						} while (!res.done);
						const sha = res.value;
						lfsShas.set(op.path, res.value);
						return sha;
					}),
					CONCURRENT_SHAS,
				).then(returnCallback, rejectCallack);
			});

			abortSignal?.throwIfAborted();

			const payload: ApiLfsBatchRequest = {
				operation: "upload",
				// multipart is a custom protocol for HF
				transfers: ["basic", "multipart", ...(useXet ? ["xet" as const] : [])],
				hash_algo: "sha_256",
				...(!params.isPullRequest && {
					ref: {
						name: params.branch ?? "main",
					},
				}),
				objects: operations.map((op, i) => ({
					oid: shas[i],
					size: op.content.size,
				})),
			};

			const res = await (params.fetch ?? fetch)(
				`${params.hubUrl ?? HUB_URL}/${repoId.type === "model" ? "" : repoId.type + "s/"}${
					repoId.name
				}.git/info/lfs/objects/batch`,
				{
					method: "POST",
					headers: {
						...(accessToken && { Authorization: `Bearer ${accessToken}` }),
						Accept: "application/vnd.git-lfs+json",
						"Content-Type": "application/vnd.git-lfs+json",
					},
					body: JSON.stringify(payload),
					signal: abortSignal,
				},
			);

			if (!res.ok) {
				throw await createApiError(res);
			}

			const json: ApiLfsBatchResponse = await res.json();
			const batchRequestId = res.headers.get("X-Request-Id") || undefined;

			const shaToOperation = new Map(operations.map((op, i) => [shas[i], op]));

			if (useXet && json.transfer !== "xet") {
				useXet = false;
			}

			let xetParams: XetTokenParams | null = null;

			if (useXet) {
				// First get all the files that are already uploaded out of the way
				for (const obj of json.objects) {
					const op = shaToOperation.get(obj.oid);
					if (!op) {
						throw new InvalidApiResponseFormatError("Unrequested object ID in response");
					}

					if (obj.error) {
						const errorMessage = `Error while doing LFS batch call for ${operations[shas.indexOf(obj.oid)].path}: ${
							obj.error.message
						}${batchRequestId ? ` - Request ID: ${batchRequestId}` : ""}`;
						throw new HubApiError(res.url, obj.error.code, batchRequestId, errorMessage);
					}

					if (!obj.actions?.upload) {
						// Already uploaded
						yield {
							event: "fileProgress",
							path: op.path,
							progress: 1,
							state: "uploading",
						};
					} else {
						const headers = new Headers(obj.actions.upload.header);

						xetParams = {
							sessionId: headers.get("X-Xet-Session-Id") ?? undefined,
							casUrl: headers.get("X-Xet-Cas-Url") ?? undefined,
							accessToken: headers.get("X-Xet-Access-Token") ?? undefined,
							expiresAt: headers.get("X-Xet-Token-Expiration")
								? new Date(parseInt(headers.get("X-Xet-Token-Expiration") ?? "0") * 1000)
								: undefined,
							refreshWriteTokenUrl: obj.actions.upload.href,
						};
					}
				}
				const source = (async function* () {
					for (const obj of json.objects) {
						const op = shaToOperation.get(obj.oid);
						if (!op || !obj.actions?.upload) {
							continue;
						}
						abortSignal?.throwIfAborted();
						yield { content: op.content, path: op.path, sha256: obj.oid };
					}
				})();
				if (xetParams) {
					const fixedXetParams = xetParams;
					const sources = splitAsyncGenerator(source, 5);
					yield* eventToGenerator((yieldCallback, returnCallback, rejectCallback) =>
						Promise.all(
							sources.map(async function (source) {
								for await (const event of uploadShards(source, {
									fetch: params.fetch,
									accessToken,
									hubUrl: params.hubUrl ?? HUB_URL,
									repo: repoId,
									xetParams: fixedXetParams,
									// todo: maybe leave empty if PR?
									rev: params.branch ?? "main",
									isPullRequest: params.isPullRequest,
									yieldCallback: (event) => yieldCallback({ ...event, state: "uploading" }),
								})) {
									if (event.event === "file") {
										yieldCallback({
											event: "fileProgress" as const,
											path: event.path,
											progress: 1,
											state: "uploading" as const,
										});
									} else if (event.event === "fileProgress") {
										yieldCallback({
											event: "fileProgress" as const,
											path: event.path,
											progress: event.progress,
											state: "uploading" as const,
										});
									}
								}
							}),
						).then(() => returnCallback(undefined), rejectCallback),
					);
				} else {
					// No LFS file to upload
				}
			} else {
				yield* eventToGenerator<CommitProgressEvent, void>((yieldCallback, returnCallback, rejectCallback) => {
					return promisesQueueStreaming(
						json.objects.map((obj) => async () => {
							const op = shaToOperation.get(obj.oid);

							if (!op) {
								throw new InvalidApiResponseFormatError("Unrequested object ID in response");
							}

							abortSignal?.throwIfAborted();

							if (obj.error) {
								const errorMessage = `Error while doing LFS batch call for ${operations[shas.indexOf(obj.oid)].path}: ${
									obj.error.message
								}${batchRequestId ? ` - Request ID: ${batchRequestId}` : ""}`;
								throw new HubApiError(res.url, obj.error.code, batchRequestId, errorMessage);
							}
							if (!obj.actions?.upload) {
								// Already uploaded
								yieldCallback({
									event: "fileProgress",
									path: op.path,
									progress: 1,
									state: "uploading",
								});
								return;
							}
							yieldCallback({
								event: "fileProgress",
								path: op.path,
								progress: 0,
								state: "uploading",
							});
							const content = op.content;

							const header = obj.actions.upload.header;
							if (header?.chunk_size) {
								const chunkSize = parseInt(header.chunk_size);

								// multipart upload
								// parts are in upload.header['00001'] to upload.header['99999']

								const completionUrl = obj.actions.upload.href;
								const parts = Object.keys(header).filter((key) => /^[0-9]+$/.test(key));

								if (parts.length !== Math.ceil(content.size / chunkSize)) {
									throw new Error("Invalid server response to upload large LFS file, wrong number of parts");
								}

								const completeReq: ApiLfsCompleteMultipartRequest = {
									oid: obj.oid,
									parts: parts.map((part) => ({
										partNumber: +part,
										etag: "",
									})),
								};

								// Defined here so that it's not redefined at each iteration (and the caller can tell it's for the same file)
								const progressCallback = (progress: number) =>
									yieldCallback({ event: "fileProgress", path: op.path, progress, state: "uploading" });

								await promisesQueueStreaming(
									parts.map((part) => async () => {
										abortSignal?.throwIfAborted();

										const index = parseInt(part) - 1;
										const slice = content.slice(index * chunkSize, (index + 1) * chunkSize);

										const res = await (params.fetch ?? fetch)(header[part], {
											method: "PUT",
											/** Unfortunately, browsers don't support our inherited version of Blob in fetch calls */
											body: slice instanceof WebBlob && isFrontend ? await slice.arrayBuffer() : slice,
											signal: abortSignal,
											...({
												progressHint: {
													path: op.path,
													part: index,
													numParts: parts.length,
													progressCallback,
												},
												// eslint-disable-next-line @typescript-eslint/no-explicit-any
											} as any),
										});

										if (!res.ok) {
											throw await createApiError(res, {
												requestId: batchRequestId,
												message: `Error while uploading part ${part} of ${
													operations[shas.indexOf(obj.oid)].path
												} to LFS storage`,
											});
										}

										const eTag = res.headers.get("ETag");

										if (!eTag) {
											throw new Error("Cannot get ETag of part during multipart upload");
										}

										completeReq.parts[Number(part) - 1].etag = eTag;
									}),
									MULTIPART_PARALLEL_UPLOAD,
								);

								abortSignal?.throwIfAborted();

								const res = await (params.fetch ?? fetch)(completionUrl, {
									method: "POST",
									body: JSON.stringify(completeReq),
									headers: {
										Accept: "application/vnd.git-lfs+json",
										"Content-Type": "application/vnd.git-lfs+json",
									},
									signal: abortSignal,
								});

								if (!res.ok) {
									throw await createApiError(res, {
										requestId: batchRequestId,
										message: `Error completing multipart upload of ${
											operations[shas.indexOf(obj.oid)].path
										} to LFS storage`,
									});
								}

								yieldCallback({
									event: "fileProgress",
									path: op.path,
									progress: 1,
									state: "uploading",
								});
							} else {
								const res = await (params.fetch ?? fetch)(obj.actions.upload.href, {
									method: "PUT",
									headers: {
										...(batchRequestId ? { "X-Request-Id": batchRequestId } : undefined),
									},
									/** Unfortunately, browsers don't support our inherited version of Blob in fetch calls */
									body: content instanceof WebBlob && isFrontend ? await content.arrayBuffer() : content,
									signal: abortSignal,
									...({
										progressHint: {
											path: op.path,
											progressCallback: (progress: number) =>
												yieldCallback({
													event: "fileProgress",
													path: op.path,
													progress,
													state: "uploading",
												}),
										},
										// eslint-disable-next-line @typescript-eslint/no-explicit-any
									} as any),
								});

								if (!res.ok) {
									throw await createApiError(res, {
										requestId: batchRequestId,
										message: `Error while uploading ${operations[shas.indexOf(obj.oid)].path} to LFS storage`,
									});
								}

								yieldCallback({
									event: "fileProgress",
									path: op.path,
									progress: 1,
									state: "uploading",
								});
							}
						}),
						CONCURRENT_LFS_UPLOADS,
					).then(returnCallback, rejectCallback);
				});
			}
		}

		abortSignal?.throwIfAborted();

		yield { event: "phase", phase: "committing" };

		return yield* eventToGenerator<CommitProgressEvent, CommitOutput>(
			async (yieldCallback, returnCallback, rejectCallback) =>
				(params.fetch ?? fetch)(
					`${params.hubUrl ?? HUB_URL}/api/${repoId.type}s/${repoId.name}/commit/${encodeURIComponent(
						params.branch ?? "main",
					)}` + (params.isPullRequest ? "?create_pr=1" : ""),
					{
						method: "POST",
						headers: {
							...(accessToken && { Authorization: `Bearer ${accessToken}` }),
							"Content-Type": "application/x-ndjson",
						},
						body: [
							{
								key: "header",
								value: {
									summary: params.title,
									description: params.description,
									parentCommit: params.parentCommit,
								} satisfies ApiCommitHeader,
							},
							...((await Promise.all(
								allOperations.map((operation) => {
									if (isFileOperation(operation)) {
										const sha = lfsShas.get(operation.path);
										if (sha) {
											return {
												key: "lfsFile",
												value: {
													path: operation.path,
													algo: "sha256",
													size: operation.content.size,
													oid: sha,
												} satisfies ApiCommitLfsFile,
											};
										}
									}

									return convertOperationToNdJson(operation);
								}),
							)) satisfies ApiCommitOperation[]),
						]
							.map((x) => JSON.stringify(x))
							.join("\n"),
						signal: abortSignal,
						...({
							progressHint: {
								progressCallback: (progress: number) => {
									// For now, we display equal progress for all files
									// We could compute the progress based on the size of `convertOperationToNdJson` for each of the files instead
									for (const op of allOperations) {
										if (isFileOperation(op) && !lfsShas.has(op.path)) {
											yieldCallback({
												event: "fileProgress",
												path: op.path,
												progress,
												state: "uploading",
											});
										}
									}
								},
							},
							// eslint-disable-next-line @typescript-eslint/no-explicit-any
						} as any),
					},
				)
					.then(async (res) => {
						if (!res.ok) {
							throw await createApiError(res);
						}

						const json = await res.json();

						returnCallback({
							pullRequestUrl: json.pullRequestUrl,
							commit: {
								oid: json.commitOid,
								url: json.commitUrl,
							},
							hookOutput: json.hookOutput,
						});
					})
					.catch(rejectCallback),
		);
	} catch (err) {
		// For parallel requests, cancel them all if one fails
		abortController.abort();
		throw err;
	}
}

export async function* commitIterBucket(params: CommitParams): AsyncGenerator<CommitProgressEvent> {
	const accessToken = checkCredentials(params);
	const repoId = toRepoId(params.repo);

	if (params.useXet === false) {
		throw new Error("useXet must be true or undefined for buckets");
	}

	const abortController = new AbortController();
	const abortSignal = abortController.signal;

	// Polyfill see https://discuss.huggingface.co/t/why-cant-i-upload-a-parquet-file-to-my-dataset-error-o-throwifaborted-is-not-a-function/62245
	if (!abortSignal.throwIfAborted) {
		abortSignal.throwIfAborted = () => {
			if (abortSignal.aborted) {
				throw new DOMException("Aborted", "AbortError");
			}
		};
	}

	if (params.abortSignal) {
		params.abortSignal.addEventListener("abort", () => abortController.abort());
	}

	try {
		const allOperations = (
			await Promise.all(
				params.operations.map(async (operation) => {
					if (operation.operation === "edit") {
						// Convert EditFile operation to a file operation with SplicedBlob
						const splicedBlob = SplicedBlob.create(
							operation.originalContent,
							operation.edits.map((splice) => ({ insert: splice.content, start: splice.start, end: splice.end })),
						);
						return {
							operation: "addOrUpdate" as const,
							path: operation.path,
							content: splicedBlob,
						};
					}

					if (operation.operation !== "addOrUpdate") {
						return operation;
					}

					if (!(operation.content instanceof URL)) {
						/** TS trick to enforce `content` to be a `Blob` */
						return { ...operation, content: operation.content };
					}

					const lazyBlobs = await createBlobs(operation.content, operation.path, {
						fetch: params.fetch,
						maxFolderDepth: params.maxFolderDepth,
					});

					abortSignal?.throwIfAborted();

					return lazyBlobs.map((blob) => ({
						...operation,
						content: blob.blob,
						path: blob.path,
					}));
				}),
			)
		).flat(1);

		yield { event: "phase", phase: "uploadingLargeFiles" };

		for (const operations of chunk(allOperations.filter(isFileOperation), 100)) {
			const xetHashes = new Map<string, string>();
			abortSignal?.throwIfAborted();

			// First get all the files that are already uploaded out of the way

			const source = (async function* () {
				for (const operation of operations) {
					abortSignal?.throwIfAborted();
					yield { content: operation.content, path: operation.path };
				}
			})();

			const xetParams: XetTokenParams = {
				sessionId: crypto.randomUUID(),
				refreshWriteTokenUrl: `${params.hubUrl ?? HUB_URL}/api/${repoId.type}s/${repoId.name}/xet-write-token`,
			};
			const sources = splitAsyncGenerator(source, 5);
			yield* eventToGenerator((yieldCallback, returnCallback, rejectCallback) =>
				Promise.all(
					sources.map(async function (source) {
						for await (const event of uploadShards(source, {
							fetch: params.fetch,
							accessToken,
							hubUrl: params.hubUrl ?? HUB_URL,
							repo: repoId,
							xetParams,
							rev: params.branch ?? "main",
							yieldCallback: (event) => yieldCallback({ ...event, state: "uploading" }),
						})) {
							if (event.event === "file") {
								yieldCallback({
									event: "fileProgress" as const,
									path: event.path,
									progress: 1,
									state: "uploading" as const,
								});
								xetHashes.set(event.path, event.xetHash);
							} else if (event.event === "fileProgress") {
								yieldCallback({
									event: "fileProgress" as const,
									path: event.path,
									progress: event.progress,
									state: "uploading" as const,
								});
							}
						}
					}),
				).then(() => returnCallback(undefined), rejectCallback),
			);

			const resp = await (params.fetch ?? fetch)(
				`${params.hubUrl ?? HUB_URL}/api/${repoId.type}s/${repoId.name}/batch`,
				{
					method: "POST",
					headers: {
						...(accessToken && { Authorization: `Bearer ${accessToken}` }),
						"Content-Type": "application/x-ndjson",
					},
					body: [...xetHashes.entries()]
						.map(([path, xetHash]) =>
							JSON.stringify({
								type: "addFile",
								path,
								xetHash,
							}),
						)
						.join("\n"),
					signal: abortSignal,
				},
			);

			if (!resp.ok && resp.status !== 422) {
				throw await createApiError(resp);
			}

			const json = (await resp.json()) as ApiBucketBatchResponse;

			for (const failed of json.failed) {
				yield {
					event: "fileProgress",
					path: failed.path,
					progress: 0,
					state: "error",
				};
			}
		}

		abortSignal?.throwIfAborted();

		const deletedOperations = allOperations.filter((operation) => operation.operation === "delete");

		if (deletedOperations.length > 0) {
			const resp = await (params.fetch ?? fetch)(
				`${params.hubUrl ?? HUB_URL}/api/${repoId.type}s/${repoId.name}/batch`,
				{
					method: "POST",
					headers: {
						...(accessToken && { Authorization: `Bearer ${accessToken}` }),
						"Content-Type": "application/x-ndjson",
					},
					body: deletedOperations
						.map((operation) =>
							JSON.stringify({
								type: "deleteFile",
								path: operation.path,
							}),
						)
						.join("\n"),
					signal: abortSignal,
				},
			);

			if (!resp.ok) {
				throw await createApiError(resp);
			}

			const json = await resp.json();

			if (json.failed.length > 0) {
				const failedPaths = json.failed.slice(0, 5).map((f: { path: string }) => f.path);
				throw new Error(
					`Failed to delete ${json.failed.length} file(s): ${failedPaths.join(", ")}${json.failed.length > 5 ? "..." : ""}, request ID: ${resp.headers.get("X-Request-Id")}`,
				);
			}
		}

		abortSignal?.throwIfAborted();
	} catch (err) {
		// For parallel requests, cancel them all if one fails
		abortController.abort();
		throw err;
	}
}

/**
 * @returns undefined for bucket uploads, CommitOutput otherwise
 */
export async function commit(params: CommitParams): Promise<CommitOutput | undefined> {
	const iterator = commitIter(params);
	const failedPaths: string[] = [];
	let failedCount = 0;
	let res = await iterator.next();
	while (!res.done) {
		if (res.value.event === "fileProgress" && res.value.state === "error") {
			failedCount++;
			if (failedPaths.length < 5) {
				failedPaths.push(res.value.path);
			}
		}
		res = await iterator.next();
	}
	if (failedCount > 0) {
		throw new Error(
			`Failed to upload ${failedCount} file(s): ${failedPaths.join(", ")}${failedCount > 5 ? "..." : ""}`,
		);
	}
	return res.value;
}

async function convertOperationToNdJson(operation: CommitBlobOperation): Promise<ApiCommitOperation> {
	switch (operation.operation) {
		case "addOrUpdate": {
			// todo: handle LFS
			return {
				key: "file",
				value: {
					content: base64FromBytes(new Uint8Array(await operation.content.arrayBuffer())),
					path: operation.path,
					encoding: "base64",
				},
			};
		}
		// case "rename": {
		// 	// todo: detect when remote file is already LFS, and in that case rename as LFS
		// 	return {
		// 		key:   "file",
		// 		value: {
		// 			content: operation.content,
		// 			path:    operation.path,
		// 			oldPath: operation.oldPath
		// 		}
		// 	};
		// }
		case "delete": {
			return {
				key: "deletedFile",
				value: {
					path: operation.path,
				},
			};
		}
		case "edit": {
			// Note: By the time we get here, splice operations should have been converted to addOrUpdate operations with SplicedBlob
			// But we handle this case for completeness
			throw new Error(
				"Edit operations should be converted to addOrUpdate operations before reaching convertOperationToNdJson",
			);
		}
		default:
			throw new TypeError("Unknown operation: " + (operation as { operation: string }).operation);
	}
}