From 47202e80f39a7d9214b7d4f1207a62643daea587 Mon Sep 17 00:00:00 2001 From: Jimmy li Date: Mon, 6 Jul 2026 21:49:17 +0800 Subject: [PATCH] fix: preserve session store when reloading with callback Signed-off-by: Jimmy li --- lib/session.js | 3 ++- test/session.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/session.js b/lib/session.js index 9cbace1..953d402 100644 --- a/lib/session.js +++ b/lib/session.js @@ -165,8 +165,9 @@ module.exports = class Session { reload (callback) { if (callback) { this[sessionStoreKey].get(this[sessionIdKey], (error, session) => { - this[requestKey].session = new Session(this[requestKey], + this[requestKey].session = new Session( this[sessionStoreKey], + this[requestKey], this[generateId], this[cookieOptsKey], this[cookieSignerKey], diff --git a/test/session.test.js b/test/session.test.js index f5acfa8..1358fd1 100644 --- a/test/session.test.js +++ b/test/session.test.js @@ -613,6 +613,32 @@ test('should reload the session', async (t) => { t.assert.strictEqual(response.statusCode, 200) }) +test('should save the reloaded session with callback', async (t) => { + t.plan(3) + const fastify = await buildFastify((request, reply) => { + request.session.someData = 'some-data' + request.session.save((err) => { + t.assert.ifError(err) + + request.session.reload((err) => { + t.assert.ifError(err) + + reply.send(200) + }) + }) + }, { + ...DEFAULT_OPTIONS, + cookie: { secure: false } + }) + t.after(() => fastify.close()) + + const response = await fastify.inject({ + url: '/' + }) + + t.assert.strictEqual(response.statusCode, 200) +}) + test('should save the session', async (t) => { t.plan(6) const fastify = await buildFastify((request, reply) => {