diff --git a/README.md b/README.md index 8ee50b9894..df3167cb16 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,12 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/ const repo = // Create the IPFS node instance -const node = new IPFS(repo) +const node = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: false + } +}) // We need to init our repo, in this case the repo was empty // We are picking 2048 bits for the RSA key that will be our PeerId diff --git a/examples/basics/index.js b/examples/basics/index.js index a3c9c1c2ee..0c1528ea62 100644 --- a/examples/basics/index.js +++ b/examples/basics/index.js @@ -12,7 +12,12 @@ const IPFS = require('../../src/core') /* * Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs) */ -const node = new IPFS(path.join(os.tmpDir() + '/' + new Date().toString())) +const node = new IPFS({ + repo: path.join(os.tmpDir() + '/' + new Date().toString()), + EXPERIMENTAL: { + pubsub: false + } +}) const fileToAdd = { path: 'hello.txt', diff --git a/examples/bundle-browserify/src/index.js b/examples/bundle-browserify/src/index.js index f4fa15d2ee..bb95e3f48a 100644 --- a/examples/bundle-browserify/src/index.js +++ b/examples/bundle-browserify/src/index.js @@ -7,7 +7,12 @@ var IPFS = require('../../../src/core') // replace this by line below // for simplicity, we create a new repo everytime the node // is created, because you can't init already existing repos const repoPath = String(Math.random()) -const node = new IPFS(repoPath) +const node = new IPFS({ + repo: repoPath, + EXPERIMENTAL: { + pubsub: false + } +}) const concat = require('concat-stream') node.init({ emptyRepo: true, bits: 2048 }, function (err) { diff --git a/examples/bundle-webpack/src/components/app.js b/examples/bundle-webpack/src/components/app.js index 33c145b912..0b84e6404d 100644 --- a/examples/bundle-webpack/src/components/app.js +++ b/examples/bundle-webpack/src/components/app.js @@ -29,7 +29,12 @@ class App extends React.Component { // for simplicity, we create a new repo everytime the node // is created, because you can't init already existing repos const repoPath = String(Math.random()) - node = new IPFS(repoPath) + node = new IPFS({ + repo: repoPath, + EXPERIMENTAL: { + pubsub: false + } + }) node.init({ emptyRepo: true, bits: 2048 }, function (err) { if (err) { diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index dae5dbb61b..c165b53e9f 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,7 +1,7 @@ 'use strict' -const IpfsRepo = require('ipfs-repo') -const Ipfs = require('../../core') +const Repo = require('ipfs-repo') +const IPFS = require('../../core') const Store = require('fs-pull-blob-store') const utils = require('../utils') @@ -35,18 +35,21 @@ module.exports = { const log = utils.createLogger(true) log(`initializing ipfs node at ${path}`) - const repo = new IpfsRepo(path, { + const repo = new Repo(path, { stores: Store }) - const ipfs = new Ipfs(repo) + const ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: {} + }) ipfs.init({ bits: argv.bits, force: argv.force, emptyRepo: argv.emptyRepo, log - }, function (err) { + }, (err) => { if (err) { console.error(err.toString()) process.exit(1) diff --git a/src/cli/utils.js b/src/cli/utils.js index be2695f815..b46731f78d 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -36,10 +36,13 @@ function getAPICtl () { exports.getIPFS = (callback) => { if (!isDaemonOn()) { - const ipfs = new IPFS(exports.getRepoPath()) - ipfs.load(() => { - callback(null, ipfs) + const ipfs = new IPFS({ + repo: exports.getRepoPath(), + EXPERIMENTAL: { + pubsub: true + } }) + ipfs.load(() => callback(null, ipfs)) return } diff --git a/src/core/components/go-offline.js b/src/core/components/go-offline.js index f0786702da..e68f450559 100644 --- a/src/core/components/go-offline.js +++ b/src/core/components/go-offline.js @@ -4,11 +4,18 @@ module.exports = (self) => { return (callback) => { self._blockService.goOffline() self._bitswap.stop() - self._pubsub.stop((err) => { + + if (self._configOpts.EXPERIMENTAL.pubsub) { + self._pubsub.stop(next) + } else { + next() + } + + function next (err) { if (err) { return callback(err) } self.libp2p.stop(callback) - }) + } } } diff --git a/src/core/components/go-online.js b/src/core/components/go-online.js index d710478899..fb9b407b17 100644 --- a/src/core/components/go-online.js +++ b/src/core/components/go-online.js @@ -31,9 +31,13 @@ module.exports = (self) => { self._blockService.goOnline(self._bitswap) cb() }, - (cb) => self._pubsub.start(cb) // , - // For all of the protocols to handshake with each other - // (cb) => setTimeout(cb, 1000) // Still not decided if we want this + (cb) => { + if (self._configOpts.EXPERIMENTAL.pubsub) { + self._pubsub.start(cb) + } else { + cb() + } + } ], callback) }) } diff --git a/src/core/components/index.js b/src/core/components/index.js new file mode 100644 index 0000000000..6d8193c5e4 --- /dev/null +++ b/src/core/components/index.js @@ -0,0 +1,20 @@ +'use strict' + +exports.goOnline = require('./go-online') +exports.goOffline = require('./go-offline') +exports.isOnline = require('./is-online') +exports.load = require('./load') +exports.version = require('./version') +exports.id = require('./id') +exports.repo = require('./repo') +exports.init = require('./init') +exports.bootstrap = require('./bootstrap') +exports.config = require('./config') +exports.block = require('./block') +exports.object = require('./object') +exports.libp2p = require('./libp2p') +exports.swarm = require('./swarm') +exports.ping = require('./ping') +exports.files = require('./files') +exports.bitswap = require('./bitswap') +exports.pubsub = require('./pubsub') diff --git a/src/core/index.js b/src/core/index.js index fa4c2e8ae2..3dc676a019 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -3,71 +3,66 @@ const BlockService = require('ipfs-block-service') const IPLDResolver = require('ipld-resolver') const PeerBook = require('peer-book') +const debug = require('debug') const defaultRepo = require('./default-repo') -const goOnline = require('./components/go-online') -const goOffline = require('./components/go-offline') -const isOnline = require('./components/is-online') -const load = require('./components/load') -const version = require('./components/version') -const id = require('./components/id') -const repo = require('./components/repo') -const init = require('./components/init') -const bootstrap = require('./components/bootstrap') -const config = require('./components/config') -const block = require('./components/block') -const object = require('./components/object') -const libp2p = require('./components/libp2p') -const swarm = require('./components/swarm') -const ping = require('./components/ping') -const files = require('./components/files') -const bitswap = require('./components/bitswap') -const pubsub = require('./components/pubsub') +const components = require('./components') -exports = module.exports = IPFS +class IPFS { + constructor (configOpts) { + let repoInstance + if (typeof configOpts.repo === 'string' || configOpts.repo === undefined) { + repoInstance = defaultRepo(configOpts.repo) + } else { + repoInstance = configOpts.repo + } + delete configOpts.repo -function IPFS (repoInstance) { - if (!(this instanceof IPFS)) { - throw new Error('Must be instantiated with new') - } - - if (typeof repoInstance === 'string' || - repoInstance === undefined) { - repoInstance = defaultRepo(repoInstance) - } + configOpts.EXPERIMENTAL = configOpts.EXPERIMENTAL || {} - // IPFS Core Internals - this._repo = repoInstance - this._peerInfoBook = new PeerBook() - this._peerInfo = null - this._libp2pNode = null - this._bitswap = null - this._blockService = new BlockService(this._repo) - this._ipldResolver = new IPLDResolver(this._blockService) - this._pubsub = null + // IPFS utils + this.types = {} + this.log = debug('jsipfs') + this.log.err = debug('jsipfs:err') - // IPFS Core exposed components + // IPFS Core Internals + this._configOpts = configOpts + this._repo = repoInstance + this._peerInfoBook = new PeerBook() + this._peerInfo = undefined + this._libp2pNode = undefined + this._bitswap = undefined + this._blockService = new BlockService(this._repo) + this._ipldResolver = new IPLDResolver(this._blockService) + this._pubsub = undefined - // for booting up a node - this.goOnline = goOnline(this) - this.goOffline = goOffline(this) - this.isOnline = isOnline(this) - this.load = load(this) - this.init = init(this) + // IPFS Core exposed components + // - for booting up a node + this.goOnline = components.goOnline(this) + this.goOffline = components.goOffline(this) + this.isOnline = components.isOnline(this) + this.load = components.load(this) + this.init = components.init(this) + // - interface-ipfs-core defined API + this.version = components.version(this) + this.id = components.id(this) + this.repo = components.repo(this) + this.bootstrap = components.bootstrap(this) + this.config = components.config(this) + this.block = components.block(this) + this.object = components.object(this) + this.libp2p = components.libp2p(this) + this.swarm = components.swarm(this) + this.files = components.files(this) + this.bitswap = components.bitswap(this) + this.ping = components.ping(this) + this.pubsub = components.pubsub(this) - // interface-ipfs-core defined API - this.version = version(this) - this.id = id(this) - this.repo = repo(this) - this.bootstrap = bootstrap(this) - this.config = config(this) - this.block = block(this) - this.object = object(this) - this.libp2p = libp2p(this) - this.swarm = swarm(this) - this.files = files(this) - this.bitswap = bitswap(this) - this.ping = ping(this) - this.pubsub = pubsub(this) + if (configOpts.EXPERIMENTAL.pubsub) { + this.log('EXPERIMENTAL pubsub is enabled') + } + } } + +module.exports = IPFS diff --git a/src/http-api/index.js b/src/http-api/index.js index 21a6bc5e44..4530d64873 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -25,7 +25,13 @@ exports = module.exports = function HttpApi (repo) { repo = new IPFSRepo(repo, {stores: Store}) } - this.ipfs = new IPFS(repo) + this.ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) + const repoPath = this.ipfs.repo.path() try { diff --git a/test/cli/index.js b/test/cli/index.js index 8682844680..80b128248f 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -20,9 +20,7 @@ describe('cli', () => { }) }) - after(() => { - clean(repoTests) - }) + after(() => clean(repoTests)) describe('--all', () => { const tests = fs.readdirSync(__dirname) diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 83bb63f4e5..57ebd48b4b 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -32,7 +32,12 @@ describe('bitswap', () => { beforeEach((done) => { const repo = createTempRepo() - inProcNode = new IPFS(repo) + inProcNode = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) series([ (cb) => inProcNode.init({ bits: 2048 }, cb), (cb) => { diff --git a/test/core/bootstrap.spec.js b/test/core/bootstrap.spec.js index 729f2ec31e..c5c0bb5f2c 100644 --- a/test/core/bootstrap.spec.js +++ b/test/core/bootstrap.spec.js @@ -18,7 +18,12 @@ describe('bootstrap', () => { before((done) => { const repo = createTempRepo() - ipfs = new IPFS(repo) + ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) series([ (cb) => ipfs.init({ bits: 1024 }, cb), (cb) => ipfs.load(cb) diff --git a/test/core/init.spec.js b/test/core/init.spec.js index 89176bbabe..16b382eba9 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -18,7 +18,12 @@ describe('init', () => { beforeEach(() => { repo = createTempRepo() - ipfs = new IPFS(repo) + ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) }) afterEach((done) => repo.teardown(done)) diff --git a/test/utils/factory-core/index.js b/test/utils/factory-core/index.js index 56598e9060..2c81ae6489 100644 --- a/test/utils/factory-core/index.js +++ b/test/utils/factory-core/index.js @@ -43,7 +43,13 @@ function Factory () { const repo = createTempRepo(repoPath) // create the IPFS node - const ipfs = new IPFS(repo) + const ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) + ipfs.init({ emptyRepo: true, bits: 1024 }, (err) => { if (err) { return callback(err) diff --git a/test/utils/factory-http/index.js b/test/utils/factory-http/index.js index 1c863adcb4..5ffe839119 100644 --- a/test/utils/factory-http/index.js +++ b/test/utils/factory-http/index.js @@ -51,7 +51,13 @@ function Factory () { } // create the IPFS node - const ipfs = new IPFS(repo) + const ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) + ipfs.init({ emptyRepo: true, bits: 1024 diff --git a/test/utils/temp-node.js b/test/utils/temp-node.js index 85fdbb5ac9..16b2df53c6 100644 --- a/test/utils/temp-node.js +++ b/test/utils/temp-node.js @@ -30,7 +30,12 @@ function setAddresses (repo, num, callback) { function createTempNode (num, callback) { const repo = createTempRepo() - const ipfs = new IPFS(repo) + const ipfs = new IPFS({ + repo: repo, + EXPERIMENTAL: { + pubsub: true + } + }) num = leftPad(num, 3, 0)