Skip to content

Commit

Permalink
chore: use newest libp2p release
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 1, 2019
2 parents 1ae0a19 + 46d0c18 commit 710c192
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 62 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"cids": "~0.6.0",
"debug": "^4.1.1",
"length-prefixed-stream": "github:jacobheun/length-prefixed-stream#v2.0.0-rc.1",
"libp2p": "~0.26.0-rc.0",
"libp2p": "~0.26.0-rc.2",
"libp2p-bootstrap": "~0.9.7",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "~0.0.4",
Expand Down
10 changes: 5 additions & 5 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Daemon {
let successfulProto
for (const protocol of proto) {
try {
connection = await this.libp2p._dial(peerInfo, protocol)
connection = await this.libp2p.dial(peerInfo, protocol)
successfulProto = protocol
break
} catch (err) {
Expand Down Expand Up @@ -186,7 +186,7 @@ class Daemon {
* @returns {Promise<void>}
*/
async start () {
await this.libp2p._start()
await this.libp2p.start()
return new Promise((resolve, reject) => {
const options = multiaddrToNetConfig(this.multiaddr)
this.server.listen(options, (err) => {
Expand Down Expand Up @@ -256,7 +256,7 @@ class Daemon {
async handlePubsubRequest ({ pubsub }, enc) {
switch (pubsub.type) {
case PSRequest.Type.GET_TOPICS: {
const topics = await this.libp2p.pubsub.getTopics()
const topics = await this.libp2p.pubsub.ls()

await new Promise((resolve) => {
enc.write(
Expand All @@ -283,7 +283,7 @@ class Daemon {
case PSRequest.Type.SUBSCRIBE: {
const topic = pubsub.topic

await this.libp2p.pubsub.subscribe(topic, {}, async (msg) => {
await this.libp2p.pubsub.subscribe(topic, async (msg) => {
await new Promise((resolve) => {
enc.write(PSMessage.encode({
from: msg.from && Buffer.from(msg.from),
Expand All @@ -294,7 +294,7 @@ class Daemon {
key: msg.key
}), resolve)
})
})
}, {})

await new Promise((resolve) => {
enc.write(OkResponse(), resolve)
Expand Down
55 changes: 2 additions & 53 deletions src/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,52 +107,6 @@ class ContentRouting {
}
}

class Pubsub {
/**
* @param {Libp2p} libp2p The libp2p instance to use
*/
constructor (libp2p) {
this.libp2p = libp2p
}

/**
* Subscribe to a pubsub topic
* @param {string} topic
* @param {Object} options
* @param {function(msg)} handler handle received messages
* @returns { Promise<void>}
*/
subscribe (topic, options, handler) {
return this.libp2p._pubsub.subscribe(topic, handler, options)
}

/**
* Publish data in the context of a topic
* @param {string} topic
* @param {Buffer} data
* @returns {Promise<void>}
*/
publish (topic, data) {
return this.libp2p._pubsub.publish(topic, data)
}

/**
* Get the list of subscriptions the peer is subscribed to.
* @returns {Promise<Array<string>>}
*/
getTopics () {
return this.libp2p._pubsub.ls()
}

start (cb) {
this.libp2p._pubsub.start(cb)
}

stop (cb) {
this.libp2p._pubsub.stop(cb)
}
}

class DHT {
/**
* @param {Libp2p} libp2p The libp2p instance to use
Expand Down Expand Up @@ -249,11 +203,6 @@ class DaemonLibp2p extends Libp2p {
super(libp2pOpts)
this.announceAddrs = announceAddrs
this.needsPullStream = libp2pOpts.config.pubsub.enabled

if (libp2pOpts.config.pubsub.enabled) {
this._pubsub = this.pubsub
this.pubsub = new Pubsub(this)
}
}
get contentRouting () {
return this._contentRouting
Expand All @@ -279,7 +228,7 @@ class DaemonLibp2p extends Libp2p {
*
* @returns {Promise<void>}
*/
_start () {
start () {
return new Promise((resolve, reject) => {
super.start((err) => {
if (err) return reject(err)
Expand Down Expand Up @@ -317,7 +266,7 @@ class DaemonLibp2p extends Libp2p {
* @param {string} protocol
* @returns {Promise<Connection>}
*/
_dial (peerInfo, protocol) {
dial (peerInfo, protocol) {
return new Promise((resolve, reject) => {
this.dialProtocol(peerInfo, protocol, (err, conn) => {
if (err) return reject(err)
Expand Down
4 changes: 2 additions & 2 deletions test/daemon/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ const testPubsub = (router) => {
stream.end()

// subscribe topic
await libp2pPeer.pubsub.subscribe(topic, {}, (msg) => {
await libp2pPeer.pubsub.subscribe(topic, (msg) => {
expect(msg.data).to.equalBytes(data)
resolve()
})
}, {})

// wait to pubsub to propagate messages
await new Promise(resolve => setTimeout(resolve, 1000))
Expand Down
2 changes: 1 addition & 1 deletion test/daemon/streams.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('streams', function () {

// Open a connection between the peer and our daemon
// Then send hello from the peer to the daemon
const connection = await libp2pPeer._dial(daemon.libp2p.peerInfo, '/echo/1.0.0')
const connection = await libp2pPeer.dial(daemon.libp2p.peerInfo, '/echo/1.0.0')
const hello = Buffer.from('hello, peer')
connection.write(hello)

Expand Down

0 comments on commit 710c192

Please sign in to comment.