From e71aa0f1f723f13233c0b1344092b5af9c147020 Mon Sep 17 00:00:00 2001 From: Michael Mason <105235096+mmason2-godaddy@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:08:26 -0800 Subject: [PATCH] adjust cookie parser invocation to earlier in the lifecycle chain (#1009) * adjust cookie parser invocation to earlier in the lifecycle chain * CHANGELOG --- packages/gasket-plugin-express/CHANGELOG.md | 3 +++ packages/gasket-plugin-express/lib/create-servers.js | 1 - packages/gasket-plugin-express/lib/index.js | 11 ++++++++++- packages/gasket-plugin-express/test/plugin.test.js | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/gasket-plugin-express/CHANGELOG.md b/packages/gasket-plugin-express/CHANGELOG.md index 8dfcc07b8..f115ebfcb 100644 --- a/packages/gasket-plugin-express/CHANGELOG.md +++ b/packages/gasket-plugin-express/CHANGELOG.md @@ -1,5 +1,7 @@ # `@gasket/plugin-express` +- adjust cookie parser invocation to earlier in the lifecycle chain ([#1009]) + ### 7.1.1 - Ensure cookies are parsed ([#1001]) @@ -146,3 +148,4 @@ [#959]: https://github.com/godaddy/gasket/pull/959 [#969]: https://github.com/godaddy/gasket/pull/969 [#1001]: https://github.com/godaddy/gasket/pull/1001 +[#1009]: https://github.com/godaddy/gasket/pull/1009 diff --git a/packages/gasket-plugin-express/lib/create-servers.js b/packages/gasket-plugin-express/lib/create-servers.js index f13932885..1288c283e 100644 --- a/packages/gasket-plugin-express/lib/create-servers.js +++ b/packages/gasket-plugin-express/lib/create-servers.js @@ -8,7 +8,6 @@ */ module.exports = async function createServers(gasket, serverOpts) { const app = gasket.actions.getExpressApp(); - app.use(require('cookie-parser')()); await gasket.exec('express', app); diff --git a/packages/gasket-plugin-express/lib/index.js b/packages/gasket-plugin-express/lib/index.js index e2dfbe39b..b14b98b19 100644 --- a/packages/gasket-plugin-express/lib/index.js +++ b/packages/gasket-plugin-express/lib/index.js @@ -17,7 +17,16 @@ const plugin = { getExpressApp(gasket) { const express = require('express'); const { http2 } = gasket.config; - app ??= http2 ? require('http2-express')(express) : express(); + + if (!app) { + if (http2) { + app = require('http2-express')(express); + } else { + app = express(); + } + + app.use(require('cookie-parser')()); + } return app; } diff --git a/packages/gasket-plugin-express/test/plugin.test.js b/packages/gasket-plugin-express/test/plugin.test.js index 24a9b6446..5169ab832 100644 --- a/packages/gasket-plugin-express/test/plugin.test.js +++ b/packages/gasket-plugin-express/test/plugin.test.js @@ -87,7 +87,7 @@ describe('createServers', () => { }); it('attaches cookie parser middleware', async function () { - await plugin.hooks.createServers(gasket, {}); + plugin.actions.getExpressApp(gasket); expect(app.use).toHaveBeenCalledWith(cookieParserMiddleware); });