Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
feat: add semver matcher and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 3, 2016
1 parent 3659848 commit 56a8b0a
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"pull-handshake": "^1.1.4",
"pull-length-prefixed": "^1.2.0",
"pull-stream": "^3.5.0",
"semver": "^5.3.0",
"varint": "^4.0.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

exports.Listener = exports.listener = require('./listener')
exports.Dialer = exports.dialer = require('./dialer')
exports.matchSemver = require('./listener/match-semver')
5 changes: 2 additions & 3 deletions src/listener/ls-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const pullLP = require('pull-length-prefixed')
const varint = require('varint')

function lsHandler (self, conn) {
const protos = Object
.keys(self.handlers)
.filter((key) => key !== 'ls')
const protos = Object.keys(self.handlers)
.filter((key) => key !== 'ls')

const nProtos = protos.length
// total size of the list of protocols, including varint and newline
Expand Down
7 changes: 2 additions & 5 deletions src/listener/match-exact.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict'

function matchExact (myProtocol, senderProtocol, callback) {
if (myProtocol === senderProtocol) {
callback(null, true)
} else {
callback(null, false)
}
const result = myProtocol === senderProtocol
callback(null, result)
}

module.exports = matchExact
23 changes: 23 additions & 0 deletions src/listener/match-semver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const semver = require('semver')

function matchSemver (myProtocol, senderProtocol, callback) {
const mps = myProtocol.split('/')
const sps = senderProtocol.split('/')
const myName = mps[1]
const myVersion = mps[2]

const senderName = sps[1]
const senderVersion = sps[2]

if (myName !== senderName) {
return callback(null, false)
}
// does my protocol satisfy the sender?
const valid = semver.satisfies(myVersion, '~' + senderVersion)

callback(null, valid)
}

module.exports = matchSemver
4 changes: 2 additions & 2 deletions src/listener/select-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ function selectHandler (rawConn, handlersMap, log) {
}
}

module.exports = selectHandler

function matcher (protocol, handlers, callback) {
const supportedProtocols = Object.keys(handlers)
let supportedProtocol = false
Expand All @@ -74,3 +72,5 @@ function matcher (protocol, handlers, callback) {
}
)
}

module.exports = selectHandler
2 changes: 2 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const pull = require('pull-stream')
const pullLP = require('pull-length-prefixed')
const debug = require('debug')
Expand Down
129 changes: 105 additions & 24 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const expect = require('chai').expect
const pull = require('pull-stream')
const pullLP = require('pull-length-prefixed')
const pullPair = require('pull-pair/duplex')
const multistream = require('../src')
const mss = require('../src')
const parallel = require('run-parallel')
const series = require('run-series')

describe('multistream dialer', () => {
it('sends the multistream multicodec', (done) => {
describe('mss dialer', () => {
it('sends the mss multicodec', (done) => {
const p = pullPair()
const dialerConn = p[0]
const listenerConn = p[1]
Expand All @@ -25,13 +25,13 @@ describe('multistream dialer', () => {
})
)

const msd = new multistream.Dialer()
const msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, () => {})
})
})
describe('multistream listener', () => {
it('sends the multistream multicodec', (done) => {
describe('mss listener', () => {
it('sends the mss multicodec', (done) => {
const p = pullPair()
const dialerConn = p[0]
const listenerConn = p[1]
Expand All @@ -45,26 +45,26 @@ describe('multistream listener', () => {
})
)

const msl = new multistream.Listener()
const msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, () => {})
})
})

describe('multistream handshake', () => {
describe('mss handshake', () => {
it('performs the handshake handshake', (done) => {
const p = pullPair()
const dialerConn = p[0]
const listenerConn = p[1]

parallel([
(cb) => {
const msl = new multistream.Listener()
const msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
const msd = new multistream.Dialer()
const msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand All @@ -82,12 +82,12 @@ describe('multistream handshake', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand Down Expand Up @@ -130,12 +130,12 @@ describe('multistream handshake', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand All @@ -161,12 +161,12 @@ describe('multistream handshake', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand Down Expand Up @@ -214,12 +214,12 @@ describe('multistream handshake', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand Down Expand Up @@ -270,12 +270,12 @@ describe('multistream handshake', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('multistream handshake', () => {
(cb) => {
series([
(next) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
setTimeout(() => {
msl.handle(listenerConn, next)
Expand All @@ -318,7 +318,7 @@ describe('multistream handshake', () => {
], cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
msd.handle(dialerConn, (err) => {
expect(err).to.not.exist
msd.select('/monkey/1.0.0', (err, conn) => {
Expand Down Expand Up @@ -352,12 +352,12 @@ describe('custom matching function', () => {
(next) => {
parallel([
(cb) => {
msl = new multistream.Listener()
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new multistream.Dialer()
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
Expand Down Expand Up @@ -390,6 +390,87 @@ describe('custom matching function', () => {
})

describe('semver-match', () => {
it('should match', (done) => {
const p = pullPair()
const dialerConn = p[0]
const listenerConn = p[1]

let msl
let msd
series([
(next) => {
parallel([
(cb) => {
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
], next)
},
(next) => {
msl.addHandler('/monster/1.0.0', (p, conn) => {
pull(conn, conn)
}, mss.matchSemver)
next()
},
(next) => {
msd.select('/monster/1.0.0', (err, conn) => {
expect(err).to.not.exist

pull(
pull.values(['cookie']),
conn,
pull.collect((err, data) => {
expect(err).to.not.exist
expect(data).to.be.eql(['cookie'])
next()
})
)
})
}
], done)
})

it('should not match', (done) => {
const p = pullPair()
const dialerConn = p[0]
const listenerConn = p[1]

let msl
let msd
series([
(next) => {
parallel([
(cb) => {
msl = new mss.Listener()
expect(msl).to.exist
msl.handle(listenerConn, cb)
},
(cb) => {
msd = new mss.Dialer()
expect(msd).to.exist
msd.handle(dialerConn, cb)
}
], next)
},
(next) => {
msl.addHandler('/monster/1.1.0', (p, conn) => {
pull(conn, conn)
}, mss.matchSemver)
next()
},
(next) => {
msd.select('/monster/2.0.0', (err, conn) => {
expect(err).to.exist
next()
})
}
], done)
})
})
})

0 comments on commit 56a8b0a

Please sign in to comment.