Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

feat: async await and upgrades Hapi to v18 #35

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,30 @@
],
"license": "MIT",
"dependencies": {
"async": "^2.6.2",
"data-queue": "0.0.3",
"debug": "^4.1.1",
"epimetheus": "^1.0.92",
"hapi": "^16.6.2",
"inert": "^4.2.1",
"libp2p-crypto": "~0.16.1",
"epimetheus": "achingbrain/node-epimetheus#hapi-18-support",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending merging of roylines/node-epimetheus#63

"@hapi/hapi": "^18.3.1",
"@hapi/inert": "^5.2.1",
"libp2p-crypto": "~0.17.0",
"mafmt": "^6.0.7",
"merge-recursive": "0.0.3",
"minimist": "^1.2.0",
"multiaddr": "^6.0.6",
"multiaddr": "^6.1.0",
"once": "^1.4.0",
"peer-id": "~0.12.2",
"peer-info": "~0.15.1",
"prom-client": "^11.3.0",
"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0",
"socket.io-pull-stream": "~0.1.5",
"uuid": "^3.3.2"
"peer-id": "~0.13.1",
"peer-info": "~0.16.0",
"prom-client": "^11.5.3",
"socket.io": "^2.0.4",
"socket.io-client": "^2.0.4",
"socket.io-pull-stream": "^0.1.1",
"uuid": "^3.1.0"
},
"directories": {
"test": "test"
},
"devDependencies": {
"aegir": "^18.2.2",
"aegir": "^19.0.5",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"lodash": "^4.17.11"
Expand Down
48 changes: 28 additions & 20 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,38 @@
const signalling = require('./index')
const argv = require('minimist')(process.argv.slice(2))

let server

/* eslint-disable no-console */

signalling.start({
port: argv.port || argv.p || process.env.PORT || 9090,
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
key: argv.key || process.env.KEY,
cert: argv.cert || process.env.CERT,
pfx: argv.pfx || process.env.PFX,
passphrase: argv.passphrase || process.env.PFX_PASSPHRASE,
cryptoChallenge: !(argv.disableCryptoChallenge || process.env.DISABLE_CRYPTO_CHALLENGE),
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR),
metrics: !(argv.disableMetrics || process.env.DISABLE_METRICS)
}, (err, _server) => {
if (err) { throw err }
server = _server
async function start () {
const server = await signalling.start({
port: argv.port || argv.p || process.env.PORT || 9090,
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
key: argv.key || process.env.KEY,
cert: argv.cert || process.env.CERT,
pfx: argv.pfx || process.env.PFX,
passphrase: argv.passphrase || process.env.PFX_PASSPHRASE,
cryptoChallenge: !(argv.disableCryptoChallenge || process.env.DISABLE_CRYPTO_CHALLENGE),
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR),
metrics: !(argv.disableMetrics || process.env.DISABLE_METRICS)
})

console.log('Listening on:', server.info.uri)
})

