@browser-network/switchboard
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -8,4 +8,4 @@ #!/usr/bin/env node | ||
var http_1 = __importDefault(require("http")); | ||
var MAX_NEGOTIATIONS = 20; | ||
var MAX_NEGOTIATION_AGE = 1000 * 15; | ||
var MAX_NEGOTIATIONS = 500; | ||
var MAX_NEGOTIATION_AGE = 1000 * 60 * 3; | ||
var CORS_HEADERS = { | ||
@@ -23,3 +23,2 @@ 'Access-Control-Allow-Origin': '*', | ||
var ok = function (json) { | ||
console.log('returning ok:', json.map(function (j) { return [j.type, j.clientId]; })); | ||
res.writeHead(200, HEADERS); | ||
@@ -62,4 +61,4 @@ res.end(JSON.stringify(json)); | ||
return nope('sdp'); | ||
if (!body.clientId) | ||
return nope('clientId'); | ||
if (!body.address) | ||
return nope('address'); | ||
if (!body.networkId) | ||
@@ -71,7 +70,7 @@ return nope('networkId'); | ||
return nope('type'); | ||
console.log('receiving request from', body.clientId); | ||
console.log('receiving request from', body.address); | ||
// No random objects | ||
var negotiation = { | ||
sdp: body.sdp, | ||
clientId: body.clientId, | ||
address: body.address, | ||
networkId: body.networkId, | ||
@@ -83,3 +82,3 @@ type: body.type, | ||
// Now it's time to process the negotiation | ||
var clientId = negotiation.clientId, networkId = negotiation.networkId; | ||
var address = negotiation.address, networkId = negotiation.networkId, connectionId = negotiation.connectionId; | ||
// Ensure the network exists | ||
@@ -89,7 +88,18 @@ if (!book[networkId]) { | ||
} | ||
// Send back the pool | ||
ok(Object.values(book[networkId])); | ||
// TODO: bring back efficiency of intelligent adding/removal | ||
// of negotiations from the book. | ||
// | ||
// Send back the pool. Convenience method for console verbosity. | ||
var verboseOk = function () { | ||
var okData = Object.values(book[networkId]); | ||
console.log('returning ok: ' + networkId, okData.map(function (j) { return [j.type, j.address]; })); | ||
ok(okData); | ||
}; | ||
var getOfferByConId = function (conId, networkId, book) { | ||
return Object.values(book[networkId]).find(function (negotiation) { | ||
return negotiation.connectionId === conId && negotiation.type === 'offer'; | ||
}); | ||
}; | ||
var getAnswerByConId = function (conId, networkId, book) { | ||
return Object.values(book[networkId]).find(function (negotiation) { | ||
return negotiation.connectionId === conId && negotiation.type === 'answer'; | ||
}); | ||
}; | ||
// Here's the scheme: | ||
@@ -107,10 +117,39 @@ // | ||
// is after sending back the answer. | ||
// Add the negotiation to our pool | ||
book[networkId][clientId] = negotiation; | ||
if (negotiation.type === 'answer') { | ||
var relatedOffer = getOfferByConId(connectionId, networkId, book); | ||
if (relatedOffer) { | ||
// remove related offer | ||
delete book[networkId][relatedOffer.address]; | ||
} | ||
// post this answer | ||
book[networkId][address] = negotiation; | ||
// send back book | ||
verboseOk(); | ||
} | ||
else { | ||
var relatedAnswer = getAnswerByConId(connectionId, networkId, book); | ||
// If there's an existing answer to this offer | ||
if (relatedAnswer) { | ||
// - send back the book with that answer | ||
verboseOk(); | ||
// - remove the answer | ||
delete book[networkId][relatedAnswer.address]; | ||
// - remove the existing offer | ||
delete book[networkId][address]; | ||
// - don't post the new offer | ||
} | ||
else { | ||
// - add this to our book | ||
book[networkId][address] = negotiation; | ||
// - send back the book | ||
verboseOk(); | ||
} | ||
} | ||
/** Garbage Collection **/ | ||
// Remove old ones from this network. We'll do just the current | ||
// network. | ||
for (var clientId_1 in book[networkId]) { | ||
var negotiation_1 = book[networkId][clientId_1]; | ||
for (var address_1 in book[networkId]) { | ||
var negotiation_1 = book[networkId][address_1]; | ||
if (Date.now() - negotiation_1.timestamp > MAX_NEGOTIATION_AGE) { | ||
delete book[networkId][clientId_1]; | ||
delete book[networkId][address_1]; | ||
} | ||
@@ -124,4 +163,4 @@ } | ||
// this function is running or not. | ||
var clientIds = Object.keys(book[networkId]); | ||
if (clientIds.length > MAX_NEGOTIATIONS) { | ||
var addresses = Object.keys(book[networkId]); | ||
if (addresses.length > MAX_NEGOTIATIONS) { | ||
// Ok so we're too long. We'll nix the entry with the most recent | ||
@@ -131,9 +170,10 @@ // timestamp | ||
// Loop through each, remembering the most recent | ||
var mostRecentClientId = void 0; | ||
// TODO just remember the highest date... | ||
var mostRecentAddress = void 0; | ||
var mostRecentDifference = Infinity; | ||
for (var _i = 0, clientIds_1 = clientIds; _i < clientIds_1.length; _i++) { | ||
var clientId_2 = clientIds_1[_i]; | ||
var difference = now - book[networkId][clientId_2].timestamp; | ||
for (var _i = 0, addresses_1 = addresses; _i < addresses_1.length; _i++) { | ||
var address_2 = addresses_1[_i]; | ||
var difference = now - book[networkId][address_2].timestamp; | ||
if (difference < mostRecentDifference) { | ||
mostRecentClientId = clientId_2; | ||
mostRecentAddress = address_2; | ||
mostRecentDifference = difference; | ||
@@ -143,3 +183,3 @@ } | ||
// Finally, remove the newest entry | ||
delete book[mostRecentClientId]; | ||
delete book[mostRecentAddress]; | ||
} | ||
@@ -146,0 +186,0 @@ }); |
{ | ||
"name": "@browser-network/switchboard", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "The switching server required for the onboarding of new nodes into a @browser-network/network", | ||
@@ -38,2 +38,3 @@ "main": "dist/index.js", | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -40,0 +41,0 @@ "np": "^7.6.1", |
@@ -34,2 +34,12 @@ # The Browser Network Switchboard | ||
## Features | ||
* **No websockets** -- This switching scheme works by regular HTTP request only. | ||
That means no extra libraries and trivial switchboard implementations. It | ||
also means the network doesn't depend on the switchboard to stay alive. Once | ||
the network is up and running, it stays healthy without the help of a | ||
switchboard. The switchboard is just a gatekeeper to let new nodes find the | ||
network. Picture it like an old man who can't lift that much but can open the | ||
gate :) | ||
## Running | ||
@@ -36,0 +46,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
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
11991
181
51