From 44505f9899ca199b68b34638d19f398cb5d3e8e4 Mon Sep 17 00:00:00 2001 From: Rahul Gupta-Iwasaki <2237985+rahulgi@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:42:05 -0700 Subject: [PATCH 1/2] Support setting cookie domain on a per-request basis. --- lib/context.js | 8 ++++---- lib/util.js | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/context.js b/lib/context.js index d3efbec..26db0bf 100644 --- a/lib/context.js +++ b/lib/context.js @@ -128,7 +128,7 @@ class ContextSession { debug('decode %j error: %s', cookie, err); if (!(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, '', util.modifyOptsForRequest(ctx, opts)); // ctx.onerror will unset all headers, and set those specified in err err.headers = { 'set-cookie': ctx.response.get('set-cookie'), @@ -287,7 +287,7 @@ class ContextSession { const externalKey = this.externalKey; if (externalKey) await this.store.destroy(externalKey, { ctx }); - ctx.cookies.set(key, '', opts); + ctx.cookies.set(key, '', util.modifyOptsForRequest(ctx, opts)); } /** @@ -328,7 +328,7 @@ 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, util.modifyOptsForRequest(this.ctx, opts)); } return; } @@ -338,7 +338,7 @@ class ContextSession { json = opts.encode(json); debug('save %s', json); - this.ctx.cookies.set(key, json, opts); + this.ctx.cookies.set(key, json, util.modifyOptsForRequest(this.ctx, opts)); } } diff --git a/lib/util.js b/lib/util.js index b3a1a28..9269be2 100644 --- a/lib/util.js +++ b/lib/util.js @@ -35,5 +35,13 @@ module.exports = { return crc(JSON.stringify(sess)); }, + modifyOptsForRequest(ctx, opts) { + const optsCopy = Object.assign({}, opts); + if (typeof opts.domain === "function") { + optsCopy.domain = opts.domain(ctx, opts); + } + return optsCopy; + }, + CookieDateEpoch: 'Thu, 01 Jan 1970 00:00:00 GMT', }; From 7a10a726634637f506871b0103efd5b3dd00dbe6 Mon Sep 17 00:00:00 2001 From: Rahul Gupta-Iwasaki <2237985+rahulgi@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:36:22 -0700 Subject: [PATCH 2/2] Use single quotes. --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 9269be2..d0bf767 100644 --- a/lib/util.js +++ b/lib/util.js @@ -37,7 +37,7 @@ module.exports = { modifyOptsForRequest(ctx, opts) { const optsCopy = Object.assign({}, opts); - if (typeof opts.domain === "function") { + if (typeof opts.domain === 'function') { optsCopy.domain = opts.domain(ctx, opts); } return optsCopy;