这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
*.swo
.DS_Store
npm-debug.log
package-lock.json
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,6 @@ Stop announcing a service
Puts a value into the DHT.
[Example](https://github.com/bitfinexcom/grenache-nodejs-link/blob/master/examples/put_get.js).

#### link.putMutable(data, opts, callback)

- `data`
- `v`: <String> value to store
- `s`: <Number> sequence number
- `opts`
- `keys`: <Object> contains `ed25519-supercop` private and public key
- `publicKey`: <Buffer> public key
- `secretKey`: <Buffer> private key
- `callback` <function>

Provides sugar for storing mutable, signed data in the DHT.

[Example raw put](https://github.com/bitfinexcom/grenache-nodejs-link/blob/master/examples/put_get_mutable_raw.js)
<br/>
[Example with putMutable](https://github.com/bitfinexcom/grenache-nodejs-link/blob/master/examples/put_get_mutable.js)

#### link.get(hash | object, callback)

- `hash` &lt;String&gt; Hash used for lookup
Expand Down
30 changes: 0 additions & 30 deletions examples/put_get_mutable.js

This file was deleted.

35 changes: 0 additions & 35 deletions examples/put_get_mutable_raw.js

This file was deleted.

40 changes: 7 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { v4: uuidv4 } = require('uuid')
const LRU = require('lru')
const request = require('request')
const CbQ = require('cbq')
const bencode = require('bencode')

class Link {
constructor (conf) {
Expand All @@ -31,7 +30,7 @@ class Link {

post (url, data, opts, cb) {
request.post(_.extend({
url: url,
url,
json: true,
body: data
}, opts), (err, res, data) => {
Expand Down Expand Up @@ -104,7 +103,7 @@ class Link {

if (fromGrape) {
if (!err && data) {
let cache = this.cache[req.type]
const cache = this.cache[req.type]
if (cache) {
cache.set(req.qhash, data)
}
Expand All @@ -122,10 +121,10 @@ class Link {
const rid = uuidv4()

const req = {
rid: rid,
type: type,
payload: payload,
opts: opts,
rid,
type,
payload,
opts,
cb: _.isFunction(cb) ? cb : () => {},
_ts: Date.now()
}
Expand Down Expand Up @@ -218,31 +217,6 @@ class Link {
this.request('put', opts, {}, cb)
}

putMutable (data, opts, cb) {
if (!data || !opts || !cb) throw new Error('ERR_MISSING_ARGS')
if (!data.seq) return cb(new Error('ERR_MISSING_SEQ'))

const { publicKey, secretKey } = opts.keys
if (!publicKey || !secretKey) return cb(new Error('ERR_MISSING_KEY'))

data.k = publicKey.toString('hex')

const toEncode = { seq: data.seq, v: data.v }

if (data.salt) toEncode.salt = data.salt

const encoded = bencode
.encode(toEncode)
.slice(1, -1)
.toString()

data.sig = ed
.sign(encoded, publicKey, secretKey)
.toString('hex')

this.put(data, cb)
}

get (hash, cb) {
this.request('get', hash, {}, cb)
}
Expand Down Expand Up @@ -287,7 +261,7 @@ class Link {
c.clear()
})

for (let info of this._announces.values()) {
for (const info of this._announces.values()) {
info.stopped = true
clearTimeout(info.timeout)
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grenache-nodejs-link",
"version": "0.7.13",
"version": "1.0.0",
"description": "Granache Node.js Link",
"author": "prdn <paolo@bitfinex.com> (https://bitfinex.com/)",
"keywords": [
Expand All @@ -11,7 +11,6 @@
],
"dependencies": {
"async": "^3.2.4",
"bencode": "^2.0.3",
"cbq": "0.0.1",
"lodash": "^4.17.21",
"lru": "^3.1.0",
Expand All @@ -21,13 +20,14 @@
"main": "index.js",
"devDependencies": {
"bfx-svc-test-helper": "git+https://github.com/bitfinexcom/bfx-svc-test-helper.git",
"mocha": "^5.2.0",
"standard": "^12.0.1"
"mocha": "^10.2.0",
"standard": "^17.1.0"
},
"scripts": {
"test": "npm run lint && npm run unit",
"unit": "mocha",
"lint": "standard"
"lint": "standard",
"lint:fix": "standard --fix"
},
"license": "Apache-2.0",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('announce and lookups', () => {
link.lookup('test', {}, (err, hash) => {
if (err) throw err
assert.deepStrictEqual(
link.cache['lookup'].get('lookup:"test"'),
[ '127.0.0.1:10000' ]
link.cache.lookup.get('lookup:"test"'),
['127.0.0.1:10000']
)
link.stop()
done()
Expand Down
4 changes: 2 additions & 2 deletions test/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ describe('announce and lookups', () => {
if (err) throw err
link.lookup('test', {}, (err, res) => {
if (err) throw err
assert.deepStrictEqual(res, [ '127.0.0.1:10000' ])
assert.deepStrictEqual(res, ['127.0.0.1:10000'])

// look ma, no options passed!
link.lookup('test', (err, res) => {
if (err) throw err
assert.deepStrictEqual(res, [ '127.0.0.1:10000' ])
assert.deepStrictEqual(res, ['127.0.0.1:10000'])
link.stop()
done()
})
Expand Down
79 changes: 0 additions & 79 deletions test/put-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict'

const assert = require('assert')
const ed = require('ed25519-supercop')

const Link = require('../')

Expand Down Expand Up @@ -42,82 +41,4 @@ describe('announce and lookups', () => {
})
})
}).timeout(7000)

it('provides sugar for mutable data', (done) => {
const link = new Link({
grape: 'http://127.0.0.1:30001'
})
link.start()

const data = { v: 'hello world', seq: 1 }
const opts = {
keys: ed.createKeyPair(ed.createSeed())
}

link.putMutable(data, opts, (err, hash) => {
if (err) throw err

link.get(hash, (err, res) => {
if (err) throw err

assert.strictEqual(res.v, 'hello world')
assert.strictEqual(typeof res.k, 'string')
assert.strictEqual(typeof res.sig, 'string')
assert.ok(res.sig)

link.stop()
done()
})
})
}).timeout(7000)

it('mutable data supports salt', (done) => {
const link = new Link({
grape: 'http://127.0.0.1:30001'
})
link.start()

const data = { v: 'hello world', seq: 1, salt: 'foobar' }
const opts = {
keys: ed.createKeyPair(ed.createSeed())
}

link.putMutable(data, opts, (err, hash) => {
if (err) throw err

link.get({ hash: hash, salt: 'foobar' }, (err, res) => {
if (err) throw err

assert.strictEqual(res.v, 'hello world')
assert.strictEqual(typeof res.k, 'string')
assert.strictEqual(typeof res.sig, 'string')
assert.strictEqual(res.salt, 'foobar')
assert.ok(res.sig)

link.stop()
done()
})
})
}).timeout(7000)

it('returns errors in case of errors', (done) => {
const link = new Link({
grape: 'http://127.0.0.1:30001'
})
link.start()

const data = { v: 'hello world', seq: 1, salt: 'foobar' }
const opts = {
keys: ed.createKeyPair(ed.createSeed())
}

link.putMutable(data, opts, (err, hash) => {
if (err) throw err
link.putMutable(data, opts, (err2, hash) => {
assert.strictEqual(err2.code, 302)
link.stop()
done()
})
})
}).timeout(7000)
})