From 65b9675ffe9d43b521ca81e90fc34d275f812654 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 7 Jan 2018 10:31:38 +0000 Subject: [PATCH] chore: fix linting --- .aegir.js | 2 +- README.md | 4 +-- benchmarks/send.js | 49 +++++++++++++-------------- package.json | 2 +- src/etm.js | 2 +- test/browser.js | 4 +-- test/{ => fixtures}/peer-browser.json | 0 test/{ => fixtures}/peer-node.json | 0 test/{index.spec.js => secio.spec.js} | 34 +++++++++---------- 9 files changed, 47 insertions(+), 50 deletions(-) rename test/{ => fixtures}/peer-browser.json (100%) rename test/{ => fixtures}/peer-node.json (100%) rename test/{index.spec.js => secio.spec.js} (91%) diff --git a/.aegir.js b/.aegir.js index f70335f..cafdc3b 100644 --- a/.aegir.js +++ b/.aegir.js @@ -7,7 +7,7 @@ const PeerId = require('peer-id') const secio = require('./src') -const peerNodeJSON = require('./test/peer-node.json') +const peerNodeJSON = require('./test/fixtures/peer-node.json') const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws') let listener diff --git a/README.md b/README.md index 8450564..ef11590 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ ![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square) -> Secio implementation in JavaScript +> SECIO implementation in JavaScript This repo contains the JavaScript implementation of secio, an encryption protocol used in libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-secio). @@ -43,7 +43,7 @@ const secio = require('libp2p-secio') The current `secio` tag, usable in `multistream`. -### `encrypt(id, key, insecure[, callback])` +### `i.encrypt(id, key, insecure[, callback])` - `id: PeerId` - The id of the node. - `key: RSAPrivateKey` - The private key of the node. diff --git a/benchmarks/send.js b/benchmarks/send.js index 386604a..e0350cb 100644 --- a/benchmarks/send.js +++ b/benchmarks/send.js @@ -8,10 +8,22 @@ const crypto = require('libp2p-crypto') const secio = require('../src') +function createSession (insecure, cb) { + crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { + if (err) { return cb(err) } + + PeerId.createFromPrivKey(key.bytes, (err, id) => { + if (err) { return cb(err) } + + cb(null, secio.encrypt(id, key, insecure)) + }) + }) +} + const suite = new Benchmark.Suite('secio') const ids = [] -suite.add('createKey', function (d) { +suite.add('createKey', (d) => { crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { if (err) { throw err } PeerId.createFromPrivKey(key.bytes, (err, id) => { @@ -21,10 +33,9 @@ suite.add('createKey', function (d) { d.resolve() }) }) -}, { - defer: true -}) -.add('send', function (deferred) { +}, { defer: true }) + +suite.add('send', (deferred) => { const p = pair() createSession(p[0], (err, local) => { @@ -48,31 +59,17 @@ suite.add('createKey', function (d) { remote, pull.take(100), pull.collect((err, chunks) => { - if (err) throw err - if (chunks.length !== 100) throw new Error('Did not receive enough chunks') + if (err) { throw err } + if (chunks.length !== 100) { throw new Error('Did not receive enough chunks') } deferred.resolve() }) ) } -}, { - defer: true -}) -.on('cycle', (event) => { +}, { defer: true }) + +suite.on('cycle', (event) => { console.log(String(event.target)) }) + // run async -.run({ - async: true -}) - -function createSession (insecure, cb) { - crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { - if (err) { return cb(err) } - - PeerId.createFromPrivKey(key.bytes, (err, id) => { - if (err) { return cb(err) } - - cb(null, secio.encrypt(id, key, insecure)) - }) - }) -} +suite.run({ async: true }) diff --git a/package.json b/package.json index 7a27a52..be98930 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "release-major": "aegir release --type major -t node browser", "coverage": "aegir coverage", "coverage-publish": "aegir coverage publish", - "bench": "node benchmarks/send.js" + "benchmark": "node benchmarks/send.js" }, "keywords": [ "IPFS", diff --git a/src/etm.js b/src/etm.js index 2f1fb5d..02b75f4 100644 --- a/src/etm.js +++ b/src/etm.js @@ -71,7 +71,7 @@ exports.createUnboxStream = (decipher, mac) => { function ensureBuffer () { return pull.map((c) => { if (typeof c === 'string') { - return new Buffer(c, 'utf-8') + return Buffer.from(c, 'utf-8') } return c diff --git a/test/browser.js b/test/browser.js index fce0073..2aff8ee 100644 --- a/test/browser.js +++ b/test/browser.js @@ -12,10 +12,10 @@ const WS = require('libp2p-websockets') const PeerId = require('peer-id') const parallel = require('async/parallel') -const peerBrowserJSON = require('./peer-browser.json') +const peerBrowserJSON = require('./fixtures/peer-browser.json') const secio = require('../src') -describe('secio browser <-> nodejs', () => { +describe('secio between browser <-> nodejs through websockets', () => { const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws') let conn let encryptedConn diff --git a/test/peer-browser.json b/test/fixtures/peer-browser.json similarity index 100% rename from test/peer-browser.json rename to test/fixtures/peer-browser.json diff --git a/test/peer-node.json b/test/fixtures/peer-node.json similarity index 100% rename from test/peer-node.json rename to test/fixtures/peer-node.json diff --git a/test/index.spec.js b/test/secio.spec.js similarity index 91% rename from test/index.spec.js rename to test/secio.spec.js index b4c9c5c..11730b0 100644 --- a/test/index.spec.js +++ b/test/secio.spec.js @@ -19,8 +19,20 @@ const Dialer = ms.Dialer const secio = require('../src') -describe('libp2p-secio', () => { - it('exports a tag', () => { +function createSession (insecure, callback) { + crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { + expect(err).to.not.exist() + + key.public.hash((err, digest) => { + expect(err).to.not.exist() + + callback(null, secio.encrypt(new PeerId(digest, key), key, insecure)) + }) + }) +} + +describe('secio', () => { + it('exports a secio multicodec', () => { expect(secio.tag).to.equal('/secio/1.0.0') }) @@ -51,7 +63,7 @@ describe('libp2p-secio', () => { }) }) - it('works over multistream', function (done) { + it('works over multistream-select', function (done) { this.timeout(20 * 1000) const p = pair() @@ -72,7 +84,7 @@ describe('libp2p-secio', () => { local, pull.collect((err, chunks) => { expect(err).to.not.exist() - expect(chunks).to.be.eql([new Buffer('hello world')]) + expect(chunks).to.eql([Buffer.from('hello world')]) done() }) ) @@ -86,7 +98,7 @@ describe('libp2p-secio', () => { createSession(conn, (err, remote) => { expect(err).to.not.exist() pull( - pull.values([new Buffer('hello world')]), + pull.values([Buffer.from('hello world')]), remote ) cb() @@ -95,15 +107,3 @@ describe('libp2p-secio', () => { ], (err) => expect(err).to.not.exist()) }) }) - -function createSession (insecure, callback) { - crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { - expect(err).to.not.exist() - - key.public.hash((err, digest) => { - expect(err).to.not.exist() - - callback(null, secio.encrypt(new PeerId(digest, key), key, insecure)) - }) - }) -}