diff --git a/.gitignore b/.gitignore index 8f7db51..3708b9a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules *.swo .DS_Store npm-debug.log +package-lock.json diff --git a/README.md b/README.md index 38f5638..b0800fc 100644 --- a/README.md +++ b/README.md @@ -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) -
-[Example with putMutable](https://github.com/bitfinexcom/grenache-nodejs-link/blob/master/examples/put_get_mutable.js) - #### link.get(hash | object, callback) - `hash` <String> Hash used for lookup diff --git a/examples/put_get_mutable.js b/examples/put_get_mutable.js deleted file mode 100644 index 1200c02..0000000 --- a/examples/put_get_mutable.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const ed = require('ed25519-supercop') -const Link = require('../') - -const data = { - seq: 1, - v: 'Hello World!' - // salt: "foobar" -} - -const opts = { - keys: ed.createKeyPair(ed.createSeed()) -} - -const link = new Link({ - grape: 'http://127.0.0.1:30001' -}) -link.start() - -setTimeout(() => { - link.putMutable(data, opts, (err, hash) => { - console.log('putMutable:', err, hash) - if (hash) { - link.get(hash, (err, res) => { - console.log('data requested from the DHT', err, res) - }) - } - }) -}, 2000) diff --git a/examples/put_get_mutable_raw.js b/examples/put_get_mutable_raw.js deleted file mode 100644 index 214e850..0000000 --- a/examples/put_get_mutable_raw.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict' - -const Link = require('../') - -const ed = require('ed25519-supercop') -const bencode = require('bencode') - -const { publicKey, secretKey } = ed.createKeyPair(ed.createSeed()) - -const value = 'Hello World!' -const data = { - k: publicKey.toString('hex'), - seq: 1, - v: value -} - -const toEncode = { seq: data.seq, v: data.v } -const encoded = bencode.encode(toEncode).slice(1, -1).toString() -data.sig = ed.sign(encoded, publicKey, secretKey).toString('hex') - -const link = new Link({ - grape: 'http://127.0.0.1:30001' -}) -link.start() - -setTimeout(() => { - link.put(data, (err, hash) => { - console.log(err, hash) - if (hash) { - link.get(hash, (err, res) => { - console.log('data requested from the DHT', err, res) - }) - } - }) -}, 2000) diff --git a/index.js b/index.js index 2bca202..ba50932 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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) => { @@ -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) } @@ -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() } @@ -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) } @@ -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) } diff --git a/package.json b/package.json index c125f2f..69ae580 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "grenache-nodejs-link", - "version": "0.7.13", + "version": "1.0.0", "description": "Granache Node.js Link", "author": "prdn (https://bitfinex.com/)", "keywords": [ @@ -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", @@ -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": { diff --git a/test/cache.js b/test/cache.js index 5173da4..40a3778 100644 --- a/test/cache.js +++ b/test/cache.js @@ -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() diff --git a/test/lookup.js b/test/lookup.js index cb7b09b..ae98c24 100644 --- a/test/lookup.js +++ b/test/lookup.js @@ -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() }) diff --git a/test/put-get.js b/test/put-get.js index 72e5c96..5b9c719 100644 --- a/test/put-get.js +++ b/test/put-get.js @@ -3,7 +3,6 @@ 'use strict' const assert = require('assert') -const ed = require('ed25519-supercop') const Link = require('../') @@ -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) })