Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 29423d1

Browse files
authored
feat: split .query into .query and .queryKeys (#34)
* feat: split .query into .query and .queryKeys Applies changes from ipfs/interface-datastore/pull/87 Depends on: - [ ] ipfs/interface-datastore#87 - [ ] ipfs/js-datastore-core#59 * chore: remove gh urls
1 parent e86f9e5 commit 29423d1

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"homepage": "https://github.com/ipfs/js-datastore-s3#readme",
3737
"dependencies": {
3838
"buffer": "^6.0.3",
39-
"datastore-core": "^3.0.0",
40-
"interface-datastore": "^3.0.5"
39+
"datastore-core": "^4.0.0",
40+
"interface-datastore": "^4.0.0",
41+
"it-filter": "^1.0.2"
4142
},
4243
"devDependencies": {
4344
"aegir": "^33.0.0",

src/index.js

+44-30
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
'use strict'
22

33
const { Buffer } = require('buffer')
4-
4+
const filter = require('it-filter')
55
const {
66
Adapter,
77
Key,
8-
Errors,
9-
utils: {
10-
filter
11-
}
8+
Errors
129
} = require('interface-datastore')
1310
const createRepo = require('./s3-repo')
1411

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+
1519
/**
1620
* A datastore backed by the file system.
1721
*
@@ -171,16 +175,21 @@ class S3Datastore extends Adapter {
171175
* Recursively fetches all keys from s3
172176
*
173177
* @param {Object} params
174-
* @returns {Iterator<Key>}
178+
* @param {Options} [options]
179+
* @returns {AsyncIterator<Key>}
175180
*/
176-
async * _listKeys (params) {
181+
async * _listKeys (params, options) {
177182
let data
178183
try {
179184
data = await this.opts.s3.listObjectsV2(params).promise()
180185
} catch (err) {
181186
throw new Error(err.code)
182187
}
183188

189+
if (options && options.signal && options.signal.aborted) {
190+
return
191+
}
192+
184193
for (const d of data.Contents) {
185194
// Remove the path from the key
186195
yield new Key(d.Key.slice(this.path.length), false)
@@ -196,31 +205,17 @@ class S3Datastore extends Adapter {
196205
}
197206
}
198207

208+
/**
209+
* @param {Query} q
210+
* @param {Options} [options]
211+
*/
199212
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)) {
218214
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)
224219
}
225220

226221
yield res
@@ -233,6 +228,25 @@ class S3Datastore extends Adapter {
233228
}
234229
}
235230

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+
236250
/**
237251
* This will check the s3 bucket to ensure access and existence
238252
*

0 commit comments

Comments
 (0)