Skip to content

Commit

Permalink
Pass debug options to DHT constructor (holepunchto#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Apr 7, 2022
1 parent 8817912 commit c80f8fe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ module.exports = class Hyperswarm extends EventEmitter {
maxParallel = MAX_PARALLEL,
firewall = allowAll
} = opts

this.keyPair = keyPair

const networkOpts = {}
if (opts.bootstrap) networkOpts.bootstrap = opts.bootstrap
this.dht = opts.dht || new DHT(networkOpts)

this.dht = opts.dht || new DHT({
bootstrap: opts.bootstrap,
debug: opts.debug
})
this.server = this.dht.createServer({
firewall: this._handleFirewall.bind(this)
}, this._handleServerConnection.bind(this))
Expand Down
45 changes: 45 additions & 0 deletions test/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,49 @@ test('one server, one client - correct deduplication when a client connection is
t.end()
})

test('constructor options - debug options forwarded to DHT constructor', async (bootstrap, t) => {
const swarm1 = new Hyperswarm({
bootstrap,
backoffs: BACKOFFS,
jitter: 0,
debug: {
handshake: {
latency: [500, 500]
}
}
})
const swarm2 = new Hyperswarm({
bootstrap,
backoffs: BACKOFFS,
jitter: 0,
debug: {
handshake: {
latency: [500, 500]
}
}
})

const connected = new Promise(resolve => {
let connections = 0
const onconnect = () => {
if (++connections === 2) resolve()
}
swarm1.once('connection', onconnect)
swarm2.once('connection', onconnect)
})

const topic = Buffer.alloc(32).fill('hello world')
await swarm1.join(topic, { server: true }).flushed()

const start = Date.now()
swarm2.join(topic, { client: true })

await connected
const duration = Date.now() - start
t.true(duration > 500)

await destroyAll(swarm1, swarm2)
t.end()
})

function noop () {}

0 comments on commit c80f8fe

Please sign in to comment.