Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit e43f063

Browse files
authored
feat: async await and upgrades Hapi to v18 (#35)
feat: async await and upgrades Hapi to v18
2 parents 50a2e2e + 7ef54b2 commit e43f063

File tree

6 files changed

+120
-122
lines changed

6 files changed

+120
-122
lines changed

package.json

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,30 @@
3030
],
3131
"license": "MIT",
3232
"dependencies": {
33-
"async": "^2.6.2",
3433
"data-queue": "0.0.3",
3534
"debug": "^4.1.1",
36-
"epimetheus": "^1.0.92",
37-
"hapi": "^16.6.2",
38-
"inert": "^4.2.1",
39-
"libp2p-crypto": "~0.16.1",
35+
"@hapi/hapi": "^18.3.1",
36+
"@hapi/inert": "^5.2.1",
37+
"libp2p-crypto": "~0.17.0",
4038
"mafmt": "^6.0.7",
39+
"menoetius": "~0.0.2",
4140
"merge-recursive": "0.0.3",
4241
"minimist": "^1.2.0",
43-
"multiaddr": "^6.0.6",
42+
"multiaddr": "^6.1.0",
4443
"once": "^1.4.0",
45-
"peer-id": "~0.12.2",
46-
"peer-info": "~0.15.1",
47-
"prom-client": "^11.3.0",
48-
"socket.io": "^2.2.0",
49-
"socket.io-client": "^2.2.0",
50-
"socket.io-pull-stream": "~0.1.5",
51-
"uuid": "^3.3.2"
44+
"peer-id": "~0.13.1",
45+
"peer-info": "~0.16.0",
46+
"prom-client": "^11.5.3",
47+
"socket.io": "^2.0.4",
48+
"socket.io-client": "^2.0.4",
49+
"socket.io-pull-stream": "^0.1.1",
50+
"uuid": "^3.1.0"
5251
},
5352
"directories": {
5453
"test": "test"
5554
},
5655
"devDependencies": {
57-
"aegir": "^18.2.2",
56+
"aegir": "^19.0.5",
5857
"chai": "^4.2.0",
5958
"dirty-chai": "^2.0.1",
6059
"lodash": "^4.17.11"

src/bin.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,38 @@
55
const signalling = require('./index')
66
const argv = require('minimist')(process.argv.slice(2))
77

8-
let server
9-
108
/* eslint-disable no-console */
119

12-
signalling.start({
13-
port: argv.port || argv.p || process.env.PORT || 9090,
14-
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
15-
key: argv.key || process.env.KEY,
16-
cert: argv.cert || process.env.CERT,
17-
pfx: argv.pfx || process.env.PFX,
18-
passphrase: argv.passphrase || process.env.PFX_PASSPHRASE,
19-
cryptoChallenge: !(argv.disableCryptoChallenge || process.env.DISABLE_CRYPTO_CHALLENGE),
20-
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR),
21-
metrics: !(argv.disableMetrics || process.env.DISABLE_METRICS)
22-
}, (err, _server) => {
23-
if (err) { throw err }
24-
server = _server
10+
async function start () {
11+
const server = await signalling.start({
12+
port: argv.port || argv.p || process.env.PORT || 9090,
13+
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
14+
key: argv.key || process.env.KEY,
15+
cert: argv.cert || process.env.CERT,
16+
pfx: argv.pfx || process.env.PFX,
17+
passphrase: argv.passphrase || process.env.PFX_PASSPHRASE,
18+
cryptoChallenge: !(argv.disableCryptoChallenge || process.env.DISABLE_CRYPTO_CHALLENGE),
19+
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR),
20+
metrics: !(argv.disableMetrics || process.env.DISABLE_METRICS)
21+
})
2522

2623
console.log('Listening on:', server.info.uri)
27-
})
2824

