Skip to content

Commit f9b8056

Browse files
authored
Merge pull request #219 from ipfs/feat-add-is-initialized-meethod
feat: add isInitialized method
2 parents ddb2f46 + 65f60d3 commit f9b8056

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ Get a value at the root of the repo.
183183

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

186+
#### `Promise<Boolean> repo.isInitialized ()`
187+
188+
The returned promise resolves to `false` if the repo has not been initialized and `true` if it has.
189+
186190
#### `Promise repo.blocks.put (block:Block)`
187191

188192
* `block` should be of type [Block](https://github.com/ipfs/js-ipfs-block#readme).

src/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ class IpfsRepo {
6969
await this.version.set(constants.repoVersion)
7070
}
7171

72+
/**
73+
* Check if the repo is already initialized.
74+
* @returns {Promise<Boolean>}
75+
*/
76+
async isInitialized () {
77+
if (!this.closed) {
78+
// repo is open, must be initialized
79+
return true
80+
}
81+
82+
try {
83+
// have to open the root datastore in the browser before
84+
// we can check whether it's been initialized
85+
await this._openRoot()
86+
await this._checkInitialized()
87+
await this.root.close()
88+
89+
return true
90+
} catch (err) {
91+
// FIXME: do not use exceptions for flow control
92+
return false
93+
}
94+
}
95+
7296
/**
7397
* Open the repo. If the repo is already open an error will be thrown.
7498
* If the repo is not initialized it will throw an error.

test/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ describe('IPFS Repo Tests on the Browser', () => {
3838
require('./config-test')(repo)
3939
require('./api-addr-test')(repo)
4040
require('./lock-test')(repo)
41+
require('./is-initialized')
4142
})

test/is-initialized.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
8+
const os = require('os')
9+
const path = require('path')
10+
const IPFSRepo = require('../src')
11+
12+
describe('isInitialized', () => {
13+
let repo
14+
15+
beforeEach(() => {
16+
const repoPath = path.join(os.tmpdir(), 'test-repo-for-' + Math.random())
17+
repo = new IPFSRepo(repoPath)
18+
})
19+
20+
it('should be false before initialization', async () => {
21+
expect(await repo.isInitialized()).to.be.false()
22+
})
23+
24+
it('should be true after initialization', async () => {
25+
await repo.init({})
26+
expect(await repo.isInitialized()).to.be.true()
27+
})
28+
29+
it('should be true after initialization and opening', async () => {
30+
await repo.init({})
31+
await repo.open()
32+
expect(await repo.isInitialized()).to.be.true()
33+
})
34+
35+
it('should be true after initialization, opening and closing', async () => {
36+
await repo.init({})
37+
await repo.open()
38+
await repo.close()
39+
expect(await repo.isInitialized()).to.be.true()
40+
})
41+
})

test/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ describe('IPFS Repo Tests onNode.js', () => {
113113
if (!r.init) {
114114
require('./interop-test')(repo)
115115
}
116+
require('./is-initialized')
116117
}))
117118

118119
require('./blockstore-utils-test')()

0 commit comments

Comments
 (0)