Browse Source

bootstrapper needs the host

session-estimator
Mathias Buus 2 years ago
parent
commit
9b200b3d7e
  1. 20
      README.md
  2. 5
      index.js
  3. 10
      lib/io.js
  4. 2
      test.js

20
README.md

@ -20,13 +20,13 @@ of improvements to NAT detection, secure routing IDs and more.
Here is an example implementing a simple key value store
First spin up a bootstrap node. You can make multiple if you want for redundancy.
There is nothing special about a bootstrap node, except it needs to know it's own host and port,
since it knows no other nodes to infer it from.
``` js
import DHT from 'dht-rpc'
// If the bootstrap node doesn't implement the same commands as your other nodes
// remember to set ephemeral: true so it isn't added to the routing table.
const bootstrap = DHT.bootstrapper(10001, { ephemeral: true })
const bootstrap = DHT.bootstrapper(10001, '127.0.0.1')
```
Now lets make some dht nodes that can store values in our key value store.
@ -129,18 +129,10 @@ For the vast majority of use-cases you should always use adaptive mode to ensure
Your DHT routing id is `hash(publicIp + publicPort)` and will be autoconfigured internally.
#### `const node = DHT.bootrapper(port, [options])`
#### `const node = DHT.bootrapper(port, host, [options])`
Sugar for the options needed to run a bootstrap node, ie
```js
{
firewalled: false, // a bootstrapper can never be firewalled
bootstrap: [] // force set no other bootstrappers.
}
```
Additionally since you'll want a known port for a bootstrap node it adds the `port` option as a primary argument.
Make a bootstrap node for your DHT. The port and host needs to be it's globally accessable port and host.
DHT nodes can use any other DHT node to bootstrap, but a bootstrap node can bootstrap itself, by itself.
#### `await node.ready()`

5
index.js

@ -69,8 +69,9 @@ class DHT extends EventEmitter {
}
}
static bootstrapper (port, opts) {
return new this({ port, firewalled: false, bootstrap: [], ...opts })
static bootstrapper (port, host, opts) {
const id = peer.id(host, port)
return new this({ port, id, ephemeral: false, firewalled: false, anyPort: false, bootstrap: [], ...opts })
}
get id () {

10
lib/io.js

@ -12,7 +12,7 @@ const TMP = b4a.alloc(32)
const EMPTY_ARRAY = []
module.exports = class IO {
constructor (table, udx, { maxWindow = 80, port = 0, firewalled = true, onrequest, onresponse = noop, ontimeout = noop } = {}) {
constructor (table, udx, { maxWindow = 80, port = 0, anyPort = true, firewalled = true, onrequest, onresponse = noop, ontimeout = noop } = {}) {
this.table = table
this.udx = udx
this.inflight = []
@ -35,6 +35,7 @@ module.exports = class IO {
this._destroying = null
this._binding = null
this._port = port
this._anyPort = anyPort !== false
}
onmessage (socket, buffer, { host, port }) {
@ -135,7 +136,12 @@ module.exports = class IO {
try {
serverSocket.bind(this._port)
} catch {
} catch (err) {
if (!this._anyPort) {
await serverSocket.close()
throw err
}
try {
serverSocket.bind()
} catch (err) {

2
test.js

@ -324,7 +324,7 @@ function freePort () {
}
async function makeSwarm (n, t) {
const node = DHT.bootstrapper()
const node = new DHT({ ephemeral: false, firewalled: false })
await node.ready()
const all = [node]
const bootstrap = ['localhost:' + node.address().port]

Loading…
Cancel
Save