Skip to content

Commit c45bf8b

Browse files
authored
fix: make sure options instance is not mutated (#230)
1 parent 6dfdcf6 commit c45bf8b

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,23 @@ export function createSession(opts: CreateSessionOptions | any, app: any): Middl
188188
throw new TypeError('app instance required: `session(opts, app)`');
189189
}
190190

191+
const options: SessionOptions = opts ?? {};
192+
191193
// back-compat maxage
192-
if (opts && !('maxAge' in opts) && 'maxage' in opts) {
193-
Reflect.set(opts, 'maxAge', Reflect.get(opts, 'maxage'));
194+
if (!('maxAge' in options) && 'maxage' in options) {
195+
Reflect.set(options, 'maxAge', Reflect.get(options, 'maxage'));
194196
if (process.env.NODE_ENV !== 'production') {
195-
console.warn('DeprecationWarning: `maxage` option has been renamed to `maxAge`');
197+
console.warn('[koa-session] DeprecationWarning: `maxage` option has been renamed to `maxAge`');
196198
}
197199
}
198-
let options = {
200+
201+
// keep backwards compatibility: make sure options instance is not mutated
202+
Object.assign(options, {
199203
...DEFAULT_SESSION_OPTIONS,
200-
...opts,
201-
};
204+
...options,
205+
});
202206
SessionOptions.parse(options);
203-
options = formatOptions(options);
207+
formatOptions(options);
204208
extendContext(app.context, options);
205209

206210
return async function session(ctx: any, next: any) {
@@ -264,7 +268,6 @@ function formatOptions(opts: SessionOptions) {
264268
opts.genid = () => randomUUID();
265269
}
266270
}
267-
return opts;
268271
}
269272

270273
/**

test/store.test.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const inspect = Symbol.for('nodejs.util.inspect.custom');
1111
function App(options: CreateSessionOptions = {}) {
1212
const app = new Koa();
1313
app.keys = [ 'a', 'b' ];
14-
options.store = store;
14+
options.store = options.store ?? store;
1515
app.use(session(options, app));
1616
return app;
1717
}
@@ -21,14 +21,15 @@ describe('Koa Session External Store', () => {
2121

2222
describe('when the session contains a ;', () => {
2323
it('should still work', async () => {
24-
const app = App();
24+
const options: CreateSessionOptions = { store };
25+
const app = App(options);
2526

2627
app.use(async (ctx: Koa.Context) => {
2728
if (ctx.method === 'POST') {
28-
ctx.session!.string = ';';
29+
ctx.session.string = ';';
2930
ctx.status = 204;
3031
} else {
31-
ctx.body = ctx.session!.string;
32+
ctx.body = ctx.session.string;
3233
}
3334
});
3435

@@ -43,6 +44,37 @@ describe('Koa Session External Store', () => {
4344
.set('Cookie', cookie.join(';'))
4445
.expect(';');
4546
});
47+
48+
it('should disable store on options', async () => {
49+
const options: CreateSessionOptions = { store };
50+
const app = App(options);
51+
52+
app.use(async (ctx: Koa.Context) => {
53+
if (ctx.method === 'POST') {
54+
ctx.session.string = ';';
55+
ctx.status = 204;
56+
} else {
57+
ctx.body = ctx.session.string ?? 'new session create';
58+
}
59+
});
60+
61+
const server = app.callback();
62+
const res = await request(server)
63+
.post('/')
64+
.expect(204);
65+
66+
const cookie = res.get('Set-Cookie')!;
67+
await request(server)
68+
.get('/')
69+
.set('Cookie', cookie.join(';'))
70+
.expect(';');
71+
72+
options.store = undefined;
73+
await request(server)
74+
.get('/')
75+
.set('Cookie', cookie.join(';'))
76+
.expect('new session create');
77+
});
4678
});
4779

4880
describe('new session', () => {

0 commit comments

Comments
 (0)