8000 💥 Do not fetch last commit by default in listFiles (#151) · huggingface/huggingface.js@ef71d2b · GitHub
[go: up one dir, main page]

Skip to content 65F9

Commit ef71d2b

Browse files
authored
💥 Do not fetch last commit by default in listFiles (#151)
1 parent 9e7aef6 commit ef71d2b

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,67 @@ describe("listFiles", () => {
1515

1616
const files: ListFileEntry[] = [];
1717

18+
for await (const entry of cursor) {
19+
files.push(entry);
20+
}
21+
22+
assert.deepStrictEqual(files, [
23+
{
24+
oid: "dc08351d4dc0732d9c8af04070ced089b201ce2f",
25+
path: ".gitattributes",
26+
size: 345,
27+
type: "file",
28+
},
29+
{
30+
oid: "fca794a5f07ff8f963fe8b61e3694b0fb7f955df",
31+
path: "config.json",
32+
size: 313,
33+
type: "file",
34+
},
35+
{
36+
lfs: {
37+
oid: "097417381d6c7230bd9e3557456d726de6e83245ec8b24f529f60198a67b203a",
38+
size: 440473133,
39+
pointerSize: 134,
40+
},
41+
oid: "ba5d19791be1dd7992e33bd61f20207b0f7f50a5",
42+
path: "pytorch_model.bin",
43+
size: 440473133,
44+
type: "file",
45+
},
46+
{
47+
lfs: {
48+
oid: "a7a17d6d844b5de815ccab5f42cad6d24496db3850a2a43d8258221018ce87d2",
49+
size: 536063208,
50+
pointerSize: 134,
51+
},
52+
oid: "9eb98c817f04b051b3bcca591bcd4e03cec88018",
53+
path: "tf_model.h5",
54+
size: 536063208,
55+
type: "file",
56+
},
57+
{
58+
oid: "fb140275c155a9c7c5a3b3e0e77a9e839594a938",
59+
path: "vocab.txt",
60+
size: 231508,
61+
type: "file",
62+
},
63+
]);
64+
});
65+
66+
it("should fetch the list of files from the repo, including last commit", async () => {
67+
const cursor = listFiles({
68+
repo: {
69+
name: "bert-base-uncased",
70+
type: "model",
71+
},
72+
revision: "dd4bc8b21efa05ec961e3efc4ee5e3832a3679c7",
73+
expand: true,
74+
hubUrl: "https://huggingface.co",
75+
});
76+
77+
const files: ListFileEntry[] = [];
78+
1879
for await (const entry of cursor) {
1980
delete entry.security; // flaky
2081
files.push(entry);

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ export interface ListFileEntry {
1717
/** Size of the raw pointer file, 100~200 bytes */
1818
pointerSize: number;
1919
};
20-
lastCommit: {
20+
/**
21+
* Only fetched if `expand` is set to `true` in the `listFiles` call.
22+
*/
23+
lastCommit?: {
2124
date: string;
2225
id: string;
2326
title: string;
24-
} | null;
27+
};
28+
/**
29+
* Only fetched if `expand` is set to `true` in the `listFiles` call.
30+
*/
2531
security?: unknown;
2632
}
2733

@@ -40,6 +46,10 @@ export async function* listFiles(params: {
4046
* files in the repo.
4147
*/
4248
path?: string;
49+
/**
50+
* Fetch `lastCommit` and `securityStatus` for each file.
51+
*/
52+
expand?: boolean;
4353
revision?: string;
4454
credentials?: Credentials;
4555
hubUrl?: string;
@@ -48,7 +58,7 @@ export async function* listFiles(params: {
4858
const repoId = toRepoId(params.repo);
4959
let url: string | undefined = `${params.hubUrl || HUB_URL}/api/${repoId.type}s/${repoId.name}/tree/${
5060
params.revision || "main"
51-
}${params.path ? "/" + params.path : ""}${params.recursive ? "?recursive=true" : ""}`;
61+
}${params.path ? "/" + params.path : ""}?recursive=${!!params.recursive}&expand=${!!params.expand}`;
5262

5363
while (url) {
5464
const res: Response = await fetch(url, {

‎packages/hub/src/types/api/api-index-tree.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export interface ApiIndexTreeEntry {
99
/** Size of the raw pointer file, 100~200 bytes */
1010
pointerSize: number;
1111
};
12-
lastCommit: {
12+
lastCommit?: {
1313
date: string;
1414
id: string;
1515
title: string;
16-
} | null;
16+
};
1717
security?: ApiFileScanResult;
1818
}
1919

0 commit comments

Comments
 (0)
0