peer-exchange
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -12,2 +12,3 @@ 'use strict'; | ||
} catch (err) {} | ||
var remove = require('./util.js').remove; | ||
@@ -284,11 +285,2 @@ module.exports = Exchange; | ||
this.emit('error', err); | ||
}; | ||
function remove(array, object) { | ||
var index = array.indexOf(object); | ||
if (index !== -1) { | ||
array.splice(index, 1); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; |
@@ -11,2 +11,3 @@ 'use strict'; | ||
var hat = require('hat'); | ||
var u = require('./util.js'); | ||
@@ -40,2 +41,4 @@ var PROTOCOL_VERSION = 0; | ||
_this._exchangeChannel.on('message:incoming', _this._onIncoming.bind(_this)); | ||
_this._exchangeChannel.on('message:accept', _this._onAccept.bind(_this)); | ||
_this._exchangeChannel.on('message:unaccept', _this._onUnaccept.bind(_this)); | ||
}); | ||
@@ -143,3 +146,3 @@ } | ||
// TODO: get addresses from peer | ||
this.remoteAddress = getRemoteAddress(this.socket); | ||
this.remoteAddress = u.getRemoteAddress(this.socket); | ||
this._accepts = message.accepts; | ||
@@ -161,2 +164,16 @@ | ||
Peer.prototype._onAccept = function (message) { | ||
if (this._accepts[message.transport]) return; | ||
if (!this._exchange._transports[message.transport]) return; | ||
this._accepts[message.transport] = message.opts || true; | ||
this._exchange._acceptPeers[message.transport].push(this); | ||
}; | ||
Peer.prototype._onUnaccept = function (message) { | ||
if (!this._accepts[message.transport]) return; | ||
if (!this._exchange._transports[message.transport]) return; | ||
delete this._accepts[message.transport]; | ||
u.remove(this._exchange._acceptPeers[message.transport], this); | ||
}; | ||
Peer.prototype._maybeReady = function () { | ||
@@ -243,6 +260,6 @@ if (this._receivedHello && this._receivedHelloack) { | ||
// select random transport that has valid accepting peers | ||
var transportId = getRandom(transports); | ||
var transportId = u.getRandom(transports); | ||
var peers = acceptPeers[transportId]; | ||
// select random peer | ||
var peer = getRandom(peers); | ||
var peer = u.getRandom(peers); | ||
var nonce = hat(); | ||
@@ -297,8 +314,15 @@ | ||
Peer.prototype._sendAccept = function () { | ||
// TODO | ||
Peer.prototype._sendAccept = function (id, opts) { | ||
this._exchangeChannel.write({ | ||
command: 'accept', | ||
transport: id, | ||
opts: opts | ||
}); | ||
}; | ||
Peer.prototype._sendUnaccept = function () { | ||
// TODO | ||
Peer.prototype._sendUnaccept = function (id) { | ||
this._exchangeChannel.write({ | ||
command: 'unaccept', | ||
transport: id | ||
}); | ||
}; | ||
@@ -312,19 +336,2 @@ | ||
} | ||
}; | ||
function getRandom(array) { | ||
return array[Math.floor(Math.random() * array.length)]; | ||
} | ||
// HACK: recursively looks for socket property that has a remote address | ||
function getRemoteAddress(socket) { | ||
if (socket.remoteAddress) return socket.remoteAddress; | ||
if (socket.socket) { | ||
var address = getRemoteAddress(socket.socket); | ||
if (address) return address; | ||
} | ||
if (socket._socket) { | ||
var address = getRemoteAddress(socket._socket); | ||
if (address) return address; | ||
} | ||
} | ||
}; |
{ | ||
"name": "peer-exchange", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Decentralized peer discovery and signaling", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard src test index.js && tap --cov test/*.js", | ||
"test": "standard src test index.js && tap -Rspec --cov test/*.js", | ||
"build": "rm -rf lib && babel --presets es2015 src -d lib", | ||
@@ -9,0 +9,0 @@ "source": "rm -rf lib && ln -s src lib", |
@@ -263,2 +263,30 @@ var test = require('tap').test | ||
test('accept/unaccept after connect', (t) => { | ||
var e4 = new Exchange('test') | ||
e4.accept('tcp', { port: 7780 }) | ||
var e5 = new Exchange('test') | ||
e5.connect('tcp', 'localhost', { port: 7780 }, (err, peer) => { | ||
t.error(err, 'no error') | ||
t.ok(peer, 'got peer') | ||
t.equal(e4._acceptPeers.tcp.length, 0, 'e5 was not added to e4\'s acceptPeers') | ||
t.equal(e4.peers.length, 1, 'e5 was added to e4\'s peers') | ||
e5.accept('tcp', { port: 7781 }, (err) => { | ||
t.error(err, 'no error') | ||
setTimeout(() => { | ||
t.equal(e4._acceptPeers.tcp.length, 1, 'e5 was added to e4\'s acceptPeers') | ||
e5.unaccept('tcp', (err) => { | ||
t.error(err, 'no error') | ||
setTimeout(() => { | ||
t.equal(e4._acceptPeers.tcp.length, 0, 'e5 was removed from e4\'s acceptPeers') | ||
e4.close(() => e5.close(t.end)) | ||
}, 200) | ||
}) | ||
}, 100) | ||
}) | ||
}) | ||
}) | ||
test('cleanup', (t) => { | ||
@@ -265,0 +293,0 @@ wrtc.electronDaemon.close() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87793
12
915