webrtc-signal-http
Advanced tools
Comparing version 1.1.1 to 2.0.0-alpha.1
@@ -5,6 +5,6 @@ const express = require('express') | ||
const PeerList = require('./peer-list') | ||
const pkgJson = require('../package.json') | ||
const serverVersion = `WebRtcSignalHttp/${pkgJson.version}` | ||
module.exports = (opts) => { | ||
const router = express.Router() | ||
const defaultExport = (opts) => { | ||
if (opts.peerList && !(opts.peerList instanceof PeerList)) { | ||
@@ -14,2 +14,17 @@ throw new Error('Invalid peerList') | ||
const router = express.Router() | ||
// set all the service-constant headers that need to be present | ||
router.use((req, res, next) => { | ||
res.set('Connection', 'close') | ||
res.set('Server', serverVersion) | ||
res.set('Access-Control-Allow-Credentials', 'true') | ||
res.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Connection, Cache-Control') | ||
res.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') | ||
res.set('Access-Control-Allow-Origin', '*') | ||
res.set('Access-Control-Expose-Headers', 'Content-Length') | ||
res.set('Cache-Control', 'no-cache') | ||
next() | ||
}) | ||
// store the peer list on the router | ||
@@ -26,3 +41,3 @@ router.peerList = opts.peerList || new PeerList() | ||
// it will buffer into the peerList | ||
const sendPeerMessage = (srcId, destId, data) => { | ||
const sendPeerMessage = (srcId, destId, data, dataMime) => { | ||
// find the current peer | ||
@@ -35,2 +50,3 @@ const peer = router.peerList.getPeer(destId) | ||
.set('Pragma', srcId) | ||
.set('Content-Type', dataMime) | ||
.send(data) | ||
@@ -40,3 +56,3 @@ } | ||
else { | ||
router.peerList.pushPeerData(srcId, destId, data) | ||
router.peerList.pushPeerData(srcId, destId, data, dataMime) | ||
} | ||
@@ -64,3 +80,3 @@ } | ||
// "ourselves", namely the srcId == destId | ||
sendPeerMessage(id, id, peerListStr) | ||
sendPeerMessage(id, id, peerListStr, 'text/plain') | ||
}) | ||
@@ -70,4 +86,3 @@ }) | ||
router.post('/message', | ||
bodyParser.text(), | ||
bodyParser.urlencoded({ extended: false }), | ||
bodyParser.raw({ type: '*/*' }), | ||
(req, res) => { | ||
@@ -89,3 +104,3 @@ | ||
// (this will write to the `to` socket, or buffer if needed) | ||
sendPeerMessage(req.query.peer_id, req.query.to, req.body) | ||
sendPeerMessage(req.query.peer_id, req.query.to, req.body, req.get('Content-Type') || 'text/html') | ||
@@ -108,2 +123,3 @@ // whether we send directly or buffer we tell the sender everything is 'OK' | ||
.set('Pragma', pop.srcId) | ||
.set('Content-Type', pop.dataMime) | ||
.send(pop.data) | ||
@@ -135,3 +151,3 @@ } | ||
// "ourselves", namely the srcId == destId | ||
sendPeerMessage(id, id, peerListStr) | ||
sendPeerMessage(id, id, peerListStr, 'text/plain') | ||
}) | ||
@@ -143,2 +159,11 @@ | ||
return router | ||
} | ||
} | ||
// expose a version | ||
defaultExport.version = serverVersion | ||
// expose PeerList | ||
defaultExport.PeerList = PeerList | ||
// export our behavior | ||
module.exports = defaultExport |
@@ -40,7 +40,8 @@ const Peer = require('./peer') | ||
pushPeerData(srcId, destId, data) { | ||
pushPeerData(srcId, destId, data, dataMime) { | ||
if (this._peers[destId] && !this._peers[destId].status()) { | ||
this._peers[destId].buffer.push({ | ||
srcId: srcId, | ||
data: data | ||
data: data, | ||
dataMime: dataMime | ||
}) | ||
@@ -47,0 +48,0 @@ } |
{ | ||
"name": "webrtc-signal-http", | ||
"version": "1.1.1", | ||
"version": "2.0.0-alpha.1", | ||
"description": "opinionated webrtc signal provider using http as a protocol", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -101,3 +101,3 @@ # webrtc-signal-http | ||
Provides a mechanism for simulated server push, using vanilla http long polling. That is, the TCP socket behind this request will remain open to the server until there is content the server needs to send. In the event of a TCP timeout the client should reconnect. Messages that contain a `Pragma` value that matches the client `peer_id` are peer status updates and should be handled the same as the status update provided in the `GET /sign_in` response. `Content-Type` headers will not reflect the type of the original content. | ||
Provides a mechanism for simulated server push, using vanilla http long polling. That is, the TCP socket behind this request will remain open to the server until there is content the server needs to send. In the event of a TCP timeout the client should reconnect. Messages that contain a `Pragma` value that matches the client `peer_id` are peer status updates and should be handled the same as the status update provided in the `GET /sign_in` response. | ||
@@ -114,3 +114,3 @@ Peer status update: | ||
Pragma: 2 | ||
Content-Type: text/html; charset=utf-8 | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Length: 18 | ||
@@ -132,3 +132,3 @@ | ||
Pragma: 3 | ||
Content-Type: text/html; charset=utf-8 | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Length: 12 | ||
@@ -180,3 +180,3 @@ | ||
[Function] - takes `srcId` (a Number), `destId` (a Number), `data` (an Object). Pushs arbitrary data onto a stack for a particular destination peer. __Returns__ nothing. | ||
[Function] - takes `srcId` (a Number), `destId` (a Number), `data` (an Object), `dataMime` (a String). Pushs arbitrary data of a given MIME type onto a stack for a particular destination peer. __Returns__ nothing. | ||
@@ -183,0 +183,0 @@ #### popPeerData |
@@ -40,5 +40,22 @@ const assert = require('assert') | ||
.expect('Content-Type', /text\/plain/) | ||
.expect('Pragma', '1') | ||
.expect(200, `${expectedPeerName},1,1`, done) | ||
}) | ||
it('should set specific headers', (done) => { | ||
const expectedPeerName = 'myName' | ||
request(appCreator(false)) | ||
.get(`/sign_in?peer_name=${expectedPeerName}`) | ||
.expect('Connection', 'close') | ||
.expect('Server', signalRouter.version) | ||
.expect('Access-Control-Allow-Credentials', 'true') | ||
.expect('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Connection, Cache-Control') | ||
.expect('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') | ||
.expect('Access-Control-Allow-Origin', '*') | ||
.expect('Access-Control-Expose-Headers', 'Content-Length') | ||
.expect('Cache-Control', 'no-cache') | ||
.expect(200, done) | ||
}) | ||
it('should support multiple sign_in', (done) => { | ||
@@ -99,2 +116,3 @@ const expectedPeerName = 'myName' | ||
.expect('Pragma', `${senderPeerId}`) | ||
.expect('Content-Type', 'application/vnd.unit.test+text; charset=utf-8') | ||
.expect(200, 'testMessage') | ||
@@ -106,3 +124,3 @@ .then(() => { /* on success, empty the chainable promise result */ }), | ||
return test.post(`/message?peer_id=${senderPeerId}&to=${receiverPeerId}`) | ||
.set('Content-Type', 'text/plain') | ||
.set('Content-Type', 'application/vnd.unit.test+text; charset=utf-8') | ||
.send('testMessage') | ||
@@ -230,2 +248,3 @@ .expect(200) | ||
const expectedDataSrcId = 2 | ||
const expectedMime = 'mime' | ||
const instance = new PeerList() | ||
@@ -237,5 +256,5 @@ | ||
instance.pushPeerData(expectedDataSrcId, id, expectedData) | ||
instance.pushPeerData(expectedDataSrcId, id, expectedData, expectedMime) | ||
assert.deepEqual(instance.popPeerData(id), {srcId: expectedDataSrcId, data: expectedData}) | ||
assert.deepEqual(instance.popPeerData(id), {srcId: expectedDataSrcId, data: expectedData, dataMime: expectedMime}) | ||
assert.equal(instance.popPeerData(id), null) | ||
@@ -242,0 +261,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2115199
469
2