Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve file storage list method #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'
import { FileSystemStorageAdapter } from './FileSystemStorageAdapter.js'
import { access, lstat, open, readdir, rm, mkdir } from 'node:fs/promises'
import { access, lstat, open, readdir, rm, mkdir, constants } from 'node:fs/promises'

vi.mock('node:fs/promises')
vi.mock('glob')
Expand Down Expand Up @@ -125,6 +125,7 @@ describe('FileSystemStorageAdapter', () => {
describe('list method', () => {
it('should list the files in the baseDir', async () => {
const storage = new FileSystemStorageAdapter()
vi.spyOn(storage, '_exists').mockResolvedValue(true)
vi.mocked(glob).mockResolvedValue([
{
isDirectory: () => false,
Expand All @@ -136,14 +137,20 @@ describe('FileSystemStorageAdapter', () => {
},
] as any[])
expect(await storage.list()).toEqual(['foo', 'bar/'])
expect(glob).toHaveBeenCalledWith('*', {
cwd: storage.baseDir,
expect(glob).toHaveBeenCalledWith(join(process.cwd(), '*'), {
withFileTypes: true,
})
})

it('should return empty array if the baseDir does not exist', async () => {
const storage = new FileSystemStorageAdapter()
vi.spyOn(storage, '_exists').mockResolvedValue(false)
expect(await storage.list()).toEqual([])
})

it('should list the files with prefix', async () => {
const storage = new FileSystemStorageAdapter()
vi.spyOn(storage, '_exists').mockResolvedValue(true)
vi.mocked(glob).mockResolvedValueOnce([
{
isDirectory: () => false,
Expand All @@ -156,19 +163,34 @@ describe('FileSystemStorageAdapter', () => {
] as any[])

expect(await storage.list('baz')).toEqual(['baz/foo', 'baz/bar/'])
expect(glob).toHaveBeenCalledWith('baz', {
cwd: storage.baseDir,
expect(glob).toHaveBeenCalledWith(join(process.cwd(), 'baz'), {
withFileTypes: true,
})
})

it('should throw if the directory is out of the base directory', async () => {
it('should throw PermissionDeniedError if the directory is out of the base directory', async () => {
const storage = new FileSystemStorageAdapter({ urlSchema: 'fs', baseDir: 'foo' })
await expect(storage.list('../bar')).rejects.toThrow(PermissionDeniedError)
})

it('should check glob base directory if given prefix has glob pattern', async () => {
const storage = new FileSystemStorageAdapter()
vi.spyOn(storage, '_exists').mockResolvedValue(true)
vi.mocked(access).mockResolvedValueOnce(undefined)
vi.mocked(glob).mockResolvedValueOnce([])

await storage.list('foo/**/bar')
expect(access).toHaveBeenCalledWith(join(process.cwd(), 'foo/'), constants.R_OK)
})

it('should throw PermissionDeniedError if storage is not readable', async () => {
const storage = new FileSystemStorageAdapter({ read: false })
await expect(storage.list()).rejects.toThrow(PermissionDeniedError)
})

it('should throw OperationFailedError if list failed', async () => {
const storage = new FileSystemStorageAdapter()
vi.spyOn(storage, '_exists').mockResolvedValue(true)
vi.mocked(access).mockResolvedValueOnce(undefined)
vi.mocked(readdir).mockRejectedValueOnce(new Error('Failed to list'))
await expect(storage.list()).rejects.toThrow(OperationFailedError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
OperationFailedError,
} from '@connectable-io/storage'
import { DEFAULT_SCHEMA } from '../constant.js'
import { getGlobBase } from '../utils/get-glob-base.js'

/**
* Options for FileSystemStorageAdapter
Expand Down Expand Up @@ -208,20 +209,30 @@ export class FileSystemStorageAdapter implements Storage {
}
}

async list(prefix?: string) {
async list(prefix: string = '*') {
const resolved = this._resolvePath(prefix)

// Check storage level read permission
if (this.read === false) throw new PermissionDeniedError(`Read permission denied. url:${prefix}`)

// Check directory is in base directory
if (relative(this.baseDir, resolved).startsWith('..'))
throw new PermissionDeniedError(`Path is out of base directory. url:${prefix}`)

// Check directory permission
const globBase = getGlobBase(resolved)
if ((await this._exists(globBase)) === false) {
// If directory does not exists, return empty array
return []
}
try {
await access(this._resolvePath(prefix), constants.R_OK)
await access(globBase, constants.R_OK)
} catch (e) {
throw new PermissionDeniedError(`Read permission denied. url:${prefix}`, { cause: e })
}

try {
const results = await glob(prefix ?? '*', {
cwd: this.baseDir,
const results = await glob(resolved, {
withFileTypes: true,
})
return results.map((result) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, test } from 'vitest'
import { getGlobBase } from './get-glob-base.js'

describe('getGlobBase', () => {
test.each([
{ pattern: 'foo/bar/*.js', expected: 'foo/bar/' },
{ pattern: 'foo/bar/**/*.js', expected: 'foo/bar/' },
{ pattern: 'foo/bar/**/baz/*.js', expected: 'foo/bar/' },
{ pattern: 'foo/bar/**/baz/**/*.js', expected: 'foo/bar/' },
{ pattern: 'foo/bar/foo*.js', expected: 'foo/bar/' },
{ pattern: 'foo/bar/foo[0-9].js', expected: 'foo/bar/' },
])('should return the base directory "$expected" for the given glob pattern "$pattern"', ({ pattern, expected }) => {
expect(getGlobBase(pattern)).toBe(expected)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Minimatch, escape } from 'minimatch'

/**
* Get the base directory of a glob pattern
*
* @example
* ```ts
* getGlobBase('foo/bar/*.js') // => 'foo/bar/'
* ```
*
* @param path
* @returns the base directory of the glob pattern
*/
export function getGlobBase(path: string) {
const match = new Minimatch(path)
if (match.hasMagic()) {
const escaped = escape(path)
const idx = escaped.split('').findIndex((c, i) => c !== path.charAt(i))
return path.slice(0, idx).split('/').slice(0, -1).join('/') + '/'
}
return path
}
1 change: 1 addition & 0 deletions packages/@connectable-io/storage/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface Storage {
* @returns list of files. relative path from the storage root.
* @throws {PermissionDeniedError} given key is outside of the storage.
* Or if directory is not readable.
* Or if storage is not readable.
* @throws {OperationFailedError} if the operation is failed.
*
* @example List all files.
Expand Down