- Added chaining method to handle sockets - Attached cookies, loaded user on each socket request - Fixed error in config-example{
    var stack = Array.prototype.slice.call(arguments, 0);# Please enter the commit message for your changes. Lines starting
    var i = 0;# with '#' will be ignored, and an empty message aborts the commit.
    return function next() {# On branch dev
        var func = stack[i++];# Changes to be committed:
        if ('function' != typeof func) {# (use "git reset HEAD <file>..." to unstage)
            // End of the function stack#
            return false;# modified: app.js
        }#	modified: config-example.js
    #	new file: lib/chain.js
        var args = Array.prototype.slice.call(arguments, 0);# new file: lib/cookies.socket.io.js
        var then = function() {# modified: lib/scanner.js
            next.apply(this, args);# modified: routes/auth.js
        };#
        if (i == 1) args.push(then);# Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
        // Call the current function in the stack#
        func.apply(this, args);#	data/db/
    };#	mongod
};{diff --git a/app.js b/app.js
    var stack = Array.prototype.slice.call(arguments, 0);index 218d3c3..90b4e45 100644 --- a/app.js +++ b/app.js @@ -9,6 +9,7 @@ var probe =
require('node-ffprobe');
    var i = 0; var encrypt = require('sha1');
    return function next() { var Cookies = require('cookies');
        var func = stack[i++]; var util = require('./lib/util'); +var chain = require('./lib/chain');
        if ('function' != typeof func) { var Scanner = require('./lib/scanner');
            // End of the function stack
            return false; var app = express(); @@ -80,7 +81,7 @@ var registerRoutes = function() {
        } 	app.get('/avatar/:name' , controllers.avatar.show);
     	app.get('/avatar/' , controllers.avatar.show);
        var args = Array.prototype.slice.call(arguments, 0); -	// Public views +	// Public views
        var then = function() { app.get('/' , loadUser, controllers.home.home);
            next.apply(this, args); app.get('/room' , loadUser, controllers.home.room);
        };
        if (i == 1) args.push(then);@@ -134,20 +135,16 @@ var registerRoutes = function() {

 	// Sockets
        // Call the current function in the stack console.log("Setting up session sockets"); + var SocketCookies = require('./lib/cookies.socket.io'), +
socketCookies = new SocketCookies(Cookies, config.cookieKeys);
        func.apply(this, args);
    }; io.sockets.on('connection', function(socket) {
};{- socket.cookies = new Cookies(socket.request, {}, config.cookieKeys);
    var stack = Array.prototype.slice.call(arguments, 0);- console.info('socket connected', socket.id); - - controllers.auth.loadUserOnSocket(socket,
function(e, socket) { - if (e) { - console.error("Error loading user on socket", e); - return; - } - socket.on('listen', inject(controllers.room.listen,
socket)); - socket.on('leave', inject(controllers.room.leave, socket)); - socket.on('disconnect', inject(controllers.room.exit, socket)); - }); + var
attachCookies = socketCookies.attach; + var loadSocketUser = controllers.auth.loadUserOnSocket; + + socket.on('listen', chain(attachCookies,
loadSocketUser, inject(controllers.room.listen, socket))); + socket.on('leave', chain(attachCookies, loadSocketUser, inject(controllers.room.leave,
socket))); + socket.on('disconnect', chain(attachCookies, loadSocketUser, inject(controllers.room.exit, socket)));
    var i = 0; });
    return function next() {
        var func = stack[i++]; console.log("Socket routing registered"); diff --git a/config-example.js b/config-example.js index c015a4b..e721c73 100644
--- a/config-example.js +++ b/config-example.js @@ -11,9 +11,6 @@ exports.settings = {
        if ('function' != typeof func) { userIdKey: 'uid',
            // End of the function stack userAuthTokenKey: 'ut',
            return false; -	// The maximum number of chat records to keep for a room -	maxChat: 40, -
        } 	// The directory where music will be added
     	songDir: __dirname + '/data/music',
        var args = Array.prototype.slice.call(arguments, 0); diff --git a/lib/chain.js b/lib/chain.js new file mode 100644 index 0000000..105efb9 ---
/dev/null +++ b/lib/chain.js @@ -0,0 +1,25 @@ +// Chains a series of functions together, one after another. +// Each function should expect the same
number of parameters. The last +// parameter will be a 'next' function that can be called to trigger the +// next method in the chain of commands. If the
'next' method is not called +// the chain of methods end. +module.exports = exports = function chain() { + var stack =
Array.prototype.slice.call(arguments, 0); + var i = 0; + return function next() { + var func = stack[i++]; + if ('function' != typeof func) { + // End of
the function stack + return false; + } + + var args = Array.prototype.slice.call(arguments, 0); + var then = function() { + next.apply(this, args); + };
+ if (i == 1) args.push(then); + + // Call the current function in the stack + func.apply(this, args); + }; +}; diff --git a/lib/cookies.socket.io.js
b/lib/cookies.socket.io.js new file mode 100644 index 0000000..5910148 --- /dev/null +++ b/lib/cookies.socket.io.js @@ -0,0 +1,9 @@ +module.exports =
function(Cookies, keys) { + this.attach = function(socket, next) { + if (!socket.request || !socket.request.headers) { + console.error("Socket does not
have headers.", socket.request); + } + socket.cookies = new Cookies(socket.request, {}, keys); + next(); + }; +}; \ No newline at end of file diff --git
a/lib/scanner.js b/lib/scanner.js index 594cad0..55b9307 100644 --- a/lib/scanner.js +++ b/lib/scanner.js @@ -1,11 +1,10 @@
        var then = function() { var fs = require('fs');
            next.apply(this, args); var path = require('path'); -var util = require('./util');
        };
        if (i == 1) args.push(then); var patterns = {
     	'all' : new RegExp('.+')
        // Call the current function in the stack , 'music' : new RegExp('\.mp3|\.m4a|\.ogg|\.flac|\.wma|\.wmv$') -} +};
        func.apply(this, args);
    }; var Scanner = function(dir, opts) {
};{
    var stack = Array.prototype.slice.call(arguments, 0);diff --git a/routes/auth.js b/routes/auth.js index b6521d2..d669a5c 100644 --- a/routes/auth.js
+++ b/routes/auth.js @@ -21,8 +21,8 @@ module.exports = function(config, User, Auth) {
    var i = 0; socket.user = null;
    return function next() {
        var func = stack[i++]; if (!socket.cookies) { - console.info("Could not load user on socket", e, socket.cookies); - return next(new
Error("Cookies not found on socket"), socket); + console.error("Could not load user on socket", socket.cookies); + return;
        if ('function' != typeof func) { }
            // End of the function stack
            return false; var uid = socket.cookies.get(userIdKey); @@ -30,7 +30,7 @@ module.exports = function(config, User, Auth) {
        }
 		if (!uid || !ut) {
     			console.info("No cookie information available"); - return next(null, socket); + return;
        var args = Array.prototype.slice.call(arguments, 0); }
        var then = function() {
            next.apply(this, args); User.findById(uid) @@ -38,14 +38,14 @@ module.exports = function(config, User, Auth) {
        }; .exec(function(e, user) {
        if (i == 1) args.push(then); if (e || ! user) {
     				console.trace("Error finding user", uid, e); - return next(new Error("Could not find user " + uid), socket); + return;
        // Call the current function in the stack }
        func.apply(this, args); if (!user.authToken || user.authToken != ut) {
    }; console.error("Invalid credentials", user);
};-				return next(new Error("Invalid credentials for user " + uid), socket);
+				return;
 			}
 			socket.user = user;
-			next(null, socket);
+			next();
 		});
 	};