Skip to content

Instantly share code, notes, and snippets.

@lpinca
Last active January 20, 2017 00:43
Show Gist options
  • Save lpinca/11222004 to your computer and use it in GitHub Desktop.
Save lpinca/11222004 to your computer and use it in GitHub Desktop.
Primus `express-session` example

Upgrade requests should not save the session. If the session is saved when the WebSocket is closed, all the changes made by normal requests in the time span that goes from the opening of the WebSocket to the close of the same WebSocket are lost.

This is evident when this example page is refreshed in Chrome. For some odd reason the WebSocket request is closed after that the new request has been answered and the session is overriden by the old session that is retained in the old upgrade request.

To make the example work, apply the patch:

patch -p0 < skiponupgrade.patch

Once patched express-session will not save the session for upgrade requests.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>`express-session` example</h1>
<pre id="session"></pre>
<script src="/primus/primus.js"></script>
<script>
(function () {
var session = document.getElementById('session')
, primus = new Primus();
primus.on('data', function (data) {
session.textContent = data;
});
})();
</script>
</body>
</html>
'use strict';
var express = require('express')
, app = express()
, cookieParser = require('cookie-parser')('shhhh, very secret')
, favicon = require('static-favicon')
, path = require('path')
, port = 3000
, Primus = require('primus')
, primus
, server = require('http').createServer(app)
, session = require('express-session')();
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(cookieParser);
app.use(session);
app.get('/', function (req, res) {
req.session.timestamp = Date.now();
res.sendfile(path.join(__dirname, 'index.html'));
});
primus = new Primus(server);
primus.before('cookies', cookieParser);
primus.before('session', session);
primus.on('connection', function(spark) {
spark.write(JSON.stringify(spark.request.session, null, ' '));
});
server.listen(port, function() {
console.log('server listening on port ' + port);
});
{
"name": "session",
"version": "0.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"cookie-parser": "^1.0.1",
"express": "^4.0.0",
"express-session": "^1.0.2",
"primus": "^2.1.2",
"static-favicon": "^1.0.2",
"ws": "^0.4.31"
}
}
--- node_modules/express-session/index~.js 2014-04-20 02:46:37.000000000 +0200
+++ node_modules/express-session/index.js 2014-04-23 21:46:19.546236925 +0200
@@ -297,7 +297,7 @@
var end = res.end;
res.end = function(data, encoding){
res.end = end;
- if (!req.session) return res.end(data, encoding);
+ if (!req.session || !writeHead) return res.end(data, encoding);
debug('saving');
req.session.resetMaxAge();
req.session.save(function(err){
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment