From 5a40fb2e159cb4879747bbfd8d9e70675f5f0f10 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Thu, 2 Jan 2025 15:05:56 +0000 Subject: [PATCH] perf: skip maxDepth filtering if natively supported Skips `maxDepth` filtering if _all_ drivers in the current storage instance natively support it (via the `maxDepth` flag). Also enables the `maxDepth` flag for `fs` and `fs-lite` drivers. --- src/drivers/fs-lite.ts | 3 +++ src/drivers/fs.ts | 3 +++ src/storage.ts | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/drivers/fs-lite.ts b/src/drivers/fs-lite.ts index a7ebbb31..2fff2094 100644 --- a/src/drivers/fs-lite.ts +++ b/src/drivers/fs-lite.ts @@ -40,6 +40,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => { return { name: DRIVER_NAME, options: opts, + flags: { + maxDepth: true, + }, hasItem(key) { return existsSync(r(key)); }, diff --git a/src/drivers/fs.ts b/src/drivers/fs.ts index b2bcc5ed..3e7e1e1b 100644 --- a/src/drivers/fs.ts +++ b/src/drivers/fs.ts @@ -59,6 +59,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => { return { name: DRIVER_NAME, options: opts, + flags: { + maxDepth: true, + }, hasItem(key) { return existsSync(r(key)); }, diff --git a/src/storage.ts b/src/storage.ts index 7cacb064..eb657847 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -348,7 +348,11 @@ export function createStorage( 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( ...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) ); },