Skip to content

Commit 52f9c02

Browse files
committed
fix
1 parent 7c0ca44 commit 52f9c02

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

packages/hub/src/lib/commit.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
162162
return { ...operation, content: operation.content };
163163
}
164164

165-
const lazyBlobs = await createBlobs(operation.content, {
165+
const lazyBlobs = await createBlobs(operation.content, operation.path, {
166166
fetch: params.fetch,
167167
maxFolderDepth: params.maxFolderDepth,
168168
});
@@ -171,7 +171,8 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
171171

172172
return lazyBlobs.map((blob) => ({
173173
...operation,
174-
content: blob,
174+
content: blob.blob,
175+
path: blob.path,
175176
}));
176177
})
177178
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ describe("deleteFiles", () => {
7878
});
7979
}
8080
});
81-
}, 10_000);
81+
});

packages/hub/src/utils/createBlobs.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ import { isFrontend } from "./isFrontend";
1212
* From the frontend:
1313
* - support http resources with absolute or relative URLs
1414
*/
15-
export async function createBlobs(url: URL, opts?: { fetch?: typeof fetch; maxFolderDepth?: number }): Promise<Blob[]> {
15+
export async function createBlobs(
16+
url: URL,
17+
destPath: string,
18+
opts?: { fetch?: typeof fetch; maxFolderDepth?: number }
19+
): Promise<Array<{ path: string; blob: Blob }>> {
1620
if (url.protocol === "http:" || url.protocol === "https:") {
17-
return [await WebBlob.create(url, { fetch: opts?.fetch })];
21+
const blob = await WebBlob.create(url, { fetch: opts?.fetch });
22+
return [{ path: destPath, blob }];
1823
}
1924

2025
if (isFrontend) {
@@ -26,7 +31,17 @@ export async function createBlobs(url: URL, opts?: { fetch?: typeof fetch; maxFo
2631
const { subPaths } = await import("./sub-paths");
2732
const paths = await subPaths(url, opts?.maxFolderDepth);
2833

29-
return Promise.all(paths.map((path) => FileBlob.create(path)));
34+
if (paths.length === 1 && paths[0].relativePath === ".") {
35+
const blob = await FileBlob.create(url);
36+
return [{ path: destPath, blob }];
37+
}
38+
39+
return Promise.all(
40+
paths.map(async (path) => ({
41+
path: `${destPath}/${path.relativePath}`.replace(/\/[.]$/, "").replaceAll("//", "/"),
42+
blob: await FileBlob.create(new URL(path.path)),
43+
}))
44+
);
3045
}
3146

3247
throw new TypeError(`Unsupported URL protocol "${url.protocol}"`);

packages/hub/src/utils/sub-paths.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@ import { readdir, stat } from "node:fs/promises";
33
/**
44
* Recursively retrieves all sub-paths of a given directory up to a specified depth.
55
*/
6-
export async function subPaths(path: URL, maxDepth = 10): Promise<URL[]> {
6+
export async function subPaths(
7+
path: URL,
8+
maxDepth = 10
9+
): Promise<
10+
Array<{
11+
path: URL;
12+
relativePath: string;
13+
}>
14+
> {
715
const state = await stat(path);
816
if (!state.isDirectory()) {
9-
return [path];
17+
return [{ path, relativePath: "." }];
1018
}
1119

1220
const files = await readdir(path, { withFileTypes: true });
13-
const ret: URL[] = [];
21+
const ret: Array<{ path: URL; relativePath: string }> = [];
1422
for (const file of files) {
1523
const filePath = new URL(file.name, path);
1624
if (file.isDirectory()) {
17-
ret.push(...(await subPaths(filePath, maxDepth - 1)));
25+
ret.push(
26+
...(await subPaths(filePath, maxDepth - 1)).map((subPath) => ({
27+
...subPath,
28+
relativePath: `${file.name}/${subPath.relativePath}`,
29+
}))
30+
);
1831
} else {
19-
ret.push(filePath);
32+
ret.push({ path: filePath, relativePath: file.name });
2033
}
2134
}
2235

0 commit comments

Comments
 (0)