Skip to content

Commit

Permalink
feat: support base for drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 11, 2021
1 parent a9178a3 commit 6844cd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/drivers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type { DriverFactory } from '../types'
import { readFile, writeFile, readdirRecursive, rmRecursive, unlink } from './utils/node-fs'

export interface FSStorageOptions {
dir: string
base: string
ingore: string[]
watchOptions: WatchOptions
}

export default <DriverFactory> function (opts: FSStorageOptions) {
if (!opts.dir) {
if (!opts.base) {
throw new Error('dir is required')
}

Expand All @@ -21,8 +21,8 @@ export default <DriverFactory> function (opts: FSStorageOptions) {
]
}

opts.dir = resolve(opts.dir)
const r = (key: string) => join(opts.dir, key.replace(/:/g, '/'))
opts.base = resolve(opts.base)
const r = (key: string) => join(opts.base, key.replace(/:/g, '/'))

let _watcher: FSWatcher

Expand Down Expand Up @@ -55,15 +55,15 @@ export default <DriverFactory> function (opts: FSStorageOptions) {
return
}
return new Promise((resolve, reject) => {
_watcher = watch(opts.dir, {
_watcher = watch(opts.base, {
ignoreInitial: true,
ignored: opts.ingore,
...opts.watchOptions
})
.on('ready', resolve)
.on('error', reject)
.on('all', (eventName, path) => {
path = relative(opts.dir, path)
path = relative(opts.base, path)
if (eventName === 'change' || eventName === 'add') {
callback('update', path)
} else if (eventName === 'unlink') {
Expand Down
22 changes: 16 additions & 6 deletions src/drivers/localstorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DriverFactory } from '../types'

export interface LocalStorageOptions {
base?: string
window?: typeof window
localStorage?: typeof window.localStorage
}
Expand All @@ -16,26 +17,35 @@ export default <DriverFactory>function (opts: LocalStorageOptions = {}) {
throw new Error('localStorage not available')
}

const r = (key: string) => (opts.base ? opts.base + ':' : '') + key


let _storageListener: (ev: StorageEvent) => void

return {
hasItem (key) {
return Object.prototype.hasOwnProperty.call(opts.localStorage!, key)
return Object.prototype.hasOwnProperty.call(opts.localStorage!, r(key))
},
getItem (key) {
return opts.localStorage!.getItem(key)
return opts.localStorage!.getItem(r(key))
},
setItem (key, value) {
return opts.localStorage!.setItem(key, value)
return opts.localStorage!.setItem(r(key), value)
},
removeItem (key) {
return opts.localStorage!.removeItem(key)
return opts.localStorage!.removeItem(r(key))
},
getKeys () {
return Object.keys(opts.localStorage!)
},
clear () {
opts.localStorage!.clear()
clear() {
if (!opts.base) {
opts.localStorage!.clear()
} else {
for (const key of Object.keys(opts.localStorage!)) {
opts.localStorage?.removeItem(key)
}
}
if (opts.window && _storageListener) {
opts.window.removeEventListener('storage', _storageListener)
}
Expand Down
2 changes: 1 addition & 1 deletion test/drivers/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('drivers: fs', () => {
const dir = resolve(__dirname, 'tmp')

testDriver({
driver: driver({ dir }),
driver: driver({ base: dir }),
additionalTests(ctx) {
it('check filesystem', async () => {
expect(await readFile(resolve(dir, 's1/a'))).toBe('test_data')
Expand Down

0 comments on commit 6844cd1

Please sign in to comment.