@@ -9,10 +9,9 @@ const promisify = require('util').promisify
9
9
const writeAtomic = promisify ( require ( 'fast-write-atomic' ) )
10
10
const path = require ( 'path' )
11
11
const {
12
- Adapter, Key, Errors, utils : {
13
- map
14
- }
12
+ Adapter, Key, Errors
15
13
} = require ( 'interface-datastore' )
14
+ const map = require ( 'it-map' )
16
15
17
16
const noop = ( ) => { }
18
17
const fsAccess = promisify ( fs . access || noop )
@@ -23,6 +22,7 @@ const fsUnlink = promisify(fs.unlink || noop)
23
22
* @typedef {import('interface-datastore').Datastore } Datastore
24
23
* @typedef {import('interface-datastore').Pair } Pair
25
24
* @typedef {import('interface-datastore').Query } Query
25
+ * @typedef {import('interface-datastore').KeyQuery } KeyQuery
26
26
*/
27
27
28
28
/**
@@ -251,9 +251,7 @@ class FsDatastore extends Adapter {
251
251
}
252
252
253
253
/**
254
- *
255
254
* @param {Query } q
256
- * @returns {AsyncIterable<Pair> }
257
255
*/
258
256
async * _all ( q ) {
259
257
let prefix = q . prefix || '**'
@@ -268,27 +266,45 @@ class FsDatastore extends Adapter {
268
266
absolute : true
269
267
} )
270
268
271
- if ( ! q . keysOnly ) {
272
- for await ( const file of files ) {
273
- try {
274
- const buf = await fsReadFile ( file )
275
-
276
- yield {
277
- key : this . _decode ( file ) ,
278
- value : buf
279
- }
280
- } catch ( err ) {
281
- // if keys are removed from the datastore while the query is
282
- // running, we may encounter missing files.
283
- if ( err . code !== 'ENOENT' ) {
284
- throw err
285
- }
269
+ for await ( const file of files ) {
270
+ try {
271
+ const buf = await fsReadFile ( file )
272
+
273
+ /** @type {Pair } */
274
+ const pair = {
275
+ key : this . _decode ( file ) ,
276
+ value : buf
277
+ }
278
+
279
+ yield pair
280
+ } catch ( err ) {
281
+ // if keys are removed from the datastore while the query is
282
+ // running, we may encounter missing files.
283
+ if ( err . code !== 'ENOENT' ) {
284
+ throw err
286
285
}
287
286
}
288
- } else {
289
- yield * map ( files , f => /** @type {Pair } */ ( { key : this . _decode ( f ) } ) )
290
287
}
291
288
}
289
+
290
+ /**
291
+ * @param {KeyQuery } q
292
+ */
293
+ async * _allKeys ( q ) {
294
+ let prefix = q . prefix || '**'
295
+
296
+ // strip leading slashes
297
+ prefix = prefix . replace ( / ^ \/ + / , '' )
298
+
299
+ const pattern = `${ prefix } /*${ this . opts . extension } `
300
+ . split ( path . sep )
301
+ . join ( '/' )
302
+ const files = glob ( this . path , pattern , {
303
+ absolute : true
304
+ } )
305
+
306
+ yield * map ( files , f => this . _decode ( f ) )
307
+ }
292
308
}
293
309
294
310
module . exports = FsDatastore
0 commit comments