Skip to content

Commit

Permalink
feat: emit event expose ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jan 9, 2018
1 parent d1d4b95 commit a2401c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,23 @@ class ContextSession {
*/

valid(value, key) {
const ctx = this.ctx;
if (!value) {
this.app.emit('session:missed', { key, value });
this.app.emit('session:missed', { key, value, ctx });
return false;
}

if (value._expire && value._expire < Date.now()) {
debug('expired session');
this.app.emit('session:expired', { key, value });
this.app.emit('session:expired', { key, value, ctx });
return false;
}

const valid = this.opts.valid;
if (typeof valid === 'function' && !valid(this.ctx, value)) {
if (typeof valid === 'function' && !valid(ctx, value)) {
// valid session value fail, ignore this session
debug('invalid session');
this.app.emit('session:invalid', { key, value });
this.app.emit('session:invalid', { key, value, ctx });
return false;
}
return true;
Expand Down
3 changes: 3 additions & 0 deletions test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ describe('Koa Session External Store', () => {
app.on('session:expired', args => {
assert(args.key.match(/^\d+-/));
assert(args.value);
assert(args.ctx);
done();
});
app.use(async function(ctx) {
Expand Down Expand Up @@ -551,6 +552,7 @@ describe('Koa Session External Store', () => {

app.on('session:missed', args => {
assert(args.key === 'invalid-key');
assert(args.ctx);
done();
});

Expand Down Expand Up @@ -590,6 +592,7 @@ describe('Koa Session External Store', () => {
app.on('session:invalid', args => {
assert(args.key);
assert(args.value);
assert(args.ctx);
done();
});

Expand Down

0 comments on commit a2401c8

Please sign in to comment.