Browse Source

fixed example (#48)

* fixed example

* updated standard version

Co-authored-by: rafapaezbas <rpaezbas@rpaezbas.com>
session-estimator
rafapaezbas 2 years ago
committed by GitHub
parent
commit
56d0b3fcec
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      examples/bootstrap.js
  2. 5
      examples/bootstrap.mjs
  3. 25
      examples/find.js
  4. 24
      examples/find.mjs
  5. 23
      examples/insert.js
  6. 20
      examples/insert.mjs
  7. 13
      examples/network.mjs
  8. 2
      package.json

4
examples/bootstrap.js

@ -1,4 +0,0 @@
const DHT = require('../')
// Set ephemeral: true since this does not implement any APIs
DHT.bootstrapper(10001, { ephemeral: true })

5
examples/bootstrap.mjs

@ -0,0 +1,5 @@
import DHT from '../index.js'
const bootstrap = new DHT({ ephemeral: false, firewalled: false, port: 10001 })
await bootstrap.ready()
console.log(bootstrap.address())

25
examples/find.js

@ -1,25 +0,0 @@
const DHT = require('../')
const crypto = require('crypto')
const hex = process.argv[2]
const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
run()
async function run () {
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) {
// We found the value! Destroy the query stream as there is no need to continue.
console.log(hex, '-->', data.value.toString())
break
}
}
console.log('(query finished)')
}
function sha256 (val) {
return crypto.createHash('sha256').update(val).digest()
}

24
examples/find.mjs

@ -0,0 +1,24 @@
import DHT from '../index.js'
import crypto from 'crypto'
const GET = 1
const hex = process.argv[2]
const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
await node.ready()
const q = node.query({ target: Buffer.from(hex, 'hex'), command: GET }, { commit: true })
for await (const data of q) {
if (data.value && sha256(data.value).toString('hex') === hex) {
// We found the value! Destroy the query stream as there is no need to continue.
console.log(hex, '-->', data.value.toString())
break
}
}
console.log('(query finished)')
function sha256 (val) {
return crypto.createHash('sha256').update(val).digest()
}

23
examples/insert.js

@ -1,23 +0,0 @@
const DHT = require('../')
const crypto = require('crypto')
// Set ephemeral: true as we are not part of the network.
const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
const val = Buffer.from(process.argv[2])
run()
async function run () {
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) {
return crypto.createHash('sha256').update(val).digest()
}

20
examples/insert.mjs

@ -0,0 +1,20 @@
import DHT from '../index.js'
import crypto from 'crypto'
const INSERT = 0
// Set ephemeral: true as we are not part of the network.
const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
const val = Buffer.from(process.argv[2])
const q = node.query({ target: sha256(val), command: INSERT }, { commit })
await q.finished()
console.log('Inserted', sha256(val).toString('hex'))
async function commit (reply) {
await node.request({ token: reply.token, target: sha256(val), command: INSERT, value: val }, reply.from)
}
function sha256 (val) {
return crypto.createHash('sha256').update(val).digest()
}

13
examples/network.js → examples/network.mjs

@ -1,5 +1,7 @@
const DHT = require('../')
const crypto = require('crypto')
import DHT from '../index.js'
import crypto from 'crypto'
const INSERT = 0
// Let's create 100 dht nodes for our example.
const swarm = []
@ -16,17 +18,16 @@ function createNode () {
const values = new Map()
node.on('request', function (req) {
if (req.command === 'values') {
if (req.command === INSERT) {
if (req.token) {
const key = sha256(req.value).toString('hex')
values.set(key, req.value)
console.log('Storing', key, '-->', req.value.toString())
return req.reply(null)
}
const value = values.get(req.target.toString('hex'))
req.reply(value)
}
const value = values.get(req.target.toString('hex'))
req.reply(value)
})
return node

2
package.json

@ -18,7 +18,7 @@
},
"devDependencies": {
"brittle": "^2.3.1",
"standard": "^16.0.3"
"standard": "^17.0.0"
},
"scripts": {
"test": "standard && brittle test.js"

Loading…
Cancel
Save