Skip to content

Commit

Permalink
fix(fs): do not throw error when listing files from non-existing dire…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
thetutlage committed May 23, 2024
1 parent d770ac1 commit 233a52d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
24 changes: 19 additions & 5 deletions drivers/fs/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<Dirent[]> {
try {
return await fsp.readdir(location, {
recursive,
withFileTypes: true,
})
} catch (error) {
if (error.code !== 'ENOENT') {
throw error
}
return []
}
}

/**
* Generic implementation to write a file
*/
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/drivers/fs/list_all.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])
})
})
11 changes: 11 additions & 0 deletions tests/drivers/gcs/list_all.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])
})
})
12 changes: 12 additions & 0 deletions tests/drivers/s3/list_all.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])
})
})

0 comments on commit 233a52d

Please sign in to comment.