Skip to content

Commit 2e9cf0d

Browse files
JonKronedaviddias
authored andcommitted
fix: daemon should recognize previously created repos. (#212)
1 parent 4d0aafb commit 2e9cf0d

File tree

7 files changed

+35
-25
lines changed

7 files changed

+35
-25
lines changed

src/endpoint/routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ module.exports = (server) => {
6363
if (err) {
6464
return reply(boom.badRequest(err))
6565
}
66-
6766
const id = hat()
67+
const initialized = ipfsd.initialized
6868
nodes[id] = ipfsd
6969

7070
let api = null
@@ -80,7 +80,7 @@ module.exports = (server) => {
8080
}
8181
}
8282

83-
reply({ id: id, api: api })
83+
reply({ id: id, api: api, initialized: initialized })
8484
})
8585
}
8686
})

src/factory-client.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ class FactoryClient {
9595
const apiAddr = res.body.api ? res.body.api.apiAddr : ''
9696
const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : ''
9797

98-
const ipfsd = new DaemonClient(this.baseUrl, res.body.id, apiAddr, gatewayAddr)
98+
const ipfsd = new DaemonClient(
99+
this.baseUrl,
100+
res.body.id,
101+
res.body.initialized,
102+
apiAddr,
103+
gatewayAddr
104+
)
99105

100106
callback(null, ipfsd)
101107
})

src/factory-in-proc.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,22 @@ class FactoryInProc {
128128
}
129129

130130
const node = new Node(options)
131-
node.once('ready', () => {
132-
series([
133-
(cb) => options.init
134-
? node.init(cb)
135-
: cb(),
136-
(cb) => options.start
137-
? node.start(options.args, cb)
138-
: cb()
139-
], (err) => callback(err, node))
140-
})
131+
132+
series([
133+
(cb) => node.once('ready', cb),
134+
(cb) => node.repo._isInitialized(err => {
135+
// if err exists, repo failed to find config or the ipfs-repo package
136+
// version is different than that of the existing repo.
137+
node.initialized = !err
138+
cb()
139+
}),
140+
(cb) => options.init
141+
? node.init(cb)
142+
: cb(),
143+
(cb) => options.start
144+
? node.start(options.args, cb)
145+
: cb()
146+
], (err) => callback(err, node))
141147
}
142148
}
143149

src/ipfsd-client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ function createApi (apiAddr, gwAddr) {
2121
}
2222

2323
class DaemonClient {
24-
constructor (baseUrl, _id, apiAddr, gwAddrs) {
24+
constructor (baseUrl, _id, initialized, apiAddr, gwAddrs) {
2525
this.baseUrl = baseUrl
2626
this._id = _id
2727
this._apiAddr = multiaddr(apiAddr)
2828
this._gwAddr = multiaddr(gwAddrs)
29-
this.initialized = false
29+
this.initialized = initialized
3030
this.started = false
3131
this.api = createApi(apiAddr, gwAddrs)
3232
}

src/ipfsd-daemon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Daemon {
5252
this.disposable = this.opts.disposable
5353
this.exec = this.opts.exec || process.env.IPFS_EXEC || findIpfsExecutable(this.opts.type, rootPath)
5454
this.subprocess = null
55-
this.initialized = fs.existsSync(path)
55+
this.initialized = fs.existsSync(this.path)
5656
this.clean = true
5757
this._apiAddr = null
5858
this._gatewayAddr = null

src/ipfsd-in-proc.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class Node extends EventEmitter {
3737
this._apiAddr = null
3838
this._gatewayAddr = null
3939
this._started = false
40-
this.initialized = false
4140
this.api = null
4241
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null
4342

@@ -155,7 +154,7 @@ class Node extends EventEmitter {
155154
(cb) => this.getConfig(cb),
156155
(conf, cb) => this.replaceConfig(defaults({}, this.opts.config, conf), cb)
157156
], (err) => {
158-
if (err) { return callback }
157+
if (err) { return callback(err) }
159158
self.clean = false
160159
self.initialized = true
161160
return callback()

test/spawn-options.spec.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('Spawn options', function () {
7979
expect(err).to.not.exist()
8080
expect(_ipfsd).to.exist()
8181
expect(_ipfsd.api).to.not.exist()
82+
expect(_ipfsd.initialized).to.eql(false)
8283

8384
ipfsd = _ipfsd
8485
repoPath = _ipfsd.repoPath
@@ -114,12 +115,7 @@ describe('Spawn options', function () {
114115
})
115116
})
116117

117-
describe('spawn from a initialized repo', () => {
118-
// TODO: figure out why `proc` IPFS refuses
119-
// to start with a provided repo
120-
// `Error: Not able to start from state: uninitalized`
121-
if (fOpts.type === 'proc') { return }
122-
118+
describe('spawn from an initialized repo', () => {
123119
let ipfsd
124120

125121
it('f.spawn', function (done) {
@@ -135,9 +131,12 @@ describe('Spawn options', function () {
135131
f.spawn(options, (err, _ipfsd) => {
136132
expect(err).to.not.exist()
137133
expect(_ipfsd).to.exist()
138-
expect(_ipfsd.api).to.not.exist()
139134

140135
ipfsd = _ipfsd
136+
137+
expect(ipfsd.api).to.not.exist()
138+
expect(ipfsd.initialized).to.eql(true)
139+
141140
done()
142141
})
143142
})

0 commit comments

Comments
 (0)