29-
process.on('SIGINT', () => {
30-
server.stop((err) => {
25+
process.on('SIGINT', async () => {
26+
try {
27+
await server.stop()
28+
} catch (err) {
29+
console.error(err)
30+
process.exit(2)
31+
}
32+
3133
console.log('Rendezvous server stopped')
32-
process.exit(err ? 2 : 0)
34+
process.exit(0)
35+
})
36+
}
37+
38+
start()
39+
.catch((err) => {
40+
console.error(err)
41+
process.exit(2)
3342
})
34-
})

src/config.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ module.exports = {
1010
port: process.env.PORT || 13579,
1111
host: '0.0.0.0',
1212
options: {
13-
connections: {
14-
routes: {
15-
cors: true
16-
}
13+
routes: {
14+
cors: true
1715
}
1816
}
1917
},

src/index.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
'use strict'
22

3-
const Hapi = require('hapi')
3+
const Hapi = require('@hapi/hapi')
44
const path = require('path')
5-
const epimetheus = require('epimetheus')
5+
const menoetius = require('menoetius')
66
const merge = require('merge-recursive').recursive
7-
const defaultConfig = require('./config')
7+
const Inert = require('@hapi/inert')
88
const { readFileSync } = require('fs')
99

1010
exports = module.exports
1111

12-
exports.start = (options, callback) => {
13-
if (typeof options === 'function') {
14-
callback = options
15-
options = {}
16-
}
17-
18-
const config = merge(Object.assign({}, defaultConfig), options)
12+
exports.start = async (options = {}) => {
13+
const config = merge(Object.assign({}, require('./config')), Object.assign({}, options))
1914
const log = config.log
2015

2116
const port = options.port || config.hapi.port
2217
const host = options.host || config.hapi.host
2318

24-
const http = new Hapi.Server(config.hapi.options)
25-
2619
let tls
2720
if (options.key && options.cert) {
2821
tls = {
@@ -37,35 +30,30 @@ exports.start = (options, callback) => {
3730
}
3831
}
3932

40-
http.connection({ port, host, tls })
33+
const http = new Hapi.Server(Object.assign({
34+
port,
35+
host,
36+
tls
37+
}, config.hapi.options))
4138

42-
http.register({ register: require('inert') }, (err) => {
43-
if (err) {
44-
return callback(err)
45-
}
39+
await http.register(Inert)
40+
await http.start()
4641

47-
http.start((err) => {
48-
if (err) {
49-
return callback(err)
50-
}
42+
log('rendezvous server has started on: ' + http.info.uri)
5143

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

54-
http.peers = require('./routes')(config, http).peers
55-
56-
http.route({
57-
method: 'GET',
58-
path: '/',
59-
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
60-
confine: false
61-
})
62-
})
63-
64-
callback(null, http)
46+
http.route({
47+
method: 'GET',
48+
path: '/',
49+
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
50+
confine: false
6551
})
6652
})
6753

68-
if (config.metrics) { epimetheus.instrument(http) }
54+
if (config.metrics) {
55+
menoetius.instrument(http)
56+
}
6957

7058
return http
7159
}

src/utils.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,14 @@ function Protocol (log) {
101101
}
102102

103103
function getIdAndValidate (pub, id, cb) {
104-
Id.createFromPubKey(Buffer.from(pub, 'hex'), (err, _id) => {
105-
if (err) {
106-
return cb(new Error('Crypto error'))
107-
}
108-
109-
if (_id.toB58String() !== id) {
110-
return cb(new Error('Id is not matching'))
111-
}
104+
Id.createFromPubKey(Buffer.from(pub, 'hex'))
105+
.then(_id => {
106+
if (_id.toB58String() !== id) {
107+
throw Error('Id is not matching')
108+
}
112109

113-
return cb(null, crypto.keys.unmarshalPublicKey(Buffer.from(pub, 'hex')))
114-
})
110+
return crypto.keys.unmarshalPublicKey(Buffer.from(pub, 'hex'))
111+
}, cb)
115112
}
116113

117114
exports = module.exports

test/rendezvous.spec.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@ const uuid = require('uuid')
1212
const rendezvous = require('../src')
1313

1414
describe('rendezvous', () => {
15+
it('start and stop signalling server (default port)', async () => {
16+
const server = await rendezvous.start()
17+
18+
expect(server.info.port).to.equal(13579)
19+
expect(server.info.protocol).to.equal('http')
20+
expect(server.info.address).to.equal('0.0.0.0')
21+
22+
await server.stop()
23+
})
24+
25+
it('start and stop signalling server (custom port)', async () => {
26+
const options = {
27+
port: 12345
28+
}
29+
30+
const server = await rendezvous.start(options)
31+
32+
expect(server.info.port).to.equal(options.port)
33+
expect(server.info.protocol).to.equal('http')
34+
expect(server.info.address).to.equal('0.0.0.0')
35+
36+
await server.stop()
37+
})
38+
})
39+
40+
describe('signalling server client', () => {
1541
const sioOptions = {
1642
transports: ['websocket'],
1743
'force new connection': true
@@ -29,31 +55,7 @@ describe('rendezvous', () => {
2955
let c3mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo3')
3056
let c4mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4')
3157

32-
it('start and stop signalling server (default port)', (done) => {
33-
rendezvous.start((err, server) => {
34-
expect(err).to.not.exist()
35-
expect(server.info.port).to.equal(13579)
36-
expect(server.info.protocol).to.equal('http')
37-
expect(server.info.address).to.equal('0.0.0.0')
38-
server.stop(done)
39-
})
40-
})
41-
42-
it('start and stop signalling server (custom port)', (done) => {
43-
const options = {
44-
port: 12345
45-
}
46-
47-
rendezvous.start(options, (err, server) => {
48-
expect(err).to.not.exist()
49-
expect(server.info.port).to.equal(12345)
50-
expect(server.info.protocol).to.equal('http')
51-
expect(server.info.address).to.equal('0.0.0.0')
52-
server.stop(done)
53-
})
54-
})
55-
56-
it('start signalling server for client tests', (done) => {
58+
before(async () => {
5759
const options = {
5860
port: 12345,
5961
refreshPeerListIntervalMS: 1000,
@@ -62,15 +64,27 @@ describe('rendezvous', () => {
6264
metrics: true
6365
}
6466

65-
rendezvous.start(options, (err, server) => {
66-
expect(err).to.not.exist()
67-
expect(server.info.port).to.equal(12345)
68-
expect(server.info.protocol).to.equal('http')
69-
expect(server.info.address).to.equal('0.0.0.0')
70-
sioUrl = server.info.uri
71-
r = server
72-
done()
73-
})
67+
const server = await rendezvous.start(options)
68+
69+
expect(server.info.port).to.equal(12345)
70+
expect(server.info.protocol).to.equal('http')
71+
expect(server.info.address).to.equal('0.0.0.0')
72+
sioUrl = server.info.uri
73+
r = server
74+
})
75+
76+
after(async () => {
77+
if (c1) {
78+
c1.disconnect()
79+
}
80+
81+
if (c2) {
82+
c2.disconnect()
83+
}
84+
85+
if (r) {
86+
await r.stop()
87+
}
7488
})
7589

7690
it('zero peers', () => {
@@ -196,10 +210,4 @@ describe('rendezvous', () => {
196210
}
197211
}
198212
}).timeout(4000)
199-
200-
it('stop signalling server', (done) => {
201-
c1.disconnect()
202-
c2.disconnect()
203-
r.stop(done)
204-
})
205213
})

0 commit comments

Comments
 (0)