Skip to content

Commit

Permalink
Merge pull request #35 from davide-scola/master-add_mutable_items_sup…
Browse files Browse the repository at this point in the history
…port

Support mutable items
  • Loading branch information
prdn committed Oct 5, 2017
2 parents 59f6200 + 704caab commit 862488a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
26 changes: 25 additions & 1 deletion lib/Grape.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const async = require('async')
const http = require('http')
const Events = require('events')
const debug = require('debug')('grenache:grape')
const crypto = require('ed25519-supercop')

const noop = () => {}

Expand Down Expand Up @@ -43,7 +44,8 @@ class Grape extends Events {
maxTables: this.conf.dht_maxTables,
host: this.conf.host || false,
bootstrap: this.conf.dht_bootstrap,
timeBucketOutdated: this.conf.dht_nodeLiveness
timeBucketOutdated: this.conf.dht_nodeLiveness,
verify: crypto.verify
})

dht.on('announce', (_peer, ih) => {
Expand Down Expand Up @@ -118,6 +120,14 @@ class Grape extends Events {
return Buffer.from(val).toString('hex')
}

str2buf (val, enc) {
try {
return Buffer.from(JSON.parse(val))
} catch (ex) {
return Buffer.from(val, enc || '')
}
}

timeslot (offset, ts) {
offset = offset || 0
ts = ts || Date.now()
Expand Down Expand Up @@ -168,6 +178,20 @@ class Grape extends Events {
}

handlePeerPut (opts, cb) {
if (opts.k) {
if (!Buffer.isBuffer(opts.k)) {
opts.k = this.str2buf(opts.k, 'hex')
}

if (opts.sig && !Buffer.isBuffer(opts.sig)) {
opts.sig = this.str2buf(opts.sig, 'hex')
}

if (opts.salt && !Buffer.isBuffer(opts.salt)) {
opts.salt = this.str2buf(opts.salt)
}
}

this.put(opts, cb)
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"debug": "~2.2.0",
"lodash": "~4.17.4",
"lru": "^3.1.0",
"yargs": "^6.3.0"
"yargs": "^6.3.0",
"ed25519-supercop": "~1.0.2"
},
"engine": {
"node": ">=6.0"
Expand Down

0 comments on commit 862488a

Please sign in to comment.