@quintype/framework 7.31.0 → 7.31.9-host-to-api.2

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.
@@ -8,6 +8,6 @@
8
8
  "files.insertFinalNewline": true,
9
9
  "files.trimTrailingWhitespace": true,
10
10
  "editor.codeActionsOnSave": {
11
- "source.organizeImports": true
11
+ "source.organizeImports": "explicit"
12
12
  }
13
13
  }
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash -e
2
2
 
3
- npm install
3
+ npm install --legacy-peer-deps
4
4
  git diff --quiet
5
5
  npm run docs
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/framework",
3
- "version": "7.31.0",
3
+ "version": "7.31.9-host-to-api.2",
4
4
  "description": "Libraries to help build Quintype Node.js apps",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -32,7 +32,7 @@
32
32
  "dependencies": {
33
33
  "@ampproject/toolbox-optimizer": "2.8.3",
34
34
  "@quintype/amp": "^2.20.0",
35
- "@quintype/backend": "^2.6.0",
35
+ "@quintype/backend": "2.6.0-host-to-api.2",
36
36
  "@quintype/components": "^3.5.0",
37
37
  "@quintype/prerender-node": "^3.2.26",
38
38
  "@quintype/seo": "^1.46.1",
@@ -11,7 +11,6 @@
11
11
  */
12
12
  // istanbul ignore file
13
13
 
14
- const config = require("./publisher-config");
15
14
  const {
16
15
  getClientImpl,
17
16
  Client,
@@ -26,19 +25,35 @@ const {
26
25
  AmpConfig,
27
26
  } = require("./impl/api-client-impl");
28
27
 
28
+ let config = require("./publisher-config");
29
+
29
30
  const defaultClient = new Client(config.sketches_host);
30
31
  const cachedSecondaryClients = {};
31
32
 
33
+ const xHostAPIToken = config["x_host_mapping_token"];
34
+ const hostInternalDomain = config["x_host_internal_domain"];
35
+ const enableSketchesHostResolution = config["x_enable_sketches_host_resolution"];
36
+
37
+ async function mappingHost() {
38
+ if (enableSketchesHostResolution && xHostAPIToken && hostInternalDomain) {
39
+ const hostMapping = {};
40
+ const sketchesHostMapping = await defaultClient.getHostToAPIMappingCache(xHostAPIToken);
41
+
42
+ for (const [key, value] of Object.entries(sketchesHostMapping)) {
43
+ hostMapping[key] = `http://${value}${hostInternalDomain}`;
44
+ }
45
+ config.host_to_api_host = { ...hostMapping, ...config["host_to_api_host"] };
46
+ }
47
+ }
48
+
32
49
  function getClient(hostname) {
33
- return (
34
- getClientImpl(config, cachedSecondaryClients, hostname) || defaultClient
35
- );
50
+ return getClientImpl(config, cachedSecondaryClients, hostname) || defaultClient;
36
51
  }
37
52
 
38
53
  function initializeAllClients() {
39
54
  const promises = [defaultClient.getConfig()];
40
55
  if (!config.skip_warm_config) {
41
- Object.entries(config.host_to_api_host || []).forEach(([host, apiHost]) => {
56
+ Object.entries(config["host_to_api_host"] || []).forEach(([host, apiHost]) => {
42
57
  const client = new Client(apiHost);
43
58
  cachedSecondaryClients[host] = client;
44
59
  promises.push(client.getConfig());
@@ -57,8 +72,8 @@ module.exports = {
57
72
  MenuGroups,
58
73
  Config,
59
74
  AmpConfig,
60
-
61
75
  client: defaultClient,
62
76
  getClient,
63
77
  initializeAllClients,
78
+ mappingHost
64
79
  };
@@ -10,6 +10,7 @@
10
10
  */
11
11
  // istanbul ignore file
12
12
 
13
+
13
14
  const fs = require("fs");
14
15
  const yaml = require("js-yaml");
15
16
 
package/server/start.js CHANGED
@@ -10,10 +10,10 @@
10
10
  // istanbul ignore file
11
11
  // This is the start file, to be called from your start.js
12
12
 
13
- const chalk = require('chalk');
13
+ const chalk = require("chalk");
14
14
  const cluster = require("cluster");
15
15
  const process = require("process");
16
- const { initializeAllClients } = require("./api-client");
16
+ const { initializeAllClients, mappingHost } = require("./api-client");
17
17
  const logger = require("./logger");
18
18
  const logSuccess = chalk.bold.cyanBright;
19
19
 
@@ -48,9 +48,7 @@ function startMaster({ workers = 4 }) {
48
48
 
49
49
  cluster.on("exit", (worker, code, signal) => {
50
50
  logger.error(`worker ${worker.process.pid} died`);
51
- const aliveWorkers = Object.values(cluster.workers).filter(
52
- (worker) => worker.state !== "dead"
53
- );
51
+ const aliveWorkers = Object.values(cluster.workers).filter((worker) => worker.state !== "dead");
54
52
 
55
53
  if (terminating) {
56
54
  if (aliveWorkers.length == 0) {
@@ -64,6 +62,8 @@ function startMaster({ workers = 4 }) {
64
62
  }
65
63
 
66
64
  async function startWorker(appThunk, opts) {
65
+ let setIntervalIndex = null;
66
+ const { hostToApiInterval } = opts;
67
67
  if (process.env.NODE_ENV !== "production") {
68
68
  require("@quintype/build")(opts);
69
69
  }
@@ -72,10 +72,11 @@ async function startWorker(appThunk, opts) {
72
72
  try {
73
73
  const app = appThunk();
74
74
 
75
+ await mappingHost();
75
76
  await initializeAllClients();
76
77
  const server = app.listen(opts.port || 3000, () => {
77
78
  console.log(logSuccess(`||=============================||`));
78
- console.log(logSuccess(`|| App listening on port ${opts.port || 3000}! ||`))
79
+ console.log(logSuccess(`|| App listening on port ${opts.port || 3000}! ||`));
79
80
  console.log(logSuccess(`||=============================||`));
80
81
  });
81
82
 
@@ -86,7 +87,14 @@ async function startWorker(appThunk, opts) {
86
87
  });
87
88
  });
88
89
  process.on("SIGHUP", () => {});
90
+
91
+ if (hostToApiInterval) {
92
+ setIntervalIndex = setInterval(() => {
93
+ mappingHost();
94
+ }, hostToApiInterval);
95
+ }
89
96
  } catch (e) {
97
+ clearInterval(setIntervalIndex);
90
98
  if (process.env.NODE_ENV !== "production") {
91
99
  console.log(e.stack);
92
100
  }