@sap-ux/ui5-proxy-middleware 1.1.9 → 1.1.10

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.
@@ -1,4 +1,4 @@
1
- import type { Options } from 'http-proxy-middleware';
1
+ import type { Filter, Options } from 'http-proxy-middleware';
2
2
  import type { ProxyConfig } from './types';
3
3
  /**
4
4
  * Function for proxying UI5 sources.
@@ -8,5 +8,5 @@ import type { ProxyConfig } from './types';
8
8
  * @param filter - custom filter function which will be applied to all requests
9
9
  * @returns Proxy function to use
10
10
  */
11
- export declare const ui5Proxy: (config: ProxyConfig, options?: Options | undefined, filter?: string | string[] | ((pathname: string, req: import("http-proxy-middleware/dist/types").Request) => boolean) | undefined) => import("http-proxy-middleware").RequestHandler;
11
+ export declare const ui5Proxy: (config: ProxyConfig, options?: Options | undefined, filter?: Filter | undefined) => import("http-proxy-middleware").RequestHandler;
12
12
  //# sourceMappingURL=proxy.d.ts.map
@@ -12,7 +12,7 @@ const logger_1 = require("@sap-ux/logger");
12
12
  * @param filter - custom filter function which will be applied to all requests
13
13
  * @returns Proxy function to use
14
14
  */
