Skip to content

Commit

Permalink
feat(fs, fs-lite): native maxDepth support (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored Jan 2, 2025
1 parent 7fd4933 commit cc0b0ca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/drivers/fs-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
}
return unlink(r(key));
},
getKeys() {
return readdirRecursive(r("."), opts.ignore);
getKeys(_base, topts) {
return readdirRecursive(r("."), opts.ignore, topts?.maxDepth);
},
async clear() {
if (opts.readOnly || opts.noClear) {
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
}
return unlink(r(key));
},
getKeys() {
return readdirRecursive(r("."), anymatch(opts.ignore || []));
getKeys(_base, topts) {
return readdirRecursive(
r("."),
anymatch(opts.ignore || []),
topts?.maxDepth
);
},
async clear() {
if (opts.readOnly || opts.noClear) {
Expand Down
13 changes: 10 additions & 3 deletions src/drivers/utils/node-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export async function ensuredir(dir: string) {

export async function readdirRecursive(
dir: string,
ignore?: (p: string) => boolean
ignore?: (p: string) => boolean,
maxDepth?: number
) {
if (ignore && ignore(dir)) {
return [];
Expand All @@ -59,8 +60,14 @@ export async function readdirRecursive(
entries.map(async (entry) => {
const entryPath = resolve(dir, entry.name);
if (entry.isDirectory()) {
const dirFiles = await readdirRecursive(entryPath, ignore);
files.push(...dirFiles.map((f) => entry.name + "/" + f));
if (maxDepth === undefined || maxDepth > 0) {
const dirFiles = await readdirRecursive(
entryPath,
ignore,
maxDepth === undefined ? undefined : maxDepth - 1
);
files.push(...dirFiles.map((f) => entry.name + "/" + f));
}
} else {
if (!(ignore && ignore(entry.name))) {
files.push(entry.name);
Expand Down
34 changes: 34 additions & 0 deletions test/drivers/fs-lite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ describe("drivers: fs-lite", () => {
await ctx.storage.setItem("s1/te..st..js", "ok");
expect(await ctx.storage.getItem("s1/te..st..js")).toBe("ok");
});

it("natively supports maxDepth in getKeys", async () => {
await ctx.storage.setItem("file0.md", "boop");
await ctx.storage.setItem("depth-test/file1.md", "boop");
await ctx.storage.setItem("depth-test/depth0/file2.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file3.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file4.md", "boop");

expect(
(
await ctx.driver.getKeys("", {
maxDepth: 0,
})
).sort()
).toMatchObject(["file0.md"]);
expect(
(
await ctx.driver.getKeys("", {
maxDepth: 1,
})
).sort()
).toMatchObject(["depth-test/file1.md", "file0.md"]);
expect(
(
await ctx.driver.getKeys("", {
maxDepth: 2,
})
).sort()
).toMatchObject([
"depth-test/depth0/file2.md",
"depth-test/file1.md",
"file0.md",
]);
});
},
});
});
23 changes: 23 additions & 0 deletions test/drivers/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ describe("drivers: fs", () => {
await ctx.storage.setItem("s1/te..st..js", "ok");
expect(await ctx.storage.getItem("s1/te..st..js")).toBe("ok");
});

it("natively supports maxDepth in getKeys", async () => {
await ctx.storage.setItem("depth-test/file0.md", "boop");
await ctx.storage.setItem("depth-test/depth0/file1.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file2.md", "boop");
await ctx.storage.setItem("depth-test/depth0/depth1/file3.md", "boop");

expect(
(
await ctx.driver.getKeys("", {
maxDepth: 1,
})
).sort()
).toMatchObject(["depth-test/file0.md"]);

expect(
(
await ctx.driver.getKeys("", {
maxDepth: 2,
})
).sort()
).toMatchObject(["depth-test/depth0/file1.md", "depth-test/file0.md"]);
});
},
});
});

0 comments on commit cc0b0ca

Please sign in to comment.