Skip to content

Commit c8acca9

Browse files
committed
only save new sessions if populated
closes koajs#17
1 parent f3d2e22 commit c8acca9

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ test:
22
@NODE_ENV=test ./node_modules/.bin/mocha \
33
--require should \
44
--harmony-generators \
5-
--reporter spec \
6-
--bail
5+
--reporter spec
76

87
clean:
98
@rm -rf node_modules

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ module.exports = function(opts){
6969
// remove
7070
if (false === sess) return this.cookies.set(key, '', opts);
7171

72+
// do nothing if new and not populated
73+
if (!json && !sess.length) return;
74+
7275
// save
7376
if (sess.changed(json)) sess.save();
7477
}
@@ -124,6 +127,19 @@ Session.prototype.changed = function(prev){
124127
return this._json != prev;
125128
};
126129

130+
/**
131+
* Return how many values there are in the session object.
132+
* Used to see if it's "populated".
133+
* Aliased as `.populated` because why not.
134+
*
135+
* @return {Number}
136+
* @api public
137+
*/
138+
139+
Session.prototype.__defineGetter__('length', function(){
140+
return Object.keys(this.toJSON()).length;
141+
});
142+
127143
/**
128144
* Save session changes by
129145
* performing a Set-Cookie.

test.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Koa Session', function(){
1212
app.keys = ['a', 'b'];
1313
app.use(session());
1414
app.use(function *(){
15+
this.session.message = 'hi';
1516
this.body = this.session;
1617
});
1718

@@ -26,7 +27,8 @@ describe('Koa Session', function(){
2627
var app = koa();
2728
app.use(session());
2829
app.use(function *(){
29-
this.session;
30+
this.session.message = 'hi';
31+
this.body = this.session;
3032
});
3133

3234
request(app.listen())
@@ -44,6 +46,7 @@ describe('Koa Session', function(){
4446
signed: false
4547
}));
4648
app.use(function *(){
49+
this.session.message = 'hi';
4750
this.body = this.session;
4851
});
4952

@@ -97,7 +100,24 @@ describe('Koa Session', function(){
97100
})
98101
})
99102

100-
describe('when accessed', function(done){
103+
describe('when accessed and not populated', function(done){
104+
it('should not Set-Cookie', function(done) {
105+
var app = App();
106+
app.use(function *(){
107+
this.session;
108+
this.body = 'greetings';
109+
})
110+
request(app.listen())
111+
.get('/')
112+
.expect(200, function(err, res){
113+
if (err) return done(err);
114+
res.header.should.not.have.property('set-cookie');
115+
done();
116+
})
117+
})
118+
})
119+
120+
describe('when populated', function(done){
101121
it('should Set-Cookie', function(done){
102122
var app = App();
103123
app.use(function *(){
@@ -115,17 +135,16 @@ describe('Koa Session', function(){
115135
})
116136
})
117137

118-
it('should not set .isNew', function(done){
138+
it('should not Set-Cookie', function(done){
119139
var app = App();
120140
app.use(function *(){
121141
this.body = this.session;
122142
})
123143
request(app.listen())
124144
.get('/')
125-
.expect('Set-Cookie', /koa:sess/)
126145
.expect(200, function(err, res){
127146
if (err) return done(err);
128-
res.header['set-cookie'].should.not.match(/isNew/);
147+
res.header.should.not.have.property('set-cookie');
129148
done();
130149
})
131150
})
@@ -212,6 +231,23 @@ describe('Koa Session', function(){
212231
})
213232

214233
describe('{}', function(){
234+
it('should not Set-Cookie', function(done){
235+
var app = App();
236+
app.use(function *(){
237+
this.session = {};
238+
this.body = 'asdf';
239+
})
240+
request(app.listen())
241+
.get('/')
242+
.expect(200, function(err, res){
243+
if (err) return done(err);
244+
res.header.should.not.have.property('set-cookie');
245+
done();
246+
});
247+
})
248+
})
249+
250+
describe('{a: b}', function(){
215251
it('should create a session', function(done){
216252
var app = App();
217253
app.use(function *(){

0 commit comments

Comments
 (0)