15
- exports.ui5Proxy = (config, options, filter) => {
15
+ const ui5Proxy = (config, options, filter) => {
16
16
  const logger = new logger_1.ToolsLogger({
17
17
  transports: [new logger_1.UI5ToolingTransport({ moduleName: 'ui5-proxy-middleware' })]
18
18
  });
@@ -40,4 +40,5 @@ exports.ui5Proxy = (config, options, filter) => {
40
40
  }
41
41
  return http_proxy_middleware_1.createProxyMiddleware(proxyFilter, proxyConfig);
42
42
  };
43
+ exports.ui5Proxy = ui5Proxy;
43
44
  //# sourceMappingURL=proxy.js.map
@@ -41,11 +41,11 @@ export declare const hideProxyCredentials: (proxy: string | undefined) => string
41
41
  /**
42
42
  * Checks if a host is excluded from user's corporate proxy.
43
43
  *
44
- * @param noProxyConfig - user's no_proxy configuration
45
44
  * @param url - url to be checked
45
+ * @param noProxyConfig - user's no_proxy configuration
46
46
  * @returns true if host is excluded from user's corporate server, false otherwise
47
47
  */
48
- export declare const isHostExcludedFromProxy: (noProxyConfig: string | undefined, url: string) => boolean;
48
+ export declare const isHostExcludedFromProxy: (url: string, noProxyConfig?: string | undefined) => boolean;
49
49
  /**
50
50
  * Returns the name of html file, which is used to preview the application, from the URL.
51
51
  *
@@ -22,10 +22,11 @@ const i18n_1 = require("../i18n");
22
22
  * @param proxyRes - proxy response object
23
23
  * @param etag - ETag for the cached sources, normally the UI5 version
24
24
  */
25
- exports.proxyResponseHandler = (proxyRes, etag) => {
25
+ const proxyResponseHandler = (proxyRes, etag) => {
26
26
  proxyRes.headers['Etag'] = etag;
27
27
  proxyRes.headers['cache-control'] = 'no-cache';
28
28
  };
29
+ exports.proxyResponseHandler = proxyResponseHandler;
29
30
  /**
30
31
  * Handler for the proxy request event.
31
32
  * Re-validates the cached UI5 sources based on the ETag.
@@ -36,20 +37,21 @@ exports.proxyResponseHandler = (proxyRes, etag) => {
36
37
  * @param etag - Etag of the cached UI5 sources, normally the UI5 version
37
38
  * @param logger - Logger for loging the requests
38
39
  */
39
- exports.proxyRequestHandler = (proxyReq, res, etag, logger) => {
40
+ const proxyRequestHandler = (proxyReq, res, etag, logger) => {
40
41
  logger.info(proxyReq.path);
41
42
  if (proxyReq.getHeader('if-none-match') === etag) {
42
43
  res.statusCode = 304;
43
44
  res.end();
44
45
  }
45
46
  };
47
+ exports.proxyRequestHandler = proxyRequestHandler;
46
48
  /**
47
49
  * Get user's proxy configuration.
48
50
  *
49
51
  * @param yamlProxyServer - proxy server config from yaml file
50
52
  * @returns User's proxy configuration or undefined
51
53
  */
52
- exports.getCorporateProxyServer = (yamlProxyServer) => {
54
+ const getCorporateProxyServer = (yamlProxyServer) => {
53
55
  return (yamlProxyServer ||
54
56
  process.env.FIORI_TOOLS_PROXY ||
55
57
  process.env.http_proxy ||
@@ -59,13 +61,14 @@ exports.getCorporateProxyServer = (yamlProxyServer) => {
59
61
  process.env.npm_config_proxy ||
60
62
  process.env.npm_config_https_proxy);
61
63
  };
64
+ exports.getCorporateProxyServer = getCorporateProxyServer;
62
65
  /**
63
66
  * Hides the proxy credentials for displaying the proxy configuration in the console.
64
67
  *
65
68
  * @param proxy - user's proxy server
66
69
  * @returns proxy with hidden credentials for displaying in the console
67
70
  */
68
- exports.hideProxyCredentials = (proxy) => {
71
+ const hideProxyCredentials = (proxy) => {
69
72
  if (proxy) {
70
73
  const forwardSlashIndex = proxy.indexOf('//');
71
74
  const atIndex = proxy.indexOf('@');
@@ -75,14 +78,15 @@ exports.hideProxyCredentials = (proxy) => {
75
78
  }
76
79
  return proxy;
77
80
  };
81
+ exports.hideProxyCredentials = hideProxyCredentials;
78
82
  /**
79
83
  * Checks if a host is excluded from user's corporate proxy.
80
84
  *
81
- * @param noProxyConfig - user's no_proxy configuration
82
85
  * @param url - url to be checked
86
+ * @param noProxyConfig - user's no_proxy configuration
83
87
  * @returns true if host is excluded from user's corporate server, false otherwise
84
88
  */
85
- exports.isHostExcludedFromProxy = (noProxyConfig, url) => {
89
+ const isHostExcludedFromProxy = (url, noProxyConfig = process.env.no_proxy || process.env.npm_config_noproxy) => {
86
90
  if (noProxyConfig === '*') {
87
91
  return true;
88
92
  }
@@ -92,13 +96,14 @@ exports.isHostExcludedFromProxy = (noProxyConfig, url) => {
92
96
  return !!noProxyList.find((entry) => entry.startsWith('.') ? host.endsWith(entry) : host.endsWith(`.${entry}`));
93
97
  }
94
98
  };
99
+ exports.isHostExcludedFromProxy = isHostExcludedFromProxy;
95
100
  /**
96
101
  * Returns the name of html file, which is used to preview the application, from the URL.
97
102
  *
98
103
  * @param url - html request url
99
104
  * @returns Name of the html file
100
105
  */
101
- exports.getHtmlFile = (url) => {
106
+ const getHtmlFile = (url) => {
102
107
  let html = url;
103
108
  if (html.indexOf('?') !== -1) {
104
109
  html = html.split('?')[0].replace(/["']/g, '');
@@ -111,13 +116,14 @@ exports.getHtmlFile = (url) => {
111
116
  }
112
117
  return html;
113
118
  };
119
+ exports.getHtmlFile = getHtmlFile;
114
120
  /**
115
121
  * Returns the name of the yaml file, which is used to for the server configuration, from the runtime arguments.
116
122
  *
117
123
  * @param args - runtime arguments
118
124
  * @returns Name of the YAML file
119
125
  */
120
- exports.getYamlFile = (args) => {
126
+ const getYamlFile = (args) => {
121
127
  let yaml = 'ui5.yaml';
122
128
  const index = args.indexOf('--config') !== -1 ? args.indexOf('--config') : args.indexOf('-c');
123
129
  if (index !== -1) {
@@ -125,13 +131,14 @@ exports.getYamlFile = (args) => {
125
131
  }
126
132
  return yaml;
127
133
  };
134
+ exports.getYamlFile = getYamlFile;
128
135
  /**
129
136
  * Gets the path to the webapp folder from the YAML file.
130
137
  *
131
138
  * @param ui5YamlPath - path to the yaml file
132
139
  * @returns Path to the webapp folder
133
140
  */
134
- exports.getWebAppFolderFromYaml = (ui5YamlPath) => __awaiter(void 0, void 0, void 0, function* () {
141
+ const getWebAppFolderFromYaml = (ui5YamlPath) => __awaiter(void 0, void 0, void 0, function* () {
135
142
  var _a, _b;
136
143
  if (fs_1.existsSync(ui5YamlPath)) {
137
144
  const ui5Config = yield ui5_config_1.UI5Config.newInstance(fs_1.readFileSync(ui5YamlPath, { encoding: 'utf8' }));
@@ -141,13 +148,14 @@ exports.getWebAppFolderFromYaml = (ui5YamlPath) => __awaiter(void 0, void 0, voi
141
148
  return 'webapp';
142
149
  }
143
150
  });
151
+ exports.getWebAppFolderFromYaml = getWebAppFolderFromYaml;
144
152
  /**
145
153
  * Sends HTML content as a response.
146
154
  *
147
155
  * @param res - The http response object
148
156
  * @param html - The HTML content
149
157
  */
150
- exports.setHtmlResponse = (res, html) => {
158
+ const setHtmlResponse = (res, html) => {
151
159
  if (res['_livereload']) {
152
160
  res.write(html);
153
161
  res.end();
@@ -156,13 +164,14 @@ exports.setHtmlResponse = (res, html) => {
156
164
  res.status(200).contentType('html').send(html);
157
165
  }
158
166
  };
167
+ exports.setHtmlResponse = setHtmlResponse;
159
168
  /**
160
169
  * Gets the manifest.json for a given application.
161
170
  *
162
171
  * @param args list of runtime arguments
163
172
  * @returns The content of the manifest.json
164
173
  */
165
- exports.getManifest = (args) => __awaiter(void 0, void 0, void 0, function* () {
174
+ const getManifest = (args) => __awaiter(void 0, void 0, void 0, function* () {
166
175
  const projectRoot = process.cwd();
167
176
  const yamlFileName = exports.getYamlFile(args);
168
177
  const ui5YamlPath = path_1.join(projectRoot, yamlFileName);
@@ -171,6 +180,7 @@ exports.getManifest = (args) => __awaiter(void 0, void 0, void 0, function* () {
171
180
  const manifest = JSON.parse(fs_1.readFileSync(manifestPath, { encoding: 'utf8' }));
172
181
  return manifest;
173
182
  });
183
+ exports.getManifest = getManifest;
174
184
  /**
175
185
  * Gets the minUI5Version from the manifest.json.
176
186
  *
@@ -255,7 +265,7 @@ exports.injectUI5Url = injectUI5Url;
255
265
  * @param next - the next function, used to forward the request to the next available handler
256
266
  * @param ui5Configs - the UI5 configuration of the ui5-proxy-middleware
257
267
  */
258
- exports.injectScripts = (req, res, next, ui5Configs) => __awaiter(void 0, void 0, void 0, function* () {
268
+ const injectScripts = (req, res, next, ui5Configs) => __awaiter(void 0, void 0, void 0, function* () {
259
269
  try {
260
270
  const projectRoot = process.cwd();
261
271
  const args = process.argv;
@@ -276,6 +286,7 @@ exports.injectScripts = (req, res, next, ui5Configs) => __awaiter(void 0, void 0
276
286
  next(error);
277
287
  }
278
288
  });
289
+ exports.injectScripts = injectScripts;
279
290
  /**
280
291
  * Filters comressed html files from UI5 CDN.
281
292
  * Avoid ERR_CONTENT_DECODING_FAILED on http request for gzip'd html files.
@@ -285,7 +296,7 @@ exports.injectScripts = (req, res, next, ui5Configs) => __awaiter(void 0, void 0
285
296
  * @param req - the http request object
286
297
  * @returns True, indicating that the request should be proxied
287
298
  */
288
- exports.filterCompressedHtmlFiles = (_pathname, req) => {
299
+ const filterCompressedHtmlFiles = (_pathname, req) => {
289
300
  const acceptHeader = req.headers['accept'] || '';
290
301
  if (req.headers['accept-encoding'] &&
291
302
  (acceptHeader.includes('text/html') || acceptHeader.includes('application/xhtml+xml'))) {
@@ -293,6 +304,7 @@ exports.filterCompressedHtmlFiles = (_pathname, req) => {
293
304
  }
294
305
  return true;
295
306
  };
307
+ exports.filterCompressedHtmlFiles = filterCompressedHtmlFiles;
296
308
  /**
297
309
  * Specifically handling errors due to undefined and empty errors.
298
310
  *
@@ -12,35 +12,57 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const express_1 = __importDefault(require("express"));
16
15
  const https_proxy_agent_1 = require("https-proxy-agent");
17
16
  const logger_1 = require("@sap-ux/logger");
18
17
  const base_1 = require("../base");
19
18
  const dotenv_1 = __importDefault(require("dotenv"));
19
+ /**
20
+ * Create proxy options based on the middleware config.
21
+ *
22
+ * @param logger logger to be used when running the middleware
23
+ * @param config middleware configuration
24
+ * @returns options object
25
+ */
26
+ function createProxyOptions(logger, config) {
27
+ return {
28
+ secure: config.secure !== undefined ? !!config.secure : true,
29
+ logLevel: !!config.debug ? 'debug' : 'info',
30
+ logProvider: () => logger
31
+ };
32
+ }
33
+ /**
34
+ * Creates a function handling the routes the need to be rerouted to a UI5 provider.
35
+ *
36
+ * @param routes routes that need to be handled
37
+ * @returns handler function
38
+ */
39
+ function createRequestHandler(routes) {
40
+ return (req, res, next) => {
41
+ for (const route of routes) {
42
+ if (req.path.startsWith(route.route)) {
43
+ return route.handler(req, res, next);
44
+ }
45
+ }
46
+ next();
47
+ };
48
+ }
20
49
  module.exports = ({ options }) => __awaiter(void 0, void 0, void 0, function* () {
21
50
  const logger = new logger_1.ToolsLogger({
22
51
  transports: [new logger_1.UI5ToolingTransport({ moduleName: 'ui5-proxy-middleware' })]
23
52
  });
24
53
  dotenv_1.default.config();
25
- const router = express_1.default.Router();
26
54
  const config = options.configuration;
27
55
  const ui5Version = yield base_1.resolveUI5Version(config.version, logger);
28
56
  const envUI5Url = process.env.FIORI_TOOLS_UI5_URI;
29
- const secure = config.secure !== undefined ? !!config.secure : true;
30
- const debug = !!config.debug;
31
57
  const directLoad = !!config.directLoad;
32
- const noProxyVal = process.env.no_proxy || process.env.npm_config_noproxy;
33
58
  const corporateProxyServer = base_1.getCorporateProxyServer(config.proxy);
34
59
  // hide user and pass from proxy configuration for displaying it in the terminal
35
60
  const proxyInfo = base_1.hideProxyCredentials(corporateProxyServer);
36
- const proxyOptions = {
37
- secure,
38
- logLevel: debug ? 'debug' : 'info',
39
- logProvider: () => logger
40
- };
41
- logger.info(`Starting ui5-proxy-middleware using following configuration:\nproxy: '${proxyInfo}'\nsecure: '${secure}'\ndebug: '${debug}''\ndirectLoad: '${directLoad}'`);
61
+ const proxyOptions = createProxyOptions(logger, config);
62
+ logger.info(`Starting ui5-proxy-middleware using following configuration:\nproxy: '${proxyInfo}'\nsecure: '${proxyOptions.secure}'\nlog: '${proxyOptions.logLevel}''\ndirectLoad: '${directLoad}'`);
42
63
  const configs = Array.isArray(config.ui5) ? config.ui5 : [config.ui5];
43
64
  const ui5Configs = [];
65
+ const routes = [];
44
66
  for (const ui5 of configs) {
45
67
  const paths = Array.isArray(ui5.path) ? ui5.path : [ui5.path];
46
68
  for (const ui5Path of paths) {
@@ -49,18 +71,21 @@ module.exports = ({ options }) => __awaiter(void 0, void 0, void 0, function* ()
49
71
  url: envUI5Url || ui5.url,
50
72
  version: ui5Version
51
73
  };
52
- if (corporateProxyServer && !base_1.isHostExcludedFromProxy(noProxyVal, ui5Config.url)) {
74
+ if (corporateProxyServer && !base_1.isHostExcludedFromProxy(ui5Config.url)) {
53
75
  proxyOptions.agent = new https_proxy_agent_1.HttpsProxyAgent(corporateProxyServer);
54
76
  }
55
- router.use(ui5Config.path, base_1.ui5Proxy(ui5Config, proxyOptions));
77
+ routes.push({ route: ui5Config.path, handler: base_1.ui5Proxy(ui5Config, proxyOptions) });
56
78
  ui5Configs.push(ui5Config);
57
79
  }
58
80
  }
59
81
  if (directLoad) {
60
- router.use(base_1.HTML_MOUNT_PATHS, (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
82
+ const directLoadProxy = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
61
83
  yield base_1.injectScripts(req, res, next, ui5Configs);
62
- }));
84
+ });
85
+ base_1.HTML_MOUNT_PATHS.forEach((path) => {
86
+ routes.push({ route: path, handler: directLoadProxy });
87
+ });
63
88
  }
64
- return router;
89
+ return createRequestHandler(routes);
65
90
  });
66
91
  //# sourceMappingURL=middleware.js.map
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.1.9",
12
+ "version": "1.1.10",
13
13
  "license": "Apache-2.0",
14
14
  "author": "@SAP/ux-tools-team",
15
15
  "main": "dist/index.js",
@@ -24,13 +24,13 @@
24
24
  "@sap-ux/logger": "0.3.0",
25
25
  "@sap-ux/ui5-config": "0.15.0",
26
26
  "dotenv": "16.0.0",
27
- "express": "4.17.2",
28
27
  "http-proxy-middleware": "2.0.1",
29
28
  "https-proxy-agent": "5.0.0",
30
29
  "i18next": "20.3.2"
31
30
  },
32
31
  "devDependencies": {
33
32
  "@types/express": "4.17.13",
33
+ "express": "4.17.2",
34
34
  "nock": "13.2.4",
35
35
  "supertest": "6.2.2",
36
36
  "@types/supertest": "2.0.12",