Skip to content

Commit

Permalink
v1.4.0 (#10)
Browse files Browse the repository at this point in the history
* Added DataFor(id) method to increase extensibility (#9)

* Added the dataFor() method to make the peerList actually extensible, this is a non breaking change

* Added unit test for dataFor() method support

* bump to v1.4.0
  • Loading branch information
bengreenier authored Jun 5, 2018
1 parent e5d678e commit f903f85
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
10 changes: 3 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ module.exports = (opts) => {

// add the peer
const peerId = router.peerList.addPeer(req.query.peer_name, res)
const peerListStr = router.peerList.format()

// send back the list of peers
res.status(200)
.set('Pragma', peerId)
.set('Content-Type', 'text/plain')
.send(peerListStr)
.send(router.peerList.dataFor(peerId))

// send an updated peer list to all peers
router.peerList.getPeerIds().filter(id => id != peerId).forEach((id) => {
// updated peer lists must always appear to come from
// "ourselves", namely the srcId == destId
sendPeerMessage(id, id, peerListStr)
sendPeerMessage(id, id, router.peerList.dataFor(id))
})
})

Expand Down Expand Up @@ -123,14 +122,11 @@ module.exports = (opts) => {
// remove the peer
router.peerList.removePeer(req.query.peer_id)

// format the updated peerList
const peerListStr = router.peerList.format()

// send an updated peer list to all peers
router.peerList.getPeerIds().forEach((id) => {
// updated peer lists must always appear to come from
// "ourselves", namely the srcId == destId
sendPeerMessage(id, id, peerListStr)
sendPeerMessage(id, id, router.peerList.dataFor(id))
})

res.status(200).end()
Expand Down
7 changes: 7 additions & 0 deletions lib/peer-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = class PeerList {
if (this._peers[id]) {
delete this._peers[id]
}

}

getPeer(id) {
Expand Down Expand Up @@ -63,4 +64,10 @@ module.exports = class PeerList {
return `${e.name},${e.id},${e.status() ? 1 : 0}`
}).join('\n') + '\n'
}

dataFor(id) {
//returns the data that should appear for a given ID
//This is the primary part of peer-list that is extensible
return this.format()
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webrtc-signal-http",
"version": "1.3.0",
"version": "1.4.0",
"description": "opinionated webrtc signal provider using http as a protocol",
"main": "lib/index.js",
"directories": {
Expand Down
14 changes: 14 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ describe('webrtc-signal-http', () => {

assert.equal(instance.format(), 'test2,2,0\ntest,1,0\n')
})

it('should support formatting via dataFor() method', () => {
const instance = new PeerList()

instance.addPeer('test', { obj: true })

assert.equal(instance.dataFor('test'), 'test,1,0\n')

instance.addPeer('test2', { obj: true })

assert.equal(instance.dataFor('test2'), 'test2,2,0\ntest,1,0\n')
})


})

describe('Peer', () => {
Expand Down

0 comments on commit f903f85

Please sign in to comment.