Skip to content
Closed
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
22 changes: 17 additions & 5 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export class ContextSession {
this.store = this.opts.ContextStore ? new this.opts.ContextStore(ctx) : this.opts.store;
}

/**
* modify cookie options based on request's domain
*/
modifyOptsForRequest(options: SessionOptions) {
const opts = { ...options };
const ctx = this.ctx;
if (typeof opts.domain === 'function') {
opts.domain = opts.domain(ctx, ctx.session);
}
return opts;
}

/**
* internal logic of `ctx.session`
* @return {Session} session object
Expand Down Expand Up @@ -128,7 +140,7 @@ export class ContextSession {
debug('decode %j error: %s', cookie, err);
if (err instanceof Error && !(err instanceof SyntaxError)) {
// clean this cookie to ensure next request won't throw again
ctx.cookies.set(opts.key, '', opts);
ctx.cookies.set(opts.key, '', this.modifyOptsForRequest(opts));
// `ctx.onerror` will unset all headers, and set those specified in err
Reflect.set(err, 'headers', {
'set-cookie': ctx.response.get('set-cookie'),
Expand Down Expand Up @@ -291,7 +303,7 @@ export class ContextSession {
const opts = {
...this.opts,
expires: COOKIE_EXP_DATE,
maxAge: false,
maxAge: 0,
};
const ctx = this.ctx;
const key = opts.key;
Expand All @@ -300,7 +312,7 @@ export class ContextSession {
if (externalKey) {
await this.store!.destroy(externalKey, { ctx });
}
ctx.cookies.set(key, '', opts);
ctx.cookies.set(key, '', this.modifyOptsForRequest(opts));
}

/**
Expand Down Expand Up @@ -340,7 +352,7 @@ export class ContextSession {
if (opts.externalKey) {
opts.externalKey.set(this.ctx, externalKey);
} else {
this.ctx.cookies.set(key, externalKey, opts);
this.ctx.cookies.set(key, externalKey, this.modifyOptsForRequest(opts));
}
return;
}
Expand All @@ -350,6 +362,6 @@ export class ContextSession {
const base64String = opts.encode(sessionData);
debug('save session data json base64 format: %s to cookie key: %s with options: %j',
base64String, key, opts);
this.ctx.cookies.set(key, base64String, opts);
this.ctx.cookies.set(key, base64String, this.modifyOptsForRequest(opts));
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ export const SessionOptions = z.object({
.args(z.any(), z.any())
.returns(z.void())
.optional(),
/**
* Restrict the cookie domain with either a static string or a function that
* return a string.
*/
domain: z.union([ z.string(), z.function().args(z.any(), z.any()).returns(z.string()) ]).optional(),
});

const DEFAULT_SESSION_OPTIONS = SessionOptions.parse({});
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export default {

CookieDateEpoch: 'Thu, 01 Jan 1970 00:00:00 GMT',
};

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
"moduleResolution": "NodeNext",
}
}