Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: using libp2p new state methods (#12)
Browse files Browse the repository at this point in the history
* feat: using libp2p new state methods
* fix: remove dep of previous bundles
  • Loading branch information
pgte authored and daviddias committed Jul 7, 2017
1 parent 6426e92 commit 982f789
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 30 deletions.
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 <dignifiedquire@gmail.com>"
]
}
}
4 changes: 2 additions & 2 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}

Expand Down Expand Up @@ -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
}

/**
Expand Down
67 changes: 67 additions & 0 deletions test/browser-bundle.js
Original file line number Diff line number Diff line change
@@ -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
19 changes: 7 additions & 12 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions test/network.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down
80 changes: 80 additions & 0 deletions test/nodejs-bundle.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions test/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
})
}
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions test/rpc/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 982f789

Please sign in to comment.