Browse Source

update examples

session-estimator
Mathias Buus 3 years ago
parent
commit
56e0cc522c
  1. 6
      examples/bootstrap.js
  2. 2
      examples/find.js
  3. 6
      examples/insert.js
  4. 2
      examples/network.js

6
examples/bootstrap.js

@ -1,6 +1,4 @@
const DHT = require('../')
// Set ephemeral: true so other peers do not add us to the peer list, simply bootstrap
const bootstrap = new DHT({ ephemeral: true })
bootstrap.bind(10001)
// Set ephemeral: true since this does not implement any APIs
DHT.bootstrapper(10001, { ephemeral: true })

2
examples/find.js

@ -7,7 +7,7 @@ const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
run()
async function run () {
const q = node.query(Buffer.from(hex, 'hex'), 'values')
const q = node.query({ target: Buffer.from(hex, 'hex'), command: 'values' })
for await (const data of q) {
if (data.value && sha256(data.value).toString('hex') === hex) {

6
examples/insert.js

@ -8,10 +8,14 @@ const val = Buffer.from(process.argv[2])
run()
async function run () {
const q = node.query(sha256(val), 'values')
const q = node.query({ target: sha256(val), command: 'values', commit })
await q.finished()
await q.commit('values', val)
console.log('Inserted', sha256(val).toString('hex'))
async function commit (reply) {
await node.request({ token: reply.token, target: sha256(val), command: 'values', value: val }, reply.from)
}
}
function sha256 (val) {

2
examples/network.js

@ -17,7 +17,7 @@ function createNode () {
node.on('request', function (req) {
if (req.command === 'values') {
if (req.commit) {
if (req.token) {
const key = sha256(req.value).toString('hex')
values.set(key, req.value)
console.log('Storing', key, '-->', req.value.toString())

Loading…
Cancel
Save