From 11283e990b6735dc8927286ffc80107ff4ac64e3 Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 13:54:21 +0200 Subject: [PATCH 1/8] fix: path replace might not work for nested router instances --- packages/ui5-proxy-middleware/package.json | 4 +++- packages/ui5-proxy-middleware/src/base/utils.ts | 10 +++++++++- pnpm-lock.yaml | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/ui5-proxy-middleware/package.json b/packages/ui5-proxy-middleware/package.json index e5939e8bd19..e1794dedba8 100644 --- a/packages/ui5-proxy-middleware/package.json +++ b/packages/ui5-proxy-middleware/package.json @@ -49,7 +49,9 @@ "express": "4.21.2", "nock": "13.4.0", "supertest": "7.1.4", - "yaml": "2.2.2" + "yaml": "2.2.2", + "connect": "^3.7.0", + "@types/connect": "^3.4.38" }, "ui5": { "dependencies": [] diff --git a/packages/ui5-proxy-middleware/src/base/utils.ts b/packages/ui5-proxy-middleware/src/base/utils.ts index 8eb5013ddf8..666fa2f0862 100644 --- a/packages/ui5-proxy-middleware/src/base/utils.ts +++ b/packages/ui5-proxy-middleware/src/base/utils.ts @@ -17,6 +17,9 @@ import type { Url } from 'url'; import { t } from '../i18n'; import type { ReaderCollection } from '@ui5/fs'; import type { Socket } from 'node:net'; +import type connect from 'connect'; + +export type EnhancedIncomingMessage = (IncomingMessage & Pick) | connect.IncomingMessage; /** * Handler for the proxy response event. @@ -359,7 +362,12 @@ export function getPathRewrite(config: ProxyConfig, ui5Ver: string): Options['pa if (config.pathReplace) { // Remove trailing slash from pathReplace if present const sanitizedPathReplace = config.pathReplace?.replace(/\/$/, ''); - return (path: string) => path.replace(config.path, sanitizedPathReplace); + return (path: string, req: EnhancedIncomingMessage) => { + // ensure that the path starts with the configured path + // it might get lost when you nest router instances + const originalUrl = req.originalUrl?.includes(path) ? req.originalUrl : path; + return originalUrl.replace(config.path, sanitizedPathReplace); + } } return (path: string) => (path.startsWith(config.path) ? ui5Ver + path : ui5Ver + config.path + path); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0dd67dbfdb0..b8081d9e384 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4501,6 +4501,9 @@ importers: '@sap-ux/project-access': specifier: workspace:* version: link:../project-access + '@types/connect': + specifier: ^3.4.38 + version: 3.4.38 '@types/express': specifier: 4.17.21 version: 4.17.21 @@ -4510,6 +4513,9 @@ importers: '@types/supertest': specifier: 2.0.12 version: 2.0.12 + connect: + specifier: ^3.7.0 + version: 3.7.0 express: specifier: 4.21.2 version: 4.21.2 @@ -11133,7 +11139,7 @@ packages: dependencies: '@typescript-eslint/typescript-estree': 5.62.0(typescript@5.9.2) '@typescript-eslint/utils': 5.62.0(eslint@8.57.0)(typescript@5.9.2) - debug: 4.3.7 + debug: 4.4.1 eslint: 8.57.0 tsutils: 3.21.0(typescript@5.9.2) typescript: 5.9.2 From 570966da27ab98e8789afddf54fcd7d1c1afe213 Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 14:02:18 +0200 Subject: [PATCH 2/8] refactoring --- packages/ui5-proxy-middleware/package.json | 4 +--- packages/ui5-proxy-middleware/src/base/utils.ts | 10 +--------- pnpm-lock.yaml | 6 ------ 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/packages/ui5-proxy-middleware/package.json b/packages/ui5-proxy-middleware/package.json index e1794dedba8..e5939e8bd19 100644 --- a/packages/ui5-proxy-middleware/package.json +++ b/packages/ui5-proxy-middleware/package.json @@ -49,9 +49,7 @@ "express": "4.21.2", "nock": "13.4.0", "supertest": "7.1.4", - "yaml": "2.2.2", - "connect": "^3.7.0", - "@types/connect": "^3.4.38" + "yaml": "2.2.2" }, "ui5": { "dependencies": [] diff --git a/packages/ui5-proxy-middleware/src/base/utils.ts b/packages/ui5-proxy-middleware/src/base/utils.ts index 666fa2f0862..4431aa1cf98 100644 --- a/packages/ui5-proxy-middleware/src/base/utils.ts +++ b/packages/ui5-proxy-middleware/src/base/utils.ts @@ -17,9 +17,6 @@ import type { Url } from 'url'; import { t } from '../i18n'; import type { ReaderCollection } from '@ui5/fs'; import type { Socket } from 'node:net'; -import type connect from 'connect'; - -export type EnhancedIncomingMessage = (IncomingMessage & Pick) | connect.IncomingMessage; /** * Handler for the proxy response event. @@ -362,12 +359,7 @@ export function getPathRewrite(config: ProxyConfig, ui5Ver: string): Options['pa if (config.pathReplace) { // Remove trailing slash from pathReplace if present const sanitizedPathReplace = config.pathReplace?.replace(/\/$/, ''); - return (path: string, req: EnhancedIncomingMessage) => { - // ensure that the path starts with the configured path - // it might get lost when you nest router instances - const originalUrl = req.originalUrl?.includes(path) ? req.originalUrl : path; - return originalUrl.replace(config.path, sanitizedPathReplace); - } + return (path: string) => (path.startsWith(config.path) ? path.replace(config.path, sanitizedPathReplace) : sanitizedPathReplace + path); } return (path: string) => (path.startsWith(config.path) ? ui5Ver + path : ui5Ver + config.path + path); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b8081d9e384..d7454daa1ba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4501,9 +4501,6 @@ importers: '@sap-ux/project-access': specifier: workspace:* version: link:../project-access - '@types/connect': - specifier: ^3.4.38 - version: 3.4.38 '@types/express': specifier: 4.17.21 version: 4.17.21 @@ -4513,9 +4510,6 @@ importers: '@types/supertest': specifier: 2.0.12 version: 2.0.12 - connect: - specifier: ^3.7.0 - version: 3.7.0 express: specifier: 4.21.2 version: 4.21.2 From 21be44b9a649e22d8efba56072a2ca1f765f755e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 2 Oct 2025 12:09:45 +0000 Subject: [PATCH 3/8] Linting auto fix commit --- packages/ui5-proxy-middleware/src/base/utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/ui5-proxy-middleware/src/base/utils.ts b/packages/ui5-proxy-middleware/src/base/utils.ts index 4431aa1cf98..218a84bf202 100644 --- a/packages/ui5-proxy-middleware/src/base/utils.ts +++ b/packages/ui5-proxy-middleware/src/base/utils.ts @@ -359,7 +359,10 @@ export function getPathRewrite(config: ProxyConfig, ui5Ver: string): Options['pa if (config.pathReplace) { // Remove trailing slash from pathReplace if present const sanitizedPathReplace = config.pathReplace?.replace(/\/$/, ''); - return (path: string) => (path.startsWith(config.path) ? path.replace(config.path, sanitizedPathReplace) : sanitizedPathReplace + path); + return (path: string) => + path.startsWith(config.path) + ? path.replace(config.path, sanitizedPathReplace) + : sanitizedPathReplace + path; } return (path: string) => (path.startsWith(config.path) ? ui5Ver + path : ui5Ver + config.path + path); } From a918ec971f46a832452c093040a9742b400ad820 Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 14:18:37 +0200 Subject: [PATCH 4/8] fix middleware type and implementation --- packages/ui5-config/src/types/middlewares.ts | 1 + packages/ui5-proxy-middleware/src/ui5/middleware.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/ui5-config/src/types/middlewares.ts b/packages/ui5-config/src/types/middlewares.ts index 1a3ab849c66..483c702720e 100644 --- a/packages/ui5-config/src/types/middlewares.ts +++ b/packages/ui5-config/src/types/middlewares.ts @@ -2,6 +2,7 @@ import type { AuthenticationType } from '@sap-ux/store'; export interface UI5ProxyConfigTarget { path: string | string[]; url: string; + pathReplace?: string; } export interface UI5ProxyConfig { diff --git a/packages/ui5-proxy-middleware/src/ui5/middleware.ts b/packages/ui5-proxy-middleware/src/ui5/middleware.ts index da184812b68..c6aaff0c306 100644 --- a/packages/ui5-proxy-middleware/src/ui5/middleware.ts +++ b/packages/ui5-proxy-middleware/src/ui5/middleware.ts @@ -100,6 +100,7 @@ module.exports = async ({ resources, options }: MiddlewareParameters Date: Thu, 2 Oct 2025 14:41:36 +0200 Subject: [PATCH 5/8] add unit test --- .../test/ui5/middleware.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts b/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts index d92fcaf0bb3..3ee1e796e70 100644 --- a/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts +++ b/packages/ui5-proxy-middleware/test/ui5/middleware.test.ts @@ -187,6 +187,23 @@ describe('middleware', () => { ); }); + test('pathReplace', async () => { + await getTestServer({ + ...config, + ui5: { + path: '/resources', + url: 'http://ui5.example', + pathReplace: '/new-resources' + } + }); + expect(ui5ProxySpy).toHaveBeenCalledWith( + expect.objectContaining({ pathReplace: '/new-resources' }), + expect.objectContaining({}), + undefined, + expect.any(ToolsLogger) + ); + }); + test('secure', async () => { await getTestServer({ ...config, From 3c044b1a3b17181ac854fb756dffb14b6b433f84 Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 14:52:28 +0200 Subject: [PATCH 6/8] add unit test --- packages/ui5-proxy-middleware/test/base/utils.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/ui5-proxy-middleware/test/base/utils.test.ts b/packages/ui5-proxy-middleware/test/base/utils.test.ts index c2c81be681d..d5425ded6c6 100644 --- a/packages/ui5-proxy-middleware/test/base/utils.test.ts +++ b/packages/ui5-proxy-middleware/test/base/utils.test.ts @@ -569,6 +569,17 @@ describe('utils', () => { expect((rewrite as Function)('/mypath')).toEqual('this/path/should/rewrite/mypath'); }); + test('custom pathRewrite (simulate missing path because of nested router instances)', () => { + const config = { + pathReplace: 'this/path/should/rewrite/mypath', + path: '/mypath', + url: 'https://example.example' + }; + const ui5Ver = ''; + const rewrite = getPathRewrite(config, ui5Ver); + expect((rewrite as Function)('/test.ts')).toEqual('this/path/should/rewrite/mypath/test.ts'); + }); + test('handle pathRewrite with trailing slash', () => { const config = { path: '/resources', From f742826c54a6444a332d27e6a66ccf30a70eb1e8 Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 14:57:49 +0200 Subject: [PATCH 7/8] enhance jsdoc --- packages/ui5-proxy-middleware/src/base/utils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/ui5-proxy-middleware/src/base/utils.ts b/packages/ui5-proxy-middleware/src/base/utils.ts index 218a84bf202..6270e2064c8 100644 --- a/packages/ui5-proxy-middleware/src/base/utils.ts +++ b/packages/ui5-proxy-middleware/src/base/utils.ts @@ -350,6 +350,10 @@ export function directLoadProxy( /** * Create a rewrite based on the provided configuration. + * This will either replace the configured path with the provided pathReplace value or + * will prepend the UI5 version to the path. + * It also ensures that the path from the http-proxy-middleware starts with the + * config path in case of nested router instances. * * @param config proxy configuration * @param ui5Ver UI5 version string From 08dd01b8659323673bd1bb7e9abfdc6bccdf196b Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 2 Oct 2025 15:00:38 +0200 Subject: [PATCH 8/8] add cset --- .changeset/fast-pots-sit.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/fast-pots-sit.md diff --git a/.changeset/fast-pots-sit.md b/.changeset/fast-pots-sit.md new file mode 100644 index 00000000000..63afe98354f --- /dev/null +++ b/.changeset/fast-pots-sit.md @@ -0,0 +1,6 @@ +--- +'@sap-ux/ui5-proxy-middleware': patch +'@sap-ux/ui5-config': patch +--- + +fix: pathReplace not taken into account or truncated in case of nested router instances