From 2c93001047c596cb51373e18e29d957cc2e50061 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 3 Jul 2017 07:12:09 +0100 Subject: [PATCH 1/3] feat: move runtime dependent configs (repo and config) to runtime folder --- gulpfile.js | 2 +- package.json | 7 +++++-- src/core/components/bootstrap.js | 6 +----- src/core/components/init.js | 6 +----- src/core/index.js | 3 ++- .../runtime/config-browser.json} | 0 .../runtime/config-nodejs.json} | 0 .../{default-repo-browser.js => runtime/repo-browser.js} | 0 src/core/{default-repo.js => runtime/repo-nodejs.js} | 0 test/core/bitswap.spec.js | 2 +- test/core/bootstrap.spec.js | 5 ++--- test/core/create-node.spec.js | 5 ++--- test/core/files-sharding.spec.js | 2 +- test/core/init.spec.js | 5 ++--- test/http-api/over-ipfs-api/bootstrap.js | 1 + test/http-api/spec/bootstrap.js | 2 +- test/utils/{create-repo-node.js => create-repo-nodejs.js} | 0 test/utils/ipfs-factory-instance/index.js | 2 +- 18 files changed, 21 insertions(+), 27 deletions(-) rename src/{init-files/default-config-browser.json => core/runtime/config-browser.json} (100%) rename src/{init-files/default-config-node.json => core/runtime/config-nodejs.json} (100%) rename src/core/{default-repo-browser.js => runtime/repo-browser.js} (100%) rename src/core/{default-repo.js => runtime/repo-nodejs.js} (100%) rename test/utils/{create-repo-node.js => create-repo-nodejs.js} (100%) diff --git a/gulpfile.js b/gulpfile.js index 323a636c17..2be23a3e2b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,7 +3,7 @@ const gulp = require('gulp') const parallel = require('async/parallel') const series = require('async/series') -const createTempRepo = require('./test/utils/create-repo-node.js') +const createTempRepo = require('./test/utils/create-repo-nodejs.js') const HTTPAPI = require('./src/http-api') const leftPad = require('left-pad') diff --git a/package.json b/package.json index 634237a757..eb9c6cfb94 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,12 @@ "main": "src/core/index.js", "browser": { "libp2p-ipfs-nodejs": "libp2p-ipfs-browser", - "./src/core/default-repo.js": "./src/core/default-repo-browser.js", + "./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js", + + "./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js", + "./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js", "./src/core/components/init-assets.js": false, - "./test/utils/create-repo-node.js": "./test/utils/create-repo-browser.js", + "./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js", "stream": "readable-stream" }, "engines": { diff --git a/src/core/components/bootstrap.js b/src/core/components/bootstrap.js index af2bd95818..affa57b136 100644 --- a/src/core/components/bootstrap.js +++ b/src/core/components/bootstrap.js @@ -1,10 +1,6 @@ 'use strict' -const isNode = require('detect-node') - -const defaultNodes = isNode - ? require('../../init-files/default-config-node.json').Bootstrap - : require('../../init-files/default-config-browser.json').Bootstrap +const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap module.exports = function bootstrap (self) { return { diff --git a/src/core/components/init.js b/src/core/components/init.js index 0e796ea58f..b708c9ac00 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -3,8 +3,8 @@ const peerId = require('peer-id') const waterfall = require('async/waterfall') const parallel = require('async/parallel') -const isNode = require('detect-node') const promisify = require('promisify-es6') +const config = require('../runtime/config-nodejs.json') const addDefaultAssets = require('./init-assets') @@ -37,10 +37,6 @@ module.exports = function init (self) { opts.bits = Number(opts.bits) || 2048 opts.log = opts.log || function () {} - const config = isNode - ? require('../../init-files/default-config-node.json') - : require('../../init-files/default-config-browser.json') - waterfall([ // Verify repo does not yet exist. (cb) => self._repo.exists(cb), diff --git a/src/core/index.js b/src/core/index.js index 22acff0c8f..6f01d7f2ef 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -12,9 +12,10 @@ const debug = require('debug') const extend = require('deep-extend') const EventEmitter = require('events') -const defaultRepo = require('./default-repo') const boot = require('./boot') const components = require('./components') +// replaced by repo-browser when running in the browser +const defaultRepo = require('./runtime/repo-nodejs') class IPFS extends EventEmitter { constructor (options) { diff --git a/src/init-files/default-config-browser.json b/src/core/runtime/config-browser.json similarity index 100% rename from src/init-files/default-config-browser.json rename to src/core/runtime/config-browser.json diff --git a/src/init-files/default-config-node.json b/src/core/runtime/config-nodejs.json similarity index 100% rename from src/init-files/default-config-node.json rename to src/core/runtime/config-nodejs.json diff --git a/src/core/default-repo-browser.js b/src/core/runtime/repo-browser.js similarity index 100% rename from src/core/default-repo-browser.js rename to src/core/runtime/repo-browser.js diff --git a/src/core/default-repo.js b/src/core/runtime/repo-nodejs.js similarity index 100% rename from src/core/default-repo.js rename to src/core/runtime/repo-nodejs.js diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index fee7003875..ec8044d0e3 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -21,7 +21,7 @@ const CID = require('cids') const Buffer = require('safe-buffer').Buffer // This gets replaced by '../utils/create-repo-browser.js' in the browser -const createTempRepo = require('../utils/create-repo-node.js') +const createTempRepo = require('../utils/create-repo-nodejs.js') const IPFS = require('../../src/core') diff --git a/test/core/bootstrap.spec.js b/test/core/bootstrap.spec.js index 3b2d458c96..bb50666c75 100644 --- a/test/core/bootstrap.spec.js +++ b/test/core/bootstrap.spec.js @@ -8,9 +8,8 @@ chai.use(dirtyChai) const isNode = require('detect-node') -// This gets replaced by require('../utils/create-repo-browser.js') -// in the browser -const createTempRepo = require('../utils/create-repo-node.js') +// This gets replaced by `create-repo-browser.js` in the browser +const createTempRepo = require('../utils/create-repo-nodejs.js') const IPFS = require('../../src/core') diff --git a/test/core/create-node.spec.js b/test/core/create-node.spec.js index 1684448063..eff0dc8cae 100644 --- a/test/core/create-node.spec.js +++ b/test/core/create-node.spec.js @@ -11,9 +11,8 @@ const series = require('async/series') const isNode = require('detect-node') const IPFS = require('../../src/core') -// This gets replaced by require('../utils/create-repo-browser.js') -// in the browser -const createTempRepo = require('../utils/create-repo-node.js') +// This gets replaced by `create-repo-browser.js` in the browser +const createTempRepo = require('../utils/create-repo-nodejs.js') describe('create node', () => { it('custom repoPath', (done) => { diff --git a/test/core/files-sharding.spec.js b/test/core/files-sharding.spec.js index 159c792c23..1b9167dfbb 100644 --- a/test/core/files-sharding.spec.js +++ b/test/core/files-sharding.spec.js @@ -10,7 +10,7 @@ const pull = require('pull-stream') const Buffer = require('safe-buffer').Buffer const IPFS = require('../../src/core') -const createTempRepo = require('../utils/create-repo-node.js') +const createTempRepo = require('../utils/create-repo-nodejs.js') describe('files dir', () => { const files = [] diff --git a/test/core/init.spec.js b/test/core/init.spec.js index 20822bf98b..309ef2265b 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -14,9 +14,8 @@ const multihash = require('multihashes') const CID = require('cids') const IPFS = require('../../src/core') -// This gets replaced by require('../utils/create-repo-browser.js') -// in the browser -const createTempRepo = require('../utils/create-repo-node.js') +// This gets replaced by `create-repo-browser.js` in the browser +const createTempRepo = require('../utils/create-repo-nodejs.js') describe('init', () => { if (!isNode) { return } diff --git a/test/http-api/over-ipfs-api/bootstrap.js b/test/http-api/over-ipfs-api/bootstrap.js index 1e31b40412..bbdad308ca 100644 --- a/test/http-api/over-ipfs-api/bootstrap.js +++ b/test/http-api/over-ipfs-api/bootstrap.js @@ -32,6 +32,7 @@ module.exports = (ctl) => { ctl.bootstrap.add({ default: true }, (err, res) => { expect(err).to.not.exist() peers = res.Peers + console.log(res) expect(peers).to.exist() expect(peers.length).to.be.above(1) done() diff --git a/test/http-api/spec/bootstrap.js b/test/http-api/spec/bootstrap.js index e9f726f1a9..758981a747 100644 --- a/test/http-api/spec/bootstrap.js +++ b/test/http-api/spec/bootstrap.js @@ -3,7 +3,7 @@ const expect = require('chai').expect const qs = require('qs') -const defaultList = require('../../../src/init-files/default-config-node.json').Bootstrap +const defaultList = require('../../../src/core/runtime/config-nodejs.json').Bootstrap module.exports = (http) => { describe('/bootstrap', () => { diff --git a/test/utils/create-repo-node.js b/test/utils/create-repo-nodejs.js similarity index 100% rename from test/utils/create-repo-node.js rename to test/utils/create-repo-nodejs.js diff --git a/test/utils/ipfs-factory-instance/index.js b/test/utils/ipfs-factory-instance/index.js index 2abdcbf115..9d36a361f0 100644 --- a/test/utils/ipfs-factory-instance/index.js +++ b/test/utils/ipfs-factory-instance/index.js @@ -5,7 +5,7 @@ const each = require('async/each') const defaultConfig = require('./default-config.json') const IPFS = require('../../../src/core') -const createTempRepo = require('../create-repo-node') +const createTempRepo = require('../create-repo-nodejs') module.exports = Factory From b0202602219a631f4b1622963764fbe3b6241098 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 3 Jul 2017 07:28:50 +0100 Subject: [PATCH 2/3] feat: bring libp2p bundles to this repo --- package.json | 23 ++++++--- src/core/components/libp2p.js | 2 +- src/core/runtime/libp2p-browser.js | 67 +++++++++++++++++++++++++ src/core/runtime/libp2p-nodejs.js | 80 ++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 src/core/runtime/libp2p-browser.js create mode 100644 src/core/runtime/libp2p-nodejs.js diff --git a/package.json b/package.json index eb9c6cfb94..91be568b56 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,10 @@ }, "main": "src/core/index.js", "browser": { - "libp2p-ipfs-nodejs": "libp2p-ipfs-browser", + "./src/core/components/init-assets.js": false, "./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js", - "./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js", "./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js", - "./src/core/components/init-assets.js": false, "./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js", "stream": "readable-stream" }, @@ -113,9 +111,18 @@ "is-ipfs": "^0.3.0", "isstream": "^0.1.2", "joi": "^10.6.0", + "libp2p": "^0.9.1", "libp2p-floodsub": "~0.9.4", - "libp2p-ipfs-browser": "~0.25.0", - "libp2p-ipfs-nodejs": "~0.26.0", + "libp2p-kad-dht": "^0.1.0", + "libp2p-mdns": "^0.7.0", + "libp2p-multiplex": "^0.4.3", + "libp2p-railing": "^0.5.1", + "libp2p-secio": "^0.6.8", + "libp2p-spdy": "^0.10.6", + "libp2p-swarm": "^0.29.1", + "libp2p-tcp": "^0.10.1", + "libp2p-webrtc-star": "^0.11.0", + "libp2p-websockets": "^0.10.0", "lodash.flatmap": "^4.5.0", "lodash.get": "^4.4.2", "lodash.has": "^4.5.2", @@ -128,9 +135,9 @@ "multihashes": "~0.4.5", "once": "^1.4.0", "path-exists": "^3.0.0", - "peer-book": "~0.4.0", - "peer-id": "~0.8.7", - "peer-info": "~0.9.2", + "peer-book": "^0.4.0", + "peer-id": "^0.8.7", + "peer-info": "^0.9.2", "promisify-es6": "^1.0.2", "pull-file": "^1.0.0", "pull-paramap": "^1.2.2", diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 8a7cd6bc3c..b13c8e37c2 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -1,6 +1,6 @@ 'use strict' -const Node = require('libp2p-ipfs-nodejs') +const Node = require('../runtime/libp2p-nodejs') const promisify = require('promisify-es6') const get = require('lodash.get') diff --git a/src/core/runtime/libp2p-browser.js b/src/core/runtime/libp2p-browser.js new file mode 100644 index 0000000000..1db93dea73 --- /dev/null +++ b/src/core/runtime/libp2p-browser.js @@ -0,0 +1,67 @@ +'use strict' + +const WS = require('libp2p-websockets') +const WebRTCStar = require('libp2p-webrtc-star') +const spdy = require('libp2p-spdy') +const multiplex = require('libp2p-multiplex') +const secio = require('libp2p-secio') +const Railing = require('libp2p-railing') +const libp2p = require('libp2p') + +function mapMuxers (list) { + return list.map((pref) => { + if (typeof pref !== 'string') { + return pref + } + switch (pref.trim().toLowerCase()) { + case 'spdy': + return spdy + case 'multiplex': + return multiplex + default: + throw new Error(pref + ' muxer not available') + } + }) +} + +function getMuxers (options) { + if (options) { + return mapMuxers(options) + } else { + return [multiplex, spdy] + } +} + +class Node extends libp2p { + constructor (peerInfo, peerBook, options) { + options = options || {} + const webRTCStar = new WebRTCStar() + + const modules = { + transport: [ + new WS(), + webRTCStar + ], + connection: { + muxer: getMuxers(options.muxer), + crypto: [ + secio + ] + }, + discovery: [] + } + + if (options.webRTCStar) { + modules.discovery.push(webRTCStar.discovery) + } + + if (options.bootstrap) { + const r = new Railing(options.bootstrap) + modules.discovery.push(r) + } + + super(modules, peerInfo, peerBook, options) + } +} + +module.exports = Node diff --git a/src/core/runtime/libp2p-nodejs.js b/src/core/runtime/libp2p-nodejs.js new file mode 100644 index 0000000000..08e6e077e1 --- /dev/null +++ b/src/core/runtime/libp2p-nodejs.js @@ -0,0 +1,80 @@ +'use strict' + +const TCP = require('libp2p-tcp') +const MulticastDNS = require('libp2p-mdns') +const WS = require('libp2p-websockets') +const Railing = require('libp2p-railing') +const spdy = require('libp2p-spdy') +const KadDHT = require('libp2p-kad-dht') +const multiplex = require('libp2p-multiplex') +const secio = require('libp2p-secio') +const libp2p = require('libp2p') + +function mapMuxers (list) { + return list.map((pref) => { + if (typeof pref !== 'string') { + return pref + } + switch (pref.trim().toLowerCase()) { + case 'spdy': return spdy + case 'multiplex': return multiplex + default: + throw new Error(pref + ' muxer not available') + } + }) +} + +function getMuxers (muxers) { + const muxerPrefs = process.env.LIBP2P_MUXER + if (muxerPrefs && !muxers) { + return mapMuxers(muxerPrefs.split(',')) + } else if (muxers) { + return mapMuxers(muxers) + } else { + return [multiplex, spdy] + } +} + +class Node extends libp2p { + constructor (peerInfo, peerBook, options) { + options = options || {} + + const modules = { + transport: [ + new TCP(), + new WS() + ], + connection: { + muxer: getMuxers(options.muxer), + crypto: [ secio ] + }, + discovery: [] + } + + if (options.dht) { + modules.DHT = KadDHT + } + + if (options.mdns) { + const mdns = new MulticastDNS(peerInfo, 'ipfs.local') + modules.discovery.push(mdns) + } + + if (options.bootstrap) { + const r = new Railing(options.bootstrap) + modules.discovery.push(r) + } + + if (options.modules && options.modules.transport) { + options.modules.transport.forEach((t) => modules.transport.push(t)) + } + + if (options.modules && options.modules.discovery) { + options.modules.discovery.forEach((d) => modules.discovery.push(d)) + } + + super(modules, peerInfo, peerBook, options) + } +} + +module.exports = Node From 9ff809625660d319fdc434a760b2669f51fb9b59 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 4 Jul 2017 11:46:28 +0100 Subject: [PATCH 3/3] apply cr --- src/core/components/libp2p.js | 1 + test/http-api/over-ipfs-api/bootstrap.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index b13c8e37c2..54328fe255 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -1,5 +1,6 @@ 'use strict' +// libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified const Node = require('../runtime/libp2p-nodejs') const promisify = require('promisify-es6') const get = require('lodash.get') diff --git a/test/http-api/over-ipfs-api/bootstrap.js b/test/http-api/over-ipfs-api/bootstrap.js index bbdad308ca..1e31b40412 100644 --- a/test/http-api/over-ipfs-api/bootstrap.js +++ b/test/http-api/over-ipfs-api/bootstrap.js @@ -32,7 +32,6 @@ module.exports = (ctl) => { ctl.bootstrap.add({ default: true }, (err, res) => { expect(err).to.not.exist() peers = res.Peers - console.log(res) expect(peers).to.exist() expect(peers.length).to.be.above(1) done()