@salesforce/pwa-kit-runtime 3.8.0-preview.0-basepath → 3.8.0-preview.2-basepath

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.
Files changed (52) hide show
  1. package/package.json +6 -5
  2. package/ssr/server/build-remote-server.js +1159 -0
  3. package/ssr/server/build-remote-server.test.js +41 -0
  4. package/ssr/server/constants.js +37 -0
  5. package/ssr/server/express.js +462 -0
  6. package/ssr/server/express.lambda.test.js +390 -0
  7. package/ssr/server/express.test.js +963 -0
  8. package/ssr/server/test_fixtures/favicon.ico +0 -0
  9. package/ssr/server/test_fixtures/loadable-stats.json +1 -0
  10. package/ssr/server/test_fixtures/localhost.pem +45 -0
  11. package/ssr/server/test_fixtures/main.js +7 -0
  12. package/ssr/server/test_fixtures/mobify.png +0 -0
  13. package/ssr/server/test_fixtures/server-renderer.js +12 -0
  14. package/ssr/server/test_fixtures/worker.js +7 -0
  15. package/ssr/server/test_fixtures/worker.js.map +1 -0
  16. package/utils/logger-factory.js +154 -0
  17. package/utils/logger-factory.test.js +71 -0
  18. package/utils/logger-instance.js +19 -0
  19. package/utils/middleware/index.js +16 -0
  20. package/utils/middleware/security.js +111 -0
  21. package/utils/middleware/security.test.js +110 -0
  22. package/utils/morgan-stream.js +28 -0
  23. package/utils/ssr-cache.js +177 -0
  24. package/utils/ssr-cache.test.js +64 -0
  25. package/utils/ssr-config.client.js +23 -0
  26. package/utils/ssr-config.client.test.js +25 -0
  27. package/utils/ssr-config.js +20 -0
  28. package/utils/ssr-config.server.js +98 -0
  29. package/utils/ssr-config.server.test.js +50 -0
  30. package/utils/ssr-namespace-paths.js +60 -0
  31. package/utils/ssr-namespace-paths.test.js +30 -0
  32. package/utils/ssr-paths.js +51 -0
  33. package/utils/ssr-paths.test.js +49 -0
  34. package/utils/ssr-proxying.js +859 -0
  35. package/utils/ssr-proxying.test.js +593 -0
  36. package/utils/ssr-request-processing.js +164 -0
  37. package/utils/ssr-request-processing.test.js +95 -0
  38. package/utils/ssr-server/cached-response.js +116 -0
  39. package/utils/ssr-server/configure-proxy.js +304 -0
  40. package/utils/ssr-server/metrics-sender.js +204 -0
  41. package/utils/ssr-server/outgoing-request-hook.js +139 -0
  42. package/utils/ssr-server/parse-end-parameters.js +38 -0
  43. package/utils/ssr-server/process-express-response.js +56 -0
  44. package/utils/ssr-server/process-lambda-response.js +43 -0
  45. package/utils/ssr-server/update-global-agent-options.js +41 -0
  46. package/utils/ssr-server/update-global-agent-options.test.js +28 -0
  47. package/utils/ssr-server/utils.js +124 -0
  48. package/utils/ssr-server/utils.test.js +69 -0
  49. package/utils/ssr-server/wrap-response-write.js +40 -0
  50. package/utils/ssr-server.js +115 -0
  51. package/utils/ssr-server.test.js +869 -0
  52. package/utils/ssr-shared.js +183 -0
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.validProxyProtocols = exports.updatePackageMobify = exports.ssrFiles = exports.reset = exports.proxyConfigs = exports.getSSRParameters = exports.getPackageMobify = exports.MAX_PROXY_CONFIGS = void 0;
7
+ var _ssrPaths = require("./ssr-paths");
8
+ /*
9
+ * Copyright (c) 2021, salesforce.com, inc.
10
+ * All rights reserved.
11
+ * SPDX-License-Identifier: BSD-3-Clause
12
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
13
+ */
14
+
15
+ /**
16
+ * @module progressive-web-sdk/utils/ssr-shared
17
+ * @private
18
+ */
19
+ /*
20
+ The ssr-shared-utils module is used in the PWA and in the Express app. It
21
+ should contain ONLY the code that is required in both, to avoid adding
22
+ to the size of vendor.js
23
+ */
24
+
25
+ /**
26
+ * Set of valid HTTP protocols for proxying
27
+ * @private
28
+ * @type {string[]}
29
+ */
30
+ const validProxyProtocols = exports.validProxyProtocols = ['http', 'https'];
31
+
32
+ // This value is the 'mobify' object from package.json. It should be accessed
33
+ // via getPackageMobify() so that test code can update the value to test
34
+ // specific functions.
35
+ let _packageMobify;
36
+ const getPackageMobify = () => _packageMobify || {};
37
+ exports.getPackageMobify = getPackageMobify;
38
+ const getSSRParameters = () => _packageMobify && _packageMobify.ssrParameters || {};
39
+
40
+ /**
41
+ * Maximum number of proxy configs
42
+ * @private
43
+ * @type {number}
44
+ */
45
+ exports.getSSRParameters = getSSRParameters;
46
+ const MAX_PROXY_CONFIGS = exports.MAX_PROXY_CONFIGS = 8;
47
+
48
+ /**
49
+ * An Array of objects with protocol, host, path for each configured proxy.
50
+ * This is *only* usable server-side, where it's set when the SSRServer
51
+ * calls updatePackageMobify.
52
+ *
53
+ * The objects in this array also have the following additional properties,
54
+ * added by configureProxyConfigs in utils/ssr-server:
55
+ * proxyPath is the full /mobify/proxy... path
56
+ * proxy is an http-proxy-middleware function that can be used to proxy a
57
+ * request to a target (it's an ExpressJS function taking req, res and next)
58
+ *
59
+ * @private
60
+ * @type {Array}
61
+ */
62
+ let proxyConfigs = exports.proxyConfigs = [];
63
+
64
+ /**
65
+ * An Array with all the glob patterns from ssrOnly and ssrShared.
66
+ * The names are relative to the 'build' directory.
67
+ * @private
68
+ */
69
+ let ssrFiles = exports.ssrFiles = [];
70
+
71
+ /**
72
+ * RegExp that matches a proxy override string
73
+ * match[1] is the protocol
74
+ * match[3] is the host
75
+ * match[5] (optional) is the path
76
+ * match[6] (optional) is '/caching' for a caching proxy, undefined for a standard proxy
77
+ * @private
78
+ * @type {RegExp}
79
+ */
80
+ const proxyOverrideRE = /^(http(s)?):\/\/([^/]+)(\/)?([^/]+)?(\/caching)?/;
81
+
82
+ /**
83
+ * Updates the value of _packageMobify and dependent values.
84
+ *
85
+ * @private
86
+ * @param newValue {Object} the new value of the Mobify object
87
+ */
88
+ const updatePackageMobify = newValue => {
89
+ _packageMobify = newValue || _packageMobify || {};
90
+
91
+ // Clear and update the proxyConfigs array
92
+ exports.proxyConfigs = proxyConfigs = [];
93
+ const ssrParameters = getSSRParameters();
94
+ const newFormatConfigs = ssrParameters.proxyConfigs || [];
95
+ if (newFormatConfigs.length && (ssrParameters.proxyHost1 || ssrParameters.proxyHost2)) {
96
+ throw new Error('Cannot use both proxyConfigs and old proxy declarations in ssrParameters');
97
+ }
98
+ if (newFormatConfigs.length > MAX_PROXY_CONFIGS) {
99
+ throw new Error(`Cannot define more than ${MAX_PROXY_CONFIGS} proxy configurations`);
100
+ }
101
+
102
+ // Make proxyConfigs an Array containing all the valid proxy configs
103
+ for (let index0 = 0; index0 < MAX_PROXY_CONFIGS; index0++) {
104
+ const index1 = index0 + 1;
105
+ let config;
106
+ const defaultPath = index0 ? `base${index1}` : 'base';
107
+
108
+ // Support the OLD format of proxy configs.
109
+ const oldHost = ssrParameters[`proxyHost${index1}`];
110
+ if (oldHost) {
111
+ config = {
112
+ /* istanbul ignore else */
113
+ protocol: ssrParameters[`proxyProtocol${index1}`],
114
+ host: oldHost,
115
+ /* istanbul ignore else */
116
+ path: ssrParameters[`proxyPath${index1}`]
117
+ };
118
+ } else {
119
+ // New format of proxy configs
120
+ config = newFormatConfigs[index0]; // may be undefined
121
+ }
122
+
123
+ // Allow override via environment variable
124
+ const overrideKey = `SSR_PROXY${index1}`;
125
+ const override = process.env[overrideKey];
126
+ let overridden = '';
127
+ if (override) {
128
+ const match = proxyOverrideRE.exec(override);
129
+ /* istanbul ignore else */
130
+ if (match) {
131
+ config = {
132
+ protocol: match[1],
133
+ host: match[3],
134
+ path: match[5] || config && config.path
135
+ };
136
+ overridden = ` (overridden by ${overrideKey}="${override}")`;
137
+ }
138
+ }
139
+
140
+ // If there's no config for this index, try the next one.
141
+ if (!config) {
142
+ continue;
143
+ }
144
+
145
+ // Apply defaults
146
+ if (!config.protocol) {
147
+ config.protocol = 'https';
148
+ }
149
+ if (!config.path) {
150
+ config.path = defaultPath;
151
+ }
152
+
153
+ // Validate
154
+ const msg = `Proxy config ${index1}${overridden}`;
155
+ if (!config.host) {
156
+ throw new Error(`${msg} has missing 'host' property`);
157
+ }
158
+ if (validProxyProtocols.indexOf(config.protocol) < 0) {
159
+ throw new Error(`${msg} has invalid protocol '${config.protocol}'`);
160
+ }
161
+ const existing = proxyConfigs.findIndex(conf => conf.path === config.path);
162
+ if (existing >= 0) {
163
+ throw new Error(`${msg} ` + `has path "${config.path}" ` + `but that has already been used in config ${existing}`);
164
+ }
165
+
166
+ // Generate paths
167
+ config.proxyPath = `${(0, _ssrPaths.getProxyPath)()}/${config.path}`;
168
+ config.cachingPath = `${(0, _ssrPaths.getCachingPath)()}/${config.path}`;
169
+ proxyConfigs.push(config);
170
+ }
171
+
172
+ // Clear and reset the ssrFiles array
173
+ exports.ssrFiles = ssrFiles = (_packageMobify.ssrOnly || []).concat(_packageMobify.ssrShared || []);
174
+ };
175
+
176
+ // For testing
177
+ exports.updatePackageMobify = updatePackageMobify;
178
+ const reset = () => {
179
+ _packageMobify = undefined;
180
+ exports.proxyConfigs = proxyConfigs = [];
181
+ exports.ssrFiles = ssrFiles = [];
182
+ };
183
+ exports.reset = reset;