Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.

Commit f7dc83a

Browse files
authoredMay 8, 2019
fix: update hapi (#173)
BREAKING CHANGE: signaling server api with async await instead of callbacks
1 parent 0fd71ad commit f7dc83a

File tree

7 files changed

+101
-117
lines changed

7 files changed

+101
-117
lines changed
 

‎.aegir.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ const sigServer = require('./src/sig-server')
44
let firstRun = true
55
let sigS
66

7-
function boot (done) {
7+
async function boot (done) {
88
const options = {
99
port: 15555,
1010
host: '127.0.0.1',
11-
metrics: firstRun
11+
metrics: false // firsrun TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/174
1212
}
1313

1414
if (firstRun) { firstRun = false }
1515

16-
sigServer.start(options, (err, server) => {
17-
if (err) { throw err }
16+
sigS = await sigServer.start(options)
1817

19-
sigS = server
20-
console.log('signalling on:', server.info.uri)
21-
done()
22-
})
18+
console.log('signalling on:', sigS.info.uri)
19+
20+
done()
2321
}
2422

25-
function stop (done) {
26-
sigS.stop(done)
23+
async function stop (done) {
24+
await sigS.stop()
25+
26+
done()
2727
}
2828

2929
module.exports = {

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
"wrtc": "~0.3.7"
5555
},
5656
"dependencies": {
57+
"@hapi/hapi": "^18.3.1",
58+
"@hapi/inert": "^5.2.0",
5759
"async": "^2.6.2",
5860
"class-is": "^1.1.0",
5961
"debug": "^4.1.1",
6062
"epimetheus": "^1.0.92",
61-
"hapi": "^16.6.2",
62-
"inert": "^4.2.1",
6363
"interface-connection": "~0.3.3",
6464
"mafmt": "^6.0.7",
6565
"minimist": "^1.2.0",

‎src/sig-server/bin.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@
55
const signalling = require('./index')
66
const argv = require('minimist')(process.argv.slice(2))
77

8-
let server
9-
10-
signalling.start({
11-
port: argv.port || argv.p || process.env.PORT || 9090,
12-
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
13-
metrics: !(argv.disableMetrics || process.env.DISABLE_METRICS)
14-
}, (err, _server) => {
15-
if (err) {
16-
throw err
17-
}
18-
server = _server
8+
;(async () => {
9+
const server = await signalling.start({
10+
port: argv.port || argv.p || process.env.PORT || 9090,
11+
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
12+
// Needs: https://github.com/libp2p/js-libp2p-webrtc-star/issues/174'
13+
metrics: false // !(argv.disableMetrics || process.env.DISABLE_METRICS)
14+
})
1915

2016
console.log('Listening on:', server.info.uri)
21-
})
2217

23-
process.on('SIGINT', () => {
24-
server.stop(() => {
18+
process.on('SIGINT', async () => {
19+
await server.stop()
2520
console.log('Signalling server stopped')
2621
process.exit()
2722
})
28-
})
23+
})()

‎src/sig-server/config.js

+2-4
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/sig-server/index.js

+26-34
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,48 @@
1+
/* eslint no-unreachable: "warn" */
2+
13
'use strict'
24

3-
const Hapi = require('hapi')
5+
const Hapi = require('@hapi/hapi')
6+
const Inert = require('@hapi/inert')
7+
48
const config = require('./config')
59
const log = config.log
610
const epimetheus = require('epimetheus')
711
const path = require('path')
812

913
exports = module.exports
1014

11-
exports.start = (options, callback) => {
12-
if (typeof options === 'function') {
13-
callback = options
14-
options = {}
15-
}
16-
15+
exports.start = async (options = {}) => {
1716
const port = options.port || config.hapi.port
1817
const host = options.host || config.hapi.host
1918

20-
const http = new Hapi.Server(config.hapi.options)
21-
22-
http.connection({
23-
port: port,
24-
host: host
19+
const http = new Hapi.Server({
20+
...config.hapi.options,
21+
port,
22+
host
2523
})
2624

27-
http.register({ register: require('inert') }, (err) => {
28-
if (err) {
29-
return callback(err)
30-
}
25+
await http.register(Inert)
26+
await http.start()
3127

32-
http.start((err) => {
33-
if (err) {
34-
return callback(err)
35-
}
28+
log('signaling server has started on: ' + http.info.uri)
3629

37-
log('signaling server has started on: ' + http.info.uri)
30+
http.peers = require('./routes-ws')(http, options.metrics).peers
3831

39-
http.peers = require('./routes-ws')(http, options.metrics).peers
40-
41-
http.route({
42-
method: 'GET',
43-
path: '/',
44-
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
45-
confine: false
46-
})
47-
})
48-
49-
callback(null, http)
32+
http.route({
33+
method: 'GET',
34+
path: '/',
35+
handler: (request, reply) => reply.file(path.join(__dirname, 'index.html'), {
36+
confine: false
5037
})
51-
52-
if (options.metrics) { epimetheus.instrument(http) }
5338
})
5439

40+
if (options.metrics) {
41+
// TODO: reenable epimetheus when support
42+
log('wait for epimetheus support')
43+
throw new Error('epimetheus is currently not supported by hapi. Needs: https://github.com/libp2p/js-libp2p-webrtc-star/issues/174')
44+
epimetheus.instrument(http)
45+
}
46+
5547
return http
5648
}

‎test/sig-server.js

+42-45
Original file line numberDiff line numberDiff line change
@@ -33,62 +33,59 @@ describe('signalling', () => {
3333
let c3mh = multiaddr(base('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo3'))
3434
let c4mh = multiaddr(base('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'))
3535

36-
it('start and stop signalling server (default port)', (done) => {
37-
sigServer.start((err, server) => {
38-
expect(err).to.not.exist()
39-
expect(server.info.port).to.equal(13579)
40-
expect(server.info.protocol).to.equal('http')
41-
expect(server.info.address).to.equal('0.0.0.0')
42-
server.stop(done)
43-
})
36+
it('start and stop signalling server (default port)', async () => {
37+
const server = await sigServer.start()
38+
39+
expect(server.info.port).to.equal(13579)
40+
expect(server.info.protocol).to.equal('http')
41+
expect(server.info.address).to.equal('0.0.0.0')
42+
43+
await server.stop()
4444
})
4545

46-
it('start and stop signalling server (default port) and spam it with invalid requests', (done) => {
47-
sigServer.start((err, server) => {
48-
expect(err).to.not.exist()
49-
expect(server.info.port).to.equal(13579)
50-
expect(server.info.protocol).to.equal('http')
51-
expect(server.info.address).to.equal('0.0.0.0')
52-
const cl = io.connect(server.info.uri)
53-
cl.on('connect', () => {
54-
cl.emit('ss-handshake', null)
55-
cl.emit('ss-handshake', 1)
56-
cl.emit('ss-handshake', [1, 2, 3])
57-
cl.emit('ss-handshake', {})
58-
setTimeout(() => {
59-
server.stop(done)
60-
}, 1000)
61-
})
46+
it('start and stop signalling server (default port) and spam it with invalid requests', async () => {
47+
const server = await sigServer.start()
48+
49+
expect(server.info.port).to.equal(13579)
50+
expect(server.info.protocol).to.equal('http')
51+
expect(server.info.address).to.equal('0.0.0.0')
52+
53+
const cl = io.connect(server.info.uri)
54+
cl.on('connect', async () => {
55+
cl.emit('ss-handshake', null)
56+
cl.emit('ss-handshake', 1)
57+
cl.emit('ss-handshake', [1, 2, 3])
58+
cl.emit('ss-handshake', {})
59+
60+
await server.stop()
6261
})
6362
})
6463

65-
it('start and stop signalling server (custom port)', (done) => {
64+
it('start and stop signalling server (custom port)', async () => {
6665
const options = {
6766
port: 12345
6867
}
69-
sigServer.start(options, (err, server) => {
70-
expect(err).to.not.exist()
71-
expect(server.info.port).to.equal(12345)
72-
expect(server.info.protocol).to.equal('http')
73-
expect(server.info.address).to.equal('0.0.0.0')
74-
server.stop(done)
75-
})
68+
69+
const server = await sigServer.start(options)
70+
71+
expect(server.info.port).to.equal(12345)
72+
expect(server.info.protocol).to.equal('http')
73+
expect(server.info.address).to.equal('0.0.0.0')
74+
await server.stop()
7675
})
7776

78-
it('start signalling server for client tests', (done) => {
77+
it('start signalling server for client tests', async () => {
7978
const options = {
8079
port: 12345
8180
}
8281

83-
sigServer.start(options, (err, server) => {
84-
expect(err).to.not.exist()
85-
expect(server.info.port).to.equal(12345)
86-
expect(server.info.protocol).to.equal('http')
87-
expect(server.info.address).to.equal('0.0.0.0')
88-
sioUrl = server.info.uri
89-
sigS = server
90-
done()
91-
})
82+
const server = await sigServer.start(options)
83+
84+
expect(server.info.port).to.equal(12345)
85+
expect(server.info.protocol).to.equal('http')
86+
expect(server.info.address).to.equal('0.0.0.0')
87+
sioUrl = server.info.uri
88+
sigS = server
9289
})
9390

9491
it('zero peers', () => {
@@ -213,7 +210,7 @@ describe('signalling', () => {
213210
}
214211
})
215212

216-
it('stop signalling server', (done) => {
213+
it('stop signalling server', () => {
217214
parallel([
218215
(cb) => {
219216
c1.disconnect()
@@ -223,8 +220,8 @@ describe('signalling', () => {
223220
c2.disconnect()
224221
cb()
225222
}
226-
], () => {
227-
sigS.stop(done)
223+
], async () => {
224+
await sigS.stop()
228225
})
229226
})
230227
})

‎test/transport/reconnect.node.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ module.exports = (create) => {
2727
const ma2 = multiaddr(base('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo3B'))
2828
const ma3 = multiaddr(base('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo3C'))
2929

30-
before((done) => {
31-
sigS = sigServer.start({ port: SERVER_PORT }, done)
30+
before(async () => {
31+
sigS = await sigServer.start({ port: SERVER_PORT })
3232
})
3333

34-
after((done) => sigS.stop(done))
34+
after(async () => {
35+
await sigS.stop()
36+
})
3537

3638
it('listen on the first', (done) => {
3739
ws1 = create()
@@ -57,12 +59,12 @@ module.exports = (create) => {
5759
})
5860
})
5961

60-
it('stops the server', (done) => {
61-
sigS.stop(done)
62+
it('stops the server', async () => {
63+
await sigS.stop()
6264
})
6365

64-
it('starts the server again', (done) => {
65-
sigS = sigServer.start({ port: SERVER_PORT }, done)
66+
it('starts the server again', async () => {
67+
sigS = await sigServer.start({ port: SERVER_PORT })
6668
})
6769

6870
it('wait a bit for clients to reconnect', (done) => {

0 commit comments

Comments
 (0)
This repository has been archived.