Browse Source
add allowNode that can filter which nodes are added automatically (#46 )
* add allowNode that can filter which nodes are added automatically
* Add test and rename option
Co-authored-by: Kasper Isager Dalsgarð <kasperisager@hey.com>
session-estimator
Mathias Buus
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
24 additions and
0 deletions
index.js
test.js
@ -56,6 +56,7 @@ class DHT extends EventEmitter {
this . _ tickInterval = setInterval ( this . _ ontick . bind ( this ) , TICK_INTERVAL )
this . _ lastTick = Date . now ( )
this . _ lastHost = null
this . _ shouldAddNode = opts . addNode || null
this . _ onrow = ( row ) => row . on ( 'full' , ( node ) => this . _ onfullrow ( node , row ) )
this . _ nonePersistentSamples = [ ]
this . _ bootstrapping = this . _ bootstrap ( )
@ -241,6 +242,10 @@ class DHT extends EventEmitter {
}
_ addNodeFromNetwork ( sample , from , to ) {
if ( this . _ shouldAddNode !== null && ! this . _ shouldAddNode ( from ) ) {
return
}
if ( from . id === null ) {
this . _ sampleBootstrapMaybe ( from , to )
return
@ -311,6 +311,25 @@ test('relay', async function (t) {
t . is ( res . to . port , a . address ( ) . port )
} )
test ( 'filter nodes from routing table' , async function ( t ) {
const [ a , b , c ] = await makeSwarm ( 3 , t )
const node = new DHT ( {
ephemeral : false ,
bootstrap : [ a ] ,
addNode ( from ) {
return from . port !== b . port
}
} )
t . teardown ( ( ) => node . destroy ( ) )
const q = node . findNode ( c . id )
await q . finished ( )
t . absent ( node . table . has ( b . id ) , 'should not have b' )
} )
function freePort ( ) {
return new Promise ( resolve => {
const socket = dgram . createSocket ( 'udp4' )