From 4e51b4264a8a7630e627b994385c385faf03d17d Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 17 Mar 2017 12:32:36 +0000 Subject: [PATCH] test: cli tests on node 7 are now always passing, made the http-api daemon start using the new init api \o/ --- package.json | 3 +- src/http-api/index.js | 182 ++++++++++++------------ test/cli/daemon.js | 3 +- test/core/create-node.spec.js | 13 +- test/utils/ipfs-factory-daemon/index.js | 7 +- 5 files changed, 114 insertions(+), 94 deletions(-) diff --git a/package.json b/package.json index adec5ef9b3..7806dd778b 100644 --- a/package.json +++ b/package.json @@ -94,8 +94,8 @@ "async": "^2.1.5", "bl": "^1.2.0", "boom": "^4.2.0", - "debug": "^2.6.3", "cids": "^0.4.2", + "debug": "^2.6.3", "fs-pull-blob-store": "~0.4.1", "glob": "^7.1.1", "hapi": "^16.1.0", @@ -126,6 +126,7 @@ "mkdirp": "^0.5.1", "multiaddr": "^2.2.2", "multihashes": "~0.4.4", + "once": "^1.4.0", "path-exists": "^3.0.0", "peer-book": "~0.3.1", "peer-id": "~0.8.4", diff --git a/src/http-api/index.js b/src/http-api/index.js index f941c7e062..f465b8ae72 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -1,6 +1,6 @@ 'use strict' -const parallel = require('async/parallel') +const series = require('async/series') const Hapi = require('hapi') const debug = require('debug') const fs = require('fs') @@ -9,6 +9,7 @@ const IPFSRepo = require('ipfs-repo') const multiaddr = require('multiaddr') const Store = require('fs-pull-blob-store') const setHeader = require('hapi-set-header') +const once = require('once') const log = debug('api') log.error = debug('api:error') @@ -21,113 +22,116 @@ function uriToMultiaddr (uri) { return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}` } -exports = module.exports = function HttpApi (repo) { - this.ipfs = null - this.server = null +function HttpApi (repo) { + this.node = undefined + this.server = undefined this.start = (callback) => { if (typeof repo === 'string') { repo = new IPFSRepo(repo, {stores: Store}) } - this.ipfs = new IPFS({ - repo: repo, - init: false, - start: false, - EXPERIMENTAL: { - pubsub: true - } - }) - - const repoPath = this.ipfs.repo.path() - - try { - fs.statSync(repoPath) - } catch (err) { - return callback(err) - } - - console.log('Starting at %s', this.ipfs.repo.path()) - - const apiPath = path.join(repoPath, 'api') - - try { - fs.statSync(apiPath) - console.log('This repo is currently being used by another daemon') - process.exit(1) - } catch (err) {} - - this.ipfs._repo.config.get((err, config) => { - if (err) { - return callback(err) - } - - // CORS is enabled by default - this.server = new Hapi.Server({ connections: { routes: { cors: true } } }) - - this.server.app.ipfs = this.ipfs - const api = config.Addresses.API.split('/') - const gateway = config.Addresses.Gateway.split('/') - - // select which connection with server.select(