From 47271e54fc1da2c75de1fdd0ea17cab17b4d5a1c Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 19 Feb 2016 19:43:25 +0000 Subject: [PATCH 1/3] start --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index cbccdcf396..681c001598 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "ipfs-multipart": "^0.1.0", "ipfs-repo": "^0.5.0", "joi": "^8.0.2", + "libp2p-ipfs": "^0.1.0", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", "peer-id": "^0.6.0", From f2acc29eb973b808a9ac24f8824ee3cb9721bfa2 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 16 Mar 2016 12:08:54 +0000 Subject: [PATCH 2/3] add loadPeerInfo and change how `id` works underneath --- package.json | 1 + src/http-api/index.js | 3 ++ src/ipfs-core/index.js | 78 ++++++++++++++++++++++++++-------- tests/test-cli/test-id.js | 10 +---- tests/test-core/browser.js | 10 +++-- tests/test-core/test-id.js | 14 +++--- tests/test-http-api/test-id.js | 6 +-- 7 files changed, 82 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 681c001598..ac8bbc83a7 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", "peer-id": "^0.6.0", + "peer-info": "^0.6.0", "ronin": "^0.3.11", "temp": "^0.8.3" } diff --git a/src/http-api/index.js b/src/http-api/index.js index fce4526485..a99fe42e6b 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -48,6 +48,8 @@ exports.start = (callback) => { // load routes require('./routes')(server) + // TODO start libp2p.Node + server.start((err) => { if (err) { return callback(err) @@ -64,5 +66,6 @@ exports.start = (callback) => { exports.stop = (callback) => { const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' fs.unlinkSync(repoPath + '/api') + // TODO stop libp2p exports.server.stop(callback) } diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index e225296abe..6f0a740289 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -8,7 +8,9 @@ const Block = blocks.Block const mDAG = require('ipfs-merkle-dag') const DAGNode = mDAG.DAGNode const DAGService = mDAG.DAGService -const peerId = require('peer-id') +const Id = require('peer-id') +const Info = require('peer-info') +const multiaddr = require('multiaddr') exports = module.exports = IPFS @@ -23,6 +25,29 @@ function IPFS (repo) { const blockS = new BlockService(repo) const dagS = new DAGService(blockS) + var peerInfo + // var libp2pNode + + // TODO populate peerInfo + loadPeerInfo() + function loadPeerInfo () { + repo.exists((err, exists) => { + if (err) { + throw err + } + + repo.config.get((err, config) => { + if (err) { + throw err + } + const pid = Id.createFromPrivKey(config.Identity.PrivKey) + peerInfo = new Info(pid) + config.Addresses.Swarm.forEach((addr) => { + peerInfo.multiaddr.add(multiaddr(addr)) + }) + }) + }) + } this.version = (opts, callback) => { if (typeof opts === 'function') { @@ -46,23 +71,20 @@ function IPFS (repo) { callback = opts opts = {} } - repo.exists((err, exists) => { - if (err) { return callback(err) } - - repo.config.get((err, config) => { - if (err) { - return callback(err) - } - var pid = peerId.createFromPrivKey(config.Identity.PrivKey) - callback(null, { - ID: config.Identity.PeerID, - PublicKey: pid.pubKey.toString('base64'), - Addresses: config.Addresses, - AgentVersion: 'js-ipfs', - ProtocolVersion: '9000' - }) + if (!peerInfo) { // because of split second warmup + setTimeout(ready, 100) + } else { + ready() + } + function ready () { + callback(null, { + ID: peerInfo.id.toB58String(), + PublicKey: peerInfo.id.pubKey.toString('base64'), + Addresses: peerInfo.multiaddrs.map((ma) => { return ma.toString() }), + AgentVersion: 'js-ipfs', + ProtocolVersion: '9000' }) - }) + } } this.repo = { @@ -281,4 +303,26 @@ function IPFS (repo) { }) } } + + this.libp2p = { + start: () => { + + }, + stop: () => { + }, + swarm: { + peers: notImpl, + addrs: notImpl, + connect: notImpl, + disconnect: notImpl, + filters: notImpl + }, + routing: {}, + records: {}, + ping: notImpl + } +} + +function notImpl () { + throw new Error('Not implemented yet') } diff --git a/tests/test-cli/test-id.js b/tests/test-cli/test-id.js index 66077d2bfd..786d6cf181 100644 --- a/tests/test-cli/test-id.js +++ b/tests/test-cli/test-id.js @@ -11,10 +11,7 @@ describe('id', () => { .run((err, stdout, exitcode) => { var expected = [ "{ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',", " PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',", - ' Addresses: ', - " { Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],", - " API: '/ip4/127.0.0.1/tcp/6001',", - " Gateway: '/ip4/127.0.0.1/tcp/9090' },", + " Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],", " AgentVersion: 'js-ipfs',", " ProtocolVersion: '9000' }" ] @@ -53,10 +50,7 @@ describe('id', () => { .run((err, stdout, exitcode) => { var expected = [ "{ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',", " PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',", - ' Addresses: ', - " { Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],", - " API: '/ip4/127.0.0.1/tcp/6001',", - " Gateway: '/ip4/127.0.0.1/tcp/9090' },", + " Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],", " AgentVersion: 'js-ipfs',", " ProtocolVersion: '9000' }" ] diff --git a/tests/test-core/browser.js b/tests/test-core/browser.js index 415d9ed01a..ef09f016b2 100644 --- a/tests/test-core/browser.js +++ b/tests/test-core/browser.js @@ -43,8 +43,10 @@ describe('IPFS Repo Tests on the Browser', function () { }, done) }) - const testsContext = require.context('.', true, /test-*/) - testsContext - .keys() - .forEach((key) => testsContext(key)) + it('', () => { + const testsContext = require.context('.', true, /test-*/) + testsContext + .keys() + .forEach((key) => testsContext(key)) + }) }) diff --git a/tests/test-core/test-id.js b/tests/test-core/test-id.js index d64ddff413..2cfda9e239 100644 --- a/tests/test-core/test-id.js +++ b/tests/test-core/test-id.js @@ -6,17 +6,19 @@ process.env.IPFS_PATH = process.cwd() + '/tests/repo-example' const IPFS = require('../../src/ipfs-core') describe('id', () => { + var ipfs + + before((done) => { + ipfs = new IPFS() + setTimeout(done, 200) + }) + it('get id', (done) => { - const ipfs = new IPFS() ipfs.id((err, id) => { expect(err).to.not.exist expect(id).to.deep.equal({ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', - Addresses: { - Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ], - API: '/ip4/127.0.0.1/tcp/6001', - Gateway: '/ip4/127.0.0.1/tcp/9090' - }, + Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ], AgentVersion: 'js-ipfs', ProtocolVersion: '9000' }) diff --git a/tests/test-http-api/test-id.js b/tests/test-http-api/test-id.js index 517c485e65..cef1b6e671 100644 --- a/tests/test-http-api/test-id.js +++ b/tests/test-http-api/test-id.js @@ -46,11 +46,7 @@ describe('id', () => { const idResult = { ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A', PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=', - Addresses: { - Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ], - API: '/ip4/127.0.0.1/tcp/6001', - Gateway: '/ip4/127.0.0.1/tcp/9090' - }, + Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ], AgentVersion: 'js-ipfs', ProtocolVersion: '9000' } From fdd37b1ebb1e5ebac7694ba9bdfabb9191835c01 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 16 Mar 2016 14:32:41 +0000 Subject: [PATCH 3/3] lots of changes all over the place - expose a ipfs.load() function fto load its peerInfo, required to change every call to understand async --- src/cli/commands/bootstrap/add.js | 14 ++- src/cli/commands/bootstrap/list.js | 20 ++-- src/cli/commands/bootstrap/rm.js | 14 ++- src/cli/commands/cat.js | 4 +- src/cli/commands/config.js | 74 ++++++++------- src/cli/commands/config/edit.js | 146 +++++++++++++++-------------- src/cli/commands/config/replace.js | 20 ++-- src/cli/commands/config/show.js | 18 ++-- src/cli/commands/dns.js | 4 +- src/cli/commands/get.js | 3 +- src/cli/commands/id.js | 14 ++- src/cli/commands/init.js | 5 +- src/cli/commands/ls.js | 5 +- src/cli/commands/object/data.js | 37 ++++---- src/cli/commands/object/get.js | 47 +++++----- src/cli/commands/object/links.js | 39 ++++---- src/cli/commands/object/new.js | 23 ++--- src/cli/commands/object/put.js | 35 +++---- src/cli/commands/object/stat.js | 25 ++--- src/cli/commands/ping.js | 5 +- src/cli/commands/repo/gc.js | 8 +- src/cli/commands/repo/init.js | 8 +- src/cli/commands/repo/version.js | 21 +++-- src/cli/commands/resolve.js | 5 +- src/cli/commands/version.js | 24 ++--- src/cli/utils.js | 10 +- src/http-api/index.js | 79 ++++++++-------- src/ipfs-core/index.js | 5 +- tests/test-cli/test-bootstrap.js | 4 +- tests/test-cli/test-config.js | 4 +- tests/test-core/test-block.js | 7 +- tests/test-core/test-bootstrap.js | 7 +- tests/test-core/test-config.js | 8 +- tests/test-core/test-id.js | 2 +- tests/test-core/test-object.js | 2 +- tests/test-core/test-version.js | 8 +- 36 files changed, 403 insertions(+), 351 deletions(-) diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index f891438534..80625d1dd9 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -1,9 +1,9 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:bootstrap') +const utils = require('../../utils') log.error = debug('cli:bootstrap:error') module.exports = Command.extend({ @@ -12,9 +12,15 @@ module.exports = Command.extend({ options: {}, run: (multiaddr) => { - var node = new IPFS() - node.bootstrap.add(multiaddr, (err, list) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.add(multiaddr, (err, list) => { + if (err) { + return log.error(err) + } + }) }) } }) diff --git a/src/cli/commands/bootstrap/list.js b/src/cli/commands/bootstrap/list.js index 9ca2f1e238..a6b1478b49 100644 --- a/src/cli/commands/bootstrap/list.js +++ b/src/cli/commands/bootstrap/list.js @@ -1,7 +1,7 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') +const utils = require('../../utils') const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') @@ -11,12 +11,18 @@ module.exports = Command.extend({ options: {}, - run: (name) => { - var node = new IPFS() - node.bootstrap.list((err, list) => { - if (err) { return log.error(err) } - list.forEach((node) => { - console.log(node) + run: () => { + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.list((err, list) => { + if (err) { + throw err + } + list.forEach((node) => { + console.log(node) + }) }) }) } diff --git a/src/cli/commands/bootstrap/rm.js b/src/cli/commands/bootstrap/rm.js index 06bd38e935..c8a41665cf 100644 --- a/src/cli/commands/bootstrap/rm.js +++ b/src/cli/commands/bootstrap/rm.js @@ -1,10 +1,10 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Removes peers from the bootstrap list', @@ -12,9 +12,15 @@ module.exports = Command.extend({ options: {}, run: (multiaddr) => { - var node = new IPFS() - node.bootstrap.rm(multiaddr, (err, list) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.rm(multiaddr, (err, list) => { + if (err) { + return log.error(err) + } + }) }) } }) diff --git a/src/cli/commands/cat.js b/src/cli/commands/cat.js index deedf71f28..4e829e0c8c 100644 --- a/src/cli/commands/cat.js +++ b/src/cli/commands/cat.js @@ -1,7 +1,7 @@ -var Command = require('ronin').Command +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 71b8e042a4..86c4527aea 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,12 +1,10 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../ipfs-core') const debug = require('debug') const get = require('lodash.get') const set = require('lodash.set') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../utils') module.exports = Command.extend({ desc: 'Get and set IPFS config values', @@ -27,48 +25,52 @@ module.exports = Command.extend({ throw new Error("argument 'key' is required") } - var node = new IPFS() - - if (!value) { - // Get the value of a given key + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } - node.config.show((err, config) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') - } + if (!value) { + // Get the value of a given key - const value = get(config, key) - console.log(value) - }) - } else { - // Set the new value of a given key + ipfs.config.show((err, config) => { + if (err) { + log.error(err) + throw new Error('failed to read the config') + } - if (bool) { - value = (value === 'true') - } else if (json) { - try { - value = JSON.parse(value) - } catch (err) { - log.error(err) - throw new Error('invalid JSON provided') - } - } + const value = get(config, key) + console.log(value) + }) + } else { + // Set the new value of a given key - node.config.show((err, originalConfig) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') + if (bool) { + value = (value === 'true') + } else if (json) { + try { + value = JSON.parse(value) + } catch (err) { + log.error(err) + throw new Error('invalid JSON provided') + } } - const updatedConfig = set(originalConfig, key, value) - node.config.replace(updatedConfig, (err) => { + ipfs.config.show((err, originalConfig) => { if (err) { log.error(err) - throw new Error('failed to save the config') + throw new Error('failed to read the config') } + + const updatedConfig = set(originalConfig, key, value) + ipfs.config.replace(updatedConfig, (err) => { + if (err) { + log.error(err) + throw new Error('failed to save the config') + } + }) }) - }) - } + } + }) } }) diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index fd7e5f1d98..2770cecdc5 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -5,104 +5,108 @@ const spawn = require('child_process').spawn const fs = require('fs') const temp = require('temp') const async = require('async') -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Opens the config file for editing in $EDITOR', run: (name) => { - const node = new IPFS() - const editor = process.env.EDITOR - - if (!editor) { - throw new Error('ENV variable $EDITOR not set') - } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + const editor = process.env.EDITOR - function getConfig (next) { - node.config.show((err, config) => { - if (err) { - log.error(err) - next(new Error('failed to get the config')) - } + if (!editor) { + throw new Error('ENV variable $EDITOR not set') + } - next(null, config) - }) - } + function getConfig (next) { + ipfs.config.show((err, config) => { + if (err) { + log.error(err) + next(new Error('failed to get the config')) + } - function saveTempConfig (config, next) { - temp.open('ipfs-config', (err, info) => { - if (err) { - log.error(err) - next(new Error('failed to open the config')) - } + next(null, config) + }) + } - fs.write(info.fd, JSON.stringify(config, null, 2)) - fs.close(info.fd, (err) => { + function saveTempConfig (config, next) { + temp.open('ipfs-config', (err, info) => { if (err) { log.error(err) next(new Error('failed to open the config')) } + + fs.write(info.fd, JSON.stringify(config, null, 2)) + fs.close(info.fd, (err) => { + if (err) { + log.error(err) + next(new Error('failed to open the config')) + } + }) + + next(null, info.path) }) + } - next(null, info.path) - }) - } + function openEditor (path, next) { + const child = spawn(editor, [path], { + stdio: 'inherit' + }) - function openEditor (path, next) { - const child = spawn(editor, [path], { - stdio: 'inherit' - }) + child.on('exit', (err, code) => { + if (err) { + log.error(err) + throw new Error('error on the editor') + } - child.on('exit', (err, code) => { - if (err) { - log.error(err) - throw new Error('error on the editor') - } + next(null, path) + }) + } - next(null, path) - }) - } + function readTempConfig (path, next) { + fs.readFile(path, 'utf8', (err, data) => { + if (err) { + log.error(err) + next(new Error('failed to get the updated config')) + } - function readTempConfig (path, next) { - fs.readFile(path, 'utf8', (err, data) => { - if (err) { - log.error(err) - next(new Error('failed to get the updated config')) - } + try { + next(null, JSON.parse(data)) + } catch (err) { + log.error(err) + next(new Error(`failed to parse the updated config "${err.message}"`)) + } + }) + } - try { - next(null, JSON.parse(data)) - } catch (err) { - log.error(err) - next(new Error(`failed to parse the updated config "${err.message}"`)) - } - }) - } + function saveConfig (config, next) { + ipfs.config.replace(config, (err) => { + if (err) { + log.error(err) + next(new Error('failed to save the config')) + } + + next() + }) + } - function saveConfig (config, next) { - node.config.replace(config, (err) => { + async.waterfall([ + getConfig, + saveTempConfig, + openEditor, + readTempConfig, + saveConfig + ], (err) => { if (err) { - log.error(err) - next(new Error('failed to save the config')) + throw err } - - next() }) - } - - async.waterfall([ - getConfig, - saveTempConfig, - openEditor, - readTempConfig, - saveConfig - ], (err) => { - if (err) { - throw err - } }) } }) diff --git a/src/cli/commands/config/replace.js b/src/cli/commands/config/replace.js index 0279714acb..87a68014ad 100644 --- a/src/cli/commands/config/replace.js +++ b/src/cli/commands/config/replace.js @@ -1,11 +1,9 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const path = require('path') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('utils') module.exports = Command.extend({ desc: 'Replaces the config with ', @@ -13,13 +11,19 @@ module.exports = Command.extend({ options: {}, run: (configPath) => { - var node = new IPFS() - var config = require(path.resolve(process.cwd(), configPath)) + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + const config = require(path.resolve(process.cwd(), configPath)) - node.config.replace(config, (err, version) => { - if (err) { return log.error(err) } + ipfs.config.replace(config, (err, version) => { + if (err) { + throw err + } - console.log(version) + console.log(version) + }) }) } }) diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 27425530b9..444bd9f71a 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -1,10 +1,8 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Outputs the content of the config file', @@ -12,11 +10,17 @@ module.exports = Command.extend({ options: {}, run: () => { - var node = new IPFS() - node.config.show((err, config) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.config.show((err, config) => { + if (err) { + throw err + } - console.log(JSON.stringify(config, null, 4)) + console.log(JSON.stringify(config, null, 4)) + }) }) } }) diff --git a/src/cli/commands/dns.js b/src/cli/commands/dns.js index af4020b384..5d59b3e92d 100644 --- a/src/cli/commands/dns.js +++ b/src/cli/commands/dns.js @@ -1,10 +1,8 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { + run: () => { } }) diff --git a/src/cli/commands/get.js b/src/cli/commands/get.js index 947d8e4b96..104a4bccc7 100644 --- a/src/cli/commands/get.js +++ b/src/cli/commands/get.js @@ -3,6 +3,5 @@ const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index c273446f9c..24f32d3d8c 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -15,13 +15,17 @@ module.exports = Command.extend({ }, run: (name) => { - var ipfs = utils.getIPFS() - - ipfs.id((err, id) => { + utils.getIPFS((err, ipfs) => { if (err) { - return log.error(err) + throw err } - console.log(id) + + ipfs.id((err, id) => { + if (err) { + return log.error(err) + } + console.log(id) + }) }) } }) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 8b3b7af9fb..fc85528eb2 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ @@ -24,6 +22,5 @@ module.exports = Command.extend({ } }, - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/ls.js +++ b/src/cli/commands/ls.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index 98a9e32e73..1591993c42 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,25 +15,28 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.data(mh, (err, data) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } - - if (data instanceof Buffer) { - console.log(data.toString()) - return - } - - // js-ipfs-api output (http stream) - data.pipe(process.stdout) + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.data(mh, (err, data) => { + if (err) { + log.error(err) + throw err + } + + if (data instanceof Buffer) { + console.log(data.toString()) + return + } + + // js-ipfs-api output (http stream) + data.pipe(process.stdout) + }) }) } }) diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index d22e6f1502..66ecbefd8e 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,34 +15,37 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + if (utils.isDaemonOn()) { + return ipfs.object.get(key, (err, obj) => { + if (err) { + log.error(err) + throw err + } + + console.log(JSON.stringify(obj)) + }) + } - if (utils.isDaemonOn()) { - return ipfs.object.get(key, (err, obj) => { + const mh = new Buffer(bs58.decode(key)) + ipfs.object.get(mh, (err, obj) => { if (err) { log.error(err) throw err } - console.log(JSON.stringify(obj)) + console.log(JSON.stringify({ + Links: obj.links.map((link) => ({ + Name: link.name, + Hash: bs58.encode(link.hash).toString(), + Size: link.size + })), + Data: obj.data.toString() + })) }) - } - - const mh = new Buffer(bs58.decode(key)) - ipfs.object.get(mh, (err, obj) => { - if (err) { - log.error(err) - throw err - } - - console.log(JSON.stringify({ - Links: obj.links.map((link) => ({ - Name: link.name, - Hash: bs58.encode(link.hash).toString(), - Size: link.size - })), - Data: obj.data.toString() - })) }) } }) diff --git a/src/cli/commands/object/links.js b/src/cli/commands/object/links.js index d2973a04c4..7cb20a26f8 100644 --- a/src/cli/commands/object/links.js +++ b/src/cli/commands/object/links.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,27 +15,30 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.links(mh, (err, links) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } - - if (links.Links) { // js-ipfs-api output - links.Links.forEach((link) => { - console.log(link.Hash, link.Size, link.Name) + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.links(mh, (err, links) => { + if (err) { + log.error(err) + throw err + } + + if (links.Links) { // js-ipfs-api output + links.Links.forEach((link) => { + console.log(link.Hash, link.Size, link.Name) + }) + return + } + + links.forEach((link) => { + console.log(bs58.encode(link.hash).toString(), link.size, link.name) }) - return - } - - links.forEach((link) => { - console.log(bs58.encode(link.hash).toString(), link.size, link.name) }) }) } diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index e6191550e1..a6df1069a1 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -13,20 +11,23 @@ module.exports = Command.extend({ options: {}, run: (template) => { - var ipfs = utils.getIPFS() - - ipfs.object.new(template, (err, obj) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } + ipfs.object.new(template, (err, obj) => { + if (err) { + log.error(err) + throw err + } - if (typeof obj.Hash === 'string') { // js-ipfs-api output - console.log(obj.Hash) - return - } + if (typeof obj.Hash === 'string') { // js-ipfs-api output + console.log(obj.Hash) + return + } - console.log(bs58.encode(obj.Hash).toString()) + console.log(bs58.encode(obj.Hash).toString()) + }) }) } }) diff --git a/src/cli/commands/object/put.js b/src/cli/commands/object/put.js index 0608016cf3..792d16fd66 100644 --- a/src/cli/commands/object/put.js +++ b/src/cli/commands/object/put.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -29,28 +27,31 @@ function parseJSONBuffer (buf) { } function parseAndAddNode (buf) { - var ipfs = utils.getIPFS() + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + if (utils.isDaemonOn()) { + return ipfs.object.put(buf, 'json', (err, obj) => { + if (err) { + log.error(err) + throw err + } + + console.log('added', obj.Hash) + }) + } - if (utils.isDaemonOn()) { - return ipfs.object.put(buf, 'json', (err, obj) => { + const parsed = parseJSONBuffer(buf) + const dagNode = new DAGNode(parsed.data, parsed.links) + ipfs.object.put(dagNode, (err, obj) => { if (err) { log.error(err) throw err } - console.log('added', obj.Hash) + console.log('added', bs58.encode(dagNode.multihash()).toString()) }) - } - - const parsed = parseJSONBuffer(buf) - const dagNode = new DAGNode(parsed.data, parsed.links) - ipfs.object.put(dagNode, (err, obj) => { - if (err) { - log.error(err) - throw err - } - - console.log('added', bs58.encode(dagNode.multihash()).toString()) }) } diff --git a/src/cli/commands/object/stat.js b/src/cli/commands/object/stat.js index 7056eb0a9c..f0adfebe33 100644 --- a/src/cli/commands/object/stat.js +++ b/src/cli/commands/object/stat.js @@ -17,22 +17,25 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.stat(mh, (err, stats) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.stat(mh, (err, stats) => { + if (err) { + log.error(err) + throw err + } - delete stats.Hash // only for js-ipfs-api output + delete stats.Hash // only for js-ipfs-api output - Object.keys(stats).forEach((key) => { - console.log(`${key}: ${stats[key]}`) + Object.keys(stats).forEach((key) => { + console.log(`${key}: ${stats[key]}`) + }) }) }) } diff --git a/src/cli/commands/ping.js b/src/cli/commands/ping.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/ping.js +++ b/src/cli/commands/ping.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/repo/gc.js b/src/cli/commands/repo/gc.js index 6900dc7727..2228495248 100644 --- a/src/cli/commands/repo/gc.js +++ b/src/cli/commands/repo/gc.js @@ -1,11 +1,9 @@ -var Command = require('ronin').Command -// var IPFS = require('../../ipfs-core') +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - options: { - }, + options: {}, - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/repo/init.js b/src/cli/commands/repo/init.js index 6900dc7727..2228495248 100644 --- a/src/cli/commands/repo/init.js +++ b/src/cli/commands/repo/init.js @@ -1,11 +1,9 @@ -var Command = require('ronin').Command -// var IPFS = require('../../ipfs-core') +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - options: { - }, + options: {}, - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/repo/version.js b/src/cli/commands/repo/version.js index f3800e0e09..8ce4b4fe8b 100644 --- a/src/cli/commands/repo/version.js +++ b/src/cli/commands/repo/version.js @@ -1,19 +1,22 @@ -var Command = require('ronin').Command -var IPFS = require('../../../ipfs-core') +const Command = require('ronin').Command +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Shows IPFS repo version information', - options: { - }, + options: {}, - run: function (name) { - var node = new IPFS() - node.repo.version(function (err, version) { + run: function () { + utils.getIPFS((err, ipfs) => { if (err) { - return console.error(err) + throw err } - console.log(version) + ipfs.repo.version(function (err, version) { + if (err) { + return console.error(err) + } + console.log(version) + }) }) } }) diff --git a/src/cli/commands/resolve.js b/src/cli/commands/resolve.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/resolve.js +++ b/src/cli/commands/resolve.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 09375cf4b0..a206a12ce8 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../utils') const debug = require('debug') @@ -25,17 +23,21 @@ module.exports = Command.extend({ } }, - run: (name) => { - var ipfs = utils.getIPFS() - ipfs.version((err, version) => { - if (err) { return log.error(err) } - - if (typeof version === 'object') { // js-ipfs-api output - console.log('ipfs version', version.Version) - return + run: () => { + utils.getIPFS((err, ipfs) => { + if (err) { + throw err } + ipfs.version((err, version) => { + if (err) { return log.error(err) } + + if (typeof version === 'object') { // js-ipfs-api output + console.log('ipfs version', version.Version) + return + } - console.log('ipfs version', version) + console.log('ipfs version', version) + }) }) } }) diff --git a/src/cli/utils.js b/src/cli/utils.js index 7a5be59c7e..94756d6221 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -33,10 +33,14 @@ function getAPICtl () { return APIctl(apiAddr.toString()) } -exports.getIPFS = () => { +exports.getIPFS = (callback) => { if (!isDaemonOn()) { - return new IPFS() + const ipfs = new IPFS() + ipfs.load(() => { + callback(null, ipfs) + }) + return } - return getAPICtl() + callback(null, getAPICtl()) } diff --git a/src/http-api/index.js b/src/http-api/index.js index a99fe42e6b..de961a5902 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -12,53 +12,54 @@ exports = module.exports exports.start = (callback) => { const ipfs = exports.ipfs = new IPFS() - - const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' - try { - fs.statSync(repoPath + '/api') - console.log('This repo is currently being used by another daemon') - process.exit(1) - } catch (err) { - fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'}) - } - - ipfs.config.show((err, config) => { - if (err) { - return callback(err) + ipfs.load(() => { + const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' + try { + fs.statSync(repoPath + '/api') + console.log('This repo is currently being used by another daemon') + process.exit(1) + } catch (err) { + fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'}) } - // TODO: set up cors correctly, following config - var server = exports.server = new Hapi.Server({ - connections: { - routes: { - cors: true - } + ipfs.config.show((err, config) => { + if (err) { + return callback(err) } - }) - const api = config.Addresses.API.split('/') - const gateway = config.Addresses.Gateway.split('/') - // for the CLI to know the where abouts of the API - fs.writeFileSync(repoPath + '/api', config.Addresses.API) + // TODO: set up cors correctly, following config + var server = exports.server = new Hapi.Server({ + connections: { + routes: { + cors: true + } + } + }) + const api = config.Addresses.API.split('/') + const gateway = config.Addresses.Gateway.split('/') - // select which connection with server.select(