diff --git a/package.json b/package.json index 13f22fc7..252277ba 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "coverage-publish": "aegir-coverage publish" }, "browser": { - "libp2p-ipfs-nodejs": "libp2p-ipfs-browser" + "./test/nodejs-bundle": "./test/browser-bundle" }, "pre-commit": [ "lint", @@ -68,15 +68,23 @@ "dirty-chai": "^1.2.2", "interface-connection": "^0.3.2", "left-pad": "^1.1.3", - "libp2p-ipfs-browser": "~0.23.0", - "libp2p-ipfs-nodejs": "~0.23.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": "^4.17.4", "lodash.random": "^3.2.0", "lodash.range": "^3.2.0", "peer-book": "~0.4.0", - "pre-commit": "^1.2.2" + "pre-commit": "^1.2.2", + "safe-buffer": "^5.1.1" }, "contributors": [ "Friedel Ziegelmayer " ] -} \ No newline at end of file +} diff --git a/src/network.js b/src/network.js index 320bfe2e..6af6b4c8 100644 --- a/src/network.js +++ b/src/network.js @@ -43,7 +43,7 @@ class Network { return cb(new Error('Network is already running')) } - if (!this.dht.isRunning || !this.dht.libp2p.isOnline) { + if (!this.dht.isRunning || !this.dht.libp2p.isStarted()) { return cb(new Error('Can not start network')) } @@ -92,7 +92,7 @@ class Network { * @type {bool} */ get isConnected () { - return this.dht.libp2p.isOnline && this.dht.isRunning && this.isOnline + return this.dht.libp2p.isStarted() && this.dht.isRunning && this.isOnline } /** diff --git a/test/browser-bundle.js b/test/browser-bundle.js new file mode 100644 index 00000000..1db93dea --- /dev/null +++ b/test/browser-bundle.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/test/index.spec.js b/test/index.spec.js index 3bd338ff..bdec72f4 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -12,8 +12,9 @@ const retry = require('async/retry') const each = require('async/each') const waterfall = require('async/waterfall') const Record = require('libp2p-record').Record -const Libp2p = require('libp2p-ipfs-nodejs') +const Libp2p = require('./nodejs-bundle') const random = require('lodash.random') +const Buffer = require('safe-buffer').Buffer const _ = require('lodash') const KadDHT = require('../src') @@ -41,10 +42,8 @@ describe('DHT', () => { }) }) - afterEach((done) => { - // Give the nodes some time to finish request - setTimeout(() => util.teardown(done), 100) - }) + // Give the nodes some time to finish request + afterEach((done) => setTimeout(() => util.teardown(done), 100)) it('create', () => { const libp2p = new Libp2p(infos[0]) @@ -64,14 +63,10 @@ describe('DHT', () => { waterfall([ (cb) => connect(dhtA, dhtB, cb), - (cb) => { - dhtA.put(new Buffer('/v/hello'), new Buffer('world'), cb) - }, - (cb) => { - dhtB.get(new Buffer('/v/hello'), 1000, cb) - }, + (cb) => dhtA.put(Buffer.from('/v/hello'), Buffer.from('world'), cb), + (cb) => dhtB.get(Buffer.from('/v/hello'), 1000, cb), (res, cb) => { - expect(res).to.be.eql(new Buffer('world')) + expect(res).to.eql(Buffer.from('world')) cb() } ], done) diff --git a/test/network.spec.js b/test/network.spec.js index 36be1268..b4fb2b82 100644 --- a/test/network.spec.js +++ b/test/network.spec.js @@ -4,11 +4,12 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect -const Libp2p = require('libp2p-ipfs-nodejs') +const Libp2p = require('./nodejs-bundle') const Connection = require('interface-connection').Connection const pull = require('pull-stream') const lp = require('pull-length-prefixed') const series = require('async/series') +const Buffer = require('safe-buffer').Buffer const DHT = require('../src') const Message = require('../src/message') @@ -52,12 +53,12 @@ describe('Network', () => { } } - const msg = new Message(Message.TYPES.PING, new Buffer('hello'), 0) + const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0) // mock it libp2p.dial = (peer, protocol, callback) => { expect(protocol).to.eql('/ipfs/kad/1.0.0') - const msg = new Message(Message.TYPES.FIND_NODE, new Buffer('world'), 0) + const msg = new Message(Message.TYPES.FIND_NODE, Buffer.from('world'), 0) const rawConn = { source: pull( @@ -93,7 +94,7 @@ describe('Network', () => { } } - const msg = new Message(Message.TYPES.PING, new Buffer('hello'), 0) + const msg = new Message(Message.TYPES.PING, Buffer.from('hello'), 0) // mock it libp2p.dial = (peer, protocol, callback) => { diff --git a/test/nodejs-bundle.js b/test/nodejs-bundle.js new file mode 100644 index 00000000..620efe6b --- /dev/null +++ b/test/nodejs-bundle.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('..') +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 diff --git a/test/query.spec.js b/test/query.spec.js index 5d394309..2567a2c4 100644 --- a/test/query.spec.js +++ b/test/query.spec.js @@ -4,7 +4,8 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect -const Libp2p = require('libp2p-ipfs-nodejs') +const Libp2p = require('./nodejs-bundle') +const Buffer = require('safe-buffer').Buffer const DHT = require('../src') const Query = require('../src/query') @@ -44,7 +45,7 @@ describe('Query', () => { expect(p.id).to.eql(infos[2].id.id) return cb(null, { - value: new Buffer('cool'), + value: Buffer.from('cool'), success: true }) } @@ -57,7 +58,7 @@ describe('Query', () => { const q = new Query(dht, peer.id.id, query) q.run([infos[1].id], (err, res) => { expect(err).to.not.exist() - expect(res.value).to.eql(new Buffer('cool')) + expect(res.value).to.eql(Buffer.from('cool')) expect(res.success).to.eql(true) expect(res.finalSet.size).to.eql(2) done() diff --git a/test/rpc/index.spec.js b/test/rpc/index.spec.js index 0661c3b9..5426d979 100644 --- a/test/rpc/index.spec.js +++ b/test/rpc/index.spec.js @@ -7,7 +7,7 @@ const expect = chai.expect const pull = require('pull-stream') const lp = require('pull-length-prefixed') const Connection = require('interface-connection').Connection -const Libp2p = require('libp2p-ipfs-nodejs') +const Libp2p = require('./../nodejs-bundle') const Message = require('../../src/message') const Dht = require('../../src') @@ -35,13 +35,13 @@ describe('rpc', () => { const dht = new Dht(libp2p) dht.peerBook.put(infos[1]) - const msg = new Message(Message.TYPES.GET_VALUE, new Buffer('hello'), 5) + const msg = new Message(Message.TYPES.GET_VALUE, Buffer.from('hello'), 5) const conn = makeConnection(msg, infos[1], (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) const msg = Message.deserialize(res[0]) - expect(msg).to.have.property('key').eql(new Buffer('hello')) + expect(msg).to.have.property('key').eql(Buffer.from('hello')) expect(msg).to.have.property('closerPeers').eql([]) done() diff --git a/test/util.js b/test/util.js index bf55af52..f428c58e 100644 --- a/test/util.js +++ b/test/util.js @@ -8,7 +8,7 @@ const PeerInfo = require('peer-info') const leftPad = require('left-pad') const setImmediate = require('async/setImmediate') const MemoryDatastore = require('interface-datastore').MemoryDatastore -const Libp2p = require('libp2p-ipfs-nodejs') +const Libp2p = require('./nodejs-bundle') const multihashing = require('multihashing-async') const crypto = require('libp2p-crypto') const CID = require('cids')