Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.js to fix [email protected] breaking changes #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# PeerServer Changelog

### 0.2.11

* Decreased error logging for missing peer event.

### 0.2.10

* Added some more error logging.

### 0.2.6

* Ensure 16 character IDs.
Expand Down
32 changes: 26 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ app._initializeWSS = function(server) {
// Create WebSocket server as well.
this._wss = new WebSocketServer({ path: path, server: server});

this._wss.on('connection', function(socket) {
this._wss.on('connection', function(socket, req) {
socket.upgradeReq = req;
var query = url.parse(socket.upgradeReq.url, true).query;
Copy link

@lpinca lpinca May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only place where upgradeReq is used it is probably better to use:

var query = url.parse(req ? req.url : socket.upgradeReq.url, true).query;

and remove the previous line. This will work with all ws versions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the ws dependency version to 3.0.0 as well.

Thanks.

var id = query.id;
var token = query.token;
var key = query.key;
var ip = socket.upgradeReq.socket.remoteAddress;

socket.on("error", function(e) {
console.log("Error in WS socket.");
console.log(e);
socket.on('error', function(e) {
self._log('Error in WS socket.', e);
});

if (!id || !token || !key) {
self._warn('No id, token, or key supplied to websocket server', id, token, key);
socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: 'No id, token, or key supplied to websocket server' } }));
socket.close();
return;
Expand All @@ -48,6 +49,7 @@ app._initializeWSS = function(server) {
}
self._configureWS(socket, key, id, token);
} else {
self._warn('Client key check failure for wss: ' + err, key, ip);
socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: err } }));
}
});
Expand All @@ -71,6 +73,7 @@ app._configureWS = function(socket, key, id, token) {
}
} else {
// ID-taken, invalid token
self._warn('Client token already taken: ', key, id, token);
socket.send(JSON.stringify({ type: 'ID-TAKEN', payload: { msg: 'ID is taken' } }));
socket.close();
return;
Expand Down Expand Up @@ -101,7 +104,7 @@ app._configureWS = function(socket, key, id, token) {
} else if(message.type === 'PING') {
// Do nothing
} else {
util.prettyError('Message unrecognized');
self._warn('Message unrecognized:', message && message.type);
}
} catch(e) {
self._log('Invalid message', data);
Expand Down Expand Up @@ -174,6 +177,7 @@ app._initializeHTTP = function() {
self._ips[ip]++;
self._startStreaming(res, key, id, token, true);
} else {
self._warn('Client key check failure for XHR: ' + err, key, ip, id);
res.send(JSON.stringify({ type: 'HTTP-ERROR' }));
}
});
Expand Down Expand Up @@ -205,6 +209,7 @@ app._initializeHTTP = function() {
var client;
if (!self._clients[key] || !(client = self._clients[key][id])) {
if (req.params.retry) {
self._log('Client check failure on retry for: ', key, id);
res.sendStatus(401);
return;
} else {
Expand All @@ -217,6 +222,7 @@ app._initializeHTTP = function() {

// Auth the req
if (typeof client === 'undefined' || req.params.token !== client.token) {
self._warn('Client check failure on request for: ', key, id);
res.sendStatus(401);
return;
} else {
Expand Down Expand Up @@ -274,6 +280,7 @@ app._startStreaming = function(res, key, id, token, open) {
client.res = res;
this._processOutstanding(key, id);
} else {
self._warn('Client token check failure for streaming: ', key, id, token);
// ID-taken, invalid token
res.end(JSON.stringify({ type: 'HTTP-ERROR' }));
}
Expand Down Expand Up @@ -402,8 +409,21 @@ app._generateClientId = function(key) {
return clientId;
};


// logging methods

var _logLevels = {
ERROR: 1, WARN: 2, INFO: 3, DEBUG: 4
}

app._log = function() {
if (this._options.debug) {
if (this._options.debug === true || this._options.debug >= _logLevels.DEBUG) {
console.log.apply(console, arguments);
}
};

app._warn = function() {
if (this._options.debug === true || this._options.debug >= _logLevels.WARN ) {
console.warn.apply(console, arguments);
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "peer",
"version": "0.2.9",
"version": "0.2.11",
"description": "PeerJS server component",
"main": "lib/index.js",
"bin": {
Expand All @@ -16,7 +16,7 @@
"body-parser": "^1.9.0",
"express": "^4.9.8",
"optimist": "~0.6.1",
"ws": "*",
"ws": "^3.0.0",
"cors": "~2.5.0"
},
"devDependencies": {
Expand Down