Skip to content

Commit

Permalink
Merge pull request #219 from ipfs/feat-add-is-initialized-meethod
Browse files Browse the repository at this point in the history
feat: add isInitialized method
  • Loading branch information
achingbrain authored Feb 10, 2020
2 parents ddb2f46 + 65f60d3 commit f9b8056
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ Get a value at the root of the repo.

[Blocks](https://github.com/ipfs/js-ipfs-block#readme):

#### `Promise<Boolean> 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).
Expand Down
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ class IpfsRepo {
await this.version.set(constants.repoVersion)
}

/**
* Check if the repo is already initialized.
* @returns {Promise<Boolean>}
*/
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()
await this.root.close()

return true
} catch (err) {
// FIXME: do not use exceptions for flow control
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.
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
41 changes: 41 additions & 0 deletions test/is-initialized.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* 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()
})

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()
})
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('IPFS Repo Tests onNode.js', () => {
if (!r.init) {
require('./interop-test')(repo)
}
require('./is-initialized')
}))

require('./blockstore-utils-test')()
Expand Down

0 comments on commit f9b8056

Please sign in to comment.