Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Allow for added.f and added6.f to be optional #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default () => {
compact2string.multi(Buffer.from(message.added)).forEach((peer, idx) => {
delete this._remoteDroppedPeers[peer]
if (!(peer in this._remoteAddedPeers)) {
const flags = message['added.f'][idx]
const flags = message['added.f'] ? message['added.f'][idx] : undefined
gdborton marked this conversation as resolved.
Show resolved Hide resolved
this._remoteAddedPeers[peer] = { ip: 4, flags }
this.emit('peer', peer, this._decodeFlags(flags))
}
Expand All @@ -148,7 +148,7 @@ export default () => {
compact2string.multi6(Buffer.from(message.added6)).forEach((peer, idx) => {
delete this._remoteDroppedPeers[peer]
if (!(peer in this._remoteAddedPeers)) {
const flags = message['added6.f'][idx]
const flags = message['added6.f'] ? message['added6.f'][idx] : undefined
gdborton marked this conversation as resolved.
Show resolved Hide resolved
this._remoteAddedPeers[peer] = { ip: 6, flags }
this.emit('peer', peer, this._decodeFlags(flags))
}
Expand Down
61 changes: 61 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,36 @@ test('should add to remoteAddedPeers when onMessage added', t => {
t.equal(pex._remoteAddedPeers[peer].flags, encodedFlags)
})

test('should add to remoteAddedPeers when onMessage includes added without flags', t => {
t.plan(6)
const Extension = utPex()
const wire = new Protocol()
const pex = new Extension(wire)

const peer = '127.0.0.1:6889'
const decodedFlags = {
prefersEncryption: false,
isSender: false,
supportsUtp: false,
supportsUtHolepunch: false,
isReachable: false
}

pex.on('peer', (_peer, _flags) => {
t.equal(_peer, peer)
t.deepEqual(_flags, decodedFlags)
})

const message = bencode.encode({ added: string2compact(peer) })
const buf = Buffer.from(message)
pex.onMessage(buf)

t.notOk(pex._remoteDroppedPeers[peer])
t.ok(pex._remoteAddedPeers[peer])
t.equal(pex._remoteAddedPeers[peer].ip, 4)
t.equal(pex._remoteAddedPeers[peer].flags, undefined)
})

test('should add to remoteAddedPeers when onMessage added6', t => {
t.plan(6)

Expand Down Expand Up @@ -334,6 +364,37 @@ test('should add to remoteAddedPeers when onMessage added6', t => {
t.equal(pex._remoteAddedPeers[peer].flags, encodedFlags)
})

test('should add to removeAddedPeers when onMessage includes added6 without flags', t => {
t.plan(6)

const Extension = utPex()
const wire = new Protocol()
const pex = new Extension(wire)

const peer = '[::1]:6889'
const decodedFlags = {
prefersEncryption: false,
isSender: false,
supportsUtp: false,
supportsUtHolepunch: false,
isReachable: false
}

pex.on('peer', (_peer, _flags) => {
t.equal(_peer, peer)
t.deepEqual(_flags, decodedFlags)
})

const message = bencode.encode({ added6: string2compact(peer) })
const buf = Buffer.from(message)
pex.onMessage(buf)

t.notOk(pex._remoteDroppedPeers[peer])
t.ok(pex._remoteAddedPeers[peer])
t.equal(pex._remoteAddedPeers[peer].ip, 6)
t.equal(pex._remoteAddedPeers[peer].flags, undefined)
})

test('should ignore when onMessage dropped and address already in remoteDroppedPeers', t => {
const Extension = utPex()
const wire = new Protocol()
Expand Down