diff --git a/package.json b/package.json index 9718331..e5cc812 100644 --- a/package.json +++ b/package.json @@ -38,10 +38,11 @@ }, "homepage": "https://github.com/ipfs/js-datastore-fs#readme", "dependencies": { - "datastore-core": "^3.0.0", + "datastore-core": "^4.0.0", "fast-write-atomic": "^0.2.0", - "interface-datastore": "^3.0.3", + "interface-datastore": "^4.0.0", "it-glob": "^0.0.11", + "it-map": "^1.0.5", "mkdirp": "^1.0.4" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 1fdc676..7cb2572 100644 --- a/src/index.js +++ b/src/index.js @@ -9,10 +9,9 @@ const promisify = require('util').promisify const writeAtomic = promisify(require('fast-write-atomic')) const path = require('path') const { - Adapter, Key, Errors, utils: { - map - } + Adapter, Key, Errors } = require('interface-datastore') +const map = require('it-map') const noop = () => {} const fsAccess = promisify(fs.access || noop) @@ -23,6 +22,7 @@ const fsUnlink = promisify(fs.unlink || noop) * @typedef {import('interface-datastore').Datastore} Datastore * @typedef {import('interface-datastore').Pair} Pair * @typedef {import('interface-datastore').Query} Query + * @typedef {import('interface-datastore').KeyQuery} KeyQuery */ /** @@ -251,9 +251,7 @@ class FsDatastore extends Adapter { } /** - * * @param {Query} q - * @returns {AsyncIterable} */ async * _all (q) { let prefix = q.prefix || '**' @@ -268,27 +266,45 @@ class FsDatastore extends Adapter { absolute: true }) - if (!q.keysOnly) { - for await (const file of files) { - try { - const buf = await fsReadFile(file) - - yield { - key: this._decode(file), - value: buf - } - } catch (err) { - // if keys are removed from the datastore while the query is - // running, we may encounter missing files. - if (err.code !== 'ENOENT') { - throw err - } + for await (const file of files) { + try { + const buf = await fsReadFile(file) + + /** @type {Pair} */ + const pair = { + key: this._decode(file), + value: buf + } + + yield pair + } catch (err) { + // if keys are removed from the datastore while the query is + // running, we may encounter missing files. + if (err.code !== 'ENOENT') { + throw err } } - } else { - yield * map(files, f => /** @type {Pair} */({ key: this._decode(f) })) } } + + /** + * @param {KeyQuery} q + */ + async * _allKeys (q) { + let prefix = q.prefix || '**' + + // strip leading slashes + prefix = prefix.replace(/^\/+/, '') + + const pattern = `${prefix}/*${this.opts.extension}` + .split(path.sep) + .join('/') + const files = glob(this.path, pattern, { + absolute: true + }) + + yield * map(files, f => this._decode(f)) + } } module.exports = FsDatastore diff --git a/tsconfig.json b/tsconfig.json index e605b61..fe3cbd6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { - "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist" }, "include": [ - "test", // remove this line if you don't want to type-check tests + "test", "src" ] }