Skip to content

Commit

Permalink
v1.3.0 release (#8)
Browse files Browse the repository at this point in the history
* Added CORS support using NPM cors library (#7)

* Added CORS support using NPM cors library

* Added unit tests, made CORS configurable using environment variable opt

* updated README to reflect new enableCors variable

* bump version to 1.3.0
  • Loading branch information
bengreenier authored Jun 1, 2018
1 parent 69d3a75 commit e5d678e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ For example extensions, see the following:

[Bool] - enables logging (default `true`)

#### enableCors

[Bool] - enables Cross Origin Resource Sharing (default `true`)

#### peerList

[PeerList](#peerlist) - uses a given peerList implementation instead of creating one
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const signalRouterCreator = require('./lib')
const app = express()

app.use(signalRouterCreator({
enableLogging: process.env.WEBRTC_SIGNAL_LOGGING || true
enableLogging: process.env.WEBRTC_SIGNAL_LOGGING || true,
enableCors: process.env.WEBRTC_CORS || true
})).listen(process.env.PORT || 3000)
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const expressBunyan = require('express-bunyan-logger')
const PeerList = require('./peer-list')


module.exports = (opts) => {
const router = express.Router()

Expand All @@ -18,6 +20,11 @@ module.exports = (opts) => {
router.use(expressBunyan())
}

if (opts.enableCors) {
router.use(cors())
router.options("*", cors())
}

// abstracted peer message sender logic
// this will direct send if possible, otherwise
// it will buffer into the peerList
Expand Down
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webrtc-signal-http",
"version": "1.2.0",
"version": "1.3.0",
"description": "opinionated webrtc signal provider using http as a protocol",
"main": "lib/index.js",
"directories": {
Expand All @@ -22,6 +22,7 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.2",
"express-bunyan-logger": "^1.3.2"
},
Expand Down
45 changes: 36 additions & 9 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const signalRouter = require('../lib')
const PeerList = require('../lib/peer-list')
const Peer = require('../lib/peer')

const appCreator = (enableLogging) => {
const appCreator = (enableLogging, enableCors) => {
const router = signalRouter({
enableLogging: enableLogging
enableLogging: enableLogging,
enableCors: enableCors
})
const app = express()

Expand All @@ -34,17 +35,41 @@ describe('webrtc-signal-http', () => {
it('should support sign_in', (done) => {
const expectedPeerName = 'myName'

request(appCreator(false))
request(appCreator(false, false))
.get(`/sign_in?peer_name=${expectedPeerName}`)
.expect('Content-Type', /text\/plain/)
.expect(200, `${expectedPeerName},1,1\n`, done)
})

it('should support CORS requests if enabled', (done) => {
const expectedPeerName = 'myName'

request(appCreator(false, true))
.get("/")
.expect("access-control-allow-origin", "*", done)
})

it('should prevent CORS requests if disabled', (done) => {
const expectedPeerName = 'myName'

request(appCreator(false, false))
.get("/")
.end(function (error, response) {
if (error) {
return done(error)
}

var corsDisabled = response.header["access-control-allow-origin"] == undefined;
assert.equal(corsDisabled, true)
done()
})
})

it('should support multiple sign_in', (done) => {
const expectedPeerName = 'myName'
const expectedPeerName2 = 'myOtherName'

const test = request(appCreator(false))
const test = request(appCreator(false, false))

test
.get(`/sign_in?peer_name=${expectedPeerName}`)
Expand All @@ -63,7 +88,7 @@ describe('webrtc-signal-http', () => {
})

it('should support /message posting (buffered)', (done) => {
const app = appCreator(false)
const app = appCreator(false, false)

const senderPeerId = app.peerList.addPeer('sendPeer', {})
const receiverPeerId = app.peerList.addPeer('receivePeer', {})
Expand All @@ -83,7 +108,7 @@ describe('webrtc-signal-http', () => {
})

it('should support /message posting (un-buffered)', (done) => {
const app = appCreator(false)
const app = appCreator(false, false)

// simulate adding two peers
const senderPeerId = app.peerList.addPeer('sendPeer', {})
Expand All @@ -110,7 +135,7 @@ describe('webrtc-signal-http', () => {
})

it('should support /sign_out', (done) => {
const app = appCreator(false)
const app = appCreator(false, false)

// simulate adding two peers
const firstPeerId = app.peerList.addPeer('firstPeer', {})
Expand All @@ -128,7 +153,7 @@ describe('webrtc-signal-http', () => {
})

it('should support sign_in notifications', (done) => {
const app = appCreator(false)
const app = appCreator(false, false)

// simulate adding two peers
const firstPeerId = app.peerList.addPeer('firstPeer', {})
Expand All @@ -152,7 +177,7 @@ describe('webrtc-signal-http', () => {
})

it('should support sign_out notifications', (done) => {
const app = appCreator(false)
const app = appCreator(false, false)

// simulate adding two peers
const firstPeerId = app.peerList.addPeer('firstPeer', {})
Expand All @@ -175,6 +200,8 @@ describe('webrtc-signal-http', () => {
})
]).then(() => { /* on success, empty the chainable promise result */ }).then(done, done)
})


})

describe('PeerList', () => {
Expand Down

0 comments on commit e5d678e

Please sign in to comment.