From 0c016c5591b572f4ba86f861fcbbfb47dc5cb21e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 9 Feb 2020 18:22:18 +0100 Subject: [PATCH 1/2] feat: add isInitialized method A few places need ot know whether or not a repo has been initialized and do so by a variety of methods - checking whether certain files exist, etc. This PR adds an `isInitialized()` method to the ipfs repo class to take some of this guesswork away as the repo will know if it's been initialized. --- README.md | 4 ++++ src/index.js | 16 ++++++++++++++++ test/browser.js | 1 + test/is-initialized.js | 28 ++++++++++++++++++++++++++++ test/node.js | 1 + 5 files changed, 50 insertions(+) create mode 100644 test/is-initialized.js diff --git a/README.md b/README.md index 43ba20c8..341adeb1 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,10 @@ Get a value at the root of the repo. [Blocks](https://github.com/ipfs/js-ipfs-block#readme): +#### `Promise repo.isInitialized ()` + +The returned promise resolves to `false` if the repo has not been initialized and `true` if it has. + #### `Promise repo.blocks.put (block:Block)` * `block` should be of type [Block](https://github.com/ipfs/js-ipfs-block#readme). diff --git a/src/index.js b/src/index.js index cab370c8..5901687b 100644 --- a/src/index.js +++ b/src/index.js @@ -69,6 +69,22 @@ class IpfsRepo { await this.version.set(constants.repoVersion) } + /** + * Check if the repo is already initialized. + * @returns {Promise} + */ + async isInitialized () { + try { + await this._openRoot() + await this._checkInitialized() + // necessary? await this.root.close() + + return true + } catch (err) { + return false + } + } + /** * Open the repo. If the repo is already open an error will be thrown. * If the repo is not initialized it will throw an error. diff --git a/test/browser.js b/test/browser.js index 32a5e23b..f955a7f5 100644 --- a/test/browser.js +++ b/test/browser.js @@ -38,4 +38,5 @@ describe('IPFS Repo Tests on the Browser', () => { require('./config-test')(repo) require('./api-addr-test')(repo) require('./lock-test')(repo) + require('./is-initialized') }) diff --git a/test/is-initialized.js b/test/is-initialized.js new file mode 100644 index 00000000..f1e3ae12 --- /dev/null +++ b/test/is-initialized.js @@ -0,0 +1,28 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +chai.use(require('dirty-chai')) +const expect = chai.expect +const os = require('os') +const path = require('path') +const IPFSRepo = require('../src') + +describe('isInitialized', () => { + let repo + + beforeEach(() => { + const repoPath = path.join(os.tmpdir(), 'test-repo-for-' + Math.random()) + repo = new IPFSRepo(repoPath) + }) + + it('should be false before initialization', async () => { + expect(await repo.isInitialized()).to.be.false() + }) + + it('should be true after initialization', async () => { + await repo.init({}) + expect(await repo.isInitialized()).to.be.true() + }) +}) diff --git a/test/node.js b/test/node.js index 970b2724..f443e276 100644 --- a/test/node.js +++ b/test/node.js @@ -113,6 +113,7 @@ describe('IPFS Repo Tests onNode.js', () => { if (!r.init) { require('./interop-test')(repo) } + require('./is-initialized') })) require('./blockstore-utils-test')() From 65f60d387aeee63d4787c91483d9ef0b02a31f9d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 10 Feb 2020 09:42:05 +0100 Subject: [PATCH 2/2] fix: close root datastore after initialized check --- src/index.js | 10 +++++++++- test/is-initialized.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5901687b..cfe4119d 100644 --- a/src/index.js +++ b/src/index.js @@ -74,13 +74,21 @@ class IpfsRepo { * @returns {Promise} */ async isInitialized () { + if (!this.closed) { + // repo is open, must be initialized + return true + } + try { + // have to open the root datastore in the browser before + // we can check whether it's been initialized await this._openRoot() await this._checkInitialized() - // necessary? await this.root.close() + await this.root.close() return true } catch (err) { + // FIXME: do not use exceptions for flow control return false } } diff --git a/test/is-initialized.js b/test/is-initialized.js index f1e3ae12..62843276 100644 --- a/test/is-initialized.js +++ b/test/is-initialized.js @@ -25,4 +25,17 @@ describe('isInitialized', () => { await repo.init({}) expect(await repo.isInitialized()).to.be.true() }) + + it('should be true after initialization and opening', async () => { + await repo.init({}) + await repo.open() + expect(await repo.isInitialized()).to.be.true() + }) + + it('should be true after initialization, opening and closing', async () => { + await repo.init({}) + await repo.open() + await repo.close() + expect(await repo.isInitialized()).to.be.true() + }) })