Skip to content

Commit

Permalink
Allow for added.f and added6.f to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gdborton committed Jan 28, 2025
1 parent b3f9e1c commit 55a12ec
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
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
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
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

0 comments on commit 55a12ec

Please sign in to comment.