mirror of
https://github.com/unjs/unstorage.git
synced 2026-01-09 16:48:36 -05:00
perf: skip maxDepth filtering if natively supported (#560)
This commit is contained in:
@@ -40,6 +40,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
|
||||
return {
|
||||
name: DRIVER_NAME,
|
||||
options: opts,
|
||||
flags: {
|
||||
maxDepth: true,
|
||||
},
|
||||
hasItem(key) {
|
||||
return existsSync(r(key));
|
||||
},
|
||||
|
||||
@@ -59,6 +59,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
|
||||
return {
|
||||
name: DRIVER_NAME,
|
||||
options: opts,
|
||||
flags: {
|
||||
maxDepth: true,
|
||||
},
|
||||
hasItem(key) {
|
||||
return existsSync(r(key));
|
||||
},
|
||||
|
||||
@@ -348,7 +348,11 @@ export function createStorage<T extends StorageValue>(
|
||||
const mounts = getMounts(base, true);
|
||||
let maskedMounts: string[] = [];
|
||||
const allKeys = [];
|
||||
let allMountsSupportMaxDepth = true;
|
||||
for (const mount of mounts) {
|
||||
if (!mount.driver.flags?.maxDepth) {
|
||||
allMountsSupportMaxDepth = false;
|
||||
}
|
||||
const rawKeys = await asyncCall(
|
||||
mount.driver.getKeys,
|
||||
mount.relativeBase,
|
||||
@@ -368,10 +372,11 @@ export function createStorage<T extends StorageValue>(
|
||||
...maskedMounts.filter((p) => !p.startsWith(mount.mountpoint)),
|
||||
];
|
||||
}
|
||||
const shouldFilterByDepth =
|
||||
opts.maxDepth !== undefined && !allMountsSupportMaxDepth;
|
||||
return allKeys.filter(
|
||||
(key) =>
|
||||
(opts.maxDepth === undefined ||
|
||||
filterKeyByDepth(key, opts.maxDepth)) &&
|
||||
(!shouldFilterByDepth || filterKeyByDepth(key, opts.maxDepth)) &&
|
||||
filterKeyByBase(key, base)
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { resolve } from "node:path";
|
||||
import {
|
||||
createStorage,
|
||||
snapshot,
|
||||
@@ -6,6 +7,7 @@ import {
|
||||
prefixStorage,
|
||||
} from "../src";
|
||||
import memory from "../src/drivers/memory";
|
||||
import fs from "../src/drivers/fs";
|
||||
|
||||
const data = {
|
||||
"etc:conf": "test",
|
||||
@@ -222,4 +224,29 @@ describe("Regression", () => {
|
||||
await pStorage.remove("y");
|
||||
expect(await pStorage.has("y")).toBe(false);
|
||||
});
|
||||
|
||||
it("getKeys supports maxDepth with mixed native support", async () => {
|
||||
const base = resolve(__dirname, "tmp/fs");
|
||||
const mainStorage = memory();
|
||||
const secondaryStorage = fs({ base });
|
||||
const storage = createStorage({ driver: mainStorage });
|
||||
|
||||
storage.mount("/storage_b", secondaryStorage);
|
||||
|
||||
try {
|
||||
await storage.setItem("/storage_a/file_depth1", "contents");
|
||||
await storage.setItem("/storage_a/depth1/file_depth2", "contents");
|
||||
await storage.setItem("/storage_b/file_depth1", "contents");
|
||||
await storage.setItem("/storage_b/depth1/file_depth2", "contents");
|
||||
|
||||
const keys = await storage.getKeys(undefined, { maxDepth: 1 });
|
||||
|
||||
expect(keys.sort()).toMatchObject([
|
||||
"storage_a:file_depth1",
|
||||
"storage_b:file_depth1",
|
||||
]);
|
||||
} finally {
|
||||
await storage.clear();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user