Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: koajs/session
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.0.1
Choose a base ref
...
head repository: koajs/session
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Jan 19, 2025

  1. fix: make sure options instance is not mutated (#230)

    fengmk2 authored Jan 19, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c45bf8b View commit details
  2. Release 7.0.2

    [skip ci]
    
    ## [7.0.2](v7.0.1...v7.0.2) (2025-01-19)
    
    ### Bug Fixes
    
    * make sure options instance is not mutated ([#230](#230)) ([c45bf8b](c45bf8b))
    semantic-release-bot committed Jan 19, 2025
    Copy the full SHA
    36beada View commit details
Showing with 55 additions and 13 deletions.
  1. +7 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +11 −8 src/index.ts
  4. +36 −4 test/store.test.ts
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [7.0.2](https://github.com/koajs/session/compare/v7.0.1...v7.0.2) (2025-01-19)


### Bug Fixes

* make sure options instance is not mutated ([#230](https://github.com/koajs/session/issues/230)) ([c45bf8b](https://github.com/koajs/session/commit/c45bf8b236f8b79f4d2e4ed40b7fae585f30330b))

## [7.0.1](https://github.com/koajs/session/compare/v7.0.0...v7.0.1) (2025-01-19)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
"type": "git",
"url": "git@github.com:koajs/session.git"
},
"version": "7.0.1",
"version": "7.0.2",
"keywords": [
"koa",
"middleware",
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -188,19 +188,23 @@ export function createSession(opts: CreateSessionOptions | any, app: any): Middl
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'));
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`');
}
}
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) {
@@ -264,7 +268,6 @@ function formatOptions(opts: SessionOptions) {
opts.genid = () => randomUUID();
}
}
return opts;
}

/**
40 changes: 36 additions & 4 deletions test/store.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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;
}
});

@@ -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', () => {