Browse Source
* fixed example * updated standard version Co-authored-by: rafapaezbas <rpaezbas@rpaezbas.com>session-estimator
rafapaezbas
2 years ago
committed by
GitHub
8 changed files with 57 additions and 59 deletions
@ -1,4 +0,0 @@ |
|||
const DHT = require('../') |
|||
|
|||
// Set ephemeral: true since this does not implement any APIs
|
|||
DHT.bootstrapper(10001, { ephemeral: true }) |
@ -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()) |
@ -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() |
|||
} |
@ -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() |
|||
} |
@ -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() |
|||
} |
@ -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() |
|||
} |
Loading…
Reference in new issue