From 29423d13acc4ca137f9708578fe50764b33e0970 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 15 Apr 2021 15:11:34 +0100 Subject: [PATCH] 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: - [ ] https://github.com/ipfs/interface-datastore/pull/87 - [ ] https://github.com/ipfs/js-datastore-core/pull/59 * chore: remove gh urls --- package.json | 5 ++-- src/index.js | 74 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 8e0e311..09c5c44 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,9 @@ "homepage": "https://github.com/ipfs/js-datastore-s3#readme", "dependencies": { "buffer": "^6.0.3", - "datastore-core": "^3.0.0", - "interface-datastore": "^3.0.5" + "datastore-core": "^4.0.0", + "interface-datastore": "^4.0.0", + "it-filter": "^1.0.2" }, "devDependencies": { "aegir": "^33.0.0", diff --git a/src/index.js b/src/index.js index 160a87a..7cca370 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,21 @@ 'use strict' const { Buffer } = require('buffer') - +const filter = require('it-filter') const { Adapter, Key, - Errors, - utils: { - filter - } + Errors } = require('interface-datastore') const createRepo = require('./s3-repo') +/** + * @typedef {import('interface-datastore').Pair} Pair + * @typedef {import('interface-datastore').Query} Query + * @typedef {import('interface-datastore').KeyQuery} KeyQuery + * @typedef {import('interface-datastore').Options} Options + */ + /** * A datastore backed by the file system. * @@ -171,9 +175,10 @@ class S3Datastore extends Adapter { * Recursively fetches all keys from s3 * * @param {Object} params - * @returns {Iterator} + * @param {Options} [options] + * @returns {AsyncIterator} */ - async * _listKeys (params) { + async * _listKeys (params, options) { let data try { data = await this.opts.s3.listObjectsV2(params).promise() @@ -181,6 +186,10 @@ class S3Datastore extends Adapter { throw new Error(err.code) } + if (options && options.signal && options.signal.aborted) { + return + } + for (const d of data.Contents) { // Remove the path from the key yield new Key(d.Key.slice(this.path.length), false) @@ -196,31 +205,17 @@ class S3Datastore extends Adapter { } } + /** + * @param {Query} q + * @param {Options} [options] + */ async * _all (q, options) { - const prefix = [this.path, q.prefix || ''].join('/').replace(/\/\/+/g, '/') - - let values = true - if (q.keysOnly != null) { - values = !q.keysOnly - } - - // Get all the keys via list object, recursively as needed - const params = { - Prefix: prefix - } - let it = this._listKeys(params) - - if (q.prefix != null) { - it = filter(it, k => k.toString().startsWith(q.prefix)) - } - - for await (const key of it) { + for await (const key of this._allKeys({ prefix: q.prefix }, options)) { try { - const res = { key } - - if (values) { - // Fetch the object Buffer from s3 - res.value = await this.get(key) + /** @type {Pair} */ + const res = { + key, + value: await this.get(key) } yield res @@ -233,6 +228,25 @@ class S3Datastore extends Adapter { } } + /** + * @param {KeyQuery} q + * @param {Options} [options] + */ + async * _allKeys (q, options) { + const prefix = [this.path, q.prefix || ''].join('/').replace(/\/\/+/g, '/') + + // Get all the keys via list object, recursively as needed + let it = this._listKeys({ + Prefix: prefix + }, options) + + if (q.prefix != null) { + it = filter(it, k => k.toString().startsWith(q.prefix)) + } + + yield * it + } + /** * This will check the s3 bucket to ensure access and existence *