dht-keyvalue
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -18,3 +18,2 @@ const dhtKv = require('./server') | ||
dkv.put(items, (err, hash, key) => { | ||
if(err) console.log(err) | ||
console.log(`Successfully announced: ${key}, DHT address: ${hash}`) | ||
@@ -21,0 +20,0 @@ |
{ | ||
"name": "dht-keyvalue", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Store key-value pairs on the mainline bittorrent DHT network, and retreive/update them by key name.", | ||
@@ -22,3 +22,5 @@ "main": "server.js", | ||
"bittorrent-dht": "^10.0.1", | ||
"bittorrent-dht-sodium": "^1.2.0" | ||
"bittorrent-dht-sodium": "^1.2.0", | ||
"eventemitter": "^0.3.3", | ||
"inherits": "^2.0.4" | ||
}, | ||
@@ -25,0 +27,0 @@ "keywords": [ |
@@ -54,14 +54,25 @@ # dht-keyvalue | ||
```js | ||
const dhtKv = require('./server') | ||
let opts = { | ||
keep: true, // default = true. Keep the DHT object alive in the mainline bittorrent network | ||
keepalive: 3600000 // default = 3600000. Time to refresh the DHT object | ||
} | ||
const dkv = new dhtKv(opts) | ||
let items = [ | ||
{ key: "first key", value: "first initial value" }, | ||
{ key: "second key", value: "second initial value" }, | ||
{ key: "first key", value: "first key value" }, | ||
{ key: "second key", value: "second key value" }, | ||
//... | ||
] | ||
dkv.put(items, (hash, key) => { | ||
console.log('Successfully announced:', key, 'DHT address:', hash) | ||
// Put items in DHT | ||
dkv.put(items, (err, hash, key) => { | ||
if(err) console.log(err) | ||
console.log(`Successfully announced: ${key}, DHT address: ${hash}`) | ||
// Now that it is announced, retrieve it from DHT | ||
dkv.get(key, value => { | ||
console.log(value) | ||
dkv.get(key, (err, value) => { | ||
console.log(`Get successful for key, ${key}: ${value}`) | ||
}) | ||
@@ -71,10 +82,17 @@ | ||
let newValue = Math.random() // some updated value | ||
dkv.update(key, newValue, updated => { | ||
console.log('Updated in DHT', updated) | ||
console.log(`Update successful (${updated}) for key, ${key}. New value: ${newValue}`) | ||
// Retrieve the updated value | ||
dkv.get(key, value => { | ||
console.log(value) | ||
dkv.get(key, (err, value) => { | ||
console.log(`Get successful for updated ${key}: ${value}`) | ||
}) | ||
}) | ||
// Retrieve the value by hash instead of by key | ||
dkv.getHash(hash, (err, value) => { | ||
if(err) console.log(err) | ||
console.log(value) | ||
}) | ||
}) | ||
@@ -108,6 +126,6 @@ ``` | ||
```js | ||
dkv.get(key, (err, value) => { | ||
if(err) return console.log(err) | ||
console.log(`Get successful for key, ${key}: ${value}`) | ||
}) | ||
dkv.get(key, (err, value) => { | ||
if(err) return console.log(err) | ||
console.log(`Get successful for key, ${key}: ${value}`) | ||
}) | ||
``` | ||
@@ -122,5 +140,5 @@ | ||
```js | ||
dkv.update(key, newValue, updated => { | ||
console.log(`Upddate successful (${updated}) for key, ${key}. New value: ${newValue}`) | ||
}) | ||
dkv.update(key, newValue, updated => { | ||
console.log(`Upddate successful (${updated}) for key, ${key}. New value: ${newValue}`) | ||
}) | ||
``` | ||
@@ -130,1 +148,12 @@ | ||
### `dkv.getHash(hash, [callback: (err, value)])` | ||
Get an item stored on DHT by the `hash` address of the DHT record. `callback` returns the item's `key` name and the DHT object's `value`. | ||
#### Example | ||
```js | ||
dkv.getHash(hash, (err, value) => { | ||
if(err) console.log(err) | ||
console.log(value) | ||
}) | ||
``` | ||
@@ -55,3 +55,2 @@ module.exports = DhtKv | ||
dht.put(opts, function (err, hash) { | ||
if(err) console.error('Error:', err) | ||
hash = hash.toString('hex') | ||
@@ -94,3 +93,2 @@ | ||
dht.get(keyTable[item].hash, function (err, res) { | ||
if(err) console.log('Error:', err) | ||
if(res == null) return that.get(key, cb) | ||
@@ -131,6 +129,3 @@ cb(null, res.v.toString()) | ||
dht.get(key, function (err, res) { | ||
if(err) console.error('Error:', err) | ||
dht.put(opts, function (err, hash) { | ||
if(err) console.error('Error:', err) | ||
let key = hash.toString('hex') | ||
@@ -137,0 +132,0 @@ cb(true) |
11115
155
4
127
+ Addedeventemitter@^0.3.3
+ Addedinherits@^2.0.4
+ Addedeventemitter@0.3.3(transitive)