Skip to content

Commit

Permalink
fix for express 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriklv committed Mar 24, 2014
2 parents 93b9a74 + e2afa80 commit e30a791
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
example/node_modules/
node_modules/
*.log
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: javascript
before_install:
- npm install
- npm update
script: make spec
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spec:
@./node_modules/mocha/bin/mocha \
--reporter spec \
$(p) \
spec/*.spec.js

.PHONY: spec
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
session.socket.io (SessionSockets)
==================================

**ADDED FIX FOR EXPRESS 4.X (eiriklv)**

This tiny node module aims to simplify your socket.io application when using http sessions from express or connect middlewares. It has no dependencies and can be initialized using any session store and cookie parser compatible with express or connect.

It's written and tested using express 3.0.0rc4, connect 2.4.5 and socket.io 0.9.10.
Expand Down
50 changes: 50 additions & 0 deletions bench/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var count = 1e7;

var SessionSocket = require('../session.socket.io');
var EventEmitter = require('events').EventEmitter;

// stubs
var io = { sockets: new EventEmitter() };
var sessionStore = {
load: function(cookie, fn) {
fn(undefined, {test: 'hi'})
}
}
var cookieParser = function(handshake, idk, fn) {
fn();
}
var stubsocket = {
handshake: {
secureCookies: '123'
}
}
//
var s = new SessionSocket(io, sessionStore, cookieParser, 'key');
var type, startTime, d = 0;

function run() {
startTime = process.hrtime();
for (var i = 0; i < count; i++) {
io.sockets.emit('connection', stubsocket);
}
}

function done() {
var elapsed = process.hrtime(startTime);
var time = elapsed[0] + elapsed[1] / 1e9;
var opcount = count / time;
console.log('op/s: ', Math.round(opcount), ' | time: ', time);
}

if (process.argv[2] === 'session') {
type = s;
} else {
type = io.sockets;
}

type.on('connection', function() {
d++;
if (d === count) done();
})

run();
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,5 @@
},
"homepage": "https://github.com/functioncallback/session.socket.io",
"_id": "[email protected]",
"dist": {
"shasum": "fb3464a9beec66109bd47929c0a615c2797eb29e"
},
"_from": "[email protected]",
"_resolved": "https://registry.npmjs.org/session.socket.io/-/session.socket.io-0.1.6.tgz"
"_from": "[email protected]"
}
2 changes: 1 addition & 1 deletion session.socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function(io, sessionStore, cookieParser, key) {
// fix for express 4.x (parse the cookie sid to extract the correct part)
var handshake = JSON.parse(JSON.stringify(handshakeInput)); // copy of object
if(handshake.secureCookies && handshake.secureCookies[key]) handshake.secureCookies = handshake.secureCookies[key].match(/\:(.*)\./).pop();
if(handshake.signedCookies && handshake.signedCookies[key]) handshake.signedCookies[key] =handshake.signedCookies[key].match(/\:(.*)\./).pop();
if(handshake.signedCookies && handshake.signedCookies[key]) handshake.signedCookies[key] = handshake.signedCookies[key].match(/\:(.*)\./).pop();
if(handshake.cookies && handshake.cookies[key]) handshake.cookies[key] = handshake.cookies[key].match(/\:(.*)\./).pop();

// original code
Expand Down
89 changes: 89 additions & 0 deletions spec/session.socket.io.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var path = require('path')
, expect = require('expect.js')
, SessionSockets = require(path.resolve('session.socket.io'))
, jonnySocket = stub({ foo: 'bar' });

describe('SessionSockets', function () {
this.timeout(1);

beforeEach(function () {
this.sessionSockets = new SessionSockets(io(), sessionStore(), cookieParser);
});

it('gets the corresponding session for a given socket client', function (done) {
this.sessionSockets.getSession(jonnySocket, function (err, session) {
expect(err).to.be(null);
expect(session.foo).to.equal('bar');
done();
});
});

it('provides session in connection callbacks on the global namespace', function (done) {
this.sessionSockets.on('connection', function (err, socket, session) {
expect(err).to.be(null);
expect(session.foo).to.equal('bar');
done();
});
});

it('provides session in connection callbacks on a specific namespace', function (done) {
this.sessionSockets.of('/foobar').on('connection', function (err, socket, session) {
expect(err).to.be(null);
expect(session.foo).to.equal('bar');
done();
});
});

it('gives a SessionStoreError upon invalid handshakes', function (done) {
this.sessionSockets.getSession({ handshake: 'invalid' }, function (err, session) {
expect(err).to.be.a(SessionStoreError);
expect(session).to.be(null);
done();
});
});

it('gives a CookieParserError upon invalid lookups', function (done) {
this.sessionSockets.getSession({ invalid: 'socket' }, function (err, session) {
expect(err).to.be.a(CookieParserError);
expect(session).to.be(null);
done();
});
});
});

function stub(attributes) {
attributes.handshake = { signedCookies: { 'connect.sid': 42 }};
return attributes;
}

function ns() {
return {
on: function(event, callback) {
if (event === 'connection') callback(jonnySocket);
}
};
}

function io() {
return {
of: function() { return ns() },
sockets: ns()
};
}

function sessionStore() {
return {
load: function(cookie, callback) {
if (cookie) callback(null, jonnySocket);
else callback(new SessionStoreError(), null);
}
};
}

function cookieParser(handshake, options, callback) {
if (handshake) callback();
else callback(new CookieParserError());
}

function SessionStoreError() {}
function CookieParserError() {}

0 comments on commit e30a791

Please sign in to comment.