@redhat-cloud-services/frontend-components-config-utilities 1.5.15 → 1.5.18

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.15",
3
+ "version": "1.5.18",
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,15 @@ 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
+ if (isStage || (isProd && useAgent)) {
64
+ // stage is deployed with Akamai which requires a corporate proxy
58
65
  agent = new HttpsProxyAgent(proxyURL);
59
66
  }
60
67
 
61
- if (env.startsWith('stage')) {
68
+ if (isStage) {
62
69
  // stage-stable / stage-beta branches don't exist in build repos
63
70
  // Currently stage pulls from QA
64
71
  env = env.replace('stage', 'qa');
@@ -185,19 +192,37 @@ module.exports = ({
185
192
  return false;
186
193
  },
187
194
  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 '/';
195
+ bypass: async (req, res) => {
196
+ /**
197
+ * Bypass any HTML requests if using chrome
198
+ * Serves as a historyApiFallback when refreshing on any other URL than '/'
199
+ */
200
+ if (isChrome && !req.url.match(/\/api\//) && !req.url.match(/\./) && req.headers.accept.includes('text/html')) {
201
+ return '/';
202
+ }
203
+
204
+ /**
205
+ * Use node-fetch to proxy all non-GET requests (this avoids all origin/host akamai policy)
206
+ * This enables using PROD proxy without VPN and agent
207
+ */
208
+ if (isProd && bounceProd && !useAgent && req.method !== 'GET') {
209
+ const result = await fetch((target + req.url).replace(/\/\//g, '/'), {
210
+ method: req.method,
211
+ body: JSON.stringify(req.body),
212
+ headers: { cookie: req.headers.cookie, 'Content-Type': 'application/json' },
213
+ });
214
+
215
+ const text = await result.text();
216
+ try {
217
+ const data = JSON.parse(text);
218
+ res.status(result.status).json(data);
219
+ } catch (err) {
220
+ res.status(result.status).send(text);
196
221
  }
222
+ }
197
223
 
198
- return null;
199
- },
200
- }),
224
+ return null;
225
+ },
201
226
  router: router(target, useCloud),
202
227
  ...(agent && {
203
228
  agent,
@@ -235,6 +260,10 @@ module.exports = ({
235
260
  },
236
261
  onBeforeSetupMiddleware({ app, compiler, options }) {
237
262
  app.enable('strict routing'); // trailing slashes are mean
263
+
264
+ app.use(express.json());
265
+ app.use(express.urlencoded({ extended: true }));
266
+
238
267
  /**
239
268
  * Allow serving chrome assets
240
269
  * This will allow running chrome as a host application
@@ -3,11 +3,11 @@ const fs = require('fs');
3
3
  const concurrently = require('concurrently');
4
4
 
5
5
  function federate(argv, cwd) {
6
- let configPath = argv.config;
6
+ let configPath = argv.config || './node_modules/@redhat-cloud-services/frontend-components-config/src/scripts/prod.webpack.config.js';
7
7
  const WEBPACK_PATH = path.resolve(cwd, './node_modules/.bin/webpack');
8
8
  const HTTP_SERVER_PATH = path.resolve(cwd, './node_modules/.bin/http-server');
9
9
  if (typeof configPath !== 'string') {
10
- console.error('Missing webpack config path');
10
+ console.error('Invalid webpack config path!');
11
11
  process.exit(1);
12
12
  }
13
13
 
@@ -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
  });