forked from holepunchto/hyperswarm-testnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (31 loc) · 819 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const DHT = require('@web4/dht')
module.exports = async function createTestnet (size = 10, opts = {}) {
const swarm = []
const teardown = typeof opts === 'function' ? opts : (opts.teardown ? opts.teardown.bind(opts) : noop)
const host = opts.host || '127.0.0.1'
const port = opts.port || 0
if (size === 0) return swarm
const first = new DHT({
ephemeral: false,
bootstrap: [],
bind: port
})
await first.ready()
const bootstrap = [{ host, port: first.address().port }]
swarm.push(first)
while (swarm.length < size) {
const node = new DHT({
ephemeral: false,
bootstrap
})
await node.ready()
swarm.push(node)
}
teardown(async function () {
for (const node of swarm) {
await node.destroy()
}
})
return swarm
}
function noop () {}