1
1
'use strict'
2
2
3
3
const { Buffer } = require ( 'buffer' )
4
-
4
+ const filter = require ( 'it-filter' )
5
5
const {
6
6
Adapter,
7
7
Key,
8
- Errors,
9
- utils : {
10
- filter
11
- }
8
+ Errors
12
9
} = require ( 'interface-datastore' )
13
10
const createRepo = require ( './s3-repo' )
14
11
12
+ /**
13
+ * @typedef {import('interface-datastore').Pair } Pair
14
+ * @typedef {import('interface-datastore').Query } Query
15
+ * @typedef {import('interface-datastore').KeyQuery } KeyQuery
16
+ * @typedef {import('interface-datastore').Options } Options
17
+ */
18
+
15
19
/**
16
20
* A datastore backed by the file system.
17
21
*
@@ -171,16 +175,21 @@ class S3Datastore extends Adapter {
171
175
* Recursively fetches all keys from s3
172
176
*
173
177
* @param {Object } params
174
- * @returns {Iterator<Key> }
178
+ * @param {Options } [options]
179
+ * @returns {AsyncIterator<Key> }
175
180
*/
176
- async * _listKeys ( params ) {
181
+ async * _listKeys ( params , options ) {
177
182
let data
178
183
try {
179
184
data = await this . opts . s3 . listObjectsV2 ( params ) . promise ( )
180
185
} catch ( err ) {
181
186
throw new Error ( err . code )
182
187
}
183
188
189
+ if ( options && options . signal && options . signal . aborted ) {
190
+ return
191
+ }
192
+
184
193
for ( const d of data . Contents ) {
185
194
// Remove the path from the key
186
195
yield new Key ( d . Key . slice ( this . path . length ) , false )
@@ -196,31 +205,17 @@ class S3Datastore extends Adapter {
196
205
}
197
206
}
198
207
208
+ /**
209
+ * @param {Query } q
210
+ * @param {Options } [options]
211
+ */
199
212
async * _all ( q , options ) {
200
- const prefix = [ this . path , q . prefix || '' ] . join ( '/' ) . replace ( / \/ \/ + / g, '/' )
201
-
202
- let values = true
203
- if ( q . keysOnly != null ) {
204
- values = ! q . keysOnly
205
- }
206
-
207
- // Get all the keys via list object, recursively as needed
208
- const params = {
209
- Prefix : prefix
210
- }
211
- let it = this . _listKeys ( params )
212
-
213
- if ( q . prefix != null ) {
214
- it = filter ( it , k => k . toString ( ) . startsWith ( q . prefix ) )
215
- }
216
-
217
- for await ( const key of it ) {
213
+ for await ( const key of this . _allKeys ( { prefix : q . prefix } , options ) ) {
218
214
try {
219
- const res = { key }
220
-
221
- if ( values ) {
222
- // Fetch the object Buffer from s3
223
- res . value = await this . get ( key )
215
+ /** @type {Pair } */
216
+ const res = {
217
+ key,
218
+ value : await this . get ( key )
224
219
}
225
220
226
221
yield res
@@ -233,6 +228,25 @@ class S3Datastore extends Adapter {
233
228
}
234
229
}
235
230
231
+ /**
232
+ * @param {KeyQuery } q
233
+ * @param {Options } [options]
234
+ */
235
+ async * _allKeys ( q , options ) {
236
+ const prefix = [ this . path , q . prefix || '' ] . join ( '/' ) . replace ( / \/ \/ + / g, '/' )
237
+
238
+ // Get all the keys via list object, recursively as needed
239
+ let it = this . _listKeys ( {
240
+ Prefix : prefix
241
+ } , options )
242
+
243
+ if ( q . prefix != null ) {
244
+ it = filter ( it , k => k . toString ( ) . startsWith ( q . prefix ) )
245
+ }
246
+
247
+ yield * it
248
+ }
249
+
236
250
/**
237
251
* This will check the s3 bucket to ensure access and existence
238
252
*
0 commit comments