Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mrt-utilities-express-4-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@salesforce/mrt-utilities': patch
---

Add Express 4 support, improve middleware error handling, and add dual-version Express test coverage.
3 changes: 2 additions & 1 deletion packages/mrt-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"eslint-plugin-header": "catalog:",
"eslint-plugin-prettier": "catalog:",
"express": "5.1.0",
"express4": "npm:express@4.21.2",
"mocha": "catalog:",
"prettier": "catalog:",
"shx": "catalog:",
Expand All @@ -124,7 +125,7 @@
"typescript-eslint": "catalog:"
},
"peerDependencies": {
"express": "5.1.0"
"express": "^4.0.0 || ^5.0.0"
},
"engines": {
"node": ">=22.16.0"
Expand Down
28 changes: 16 additions & 12 deletions packages/mrt-utilities/src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const createMRTRequestProcessorMiddleware = (
req.headers[X_MOBIFY_REQUEST_PROCESSOR_LOCAL] = 'true'; // Mark the request as processed by the request processor
};

const ssrRequestProcessorMiddleware = async (req: Request, res: Response, next: NextFunction) => {
const ssrRequestProcessorMiddleware = (req: Request, res: Response, next: NextFunction) => {
// If the path is /, we enforce that the only methods
// allowed are GET, HEAD or OPTIONS. This is a restriction
// imposed by API Gateway: we enforce it here so that the
Expand All @@ -282,17 +282,21 @@ export const createMRTRequestProcessorMiddleware = (
return;
}

// Apply custom query parameter parsing.
await processIncomingRequest(req, res);

// Strip out API Gateway headers from the incoming request. We
// do that now so that the rest of the code don't have to deal
// with these headers, which can be large and may be accidentally
// forwarded to other servers.
cleanUpHeaders(req, false);

// Hand off to the next middleware
next();
// Apply custom query parameter parsing and forward errors via next()
processIncomingRequest(req, res)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reason for the change here, I am just curious

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We made that change so this middleware works in Express 4.
Express 4 doesn’t always handle async errors, so we now call processIncomingRequest(...) and only call next() after it finishes, and if it fails we pass the error to next(err). This keeps the same behaviour but avoids unhandled promise errors. The function signature isn't changed, so we don't have a breaking change.

.then(() => {
// Strip out API Gateway headers from the incoming request. We
// do that now so that the rest of the code don't have to deal
// with these headers, which can be large and may be accidentally
// forwarded to other servers.
cleanUpHeaders(req, false);

// Hand off to the next middleware
next();
})
.catch((err) => {
next(err);
});
};

return ssrRequestProcessorMiddleware;
Expand Down
23 changes: 23 additions & 0 deletions packages/mrt-utilities/test/helpers/express-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/

import express5 from 'express';
import {createRequire} from 'node:module';

/*
* Load Express v4 via an npm alias so tests can run against v4 and v5
* in the same process without altering production imports.
*/
const require = createRequire(import.meta.url);
const express4 = require('express4') as typeof express5;

/*
* Export both versions for parameterized test suites.
*/
export const expressVersions = [
{label: 'express4', express: express4},
{label: 'express5', express: express5},
];
Loading
Loading