File tree 5 files changed +71
-0
lines changed
5 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,10 @@ Get a value at the root of the repo.
183
183
184
184
[ Blocks] ( https://github.com/ipfs/js-ipfs-block#readme ) :
185
185
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
+
186
190
#### ` Promise repo.blocks.put (block:Block) `
187
191
188
192
* ` block ` should be of type [ Block] ( https://github.com/ipfs/js-ipfs-block#readme ) .
Original file line number Diff line number Diff line change @@ -69,6 +69,30 @@ class IpfsRepo {
69
69
await this . version . set ( constants . repoVersion )
70
70
}
71
71
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
+
72
96
/**
73
97
* Open the repo. If the repo is already open an error will be thrown.
74
98
* If the repo is not initialized it will throw an error.
Original file line number Diff line number Diff line change @@ -38,4 +38,5 @@ describe('IPFS Repo Tests on the Browser', () => {
38
38
require ( './config-test' ) ( repo )
39
39
require ( './api-addr-test' ) ( repo )
40
40
require ( './lock-test' ) ( repo )
41
+ require ( './is-initialized' )
41
42
} )
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change @@ -113,6 +113,7 @@ describe('IPFS Repo Tests onNode.js', () => {
113
113
if ( ! r . init ) {
114
114
require ( './interop-test' ) ( repo )
115
115
}
116
+ require ( './is-initialized' )
116
117
} ) )
117
118
118
119
require ( './blockstore-utils-test' ) ( )
You can’t perform that action at this time.
0 commit comments