Skip to content

Commit c199bfc

Browse files
authored
Feat/support new mss (libp2p#124)
* chore: updaet deps * feat: support new mss * docs: update readme
1 parent 77188b2 commit c199bfc

12 files changed

+41
-31
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ Hang up the muxed connection we have with the peer.
148148

149149
Start listening on all added transports that are available on the current `peerInfo`.
150150

151-
### `swarm.handle(protocol, handler)`
151+
### `swarm.handle(protocol, handlerFunc, matchFunc)`
152152

153153
Handle a new protocol.
154154

155155
- `protocol`
156-
- `handler` - function called when we receive a dial on `protocol. Signature must be `function (conn) {}`
156+
- `handlerFunc` - function called when we receive a dial on `protocol. Signature must be `function (protocol, conn) {}`
157+
- `matchFunc` - matchFunc for multistream-select
157158

158159
### `swarm.unhandle(protocol)`
159160

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ gulp.task('test:browser:before', (done) => {
7272
createListenerB(ready)
7373
sigS = sigServer.start(15555, ready)
7474

75-
function echo (conn) {
75+
function echo (protocol, conn) {
7676
pull(conn, conn)
7777
}
7878
})

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"chai": "^3.5.0",
4242
"gulp": "^3.9.1",
4343
"libp2p-multiplex": "^0.2.1",
44-
"libp2p-secio": "^0.6.0",
44+
"libp2p-secio": "^0.6.2",
4545
"libp2p-spdy": "^0.10.0",
4646
"libp2p-tcp": "^0.9.1",
4747
"libp2p-webrtc-star": "^0.5.0",
@@ -60,7 +60,7 @@
6060
"libp2p-identify": "^0.3.0",
6161
"lodash.includes": "^4.3.0",
6262
"multiaddr": "^2.0.3",
63-
"multistream-select": "^0.12.0",
63+
"multistream-select": "^0.13.0",
6464
"peer-id": "^0.8.0",
6565
"peer-info": "^0.8.0",
6666
"protocol-buffers": "^3.1.6"

src/connection.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function connection (swarm) {
1818
swarm.muxers[muxer.multicodec] = muxer
1919

2020
// for listening
21-
swarm.handle(muxer.multicodec, (conn) => {
21+
swarm.handle(muxer.multicodec, (protocol, conn) => {
2222
const muxedConn = muxer.listener(conn)
2323

2424
muxedConn.on('stream', (conn) => {
@@ -70,7 +70,7 @@ module.exports = function connection (swarm) {
7070

7171
reuse () {
7272
swarm.identify = true
73-
swarm.handle(identify.multicodec, (conn) => {
73+
swarm.handle(identify.multicodec, (protocol, conn) => {
7474
identify.listener(conn, swarm._peerInfo)
7575
})
7676
},
@@ -82,7 +82,7 @@ module.exports = function connection (swarm) {
8282
}
8383

8484
swarm.unhandle(swarm.crypto.tag)
85-
swarm.handle(tag, (conn) => {
85+
swarm.handle(tag, (protocol, conn) => {
8686
const id = swarm._peerInfo.id
8787
const secure = encrypt(id, id.privKey, conn)
8888

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ function Swarm (peerInfo) {
9090
}), callback)
9191
}
9292

93-
this.handle = (protocol, handler) => {
94-
this.protocols[protocol] = handler
93+
this.handle = (protocol, handlerFunc, matchFunc) => {
94+
this.protocols[protocol] = {
95+
handlerFunc: handlerFunc,
96+
matchFunc: matchFunc
97+
}
9598
}
9699

97-
this.handle(this.crypto.tag, (conn) => {
100+
this.handle(this.crypto.tag, (protocol, conn) => {
98101
const id = this._peerInfo.id
99102
const wrapped = this.crypto.encrypt(id, id.privKey, conn)
100103
return protocolMuxer(this.protocols, wrapped)

src/protocol-muxer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function protocolMuxer (protocols, conn) {
1010
return
1111
}
1212

13-
ms.addHandler(protocol, protocols[protocol])
13+
ms.addHandler(protocol, protocols[protocol].handlerFunc, protocols[protocol].matchFunc)
1414
})
1515

1616
ms.handle(conn, (err) => {

test/04-muxing-multiplex.node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe.skip('stream muxing with multiplex (on TCP)', () => {
7373
})
7474

7575
it('handle + dial on protocol', (done) => {
76-
swarmB.handle('/abacaxi/1.0.0', (conn) => {
76+
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
7777
pull(conn, conn)
7878
})
7979

@@ -98,7 +98,7 @@ describe.skip('stream muxing with multiplex (on TCP)', () => {
9898
})
9999

100100
it('dial on protocol, reuse warmed conn', (done) => {
101-
swarmA.handle('/papaia/1.0.0', (conn) => {
101+
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
102102
pull(conn, conn)
103103
})
104104

test/05-muxing-spdy.node.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('stream muxing with spdy (on TCP)', () => {
8080
})
8181

8282
it('handle + dial on protocol', (done) => {
83-
swarmB.handle('/abacaxi/1.0.0', (conn) => {
83+
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
8484
pull(conn, conn)
8585
})
8686

@@ -101,7 +101,7 @@ describe('stream muxing with spdy (on TCP)', () => {
101101
})
102102

103103
it('dial on protocol, reuse warmed conn', (done) => {
104-
swarmA.handle('/papaia/1.0.0', (conn) => {
104+
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
105105
pull(conn, conn)
106106
})
107107

@@ -128,7 +128,7 @@ describe('stream muxing with spdy (on TCP)', () => {
128128
})
129129

130130
it('with Identify, do getPeerInfo', (done) => {
131-
swarmA.handle('/banana/1.0.0', (conn) => {
131+
swarmA.handle('/banana/1.0.0', (protocol, conn) => {
132132
conn.getPeerInfo((err, peerInfoC) => {
133133
expect(err).to.not.exist
134134
expect(peerInfoC.id.toB58String()).to.equal(peerC.id.toB58String())
@@ -160,8 +160,11 @@ describe('stream muxing with spdy (on TCP)', () => {
160160
let count = 0
161161
const destroyed = () => ++count === 2 ? done() : null
162162

163-
swarmD.handle('/banana/1.0.0', (conn) => {
164-
pull(conn, pull.onEnd(destroyed))
163+
swarmD.handle('/banana/1.0.0', (protocol, conn) => {
164+
pull(
165+
conn,
166+
pull.onEnd(destroyed)
167+
)
165168
})
166169

167170
swarmA.dial(peerD, '/banana/1.0.0', (err, conn) => {
@@ -210,8 +213,11 @@ describe('stream muxing with spdy (on TCP)', () => {
210213
let count = 0
211214
const destroyed = () => ++count === 2 ? close() : null
212215

213-
swarmE.handle('/avocado/1.0.0', (conn) => {
214-
pull(conn, pull.onEnd(destroyed))
216+
swarmE.handle('/avocado/1.0.0', (protocol, conn) => {
217+
pull(
218+
conn,
219+
pull.onEnd(destroyed)
220+
)
215221
})
216222

217223
swarmF.dial(peerE, '/avocado/1.0.0', (err, conn) => {

test/06-conn-upgrade-secio.node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('secio conn upgrade (on TCP)', () => {
7474
})
7575

7676
it('handle + dial on protocol', (done) => {
77-
swarmB.handle('/abacaxi/1.0.0', (conn) => {
77+
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
7878
pull(conn, conn)
7979
})
8080

@@ -95,7 +95,7 @@ describe('secio conn upgrade (on TCP)', () => {
9595
})
9696

9797
it('dial on protocol, reuse warmed conn', (done) => {
98-
swarmA.handle('/papaia/1.0.0', (conn) => {
98+
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
9999
pull(conn, conn)
100100
})
101101

test/08-swarm-without-muxing.node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ describe('high level API - 1st without stream multiplexing (on TCP)', () => {
4949
})
5050

5151
it('handle a protocol', (done) => {
52-
swarmB.handle('/bananas/1.0.0', (conn) => {
52+
swarmB.handle('/bananas/1.0.0', (protocol, conn) => {
5353
pull(conn, conn)
5454
})
5555
expect(Object.keys(swarmB.protocols).length).to.equal(2)
5656
done()
5757
})
5858

5959
it('dial on protocol', (done) => {
60-
swarmB.handle('/pineapple/1.0.0', (conn) => {
60+
swarmB.handle('/pineapple/1.0.0', (protocol, conn) => {
6161
pull(conn, conn)
6262
})
6363

@@ -68,7 +68,7 @@ describe('high level API - 1st without stream multiplexing (on TCP)', () => {
6868
})
6969

7070
it('dial on protocol (returned conn)', (done) => {
71-
swarmB.handle('/apples/1.0.0', (conn) => {
71+
swarmB.handle('/apples/1.0.0', (protocol, conn) => {
7272
pull(conn, conn)
7373
})
7474

0 commit comments

Comments
 (0)