Skip to content

Commit

Permalink
Merge 3b7ef06 into 2497dc5
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Aug 28, 2018
2 parents 2497dc5 + 3b7ef06 commit f4f1077
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Expand Up @@ -53,6 +53,7 @@ const CONFIG = {
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 86400000,
autoCommit: true, /** (boolean) automatically commit headers (default true) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
Expand Down Expand Up @@ -150,6 +151,10 @@ if (this.session.isNew) {

Save this session no matter whether it is populated.

### Session#manuallyCommit()

Session headers are auto committed by default. Use this if `autoCommit` is set to `false`.

### Destroying a session

To destroy a session simply set it to `null`:
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -67,6 +67,7 @@ function formatOpts(opts) {
if (opts.overwrite == null) opts.overwrite = true;
if (opts.httpOnly == null) opts.httpOnly = true;
if (opts.signed == null) opts.signed = true;
if (opts.autoCommit == null) opts.autoCommit = true;

debug('session options %j', opts);

Expand Down
8 changes: 8 additions & 0 deletions lib/context.js
Expand Up @@ -197,6 +197,14 @@ class ContextSession {
debug('create session with val: %j externalKey: %s', val, externalKey);
if (this.store) this.externalKey = externalKey || this.opts.genid();
this.session = new Session(this.ctx, val);
if (!this.opts.autoCommit) {
const context = this;
this.session.manuallyCommit = async function() {
if (!context.opts.autoCommit) {
await context.commit();
}
};
}
}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/contextstore.test.js
Expand Up @@ -356,6 +356,53 @@ describe('Koa Session External Context Store', () => {
});
});

describe('when autoCommit is present', () => {
describe('and set to false', () => {
it('should not set headers if manuallyCommit() isn\'t called', done => {
const app = App({ autoCommit: false });
app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.message = 'hi';
ctx.body = 200;
return;
}
ctx.body = ctx.session.message;
});
const server = app.listen();

request(server)
.post('/')
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'];
should.not.exist(cookie);
})
.expect(200, done);
});
it('should set headers if manuallyCommit() is called', done => {
const app = App({ autoCommit: false });
app.use(async function(ctx, next) {
if (ctx.method === 'POST') {
ctx.session.message = 'dummy';
}
await next();
});
app.use(async function(ctx) {
ctx.body = 200;
});
const server = app.listen();

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.end(err => {
if (err) return done(err);
})
.expect(200, done);
});
});
});

describe('when maxAge present', () => {
describe('and set to be a session cookie', () => {
it('should not expire the session', done => {
Expand Down

0 comments on commit f4f1077

Please sign in to comment.