diff --git a/src/drivers/fs-lite.ts b/src/drivers/fs-lite.ts index 87e79171..a7ebbb31 100644 --- a/src/drivers/fs-lite.ts +++ b/src/drivers/fs-lite.ts @@ -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) { diff --git a/src/drivers/fs.ts b/src/drivers/fs.ts index a3bce53b..b2bcc5ed 100644 --- a/src/drivers/fs.ts +++ b/src/drivers/fs.ts @@ -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) { diff --git a/src/drivers/utils/node-fs.ts b/src/drivers/utils/node-fs.ts index 3a38c079..89f3035b 100644 --- a/src/drivers/utils/node-fs.ts +++ b/src/drivers/utils/node-fs.ts @@ -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 []; @@ -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); diff --git a/test/drivers/fs-lite.test.ts b/test/drivers/fs-lite.test.ts index 522bb2da..f4f6acf3 100644 --- a/test/drivers/fs-lite.test.ts +++ b/test/drivers/fs-lite.test.ts @@ -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", + ]); + }); }, }); }); diff --git a/test/drivers/fs.test.ts b/test/drivers/fs.test.ts index bb14a80a..43131a47 100644 --- a/test/drivers/fs.test.ts +++ b/test/drivers/fs.test.ts @@ -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"]); + }); }, }); });