diff --git a/src/endpoint/routes.js b/src/endpoint/routes.js index 9e851c7b..9488afec 100644 --- a/src/endpoint/routes.js +++ b/src/endpoint/routes.js @@ -63,8 +63,8 @@ module.exports = (server) => { if (err) { return reply(boom.badRequest(err)) } - const id = hat() + const initialized = ipfsd.initialized nodes[id] = ipfsd let api = null @@ -80,7 +80,7 @@ module.exports = (server) => { } } - reply({ id: id, api: api }) + reply({ id: id, api: api, initialized: initialized }) }) } }) diff --git a/src/factory-client.js b/src/factory-client.js index e3c31f1e..186f37eb 100644 --- a/src/factory-client.js +++ b/src/factory-client.js @@ -95,7 +95,13 @@ class FactoryClient { const apiAddr = res.body.api ? res.body.api.apiAddr : '' const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : '' - const ipfsd = new DaemonClient(this.baseUrl, res.body.id, apiAddr, gatewayAddr) + const ipfsd = new DaemonClient( + this.baseUrl, + res.body.id, + res.body.initialized, + apiAddr, + gatewayAddr + ) callback(null, ipfsd) }) diff --git a/src/factory-in-proc.js b/src/factory-in-proc.js index d844a3ca..1a3e897d 100644 --- a/src/factory-in-proc.js +++ b/src/factory-in-proc.js @@ -128,16 +128,22 @@ class FactoryInProc { } const node = new Node(options) - node.once('ready', () => { - series([ - (cb) => options.init - ? node.init(cb) - : cb(), - (cb) => options.start - ? node.start(options.args, cb) - : cb() - ], (err) => callback(err, node)) - }) + + series([ + (cb) => node.once('ready', cb), + (cb) => node.repo._isInitialized(err => { + // if err exists, repo failed to find config or the ipfs-repo package + // version is different than that of the existing repo. + node.initialized = !err + cb() + }), + (cb) => options.init + ? node.init(cb) + : cb(), + (cb) => options.start + ? node.start(options.args, cb) + : cb() + ], (err) => callback(err, node)) } } diff --git a/src/ipfsd-client.js b/src/ipfsd-client.js index b58c835d..930b43c7 100644 --- a/src/ipfsd-client.js +++ b/src/ipfsd-client.js @@ -21,12 +21,12 @@ function createApi (apiAddr, gwAddr) { } class DaemonClient { - constructor (baseUrl, _id, apiAddr, gwAddrs) { + constructor (baseUrl, _id, initialized, apiAddr, gwAddrs) { this.baseUrl = baseUrl this._id = _id this._apiAddr = multiaddr(apiAddr) this._gwAddr = multiaddr(gwAddrs) - this.initialized = false + this.initialized = initialized this.started = false this.api = createApi(apiAddr, gwAddrs) } diff --git a/src/ipfsd-daemon.js b/src/ipfsd-daemon.js index 6b8c1762..8a44a591 100644 --- a/src/ipfsd-daemon.js +++ b/src/ipfsd-daemon.js @@ -52,7 +52,7 @@ class Daemon { this.disposable = this.opts.disposable this.exec = this.opts.exec || process.env.IPFS_EXEC || findIpfsExecutable(this.opts.type, rootPath) this.subprocess = null - this.initialized = fs.existsSync(path) + this.initialized = fs.existsSync(this.path) this.clean = true this._apiAddr = null this._gatewayAddr = null diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index 8103833b..f3f37173 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -37,7 +37,6 @@ class Node extends EventEmitter { this._apiAddr = null this._gatewayAddr = null this._started = false - this.initialized = false this.api = null this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null @@ -155,7 +154,7 @@ class Node extends EventEmitter { (cb) => this.getConfig(cb), (conf, cb) => this.replaceConfig(defaults({}, this.opts.config, conf), cb) ], (err) => { - if (err) { return callback } + if (err) { return callback(err) } self.clean = false self.initialized = true return callback() diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index 6f56be4c..7cb3c0f8 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -79,6 +79,7 @@ describe('Spawn options', function () { expect(err).to.not.exist() expect(_ipfsd).to.exist() expect(_ipfsd.api).to.not.exist() + expect(_ipfsd.initialized).to.eql(false) ipfsd = _ipfsd repoPath = _ipfsd.repoPath @@ -114,12 +115,7 @@ describe('Spawn options', function () { }) }) - describe('spawn from a initialized repo', () => { - // TODO: figure out why `proc` IPFS refuses - // to start with a provided repo - // `Error: Not able to start from state: uninitalized` - if (fOpts.type === 'proc') { return } - + describe('spawn from an initialized repo', () => { let ipfsd it('f.spawn', function (done) { @@ -135,9 +131,12 @@ describe('Spawn options', function () { f.spawn(options, (err, _ipfsd) => { expect(err).to.not.exist() expect(_ipfsd).to.exist() - expect(_ipfsd.api).to.not.exist() ipfsd = _ipfsd + + expect(ipfsd.api).to.not.exist() + expect(ipfsd.initialized).to.eql(true) + done() }) })