@sap-ux/ui5-proxy-middleware 1.4.5 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -76,7 +76,7 @@ server:
76
76
  ### Loading UI5 sources directly from UI5 CDN
77
77
  If you don't load all UI5 source through the `ui5-proxy-middleware`, then you can set `directLoad: true`. This will inject the absolute UI5 URL in the bootstrap of your HTML file, thus loading the libraries directly from UI5 CDN.
78
78
 
79
- **NOTE: this works only for the default HTML files created by the [Fiori generator](https://www.npmjs.com/package/@sap/generator-fiori) when a project is generated. Hence index.html, flpSandbox.html and flpSandboxMockServer.html!**
79
+ **NOTE: this does not work with virtualized html files provided by the `preview-middleware`. In that case, call `npx @sap-ux/create add html` to generate a physical copy of the virtualized files.**
80
80
 
81
81
  ```Yaml
82
82
  server:
@@ -1,4 +1,3 @@
1
- export declare const HTML_MOUNT_PATHS: string[];
2
1
  export declare const BOOTSTRAP_LINK = "resources/sap-ui-core.js";
3
2
  export declare const BOOTSTRAP_REPLACE_REGEX: RegExp;
4
3
  export declare const SANDBOX_LINK = "test-resources/sap/ushell/bootstrap/sandbox.js";
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SANDBOX_REPLACE_REGEX = exports.SANDBOX_LINK = exports.BOOTSTRAP_REPLACE_REGEX = exports.BOOTSTRAP_LINK = exports.HTML_MOUNT_PATHS = void 0;
4
- exports.HTML_MOUNT_PATHS = ['/index.html', '/**/flpSandbox.html', '/**/flpSandboxMockServer.html'];
3
+ exports.SANDBOX_REPLACE_REGEX = exports.SANDBOX_LINK = exports.BOOTSTRAP_REPLACE_REGEX = exports.BOOTSTRAP_LINK = void 0;
5
4
  exports.BOOTSTRAP_LINK = 'resources/sap-ui-core.js';
6
5
  exports.BOOTSTRAP_REPLACE_REGEX = /src="[.{0,2}\/]*resources\/sap-ui-core\.js"/;
7
6
  exports.SANDBOX_LINK = 'test-resources/sap/ushell/bootstrap/sandbox.js';
@@ -1,3 +1,4 @@
1
+ /// <reference path="../../../../types/ui5.d.ts" />
1
2
  /// <reference types="node" />
2
3
  /// <reference types="node" />
3
4
  import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';
@@ -6,6 +7,7 @@ import { type Manifest } from '@sap-ux/project-access';
6
7
  import type { NextFunction, Request, Response } from 'express';
7
8
  import type { ProxyConfig } from './types';
8
9
  import type { Url } from 'url';
10
+ import type { ReaderCollection } from '@ui5/fs';
9
11
  /**
10
12
  * Handler for the proxy response event.
11
13
  * Sets an Etag which will be used for re-validation of the cached UI5 sources.
@@ -85,11 +87,11 @@ export declare function resolveUI5Version(version?: string, log?: ToolsLogger, m
85
87
  /**
86
88
  * Injects the absolute UI5 urls into the html file, which is used to preview the application.
87
89
  *
88
- * @param htmlFilePath - path to the html file which is used for previwing the application
90
+ * @param originalHtml - the content of the html file
89
91
  * @param ui5Configs - the configuration of the ui5-proxy-middleware
90
92
  * @returns The modified html file content
91
93
  */
92
- export declare function injectUI5Url(htmlFilePath: string, ui5Configs: ProxyConfig[]): string | undefined;
94
+ export declare function injectUI5Url(originalHtml: string, ui5Configs: ProxyConfig[]): string;
93
95
  /**
94
96
  * Injects scripts into the html file, which is used to preview the application.
95
97
  *
@@ -97,8 +99,9 @@ export declare function injectUI5Url(htmlFilePath: string, ui5Configs: ProxyConf
97
99
  * @param res - the http response object
98
100
  * @param next - the next function, used to forward the request to the next available handler
99
101
  * @param ui5Configs - the UI5 configuration of the ui5-proxy-middleware
102
+ * @param rootProject - the root project
100
103
  */
101
- export declare const injectScripts: (req: Request, res: Response, next: NextFunction, ui5Configs: ProxyConfig[]) => Promise<void>;
104
+ export declare const injectScripts: (req: Request, res: Response, next: NextFunction, ui5Configs: ProxyConfig[], rootProject: ReaderCollection) => Promise<void>;
102
105
  /**
103
106
  * Filters comressed html files from UI5 CDN.
104
107
  * Avoid ERR_CONTENT_DECODING_FAILED on http request for gzip'd html files.
@@ -4,7 +4,6 @@ exports.proxyErrorHandler = exports.filterCompressedHtmlFiles = exports.injectSc
4
4
  const project_access_1 = require("@sap-ux/project-access");
5
5
  const ui5_config_1 = require("@sap-ux/ui5-config");
6
6
  const fs_1 = require("fs");
7
- const path_1 = require("path");
8
7
  const constants_1 = require("./constants");
9
8
  const i18n_1 = require("../i18n");
10
9
  /**
@@ -213,30 +212,25 @@ exports.resolveUI5Version = resolveUI5Version;
213
212
  /**
214
213
  * Injects the absolute UI5 urls into the html file, which is used to preview the application.
215
214
  *
216
- * @param htmlFilePath - path to the html file which is used for previwing the application
215
+ * @param originalHtml - the content of the html file
217
216
  * @param ui5Configs - the configuration of the ui5-proxy-middleware
218
217
  * @returns The modified html file content
219
218
  */
220
- function injectUI5Url(htmlFilePath, ui5Configs) {
221
- if ((0, fs_1.existsSync)(htmlFilePath)) {
222
- let html = (0, fs_1.readFileSync)(htmlFilePath, { encoding: 'utf8' });
223
- for (const ui5Config of ui5Configs) {
224
- const ui5Host = ui5Config.url.replace(/\/$/, '');
225
- const ui5Url = ui5Config.version ? `${ui5Host}/${ui5Config.version}` : ui5Host;
226
- if (ui5Config.path === '/resources') {
227
- const resourcesUrl = `src="${ui5Url}/${constants_1.BOOTSTRAP_LINK}"`;
228
- html = html.replace(constants_1.BOOTSTRAP_REPLACE_REGEX, resourcesUrl);
229
- }
230
- if (ui5Config.path === '/test-resources') {
231
- const testResourcesUrl = `src="${ui5Url}/${constants_1.SANDBOX_LINK}"`;
232
- html = html.replace(constants_1.SANDBOX_REPLACE_REGEX, testResourcesUrl);
233
- }
219
+ function injectUI5Url(originalHtml, ui5Configs) {
220
+ let html = originalHtml;
221
+ for (const ui5Config of ui5Configs) {
222
+ const ui5Host = ui5Config.url.replace(/\/$/, '');
223
+ const ui5Url = ui5Config.version ? `${ui5Host}/${ui5Config.version}` : ui5Host;
224
+ if (ui5Config.path === '/resources') {
225
+ const resourcesUrl = `src="${ui5Url}/${constants_1.BOOTSTRAP_LINK}"`;
226
+ html = html.replace(constants_1.BOOTSTRAP_REPLACE_REGEX, resourcesUrl);
227
+ }
228
+ if (ui5Config.path === '/test-resources') {
229
+ const testResourcesUrl = `src="${ui5Url}/${constants_1.SANDBOX_LINK}"`;
230
+ html = html.replace(constants_1.SANDBOX_REPLACE_REGEX, testResourcesUrl);
234
231
  }
235
- return html;
236
- }
237
- else {
238
- return undefined;
239
232
  }
233
+ return html;
240
234
  }
241
235
  exports.injectUI5Url = injectUI5Url;
242
236
  /**
@@ -246,22 +240,19 @@ exports.injectUI5Url = injectUI5Url;
246
240
  * @param res - the http response object
247
241
  * @param next - the next function, used to forward the request to the next available handler
248
242
  * @param ui5Configs - the UI5 configuration of the ui5-proxy-middleware
243
+ * @param rootProject - the root project
249
244
  */
250
- const injectScripts = async (req, res, next, ui5Configs) => {
245
+ const injectScripts = async (req, res, next, ui5Configs, rootProject) => {
251
246
  try {
252
- const projectRoot = process.cwd();
253
- const args = process.argv;
254
- const htmlFileName = (0, exports.getHtmlFile)(req.baseUrl);
255
- const yamlFileName = (0, exports.getYamlFile)(args);
256
- const ui5YamlPath = (0, path_1.join)(projectRoot, yamlFileName);
257
- const webAppFolder = await (0, exports.getWebAppFolderFromYaml)(ui5YamlPath);
258
- const htmlFilePath = (0, path_1.join)(projectRoot, webAppFolder, htmlFileName);
259
- const html = injectUI5Url(htmlFilePath, ui5Configs);
260
- if (html) {
261
- (0, exports.setHtmlResponse)(res, html);
247
+ const htmlFileName = (0, exports.getHtmlFile)(req.url);
248
+ const files = await rootProject.byGlob(`**/${htmlFileName}`);
249
+ if (files.length === 0) {
250
+ next();
262
251
  }
263
252
  else {
264
- next();
253
+ const originalHtml = await files[0].getString();
254
+ const html = injectUI5Url(originalHtml, ui5Configs);
255
+ (0, exports.setHtmlResponse)(res, html);
265
256
  }
266
257
  }
267
258
  catch (error) {
@@ -96,16 +96,14 @@ module.exports = async ({ resources, options }) => {
96
96
  if (directLoad) {
97
97
  const directLoadProxy = (async (req, res, next) => {
98
98
  try {
99
- await (0, base_1.injectScripts)(req, res, next, ui5Configs);
99
+ await (0, base_1.injectScripts)(req, res, next, ui5Configs, resources.rootProject);
100
100
  }
101
101
  catch (error) {
102
102
  logger.error(error);
103
103
  next(error);
104
104
  }
105
105
  });
106
- base_1.HTML_MOUNT_PATHS.forEach((path) => {
107
- routes.push({ route: path, handler: directLoadProxy });
108
- });
106
+ routes.push({ route: '*.html', handler: directLoadProxy });
109
107
  }
110
108
  return createRequestHandler(routes);
111
109
  };
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Aui5-proxy-middleware"
11
11
  },
12
- "version": "1.4.5",
12
+ "version": "1.4.6",
13
13
  "license": "Apache-2.0",
14
14
  "author": "@SAP/ux-tools-team",
15
15
  "main": "dist/index.js",
@@ -26,8 +26,8 @@
26
26
  "https-proxy-agent": "5.0.1",
27
27
  "i18next": "20.6.1",
28
28
  "proxy-from-env": "1.1.0",
29
- "@sap-ux/logger": "0.6.0",
30
- "@sap-ux/ui5-config": "0.24.1"
29
+ "@sap-ux/ui5-config": "0.24.1",
30
+ "@sap-ux/logger": "0.6.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/express": "4.17.21",
@@ -37,7 +37,7 @@
37
37
  "nock": "13.4.0",
38
38
  "supertest": "6.3.3",
39
39
  "yaml": "2.2.2",
40
- "@sap-ux/project-access": "1.26.9"
40
+ "@sap-ux/project-access": "1.27.1"
41
41
  },
42
42
  "ui5": {
43
43
  "dependencies": []