Skip to content

Commit 2a7e398

Browse files
gdbortonThaUnknown
andauthored
BREAKING CHANGE: Allow for added.f and added6.f to be optional (#108)
* Allow for added.f and added6.f to be optional * Update index.js Co-authored-by: Cas_ <[email protected]> * Update index.js --------- Co-authored-by: Cas_ <[email protected]>
1 parent e470352 commit 2a7e398

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export default () => {
137137
compact2string.multi(Buffer.from(message.added)).forEach((peer, idx) => {
138138
delete this._remoteDroppedPeers[peer]
139139
if (!(peer in this._remoteAddedPeers)) {
140-
const flags = message['added.f'][idx]
140+
const flags = message['added.f']?.[idx]
141141
this._remoteAddedPeers[peer] = { ip: 4, flags }
142142
this.emit('peer', peer, this._decodeFlags(flags))
143143
}
@@ -148,7 +148,7 @@ export default () => {
148148
compact2string.multi6(Buffer.from(message.added6)).forEach((peer, idx) => {
149149
delete this._remoteDroppedPeers[peer]
150150
if (!(peer in this._remoteAddedPeers)) {
151-
const flags = message['added6.f'][idx]
151+
const flags = message['added6.f']?.[idx]
152152
this._remoteAddedPeers[peer] = { ip: 6, flags }
153153
this.emit('peer', peer, this._decodeFlags(flags))
154154
}

test/basic.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ test('should add to remoteAddedPeers when onMessage added', t => {
302302
t.equal(pex._remoteAddedPeers[peer].flags, encodedFlags)
303303
})
304304

305+
test('should add to remoteAddedPeers when onMessage includes added without flags', t => {
306+
t.plan(6)
307+
const Extension = utPex()
308+
const wire = new Protocol()
309+
const pex = new Extension(wire)
310+
311+
const peer = '127.0.0.1:6889'
312+
const decodedFlags = {
313+
prefersEncryption: false,
314+
isSender: false,
315+
supportsUtp: false,
316+
supportsUtHolepunch: false,
317+
isReachable: false
318+
}
319+
320+
pex.on('peer', (_peer, _flags) => {
321+
t.equal(_peer, peer)
322+
t.deepEqual(_flags, decodedFlags)
323+
})
324+
325+
const message = bencode.encode({ added: string2compact(peer) })
326+
const buf = Buffer.from(message)
327+
pex.onMessage(buf)
328+
329+
t.notOk(pex._remoteDroppedPeers[peer])
330+
t.ok(pex._remoteAddedPeers[peer])
331+
t.equal(pex._remoteAddedPeers[peer].ip, 4)
332+
t.equal(pex._remoteAddedPeers[peer].flags, undefined)
333+
})
334+
305335
test('should add to remoteAddedPeers when onMessage added6', t => {
306336
t.plan(6)
307337

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

367+
test('should add to removeAddedPeers when onMessage includes added6 without flags', t => {
368+
t.plan(6)
369+
370+
const Extension = utPex()
371+
const wire = new Protocol()
372+
const pex = new Extension(wire)
373+
374+
const peer = '[::1]:6889'
375+
const decodedFlags = {
376+
prefersEncryption: false,
377+
isSender: false,
378+
supportsUtp: false,
379+
supportsUtHolepunch: false,
380+
isReachable: false
381+
}
382+
383+
pex.on('peer', (_peer, _flags) => {
384+
t.equal(_peer, peer)
385+
t.deepEqual(_flags, decodedFlags)
386+
})
387+
388+
const message = bencode.encode({ added6: string2compact(peer) })
389+
const buf = Buffer.from(message)
390+
pex.onMessage(buf)
391+
392+
t.notOk(pex._remoteDroppedPeers[peer])
393+
t.ok(pex._remoteAddedPeers[peer])
394+
t.equal(pex._remoteAddedPeers[peer].ip, 6)
395+
t.equal(pex._remoteAddedPeers[peer].flags, undefined)
396+
})
397+
337398
test('should ignore when onMessage dropped and address already in remoteDroppedPeers', t => {
338399
const Extension = utPex()
339400
const wire = new Protocol()

0 commit comments

Comments
 (0)