process.on('SIGINT', () => {
server.stop((err) => {
process.on('SIGINT', async () => {
try {
await server.stop()
} catch (err) {
console.error(err)
process.exit(2)
}

console.log('Rendezvous server stopped')
process.exit(err ? 2 : 0)
process.exit(0)
})
}

start()
.catch((err) => {
console.error(err)
process.exit(2)
})
})
6 changes: 2 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ module.exports = {
port: process.env.PORT || 13579,
host: '0.0.0.0',
options: {
connections: {
routes: {
cors: true
}
routes: {
cors: true
}
}
},
Expand Down
54 changes: 21 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
'use strict'

const Hapi = require('hapi')
const Hapi = require('@hapi/hapi')
const path = require('path')
const epimetheus = require('epimetheus')
const merge = require('merge-recursive').recursive
const defaultConfig = require('./config')
const Inert = require('@hapi/inert')
const { readFileSync } = require('fs')

exports = module.exports

exports.start = (options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

const config = merge(Object.assign({}, defaultConfig), options)
exports.start = async (options = {}) => {
const config = merge(Object.assign({}, require('./config')), Object.assign({}, options))
const log = config.log

const port = options.port || config.hapi.port
const host = options.host || config.hapi.host

const http = new Hapi.Server(config.hapi.options)

let tls
if (options.key && options.cert) {
tls = {
Expand All @@ -37,35 +30,30 @@ exports.start = (options, callback) => {
}
}

http.connection({ port, host, tls })
const http = new Hapi.Server(Object.assign({
port,
host,
tls
}, config.hapi.options))

http.register({ register: require('inert') }, (err) => {
if (err) {
return callback(err)
}
await http.register(Inert)
await http.start()

http.start((err) => {
if (err) {
return callback(err)
}
log('rendezvous server has started on: ' + http.info.uri)

log('rendezvous server has started on: ' + http.info.uri)
http.peers = require('./routes')(config, http).peers

http.peers = require('./routes')(config, http).peers

http.route({
method: 'GET',
path: '/',
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
confine: false
})
})

callback(null, http)
http.route({
method: 'GET',
path: '/',
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
confine: false
})
})

if (config.metrics) { epimetheus.instrument(http) }
if (config.metrics) {
epimetheus.instrument(http)
}

return http
}
17 changes: 7 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,14 @@ function Protocol (log) {
}

function getIdAndValidate (pub, id, cb) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not doing async await here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't appear to be part of the public API and is only invoked in the context of a socket.io-pull-stream handler which is all callbacks.

Id.createFromPubKey(Buffer.from(pub, 'hex'), (err, _id) => {
if (err) {
return cb(new Error('Crypto error'))
}

if (_id.toB58String() !== id) {
return cb(new Error('Id is not matching'))
}
Id.createFromPubKey(Buffer.from(pub, 'hex'))
.then(_id => {
if (_id.toB58String() !== id) {
throw Error('Id is not matching')
}

return cb(null, crypto.keys.unmarshalPublicKey(Buffer.from(pub, 'hex')))
})
return crypto.keys.unmarshalPublicKey(Buffer.from(pub, 'hex'))
}, cb)
}

exports = module.exports
Expand Down
88 changes: 48 additions & 40 deletions test/rendezvous.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ const uuid = require('uuid')
const rendezvous = require('../src')

describe('rendezvous', () => {
it('start and stop signalling server (default port)', async () => {
const server = await rendezvous.start()

expect(server.info.port).to.equal(13579)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')

await server.stop()
})

it('start and stop signalling server (custom port)', async () => {
const options = {
port: 12345
}

const server = await rendezvous.start(options)

expect(server.info.port).to.equal(options.port)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')

await server.stop()
})
})

describe('signalling server client', () => {
const sioOptions = {
transports: ['websocket'],
'force new connection': true
Expand All @@ -29,31 +55,7 @@ describe('rendezvous', () => {
let c3mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo3')
let c4mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4')

it('start and stop signalling server (default port)', (done) => {
rendezvous.start((err, server) => {
expect(err).to.not.exist()
expect(server.info.port).to.equal(13579)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')
server.stop(done)
})
})

it('start and stop signalling server (custom port)', (done) => {
const options = {
port: 12345
}

rendezvous.start(options, (err, server) => {
expect(err).to.not.exist()
expect(server.info.port).to.equal(12345)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')
server.stop(done)
})
})

it('start signalling server for client tests', (done) => {
before(async () => {
const options = {
port: 12345,
refreshPeerListIntervalMS: 1000,
Expand All @@ -62,15 +64,27 @@ describe('rendezvous', () => {
metrics: true
}

rendezvous.start(options, (err, server) => {
expect(err).to.not.exist()
expect(server.info.port).to.equal(12345)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')
sioUrl = server.info.uri
r = server
done()
})
const server = await rendezvous.start(options)

expect(server.info.port).to.equal(12345)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')
sioUrl = server.info.uri
r = server
})

after(async () => {
if (c1) {
c1.disconnect()
}

if (c2) {
c2.disconnect()
}

if (r) {
await r.stop()
}
})

it('zero peers', () => {
Expand Down Expand Up @@ -196,10 +210,4 @@ describe('rendezvous', () => {
}
}
}).timeout(4000)

it('stop signalling server', (done) => {
c1.disconnect()
c2.disconnect()
r.stop(done)
})
})