@redhat-cloud-services/frontend-components-config-utilities 1.5.16 → 1.5.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redhat-cloud-services/frontend-components-config-utilities",
3
- "version": "1.5.16",
3
+ "version": "1.5.19",
4
4
  "description": "Utilities for shared config used in RedHat Cloud Services project.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -18,5 +18,8 @@
18
18
  "homepage": "https://github.com/RedHatInsights/frontend-components#readme",
19
19
  "peerDependencies": {
20
20
  "webpack": "^5.0.0"
21
+ },
22
+ "dependencies": {
23
+ "node-fetch": "2.6.7"
21
24
  }
22
25
  }
package/proxy.js CHANGED
@@ -1,6 +1,8 @@
1
1
  /* eslint-disable no-console */
2
2
  // Webpack proxy and express config for `useProxy: true` or `standalone: true`
3
3
  const { execSync } = require('child_process');
4
+ const fetch = require('node-fetch');
5
+ var express = require('express');
4
6
  const path = require('path');
5
7
  const HttpsProxyAgent = require('https-proxy-agent');
6
8
  const cookieTransform = require('./cookieTransform');
@@ -34,6 +36,8 @@ module.exports = ({
34
36
  registry = [],
35
37
  isChrome = false,
36
38
  onBeforeSetupMiddleware = () => {},
39
+ bounceProd = false,
40
+ useAgent = true,
37
41
  }) => {
38
42
  const proxy = [];
39
43
  const majorEnv = env.split('-')[0];
@@ -53,12 +57,17 @@ module.exports = ({
53
57
 
54
58
  let agent;
55
59
 
56
- if (env.startsWith('prod') || env.startsWith('stage')) {
57
- // PROD and stage are deployed with Akamai which requires a corporate proxy
60
+ const isProd = env.startsWith('prod');
61
+ const isStage = env.startsWith('stage');
62
+
63
+ const shouldBounceProdRequests = isProd && bounceProd && !useAgent;
64
+
65
+ if (isStage || (isProd && useAgent)) {
66
+ // stage is deployed with Akamai which requires a corporate proxy
58
67
  agent = new HttpsProxyAgent(proxyURL);
59
68
  }
60
69
 
61
- if (env.startsWith('stage')) {
70
+ if (isStage) {
62
71
  // stage-stable / stage-beta branches don't exist in build repos
63
72
  // Currently stage pulls from QA
64
73
  env = env.replace('stage', 'qa');
@@ -185,19 +194,37 @@ module.exports = ({
185
194
  return false;
186
195
  },
187
196
  target,
188
- ...(isChrome && {
189
- bypass: (req) => {
190
- /**
191
- * Bypass any HTML requests if using chrome
192
- * Serves as a historyApiFallback when refreshing on any other URL than '/'
193
- */
194
- if (!req.url.match(/\/api\//) && !req.url.match(/\./) && req.headers.accept.includes('text/html')) {
195
- return '/';
197
+ bypass: async (req, res) => {
198
+ /**
199
+ * Bypass any HTML requests if using chrome
200
+ * Serves as a historyApiFallback when refreshing on any other URL than '/'
201
+ */
202
+ if (isChrome && !req.url.match(/\/api\//) && !req.url.match(/\./) && req.headers.accept.includes('text/html')) {
203
+ return '/';
204
+ }
205
+
206
+ /**
207
+ * Use node-fetch to proxy all non-GET requests (this avoids all origin/host akamai policy)
208
+ * This enables using PROD proxy without VPN and agent
209
+ */
210
+ if (shouldBounceProdRequests && req.method !== 'GET') {
211
+ const result = await fetch((target + req.url).replace(/\/\//g, '/'), {
212
+ method: req.method,
213
+ body: JSON.stringify(req.body),
214
+ headers: { cookie: req.headers.cookie, 'Content-Type': 'application/json' },
215
+ });
216
+
217
+ const text = await result.text();
218
+ try {
219
+ const data = JSON.parse(text);
220
+ res.status(result.status).json(data);
221
+ } catch (err) {
222
+ res.status(result.status).send(text);
196
223
  }
224
+ }
197
225
 
198
- return null;
199
- },
200
- }),
226
+ return null;
227
+ },
201
228
  router: router(target, useCloud),
202
229
  ...(agent && {
203
230
  agent,
@@ -235,6 +262,12 @@ module.exports = ({
235
262
  },
236
263
  onBeforeSetupMiddleware({ app, compiler, options }) {
237
264
  app.enable('strict routing'); // trailing slashes are mean
265
+
266
+ if (shouldBounceProdRequests) {
267
+ app.use(express.json());
268
+ app.use(express.urlencoded({ extended: true }));
269
+ }
270
+
238
271
  /**
239
272
  * Allow serving chrome assets
240
273
  * This will allow running chrome as a host application
@@ -19,17 +19,25 @@ module.exports = ({ env }) => ({
19
19
  'entitlements-config': `https://github.com/redhatinsights/entitlements-config#${getEntitlementsBranch(env)}`
20
20
  },
21
21
  register({ app, config }) {
22
- app.get('/api/entitlements/v1/services', (_req, res) => {
23
- const configPath = path.join(config.entitlements.assets['entitlements-config'], '/configs/bundles.yml');
24
- // Use { json: true } in case of duplicate keys
25
- const serviceSKUs = yaml.load(fs.readFileSync(configPath, 'utf8'), { json: true });
26
- const services = serviceSKUs.map(serviceSKU => serviceSKU.name);
27
- // Grant access to all services
28
- const entitlements = services.reduce((acc, cur) => {
29
- acc[cur] = { is_entitled: true, is_trial: false };
30
- return acc;
31
- }, {});
32
- res.json(entitlements);
33
- });
34
- }
22
+ app.get('/api/entitlements/v1/services', (_req, res) => {
23
+ try {
24
+ const configPath = path.join(config.entitlements.assets['entitlements-config'], '/configs/stage/bundles.yml');
25
+
26
+ // Use { json: true } in case of duplicate keys
27
+ const outerYaml = yaml.load(fs.readFileSync(configPath, 'utf8'), { json: true });
28
+ const serviceSKUs = yaml.load(outerYaml.objects[0].data['bundles.yml'], { json: true });
29
+
30
+ const services = serviceSKUs.map(serviceSKU => serviceSKU.name);
31
+ // Grant access to all services
32
+ const entitlements = services.reduce((acc, cur) => {
33
+ acc[cur] = { is_entitled: true, is_trial: false };
34
+ return acc;
35
+ }, {});
36
+
37
+ res.json(entitlements);
38
+ } catch (error) {
39
+ res.status(500).json({ error });
40
+ }
41
+ });
42
+ },
35
43
  });