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

fix: make sure options instance is not mutated #230

Merged
merged 2 commits into from
Jan 19, 2025
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
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,23 @@
throw new TypeError('app instance required: `session(opts, app)`');
}

const options: SessionOptions = opts ?? {};

// back-compat maxage
if (opts && !('maxAge' in opts) && 'maxage' in opts) {
Reflect.set(opts, 'maxAge', Reflect.get(opts, 'maxage'));
if (!('maxAge' in options) && 'maxage' in options) {
Reflect.set(options, 'maxAge', Reflect.get(options, 'maxage'));

Check warning on line 195 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L195

Added line #L195 was not covered by tests
if (process.env.NODE_ENV !== 'production') {
console.warn('DeprecationWarning: `maxage` option has been renamed to `maxAge`');
console.warn('[koa-session] DeprecationWarning: `maxage` option has been renamed to `maxAge`');

Check warning on line 197 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L197

Added line #L197 was not covered by tests
}
}
let options = {

// keep backwards compatibility: make sure options instance is not mutated
Object.assign(options, {
...DEFAULT_SESSION_OPTIONS,
...opts,
};
...options,
});
SessionOptions.parse(options);
options = formatOptions(options);
formatOptions(options);
extendContext(app.context, options);

return async function session(ctx: any, next: any) {
Expand Down Expand Up @@ -264,7 +268,6 @@
opts.genid = () => randomUUID();
}
}
return opts;
}

/**
Expand Down
40 changes: 36 additions & 4 deletions test/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const inspect = Symbol.for('nodejs.util.inspect.custom');
function App(options: CreateSessionOptions = {}) {
const app = new Koa();
app.keys = [ 'a', 'b' ];
options.store = store;
options.store = options.store ?? store;
app.use(session(options, app));
return app;
}
Expand All @@ -21,14 +21,15 @@ describe('Koa Session External Store', () => {

describe('when the session contains a ;', () => {
it('should still work', async () => {
const app = App();
const options: CreateSessionOptions = { store };
const app = App(options);

app.use(async (ctx: Koa.Context) => {
if (ctx.method === 'POST') {
ctx.session!.string = ';';
ctx.session.string = ';';
ctx.status = 204;
} else {
ctx.body = ctx.session!.string;
ctx.body = ctx.session.string;
}
});

Expand All @@ -43,6 +44,37 @@ describe('Koa Session External Store', () => {
.set('Cookie', cookie.join(';'))
.expect(';');
});

it('should disable store on options', async () => {
const options: CreateSessionOptions = { store };
const app = App(options);

app.use(async (ctx: Koa.Context) => {
if (ctx.method === 'POST') {
ctx.session.string = ';';
ctx.status = 204;
} else {
ctx.body = ctx.session.string ?? 'new session create';
}
});

const server = app.callback();
const res = await request(server)
.post('/')
.expect(204);

const cookie = res.get('Set-Cookie')!;
await request(server)
.get('/')
.set('Cookie', cookie.join(';'))
.expect(';');

options.store = undefined;
await request(server)
.get('/')
.set('Cookie', cookie.join(';'))
.expect('new session create');
});
});

describe('new session', () => {
Expand Down
Loading