chore: fix linting

This commit is contained in:
David Dias 2018-01-07 10:31:38 +00:00
parent 87fb3462a4
commit 65b9675ffe
9 changed files with 47 additions and 50 deletions

View File

@ -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

View File

@ -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.

View File

@ -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 })

View File

@ -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",

View File

@ -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

View File

@ -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

View File

@ -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))
})
})
}