Skip to content

Commit 901c8e0

Browse files
committed
Fix blank responses for store with synchronous ops
fixes #61
1 parent 78b2db5 commit 901c8e0

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix blank responses for stores with synchronous operations
5+
16
1.6.3 / 2014-07-04
27
==================
38

index.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ function session(options){
177177
return false;
178178
}
179179

180+
var ret;
181+
var sync = true;
182+
180183
if (chunk === undefined) {
181184
chunk = '';
182185
}
@@ -189,9 +192,22 @@ function session(options){
189192
store.destroy(req.sessionID, function(err){
190193
if (err) console.error(err.stack);
191194
debug('destroyed');
195+
196+
if (sync) {
197+
ret = end.call(res, chunk, encoding);
198+
sync = false;
199+
return;
200+
}
201+
192202
end.call(res);
193203
});
194-
return res.write(chunk, encoding);
204+
205+
if (sync) {
206+
ret = res.write(chunk, encoding);
207+
sync = false;
208+
}
209+
210+
return ret;
195211
}
196212

197213
// no session to save
@@ -207,9 +223,22 @@ function session(options){
207223
req.session.save(function(err){
208224
if (err) console.error(err.stack);
209225
debug('saved');
226+
227+
if (sync) {
228+
ret = end.call(res, chunk, encoding);
229+
sync = false;
230+
return;
231+
}
232+
210233
end.call(res);
211234
});
212-
return res.write(chunk, encoding);
235+
236+
if (sync) {
237+
ret = res.write(chunk, encoding);
238+
sync = false;
239+
}
240+
241+
return ret;
213242
}
214243

215244
return end.call(res, chunk, encoding);

test/session.js

+58
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,44 @@ describe('session()', function(){
13571357
})
13581358
})
13591359

1360+
describe('synchronous store', function(){
1361+
it('should respond correctly on save', function(done){
1362+
var store = new SyncStore()
1363+
var server = createServer({ store: store }, function (req, res) {
1364+
req.session.count = req.session.count || 0
1365+
req.session.count++
1366+
res.end('hits: ' + req.session.count)
1367+
})
1368+
1369+
request(server)
1370+
.get('/')
1371+
.expect(200, 'hits: 1', done)
1372+
})
1373+
1374+
it('should respond correctly on destroy', function(done){
1375+
var store = new SyncStore()
1376+
var server = createServer({ store: store, unset: 'destroy' }, function (req, res) {
1377+
req.session.count = req.session.count || 0
1378+
var count = ++req.session.count
1379+
if (req.session.count > 1) {
1380+
req.session = null
1381+
res.write('destroyed\n')
1382+
}
1383+
res.end('hits: ' + count)
1384+
})
1385+
1386+
request(server)
1387+
.get('/')
1388+
.expect(200, 'hits: 1', function (err, res) {
1389+
if (err) return done(err)
1390+
request(server)
1391+
.get('/')
1392+
.set('Cookie', cookie(res))
1393+
.expect(200, 'destroyed\nhits: 2', done)
1394+
})
1395+
})
1396+
})
1397+
13601398
describe('cookieParser()', function () {
13611399
it('should read from req.cookies', function(done){
13621400
var app = express()
@@ -1487,3 +1525,23 @@ function sid(res) {
14871525
var val = match ? match[1] : undefined
14881526
return val
14891527
}
1528+
1529+
function SyncStore() {
1530+
this.sessions = Object.create(null);
1531+
}
1532+
1533+
SyncStore.prototype.__proto__ = session.Store.prototype;
1534+
1535+
SyncStore.prototype.destroy = function destroy(sid, callback) {
1536+
delete this.sessions[sid];
1537+
callback();
1538+
};
1539+
1540+
SyncStore.prototype.get = function get(sid, callback) {
1541+
callback(null, JSON.parse(this.sessions[sid]));
1542+
};
1543+
1544+
SyncStore.prototype.set = function set(sid, sess, callback) {
1545+
this.sessions[sid] = JSON.stringify(sess);
1546+
callback();
1547+
};

0 commit comments

Comments
 (0)