Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui5-proxy-middleware):directLoad support for UI5 2.x / legacy-free #3017

Merged
merged 3 commits into from
Mar 17, 2025
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/thirty-taxis-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/ui5-proxy-middleware': patch
---

fix: UI5 2 directLoad
2 changes: 2 additions & 0 deletions packages/ui5-proxy-middleware/src/base/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const BOOTSTRAP_LINK = 'resources/sap-ui-core.js';
export const BOOTSTRAP_REPLACE_REGEX = /src="[.{0,2}\/]*resources\/sap-ui-core\.js"/;
export const SANDBOX_LINK = 'test-resources/sap/ushell/bootstrap/sandbox.js';
export const SANDBOX_REPLACE_REGEX = /src="[.{0,2}\/]*test-resources\/sap\/ushell\/bootstrap\/sandbox\.js"/;
export const SANDBOX2_REPLACE_REGEX = /src="[.{0,2}\/]*resources\/sap\/ushell\/bootstrap\/sandbox2\.js"/;
export const SANDBOX2_LINK = 'resources/sap/ushell/bootstrap/sandbox2.js';
19 changes: 16 additions & 3 deletions packages/ui5-proxy-middleware/src/base/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { UI5Config } from '@sap-ux/ui5-config';
import type { NextFunction, Request, Response } from 'express';
import type { ProxyConfig } from './types';
import { existsSync, readFileSync } from 'fs';
import { BOOTSTRAP_LINK, BOOTSTRAP_REPLACE_REGEX, SANDBOX_LINK, SANDBOX_REPLACE_REGEX } from './constants';
import {
BOOTSTRAP_LINK,
BOOTSTRAP_REPLACE_REGEX,
SANDBOX_LINK,
SANDBOX_REPLACE_REGEX,
SANDBOX2_LINK,
SANDBOX2_REPLACE_REGEX
} from './constants';
import type { Url } from 'url';
import { t } from '../i18n';
import type { ReaderCollection } from '@ui5/fs';
Expand Down Expand Up @@ -241,11 +248,17 @@ export function injectUI5Url(originalHtml: string, ui5Configs: ProxyConfig[]): s
if (ui5Config.path === '/resources') {
const resourcesUrl = `src="${ui5Url}/${BOOTSTRAP_LINK}"`;
html = html.replace(BOOTSTRAP_REPLACE_REGEX, resourcesUrl);
//replace sandbox2 url as this is now part of /resources
const flpSandbox2Url = `src="${ui5Url}/${SANDBOX2_LINK}"`;
html = html.replace(SANDBOX2_REPLACE_REGEX, flpSandbox2Url);
}

if (ui5Config.path === '/test-resources') {
const testResourcesUrl = `src="${ui5Url}/${SANDBOX_LINK}"`;
html = html.replace(SANDBOX_REPLACE_REGEX, testResourcesUrl);
const flpSandboxUrl = `src="${ui5Url}/${SANDBOX_LINK}"`;
html = html.replace(SANDBOX_REPLACE_REGEX, flpSandboxUrl);
//replace sandbox2 url here as well although this is no longer part of /test-resources to be backwards compatible
const flpSandbox2Url = `src="${ui5Url}/${SANDBOX2_LINK}"`;
html = html.replace(SANDBOX2_REPLACE_REGEX, flpSandbox2Url);
}
}
return html;
Expand Down
48 changes: 44 additions & 4 deletions packages/ui5-proxy-middleware/test/base/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,24 +478,64 @@ describe('utils', () => {
respMock.write = jest.fn();
respMock.end = jest.fn();

const html = '<html></html>';
const htmlSandbox1 = '<html><script src="../test-resources/sap/ushell/bootstrap/sandbox.js" id="sap-ushell-bootstrap"></script></html>';
const htmlSandbox2 = '<html><script src="../resources/sap/ushell/bootstrap/sandbox2.js" id="sap-ushell-bootstrap"></script></html>';

beforeEach(() => {
nextMock.mockReset();
});

test('HTML is modified and response is sent', async () => {
test('HTML is modified and response is sent (UI5 1.x)', async () => {
byGlobMock.mockResolvedValueOnce([
{
getString: () => html
getString: () => htmlSandbox1
}
]);

await baseUtils.injectScripts({ url: 'test/flp.html' } as any, respMock, nextMock, [], rootProject);
await baseUtils.injectScripts({ url: 'test/flp.html' } as any, respMock, nextMock, [{path: '/test-resources', url: 'http://ui5.sap.com', version: '1.124.0'}], rootProject);
expect(respMock.writeHead).toBeCalledTimes(1);
expect(respMock.writeHead).toBeCalledWith(200, {
'Content-Type': 'text/html'
});
expect(respMock.write).toBeCalledWith('<html><script src="http://ui5.sap.com/1.124.0/test-resources/sap/ushell/bootstrap/sandbox.js" id="sap-ushell-bootstrap"></script></html>');
expect(respMock.end).toHaveBeenCalled();
expect(nextMock).not.toHaveBeenCalled();

expect(byGlobMock).toHaveBeenCalledWith('**/test/flp.html');
});

test('HTML is modified and response is sent (UI5 2.x backwards compatible)', async () => {
byGlobMock.mockResolvedValueOnce([
{
getString: () => htmlSandbox2
}
]);

await baseUtils.injectScripts({ url: 'test/flp.html' } as any, respMock, nextMock, [{path: '/test-resources', url: 'http://ui5.sap.com', version: '1.124.0'}], rootProject);
expect(respMock.writeHead).toBeCalledTimes(1);
expect(respMock.writeHead).toBeCalledWith(200, {
'Content-Type': 'text/html'
});
expect(respMock.write).toBeCalledWith('<html><script src="http://ui5.sap.com/1.124.0/resources/sap/ushell/bootstrap/sandbox2.js" id="sap-ushell-bootstrap"></script></html>');
expect(respMock.end).toHaveBeenCalled();
expect(nextMock).not.toHaveBeenCalled();

expect(byGlobMock).toHaveBeenCalledWith('**/test/flp.html');
});

test('HTML is modified and response is sent (UI5 2.x)', async () => {
byGlobMock.mockResolvedValueOnce([
{
getString: () => htmlSandbox2
}
]);

await baseUtils.injectScripts({ url: 'test/flp.html' } as any, respMock, nextMock, [{path: '/resources', url: 'http://ui5.sap.com', version: '1.124.0'}], rootProject);
expect(respMock.writeHead).toBeCalledTimes(1);
expect(respMock.writeHead).toBeCalledWith(200, {
'Content-Type': 'text/html'
});
expect(respMock.write).toBeCalledWith('<html><script src="http://ui5.sap.com/1.124.0/resources/sap/ushell/bootstrap/sandbox2.js" id="sap-ushell-bootstrap"></script></html>');
expect(respMock.end).toHaveBeenCalled();
expect(nextMock).not.toHaveBeenCalled();

Expand Down