You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

8 years ago
var dht = require('./')
var blake2b = require('./blake2b')
8 years ago
var node = dht({
bootstrap: 'localhost:49737',
ephemeral: !!process.argv[2]
8 years ago
})
var values = {}
node.on('update:store', function (query, cb) {
console.log('(onupdate)')
8 years ago
if (!query.value) return cb()
var key = blake2b(query.value).toString('hex')
8 years ago
values[key] = query.value
console.log('Storing', key, '-->', query.value.toString())
cb()
})
node.on('query:lookup', function (query, cb) {
console.log('(onquery)')
8 years ago
var value = values[query.target.toString('hex')]
cb(null, value)
})
if (process.argv.length > 3) {
var val = process.argv.slice(3).join(' ')
if (process.argv[2] === 'put') {
node.update({command: 'store', target: blake2b(Buffer.from(val)), value: val}, function (err) {
8 years ago
if (err) throw err
console.log('Inserted', blake2b(Buffer.from(val)).toString('hex'))
8 years ago
})
8 years ago
}
if (process.argv[2] === 'get') {
node.query({command: 'lookup', target: Buffer.from(val, 'hex')})
8 years ago
.on('data', function (data) {
if (data.value && blake2b(data.value).toString('hex') === val) {
8 years ago
console.log(val, '-->', data.value.toString())
this.destroy()
}
})
8 years ago
.on('end', function () {
console.log('(query finished)')
})
}
}