Skip to content

Commit 331db17

Browse files
authored
@@ -- Add Express V4 Support to @salesforce/mrt-utilities package. (#334)
* Initial Commit * Update CHANGELOG.md * Change peer to have a wider support range for 4.x * Revert manual changelog update; replace with changeset * Add some docs to better understand this pattern * No monkey patching
1 parent e4ef6db commit 331db17

7 files changed

Lines changed: 808 additions & 300 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@salesforce/mrt-utilities': patch
3+
---
4+
5+
Add Express 4 support, improve middleware error handling, and add dual-version Express test coverage.

packages/mrt-utilities/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"eslint-plugin-header": "catalog:",
116116
"eslint-plugin-prettier": "catalog:",
117117
"express": "5.1.0",
118+
"express4": "npm:express@4.21.2",
118119
"mocha": "catalog:",
119120
"prettier": "catalog:",
120121
"shx": "catalog:",
@@ -124,7 +125,7 @@
124125
"typescript-eslint": "catalog:"
125126
},
126127
"peerDependencies": {
127-
"express": "5.1.0"
128+
"express": "^4.0.0 || ^5.0.0"
128129
},
129130
"engines": {
130131
"node": ">=22.16.0"

packages/mrt-utilities/src/middleware/middleware.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const createMRTRequestProcessorMiddleware = (
272272
req.headers[X_MOBIFY_REQUEST_PROCESSOR_LOCAL] = 'true'; // Mark the request as processed by the request processor
273273
};
274274

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

285-
// Apply custom query parameter parsing.
286-
await processIncomingRequest(req, res);
287-
288-
// Strip out API Gateway headers from the incoming request. We
289-
// do that now so that the rest of the code don't have to deal
290-
// with these headers, which can be large and may be accidentally
291-
// forwarded to other servers.
292-
cleanUpHeaders(req, false);
293-
294-
// Hand off to the next middleware
295-
next();
285+
// Apply custom query parameter parsing and forward errors via next()
286+
processIncomingRequest(req, res)
287+
.then(() => {
288+
// Strip out API Gateway headers from the incoming request. We
289+
// do that now so that the rest of the code don't have to deal
290+
// with these headers, which can be large and may be accidentally
291+
// forwarded to other servers.
292+
cleanUpHeaders(req, false);
293+
294+
// Hand off to the next middleware
295+
next();
296+
})
297+
.catch((err) => {
298+
next(err);
299+
});
296300
};
297301

298302
return ssrRequestProcessorMiddleware;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025, Salesforce, Inc.
3+
* SPDX-License-Identifier: Apache-2
4+
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
import express5 from 'express';
8+
import {createRequire} from 'node:module';
9+
10+
/*
11+
* Load Express v4 via an npm alias so tests can run against v4 and v5
12+
* in the same process without altering production imports.
13+
*/
14+
const require = createRequire(import.meta.url);
15+
const express4 = require('express4') as typeof express5;
16+
17+
/*
18+
* Export both versions for parameterized test suites.
19+
*/
20+
export const expressVersions = [
21+
{label: 'express4', express: express4},
22+
{label: 'express5', express: express5},
23+
];

0 commit comments

Comments
 (0)