Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add session.externalKey #207

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ if (this.session.isNew) {

Set cookie's maxAge.

### Session#externalKey

Get session external key, only exist when external session store present.

### Session#save()

Save this session no matter whether it is populated.
Expand Down
2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ContextSession {
create(val, externalKey) {
debug('create session with val: %j externalKey: %s', val, externalKey);
if (this.store) this.externalKey = externalKey || this.opts.genid && this.opts.genid(this.ctx);
this.session = new Session(this, val);
this.session = new Session(this, val, this.externalKey);
}

/**
Expand Down
15 changes: 13 additions & 2 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Session model.
*/

const inspect = Symbol.for('nodejs.util.inspect.custom');

class Session {
/**
* Session constructor
Expand All @@ -12,9 +14,10 @@ class Session {
* @api private
*/

constructor(sessionContext, obj) {
constructor(sessionContext, obj, externalKey) {
this._sessCtx = sessionContext;
this._ctx = sessionContext.ctx;
this._externalKey = externalKey;
if (!obj) {
this.isNew = true;
} else {
Expand Down Expand Up @@ -52,7 +55,7 @@ class Session {
* @api public
*/

inspect() {
[inspect]() {
return this.toJSON();
}

Expand Down Expand Up @@ -103,6 +106,14 @@ class Session {
this._requireSave = true;
}

/**
* get session external key
* only exist if opts.store present
*/
get externalKey() {
return this._externalKey;
}

/**
* save this session no matter whether it is populated
*
Expand Down
4 changes: 3 additions & 1 deletion test/contextstore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const mm = require('mm');
const session = require('..');
const ContextStore = require('./context_store');

const inspect = Symbol.for('nodejs.util.inspect.custom');

describe('Koa Session External Context Store', () => {
let cookie;

Expand Down Expand Up @@ -280,7 +282,7 @@ describe('Koa Session External Context Store', () => {

app.use(async function(ctx) {
ctx.session.foo = 'bar';
ctx.body = ctx.session.inspect();
ctx.body = ctx.session[inspect]();
});

request(app.listen())
Expand Down
4 changes: 3 additions & 1 deletion test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const request = require('supertest');
const should = require('should');
const session = require('..');

const inspect = Symbol.for('nodejs.util.inspect.custom');

describe('Koa Session Cookie', () => {
let cookie;

Expand Down Expand Up @@ -385,7 +387,7 @@ describe('Koa Session Cookie', () => {

app.use(async function(ctx) {
ctx.session.foo = 'bar';
ctx.body = ctx.session.inspect();
ctx.body = ctx.session[inspect]();
});

request(app.listen())
Expand Down
3 changes: 3 additions & 0 deletions test/externalkey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const assert = require('assert');
const session = require('..');
const store = require('./store');
const TOKEN_KEY = 'User-Token';

describe('Koa Session External Key', () => {
describe('when the external key set/get is invalid', () => {
it('should throw a error', () => {
Expand All @@ -24,8 +25,10 @@ describe('Koa Session External Key', () => {
if (ctx.method === 'POST') {
ctx.session.string = ';';
ctx.status = 204;
assert(ctx.session.externalKey);
} else {
ctx.body = ctx.session.string;
assert(ctx.session.externalKey === ctx.get(TOKEN_KEY));
}
});
const server = app.listen();
Expand Down
4 changes: 3 additions & 1 deletion test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const pedding = require('pedding');
const assert = require('assert');
const sleep = require('mz-modules/sleep');

const inspect = Symbol.for('nodejs.util.inspect.custom');

describe('Koa Session External Store', () => {
let cookie;

Expand Down Expand Up @@ -262,7 +264,7 @@ describe('Koa Session External Store', () => {

app.use(async function(ctx) {
ctx.session.foo = 'bar';
ctx.body = ctx.session.inspect();
ctx.body = ctx.session[inspect]();
});

request(app.listen())
Expand Down