OverviewFilesCommitsIssuesDocsReleasesPeers

hyperswarm-peer / integration / test_finished.js

35 lines · javascript ref:
import createTestnet from 'hyperdht/testnet.js'
import HyperDHT from 'hyperdht'
import crypto from 'crypto'

const topic = crypto.createHash('sha256').update('test').digest()
const t = await createTestnet(3)
const dht = new HyperDHT({ bootstrap: t.bootstrap })
await dht.fullyBootstrapped()
const kp = HyperDHT.keyPair()
const srv = dht.createServer()
await srv.listen(kp)
const ann = dht.announce(topic, kp)
console.log('type of finished:', typeof ann.finished)
console.log('is function:', typeof ann.finished === 'function')
if (typeof ann.finished === 'function') {
  await ann.finished()
  console.log('finished() resolved')
} else {
  await ann.finished
  console.log('finished (property) resolved')
}

// Now lookup
const q = dht.query({ target: topic, command: 3, value: null })
let count = 0
for await (const node of q) {
  count++
  console.log('lookup reply:', node.value ? `${node.value.length}B` : 'null')
}
console.log('total:', count)

await srv.close()
await dht.destroy()
await t.destroy()