diff --git a/drivers/fs/driver.ts b/drivers/fs/driver.ts index 201b7b0..52dfe7c 100644 --- a/drivers/fs/driver.ts +++ b/drivers/fs/driver.ts @@ -16,7 +16,7 @@ import { fileURLToPath } from 'node:url' import { Retrier } from '@humanwhocodes/retry' import { RuntimeException } from '@poppinss/utils' import { dirname, join, relative } from 'node:path' -import { existsSync, rmSync, createReadStream } from 'node:fs' +import { existsSync, rmSync, createReadStream, Dirent } from 'node:fs' import debug from './debug.js' import type { FSDriverOptions } from './types.js' @@ -69,6 +69,23 @@ export class FSDriver implements DriverContract { return this.#retrier.retry(() => fsp.readFile(location)) } + /** + * Reads dir and ignores non-existing errors + */ + async #readDir(location: string, recursive: boolean): Promise { + try { + return await fsp.readdir(location, { + recursive, + withFileTypes: true, + }) + } catch (error) { + if (error.code !== 'ENOENT') { + throw error + } + return [] + } + } + /** * Generic implementation to write a file */ @@ -333,10 +350,7 @@ export class FSDriver implements DriverContract { /** * Reading files with their types. */ - const files = await fsp.readdir(location, { - recursive, - withFileTypes: true, - }) + const files = await this.#readDir(location, recursive) /** * The generator is used to lazily iterate over files and diff --git a/tests/drivers/fs/list_all.spec.ts b/tests/drivers/fs/list_all.spec.ts index 3c05812..608dba8 100644 --- a/tests/drivers/fs/list_all.spec.ts +++ b/tests/drivers/fs/list_all.spec.ts @@ -122,4 +122,14 @@ test.group('FS Driver | listAll | nested dir', () => { }, ]) }) + + test('do not throw error when listing files of a non-existing directory', async ({ + fs, + assert, + }) => { + const fdfs = new FSDriver({ location: fs.baseUrl, visibility: 'public' }) + + const { objects } = await fdfs.listAll('foo', { recursive: true }) + assert.deepEqual(Array.from(objects), []) + }) }) diff --git a/tests/drivers/gcs/list_all.spec.ts b/tests/drivers/gcs/list_all.spec.ts index 5f6749c..9248579 100644 --- a/tests/drivers/gcs/list_all.spec.ts +++ b/tests/drivers/gcs/list_all.spec.ts @@ -244,4 +244,15 @@ test.group('GCS Driver | listAll | nested dir', (group) => { }, ]) }) + + test('do not throw error when listing files of a non-existing directory', async ({ assert }) => { + const fdgcs = new GCSDriver({ + visibility: 'public', + bucket: GCS_BUCKET, + credentials: GCS_KEY, + }) + + const { objects } = await fdgcs.listAll('foo', { recursive: true }) + assert.deepEqual(Array.from(objects), []) + }) }) diff --git a/tests/drivers/s3/list_all.spec.ts b/tests/drivers/s3/list_all.spec.ts index 7311c5e..202135f 100644 --- a/tests/drivers/s3/list_all.spec.ts +++ b/tests/drivers/s3/list_all.spec.ts @@ -262,4 +262,16 @@ test.group('S3 Driver | listAll | nested dir', (group) => { }, ]) }) + + test('do not throw error when listing files of a non-existing directory', async ({ assert }) => { + const s3fs = new S3Driver({ + visibility: 'public', + client: client, + bucket: S3_BUCKET, + supportsACL: SUPPORTS_ACL, + }) + + const { objects } = await s3fs.listAll('foo', { recursive: true }) + assert.deepEqual(Array.from(objects), []) + }) })