Skip to content

Commit c7cd12c

Browse files
committed
👽️ Update listFiles to HF's updated API
1 parent 9e61d34 commit c7cd12c

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

packages/hub/src/lib/list-files.spec.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ describe("listFiles", () => {
2222
assert.deepStrictEqual(files, [
2323
{
2424
lastCommit: {
25-
author: {
26-
date: "2018-11-14T23:35:08.000Z",
27-
},
28-
id: "504939aa53e8ce310dba3dd2296dbe266c575de4",
29-
subject: "initial commit",
25+
date: "2018-11-14T23:35:08.000Z",
26+
id: "504939aa53e8ce310dba3dd2296dbe266c575de4",
27+
title: "initial commit",
3028
},
29+
oid: "dc08351d4dc0732d9c8af04070ced089b201ce2f",
3130
path: ".gitattributes",
3231
security: {
3332
avScan: {
@@ -45,12 +44,11 @@ describe("listFiles", () => {
4544
},
4645
{
4746
lastCommit: {
48-
author: {
49-
date: "2019-06-18T09:06:51.000Z",
50-
},
51-
id: "bb3c1c3256d2598217df9889a14a2e811587891d",
52-
subject: "Update config.json",
47+
date: "2019-06-18T09:06:51.000Z",
48+
id: "bb3c1c3256d2598217df9889a14a2e811587891d",
49+
title: "Update config.json",
5350
},
51+
oid: "fca794a5f07ff8f963fe8b61e3694b0fb7f955df",
5452
path: "config.json",
5553
security: {
5654
avScan: {
@@ -68,16 +66,16 @@ describe("listFiles", () => {
6866
},
6967
{
7068
lastCommit: {
71-
author: {
72-
date: "2019-06-18T09:06:34.000Z",
73-
},
74-
id: "3d2477d72b675a999d1b13ca822aaaf4908634ad",
75-
subject: "Update pytorch_model.bin",
69+
date: "2019-06-18T09:06:34.000Z",
70+
id: "3d2477d72b675a999d1b13ca822aaaf4908634ad",
71+
title: "Update pytorch_model.bin",
7672
},
7773
lfs: {
78-
oid: "097417381d6c7230bd9e3557456d726de6e83245ec8b24f529f60198a67b203a",
79-
size: 440473133,
74+
oid: "097417381d6c7230bd9e3557456d726de6e83245ec8b24f529f60198a67b203a",
75+
size: 440473133,
76+
pointerSize: 134,
8077
},
78+
oid: "ba5d19791be1dd7992e33bd61f20207b0f7f50a5",
8179
path: "pytorch_model.bin",
8280
security: {
8381
avScan: {
@@ -114,16 +112,16 @@ describe("listFiles", () => {
114112
},
115113
{
116114
lastCommit: {
117-
author: {
118-
date: "2019-09-23T19:48:44.000Z",
119-
},
120-
id: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
121-
subject: "Update tf_model.h5",
115+
date: "2019-09-23T19:48:44.000Z",
116+
id: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
117+
title: "Update tf_model.h5",
122118
},
123119
lfs: {
124-
oid: "a7a17d6d844b5de815ccab5f42cad6d24496db3850a2a43d8258221018ce87d2",
125-
size: 536063208,
120+
oid: "a7a17d6d844b5de815ccab5f42cad6d24496db3850a2a43d8258221018ce87d2",
121+
size: 536063208,
122+
pointerSize: 134,
126123
},
124+
oid: "9eb98c817f04b051b3bcca591bcd4e03cec88018",
127125
path: "tf_model.h5",
128126
security: {
129127
avScan: {
@@ -141,12 +139,11 @@ describe("listFiles", () => {
141139
},
142140
{
143141
lastCommit: {
144-
author: {
145-
date: "2018-11-14T23:35:08.000Z",
146-
},
147-
id: "2f07d813ca87c8c709147704c87210359ccf2309",
148-
subject: "Update vocab.txt",
142+
date: "2018-11-14T23:35:08.000Z",
143+
id: "2f07d813ca87c8c709147704c87210359ccf2309",
144+
title: "Update vocab.txt",
149145
},
146+
oid: "fb140275c155a9c7c5a3b3e0e77a9e839594a938",
150147
path: "vocab.txt",
151148
security: {
152149
avScan: {

packages/hub/src/lib/list-files.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { HUB_URL } from "../consts";
22
import { createApiError } from "../error";
33
import type { Credentials, RepoId } from "../types";
44
import type { ApiIndexTreeEntryData } from "../types/api";
5+
import { parseLinkHeader } from "../utils";
56

67
export type ListFileEntry = ApiIndexTreeEntryData;
78

@@ -29,7 +30,7 @@ export async function* listFiles(params: {
2930
}${params.path ? "/" + params.path : ""}${params.recursive ? "?recursive=true" : ""}`;
3031

3132
while (url) {
32-
const res = await fetch(url, {
33+
const res: Response = await fetch(url, {
3334
headers: {
3435
accept: "application/json",
3536
...(params.credentials ? { Authorization: `Bearer ${params.credentials.accessToken}` } : undefined),
@@ -40,16 +41,14 @@ export async function* listFiles(params: {
4041
throw createApiError(res);
4142
}
4243

43-
const json: { items: ApiIndexTreeEntryData[]; nextUrl?: string } = await res.json();
44+
const items: ApiIndexTreeEntryData[] = await res.json();
4445

45-
for (const item of json.items) {
46+
for (const item of items) {
4647
yield item;
4748
}
4849

49-
url = json.nextUrl
50-
? json.nextUrl?.startsWith(params.hubUrl ?? HUB_URL)
51-
? json.nextUrl
52-
: `${params.hubUrl ?? HUB_URL}${json.nextUrl}`
53-
: undefined;
50+
const linkHeader = res.headers.get("Link");
51+
52+
url = linkHeader ? parseLinkHeader(linkHeader).next : undefined;
5453
}
5554
}

packages/hub/src/types/api.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ export interface ApiIndexTreeEntryData {
210210
size: number;
211211
path: string;
212212
lfs?: {
213-
oid: string;
214-
size: number;
213+
oid: string;
214+
size: number;
215+
/** Size of the raw pointer file, 100~200 bytes */
216+
pointerSize: number;
215217
};
216218
lastCommit: {
217219
author: {

packages/hub/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { promisesQueueStreaming } from "./promisesQueueStreaming";
66
export { promisesQueue } from "./promisesQueue";
77
export { sha256 } from "./sha256";
88
export { randomUUID } from "./randomUUID";
9+
export { parseLinkHeader } from "./parseLinkHeader";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Parse Link HTTP header, eg `<https://huggingface.co/api/datasets/bigscience/P3/tree/main?recursive=1&cursor=...>; rel="next"`
3+
*/
4+
export function parseLinkHeader(header: string): Record<string, string> {
5+
const regex = /<(https?:[/][/][^>]+)>;\s+rel="([^"]+)"/g;
6+
7+
return Object.fromEntries([...header.matchAll(regex)].map(([, url, rel]) => [rel, url]));
8+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy