From 56d0b3fcec1ce58bdb75c2eb61e7f7b74ebfc9d9 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Sat, 2 Jul 2022 15:42:24 +0200 Subject: [PATCH] fixed example (#48) * fixed example * updated standard version Co-authored-by: rafapaezbas --- examples/bootstrap.js | 4 ---- examples/bootstrap.mjs | 5 +++++ examples/find.js | 25 ------------------------- examples/find.mjs | 24 ++++++++++++++++++++++++ examples/insert.js | 23 ----------------------- examples/insert.mjs | 20 ++++++++++++++++++++ examples/{network.js => network.mjs} | 13 +++++++------ package.json | 2 +- 8 files changed, 57 insertions(+), 59 deletions(-) delete mode 100644 examples/bootstrap.js create mode 100644 examples/bootstrap.mjs delete mode 100644 examples/find.js create mode 100644 examples/find.mjs delete mode 100644 examples/insert.js create mode 100644 examples/insert.mjs rename examples/{network.js => network.mjs} (77%) diff --git a/examples/bootstrap.js b/examples/bootstrap.js deleted file mode 100644 index 2757dfd..0000000 --- a/examples/bootstrap.js +++ /dev/null @@ -1,4 +0,0 @@ -const DHT = require('../') - -// Set ephemeral: true since this does not implement any APIs -DHT.bootstrapper(10001, { ephemeral: true }) diff --git a/examples/bootstrap.mjs b/examples/bootstrap.mjs new file mode 100644 index 0000000..231f0ff --- /dev/null +++ b/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()) diff --git a/examples/find.js b/examples/find.js deleted file mode 100644 index 56a0704..0000000 --- a/examples/find.js +++ /dev/null @@ -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() -} diff --git a/examples/find.mjs b/examples/find.mjs new file mode 100644 index 0000000..8d174e2 --- /dev/null +++ b/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() +} diff --git a/examples/insert.js b/examples/insert.js deleted file mode 100644 index 35c5713..0000000 --- a/examples/insert.js +++ /dev/null @@ -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() -} diff --git a/examples/insert.mjs b/examples/insert.mjs new file mode 100644 index 0000000..9c51ebc --- /dev/null +++ b/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() +} diff --git a/examples/network.js b/examples/network.mjs similarity index 77% rename from examples/network.js rename to examples/network.mjs index 337842c..e029c61 100644 --- a/examples/network.js +++ b/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 diff --git a/package.json b/package.json index 65dc8cb..22e99bf 100644 --- a/package.json +++ b/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"