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

Commit f238264

Browse files
authored
feat/moar config (#43)
* docs: document how to use the signalling server * feat: moar config options (added option to config host
1 parent 5f4c6d3 commit f238264

File tree

10 files changed

+108
-65
lines changed

10 files changed

+108
-65
lines changed

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,43 @@
2424

2525
## Example
2626

27-
```
27+
```JavaScript
2828
TODO
2929
```
3030

31-
## Installation
31+
## Usage
32+
33+
### Installation
34+
35+
```bash
36+
> npm install libp2p-webrtc-star
37+
```
38+
39+
### API
3240

33-
### npm
41+
Follows the interface defined by `interface-transport`
3442

35-
```sh
36-
> npm i libp2p-webrtc-star
43+
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
44+
45+
### Signalling server
46+
47+
`libp2p-webrtc-star` comes with its own signalling server, used for peers to handshake their signalling data and establish a connection. You can install it in your machine by installing the module globally:
48+
49+
```bash
50+
> npm install --global libp2p-webrtc-star
3751
```
3852

53+
This will expose a `star-sig` cli tool. To spawn a server do:
54+
55+
```bash
56+
> star-sig --port=9090 --host=127.0.0.1
57+
```
58+
59+
Defaults:
60+
61+
- `port` - 13579
62+
- `host` - '0.0.0.0'
63+
3964
### This module uses `pull-streams`
4065

4166
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
@@ -51,17 +76,11 @@ You can learn more about pull-streams at:
5176

5277
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
5378

54-
```js
79+
```JavaScript
5580
const pullToStream = require('pull-stream-to-stream')
5681

5782
const nodeStreamInstance = pullToStream(pullStreamInstance)
5883
// nodeStreamInstance is an instance of a Node.js Stream
5984
```
6085

6186
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
62-
63-
64-
65-
## API
66-
67-
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)

gulpfile.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const gulp = require('gulp')
4-
const sigServer = require('./src/signalling-server')
4+
const sigServer = require('./src/signalling')
55

66
let sigS
77

@@ -11,11 +11,17 @@ gulp.task('test:browser:before', boot)
1111
gulp.task('test:browser:after', stop)
1212

1313
function boot (done) {
14-
sigS = sigServer.start(15555, (err, info) => {
14+
const options = {
15+
port: 15555,
16+
host: '127.0.0.1'
17+
}
18+
19+
sigServer.start(options, (err, server) => {
1520
if (err) {
1621
throw err
1722
}
18-
console.log('sig-server started on:', info.uri)
23+
sigS = server
24+
console.log('signalling on:', server.info.uri)
1925
done()
2026
})
2127
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "libp2p WebRTC transport that includes a discovery mechanism provided by the signalling-star",
55
"main": "src/index.js",
66
"bin": {
7-
"star-sig": "src/signalling-server/bin.js"
7+
"star-sig": "src/signalling/bin.js"
88
},
99
"browser": {
1010
"wrtc": false
@@ -72,4 +72,4 @@
7272
"interfect <[email protected]>",
7373
"michaelfakhri <[email protected]>"
7474
]
75-
}
75+
}

src/signalling-server/bin.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/signalling/bin.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
const signalling = require('./index')
6+
const argv = require('minimist')(process.argv.slice(2))
7+
8+
let server
9+
10+
signalling.start({
11+
port: argv.port || argv.p || 9090,
12+
host: argv.host || argv.h || '0.0.0.0'
13+
}, (err, _server) => {
14+
if (err) {
15+
throw err
16+
}
17+
server = _server
18+
19+
console.log('Listening on:', server.info.uri)
20+
})
21+
22+
process.on('SIGINT', () => {
23+
server.stop(() => {
24+
console.log('Signalling server stopped')
25+
process.exit()
26+
})
27+
})

src/signalling-server/config.js renamed to src/signalling/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ log.error = debug('signalling-server:error')
77
module.exports = {
88
log: log,
99
hapi: {
10-
port: process.env.PORT || 8135,
10+
port: process.env.PORT || 13579,
11+
host: '0.0.0.0',
1112
options: {
1213
connections: {
1314
routes: {

src/signalling-server/index.js renamed to src/signalling/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@ const log = config.log
66

77
exports = module.exports
88

9-
exports.start = (port, callback) => {
10-
if (typeof port === 'function') {
11-
callback = port
12-
port = undefined
9+
exports.start = (options, callback) => {
10+
if (typeof options === 'function') {
11+
callback = options
12+
options = {}
1313
}
1414

15-
if (!port) {
16-
port = config.hapi.port
17-
}
15+
const port = options.port || config.hapi.port
16+
const host = options.host || config.hapi.host
1817

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

2120
http.connection({
22-
port: port
21+
port: port,
22+
host: host
2323
})
2424

2525
http.start((err) => {
2626
if (err) {
2727
return callback(err)
2828
}
29+
2930
log('signaling server has started on: ' + http.info.uri)
31+
3032
http.peers = require('./routes-ws')(http).peers
3133

32-
callback(null, http.info)
34+
callback(null, http)
3335
})
3436

3537
return http

test/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
require('./signalling-server/test-signalling-server.js')
3+
require('./signalling/test-signalling-server.js')
44
require('./webrtc-star/test-instance.js')
55
require('./webrtc-star/test-filter.js')
66
require('./webrtc-star/test-listen.js')

test/signalling-server/test-signalling-server.js renamed to test/signalling/test-signalling-server.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const io = require('socket.io-client')
66
const parallel = require('async/parallel')
77
const multiaddr = require('multiaddr')
88

9-
const sigServer = require('../../src/signalling-server')
9+
const sigServer = require('../../src/signalling')
1010

11-
describe('signalling server', () => {
11+
describe('signalling', () => {
1212
const sioOptions = {
1313
transports: ['websocket'],
1414
'force new connection': true
@@ -27,32 +27,40 @@ describe('signalling server', () => {
2727
let c4mh = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4')
2828

2929
it('start and stop signalling server (default port)', (done) => {
30-
const sigS = sigServer.start((err, info) => {
30+
sigServer.start((err, server) => {
3131
expect(err).to.not.exist
32-
expect(info.port).to.equal(8135)
33-
expect(info.protocol).to.equal('http')
34-
expect(info.address).to.equal('0.0.0.0')
35-
sigS.stop(done)
32+
expect(server.info.port).to.equal(13579)
33+
expect(server.info.protocol).to.equal('http')
34+
expect(server.info.address).to.equal('0.0.0.0')
35+
server.stop(done)
3636
})
3737
})
3838

3939
it('start and stop signalling server (custom port)', (done) => {
40-
const sigS = sigServer.start(12345, (err, info) => {
40+
const options = {
41+
port: 12345
42+
}
43+
sigServer.start(options, (err, server) => {
4144
expect(err).to.not.exist
42-
expect(info.port).to.equal(12345)
43-
expect(info.protocol).to.equal('http')
44-
expect(info.address).to.equal('0.0.0.0')
45-
sigS.stop(done)
45+
expect(server.info.port).to.equal(12345)
46+
expect(server.info.protocol).to.equal('http')
47+
expect(server.info.address).to.equal('0.0.0.0')
48+
server.stop(done)
4649
})
4750
})
4851

4952
it('start signalling server for client tests', (done) => {
50-
sigS = sigServer.start(12345, (err, info) => {
53+
const options = {
54+
port: 12345
55+
}
56+
57+
sigServer.start(options, (err, server) => {
5158
expect(err).to.not.exist
52-
expect(info.port).to.equal(12345)
53-
expect(info.protocol).to.equal('http')
54-
expect(info.address).to.equal('0.0.0.0')
55-
sioUrl = info.uri
59+
expect(server.info.port).to.equal(12345)
60+
expect(server.info.protocol).to.equal('http')
61+
expect(server.info.address).to.equal('0.0.0.0')
62+
sioUrl = server.info.uri
63+
sigS = server
5664
done()
5765
})
5866
})

0 commit comments

Comments
 (0)