@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.
- package/package.json +6 -5
- package/ssr/server/build-remote-server.js +1159 -0
- package/ssr/server/build-remote-server.test.js +41 -0
- package/ssr/server/constants.js +37 -0
- package/ssr/server/express.js +462 -0
- package/ssr/server/express.lambda.test.js +390 -0
- package/ssr/server/express.test.js +963 -0
- package/ssr/server/test_fixtures/favicon.ico +0 -0
- package/ssr/server/test_fixtures/loadable-stats.json +1 -0
- package/ssr/server/test_fixtures/localhost.pem +45 -0
- package/ssr/server/test_fixtures/main.js +7 -0
- package/ssr/server/test_fixtures/mobify.png +0 -0
- package/ssr/server/test_fixtures/server-renderer.js +12 -0
- package/ssr/server/test_fixtures/worker.js +7 -0
- package/ssr/server/test_fixtures/worker.js.map +1 -0
- package/utils/logger-factory.js +154 -0
- package/utils/logger-factory.test.js +71 -0
- package/utils/logger-instance.js +19 -0
- package/utils/middleware/index.js +16 -0
- package/utils/middleware/security.js +111 -0
- package/utils/middleware/security.test.js +110 -0
- package/utils/morgan-stream.js +28 -0
- package/utils/ssr-cache.js +177 -0
- package/utils/ssr-cache.test.js +64 -0
- package/utils/ssr-config.client.js +23 -0
- package/utils/ssr-config.client.test.js +25 -0
- package/utils/ssr-config.js +20 -0
- package/utils/ssr-config.server.js +98 -0
- package/utils/ssr-config.server.test.js +50 -0
- package/utils/ssr-namespace-paths.js +60 -0
- package/utils/ssr-namespace-paths.test.js +30 -0
- package/utils/ssr-paths.js +51 -0
- package/utils/ssr-paths.test.js +49 -0
- package/utils/ssr-proxying.js +859 -0
- package/utils/ssr-proxying.test.js +593 -0
- package/utils/ssr-request-processing.js +164 -0
- package/utils/ssr-request-processing.test.js +95 -0
- package/utils/ssr-server/cached-response.js +116 -0
- package/utils/ssr-server/configure-proxy.js +304 -0
- package/utils/ssr-server/metrics-sender.js +204 -0
- package/utils/ssr-server/outgoing-request-hook.js +139 -0
- package/utils/ssr-server/parse-end-parameters.js +38 -0
- package/utils/ssr-server/process-express-response.js +56 -0
- package/utils/ssr-server/process-lambda-response.js +43 -0
- package/utils/ssr-server/update-global-agent-options.js +41 -0
- package/utils/ssr-server/update-global-agent-options.test.js +28 -0
- package/utils/ssr-server/utils.js +124 -0
- package/utils/ssr-server/utils.test.js +69 -0
- package/utils/ssr-server/wrap-response-write.js +40 -0
- package/utils/ssr-server.js +115 -0
- package/utils/ssr-server.test.js +869 -0
- package/utils/ssr-shared.js +183 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.QueryParameters = void 0;
|
|
7
|
+
var _querystring = require("querystring");
|
|
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
|
+
* @module progressive-web-sdk/utils/ssr-request-processing
|
|
16
|
+
*/
|
|
17
|
+
/*
|
|
18
|
+
There are some special requirements for this module, which is used in the
|
|
19
|
+
SDK and also in Lambda@Edge functions run by CloudFront. Specifically:
|
|
20
|
+
- Don't add any functionality in here that is not required for
|
|
21
|
+
request-processing
|
|
22
|
+
- Avoid importing any other modules not explicitly used by this code
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A class that represents and can manipulate a parsed
|
|
29
|
+
* query string as a subclass of Array.
|
|
30
|
+
*
|
|
31
|
+
* The values in the Array are objects.
|
|
32
|
+
*
|
|
33
|
+
* The values in the Array are objects with 'key', 'originalKey'
|
|
34
|
+
* 'value' and 'originalValue'. The 'key' is decoded (from %-encoding)
|
|
35
|
+
* and lowercased.
|
|
36
|
+
* The 'originalKey' is as it originally occurred in the query string. It's
|
|
37
|
+
* not decoded or lowercased.
|
|
38
|
+
* The 'originalValue' is as it originally occurred in the query string. It's
|
|
39
|
+
* not decoded.
|
|
40
|
+
* The 'value' is decoded (from %-encoding).
|
|
41
|
+
*
|
|
42
|
+
* If the query parameter is 'key=value', all fields are set.
|
|
43
|
+
* If the query parameter is 'key=' then 'value' and 'originalValue' will be
|
|
44
|
+
* the empty string.
|
|
45
|
+
* If the query parameter is just 'key' then 'value' and 'originalValue' will
|
|
46
|
+
* be null.
|
|
47
|
+
* If the query parameter is empty (double-&) then 'key' and 'originalKey' will
|
|
48
|
+
* be '', 'value' and 'originalValue' will be null.
|
|
49
|
+
*
|
|
50
|
+
* It's important that we PRESERVE the order of the parameters
|
|
51
|
+
* because some projects rely on it. The 'querystring' module
|
|
52
|
+
* assigns values to the resulting object in the order that it
|
|
53
|
+
* encounters them in the input string. Since ES2015 there are
|
|
54
|
+
* specific rules defining the order of properties, and apart
|
|
55
|
+
* from one particular case the order is chronological, which is
|
|
56
|
+
* what we want. The order of properties in an object depends on
|
|
57
|
+
* the type of the included properties and their values. Unfortunately,
|
|
58
|
+
* integer indices always come first, and the rules state that
|
|
59
|
+
* "an integer index is a string that, if converted to a 53-bit
|
|
60
|
+
* non-negative integer and back is the same value". This means that
|
|
61
|
+
* any parameter whose key is an integer index will appear out of
|
|
62
|
+
* order in the result. Thus we can't use the 'querystring' module.
|
|
63
|
+
*/
|
|
64
|
+
class QueryParameters {
|
|
65
|
+
constructor(querystring) {
|
|
66
|
+
if (!querystring) {
|
|
67
|
+
this._parameters = [];
|
|
68
|
+
} else {
|
|
69
|
+
this._parameters = querystring.split('&').map(parameter => {
|
|
70
|
+
const eqPos = parameter.indexOf('=');
|
|
71
|
+
const noEquals = eqPos < 0;
|
|
72
|
+
const originalKey = noEquals ? parameter : parameter.slice(0, eqPos);
|
|
73
|
+
const key = (0, _querystring.unescape)(originalKey).toLowerCase();
|
|
74
|
+
const originalValue = noEquals ? null : parameter.slice(eqPos + 1);
|
|
75
|
+
return {
|
|
76
|
+
key,
|
|
77
|
+
originalKey,
|
|
78
|
+
value: originalValue === null ? null : (0, _querystring.unescape)(originalValue),
|
|
79
|
+
originalValue
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Access the array of parameters owned by this QueryParameters
|
|
87
|
+
* object.
|
|
88
|
+
*/
|
|
89
|
+
get parameters() {
|
|
90
|
+
return this._parameters;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Construct a new QueryParameters from the output of
|
|
95
|
+
* any filtering or mapping done on another QueryParameters
|
|
96
|
+
* instance's 'parameters' array.
|
|
97
|
+
* @return {QueryParameters}
|
|
98
|
+
*/
|
|
99
|
+
static from(...args) {
|
|
100
|
+
const qp = new QueryParameters();
|
|
101
|
+
qp._parameters = Array.from(...args);
|
|
102
|
+
return qp;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Delete any query parameters with the given key.
|
|
107
|
+
* Key names are matched in a case-insensitive way.
|
|
108
|
+
* @param key {String}
|
|
109
|
+
* @returns {Boolean} true if a parameter was deleted,
|
|
110
|
+
* false if not.
|
|
111
|
+
*/
|
|
112
|
+
deleteByKey(key) {
|
|
113
|
+
const keyLC = key.toLowerCase();
|
|
114
|
+
const len = this._parameters.length;
|
|
115
|
+
this._parameters = this._parameters.filter(parameter => parameter.key !== keyLC);
|
|
116
|
+
return this._parameters.length < len;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Add a query parameter to the array (at the end)
|
|
121
|
+
* @param key {String} Parameter name (key)
|
|
122
|
+
* @param value {String} Parameter value (may be
|
|
123
|
+
* omitted or set to null to store only a key).
|
|
124
|
+
*/
|
|
125
|
+
appendParameter(key, value = null) {
|
|
126
|
+
this._parameters.push({
|
|
127
|
+
key,
|
|
128
|
+
originalKey: (0, _querystring.escape)(key),
|
|
129
|
+
value,
|
|
130
|
+
originalValue: value === null ? null : (0, _querystring.escape)(value)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* An Array of all the keys (as lowercased, unescaped strings)
|
|
136
|
+
*/
|
|
137
|
+
get keys() {
|
|
138
|
+
return this._parameters.map(parameter => parameter.key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Return a querystring built from the parameters in this
|
|
143
|
+
* instance, using the originalKey and originalValue properties
|
|
144
|
+
* of each parameter.
|
|
145
|
+
*
|
|
146
|
+
* Any strings that *this* class escapes will be correctly encoded (using
|
|
147
|
+
* %20 for space). There is an issue with input querystrings that
|
|
148
|
+
* are supposed to be encoded correctly, but which contain space
|
|
149
|
+
* characters (in either the key or the value). We don't want to convert
|
|
150
|
+
* them on input because that might cause the querystring as represented
|
|
151
|
+
* by this class to differ from the actual input. Instead, we define the
|
|
152
|
+
* behaviour of this function such that it will rebuild the querystring
|
|
153
|
+
* exactly as input EXCEPT that any spaces will be +-encoded.
|
|
154
|
+
*
|
|
155
|
+
* @return {string}
|
|
156
|
+
*/
|
|
157
|
+
toString() {
|
|
158
|
+
return this._parameters.map(parameter => {
|
|
159
|
+
const k = (0, _querystring.escape)((0, _querystring.unescape)(parameter.originalKey));
|
|
160
|
+
return parameter.value === null ? k : `${k}=${(0, _querystring.escape)(parameter.value)}`;
|
|
161
|
+
}).join('&');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.QueryParameters = QueryParameters;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _ssrRequestProcessing = require("./ssr-request-processing");
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
8
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
describe('Query parameter tests', () => {
|
|
12
|
+
test('Empty querystring', () => {
|
|
13
|
+
const qp = new _ssrRequestProcessing.QueryParameters('');
|
|
14
|
+
expect(qp.parameters).toHaveLength(0);
|
|
15
|
+
expect(qp.keys).toHaveLength(0);
|
|
16
|
+
expect(qp.toString()).toBe('');
|
|
17
|
+
});
|
|
18
|
+
test('Undefined querystring', () => {
|
|
19
|
+
const qp = new _ssrRequestProcessing.QueryParameters();
|
|
20
|
+
expect(qp.parameters).toHaveLength(0);
|
|
21
|
+
expect(qp.keys).toHaveLength(0);
|
|
22
|
+
expect(qp.toString()).toBe('');
|
|
23
|
+
});
|
|
24
|
+
test('Decodes & encodes', () => {
|
|
25
|
+
const qs = 'CMP=1&xyz=2&a&b=&&c=%2e';
|
|
26
|
+
const qp = new _ssrRequestProcessing.QueryParameters(qs);
|
|
27
|
+
expect(qp.parameters).toHaveLength(6);
|
|
28
|
+
expect(qp.keys).toHaveLength(6);
|
|
29
|
+
expect(qp.toString()).toBe('CMP=1&xyz=2&a&b=&&c=.');
|
|
30
|
+
expect(qp.parameters[0].key).toBe('cmp');
|
|
31
|
+
expect(qp.parameters[0].originalKey).toBe('CMP');
|
|
32
|
+
expect(qp.parameters[0].value).toBe('1');
|
|
33
|
+
expect(qp.parameters[2].key).toBe('a');
|
|
34
|
+
expect(qp.parameters[2].value).toBeNull();
|
|
35
|
+
expect(qp.parameters[2].originalValue).toBeNull();
|
|
36
|
+
expect(qp.parameters[3].key).toBe('b');
|
|
37
|
+
expect(qp.parameters[3].value).toBe('');
|
|
38
|
+
expect(qp.parameters[3].originalValue).toBe('');
|
|
39
|
+
expect(qp.parameters[4].key).toBe('');
|
|
40
|
+
expect(qp.parameters[4].value).toBeNull();
|
|
41
|
+
expect(qp.parameters[4].originalValue).toBeNull();
|
|
42
|
+
expect(qp.parameters[5].key).toBe('c');
|
|
43
|
+
expect(qp.parameters[5].value).toBe('.');
|
|
44
|
+
expect(qp.parameters[5].originalValue).toBe('%2e');
|
|
45
|
+
});
|
|
46
|
+
test('Append and deleteByKey', () => {
|
|
47
|
+
const qp = new _ssrRequestProcessing.QueryParameters();
|
|
48
|
+
expect(qp.parameters).toHaveLength(0);
|
|
49
|
+
expect(qp.keys).toHaveLength(0);
|
|
50
|
+
expect(qp.toString()).toBe('');
|
|
51
|
+
qp.appendParameter('x', '2');
|
|
52
|
+
qp.appendParameter('y', '3');
|
|
53
|
+
qp.appendParameter('z', null);
|
|
54
|
+
qp.appendParameter('q');
|
|
55
|
+
expect(qp.toString()).toBe('x=2&y=3&z&q');
|
|
56
|
+
qp.deleteByKey('x');
|
|
57
|
+
expect(qp.toString()).toBe('y=3&z&q');
|
|
58
|
+
});
|
|
59
|
+
test('Special characters handling', () => {
|
|
60
|
+
const test_cases = [{
|
|
61
|
+
original: '@#$%^+|{}[]<>,/? "',
|
|
62
|
+
// These characters should be escaped.
|
|
63
|
+
escaped: '%40%23%24%25%5E%2B%7C%7B%7D%5B%5D%3C%3E%2C%2F%3F%20%22'
|
|
64
|
+
}, {
|
|
65
|
+
original: "-_.!~*'()",
|
|
66
|
+
// These characters won't be escaped, so they should remain the same.
|
|
67
|
+
escaped: "-_.!~*'()"
|
|
68
|
+
},
|
|
69
|
+
// We are not supposed to escape characters -_.!~*'(), so if we see them in the query string, we will
|
|
70
|
+
// unescape them.
|
|
71
|
+
{
|
|
72
|
+
original: '%2d%5f%2e%21%7e%2a%27%28%29',
|
|
73
|
+
escaped: "-_.!~*'()"
|
|
74
|
+
}, {
|
|
75
|
+
original: 'Casing-Should-Be-Preserved',
|
|
76
|
+
escaped: 'Casing-Should-Be-Preserved'
|
|
77
|
+
}];
|
|
78
|
+
test_cases.forEach(data => {
|
|
79
|
+
const qp = new _ssrRequestProcessing.QueryParameters(`${data.original}=${data.original}`);
|
|
80
|
+
expect(qp.parameters).toHaveLength(1);
|
|
81
|
+
expect(qp.toString()).toBe(`${data.escaped}=${data.escaped}`);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
test('Filter and from()', () => {
|
|
85
|
+
const qs = 'a=1&b=2&c=3';
|
|
86
|
+
const qp1 = new _ssrRequestProcessing.QueryParameters(qs);
|
|
87
|
+
expect(qp1.parameters).toHaveLength(3);
|
|
88
|
+
expect(qp1.keys).toHaveLength(3);
|
|
89
|
+
expect(qp1.toString()).toEqual(qs);
|
|
90
|
+
const qp2 = _ssrRequestProcessing.QueryParameters.from(qp1.parameters.filter(p => p.key !== 'b'));
|
|
91
|
+
expect(qp2.parameters).toHaveLength(2);
|
|
92
|
+
expect(qp2.keys).toHaveLength(2);
|
|
93
|
+
expect(qp2.toString()).toBe('a=1&c=3');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CachedResponse = void 0;
|
|
7
|
+
var _constants = require("../../ssr/server/constants");
|
|
8
|
+
/*
|
|
9
|
+
* Copyright (c) 2022, Salesforce, 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
|
+
class CachedResponse {
|
|
16
|
+
/**
|
|
17
|
+
* Construct a CachedResponse from a cache entry. Project
|
|
18
|
+
* code should never need to call this.
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
constructor({
|
|
22
|
+
entry = {},
|
|
23
|
+
req,
|
|
24
|
+
res
|
|
25
|
+
}) {
|
|
26
|
+
this._found = !!entry.found;
|
|
27
|
+
this._key = entry.key;
|
|
28
|
+
this._namespace = entry.namespace;
|
|
29
|
+
this._data = entry.data;
|
|
30
|
+
this._metadata = entry.metadata || {};
|
|
31
|
+
this._expiration = entry.expiration;
|
|
32
|
+
this._req = req;
|
|
33
|
+
this._res = res;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Send this response
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
_send() {
|
|
41
|
+
const metadata = this._metadata;
|
|
42
|
+
const res = this._res;
|
|
43
|
+
res.status(this.status);
|
|
44
|
+
if (metadata.headers) {
|
|
45
|
+
res.set(metadata.headers);
|
|
46
|
+
}
|
|
47
|
+
res.setHeader(_constants.X_MOBIFY_FROM_CACHE, 'true');
|
|
48
|
+
if (this._data) {
|
|
49
|
+
// Use write() not send(), because the body has already
|
|
50
|
+
// been processed into a Buffer.
|
|
51
|
+
res.write(this._data);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get the key used to retrieve this CachedResponse
|
|
57
|
+
*/
|
|
58
|
+
get key() {
|
|
59
|
+
return this._key;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the namespace used to retrieve this CachedResponse
|
|
64
|
+
*/
|
|
65
|
+
get namespace() {
|
|
66
|
+
return this._namespace;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* true if this instance represents a found entry in the cache,
|
|
71
|
+
* false if no entry was found.
|
|
72
|
+
* @returns {Boolean}
|
|
73
|
+
*/
|
|
74
|
+
get found() {
|
|
75
|
+
return this._found;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The status code of this CachedResponse (as a Number)
|
|
80
|
+
* @returns {Number}
|
|
81
|
+
*/
|
|
82
|
+
get status() {
|
|
83
|
+
return this._metadata.status || 200;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A reference to the data for the response, which will
|
|
88
|
+
* be a Buffer, or undefined if the response is empty.
|
|
89
|
+
* @returns {Buffer|undefined}
|
|
90
|
+
*/
|
|
91
|
+
get data() {
|
|
92
|
+
return this._data;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A reference to the headers for the response. The value is
|
|
97
|
+
* a plain JS object containing the headers in the format used
|
|
98
|
+
* by ExpressJS: keys are always lower-case, values are strings
|
|
99
|
+
* except for set-cookie, which is an array of strings.
|
|
100
|
+
* @returns {{}}
|
|
101
|
+
*/
|
|
102
|
+
get headers() {
|
|
103
|
+
return this._metadata.headers || {};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get the date & time when this entry would expire. If this
|
|
108
|
+
* instance represents an entry that was not found, returns
|
|
109
|
+
* undefined.
|
|
110
|
+
* @returns {Date|undefined}
|
|
111
|
+
*/
|
|
112
|
+
get expiration() {
|
|
113
|
+
return this._found ? new Date(this._expiration) : undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.CachedResponse = CachedResponse;
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.configureProxyConfigs = exports.configureProxy = exports.applyProxyRequestHeaders = exports.ALLOWED_CACHING_PROXY_REQUEST_METHODS = void 0;
|
|
7
|
+
var _httpProxyMiddleware = require("http-proxy-middleware");
|
|
8
|
+
var _ssrProxying = require("../ssr-proxying");
|
|
9
|
+
var _ssrShared = require("../ssr-shared");
|
|
10
|
+
var _processExpressResponse = require("./process-express-response");
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
var _loggerInstance = _interopRequireDefault(require("../logger-instance"));
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
/*
|
|
15
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
16
|
+
* All rights reserved.
|
|
17
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
18
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const ALLOWED_CACHING_PROXY_REQUEST_METHODS = exports.ALLOWED_CACHING_PROXY_REQUEST_METHODS = ['HEAD', 'GET', 'OPTIONS'];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This path matching RE matches on /mobify/proxy and then skips one path
|
|
25
|
+
* element. For example, /mobify/proxy/heffalump/woozle would be converted to
|
|
26
|
+
* /woozle on whatever host /mobify/proxy/heffalump maps to.
|
|
27
|
+
* Group 2 is the full path on the proxied host.
|
|
28
|
+
* @private
|
|
29
|
+
* @type {RegExp}
|
|
30
|
+
*/
|
|
31
|
+
const generalProxyPathRE = /^\/mobify\/proxy\/([^/]+)(\/.*)$/;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Apply proxy headers to a request that is being proxied.
|
|
35
|
+
*
|
|
36
|
+
* This function is intended to be called from within a proxy's
|
|
37
|
+
* onProxyReq method.
|
|
38
|
+
*
|
|
39
|
+
* For more details on the headers being applied,
|
|
40
|
+
* see ssr-proxying.js rewriteProxyRequestHeaders method
|
|
41
|
+
* @private
|
|
42
|
+
* @function
|
|
43
|
+
* @param proxyRequest {http.ClientRequest} the request that will be
|
|
44
|
+
* sent to the target host
|
|
45
|
+
* @param incomingRequest {http.IncomingMessage} the request made to
|
|
46
|
+
* this Express app that prompted the proxying
|
|
47
|
+
* @param caching {Boolean} true for a caching proxy, false for a standard proxy
|
|
48
|
+
* @param logging {Boolean} true to log operations
|
|
49
|
+
* @param proxyPath {String} the path being proxied (e.g. /mobify/proxy/base/
|
|
50
|
+
* or /mobify/caching/base/)
|
|
51
|
+
* @param targetHost {String} the target hostname (host+port)
|
|
52
|
+
* @param targetProtocol {String} the protocol to use to make requests to
|
|
53
|
+
* the target ('http' or 'https')
|
|
54
|
+
*/
|
|
55
|
+
const applyProxyRequestHeaders = ({
|
|
56
|
+
proxyRequest,
|
|
57
|
+
incomingRequest,
|
|
58
|
+
caching = false,
|
|
59
|
+
logging = !(0, _utils.isRemote)() && _utils.verboseProxyLogging,
|
|
60
|
+
proxyPath,
|
|
61
|
+
targetHost,
|
|
62
|
+
targetProtocol
|
|
63
|
+
}) => {
|
|
64
|
+
const url = incomingRequest.url;
|
|
65
|
+
const headers = incomingRequest.headers;
|
|
66
|
+
/* istanbul ignore next */
|
|
67
|
+
if (logging) {
|
|
68
|
+
_loggerInstance.default.info(`Proxy: request for ${proxyPath}${url} => ${targetProtocol}://${targetHost}/${url}`, {
|
|
69
|
+
namespace: 'configureProxy.applyProxyRequestHeaders',
|
|
70
|
+
additionalProperties: {
|
|
71
|
+
proxyPath,
|
|
72
|
+
targetProtocol,
|
|
73
|
+
targetHost,
|
|
74
|
+
url
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const newHeaders = (0, _ssrProxying.rewriteProxyRequestHeaders)({
|
|
79
|
+
caching,
|
|
80
|
+
headers,
|
|
81
|
+
headerFormat: 'http',
|
|
82
|
+
logging,
|
|
83
|
+
proxyPath,
|
|
84
|
+
targetHost,
|
|
85
|
+
targetProtocol
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Copy any new and updated headers to the proxyRequest
|
|
89
|
+
// using setHeader.
|
|
90
|
+
Object.entries(newHeaders).forEach(
|
|
91
|
+
// setHeader always replaces any current value.
|
|
92
|
+
([key, value]) => proxyRequest.setHeader(key, value));
|
|
93
|
+
|
|
94
|
+
// Handle deletion of headers.
|
|
95
|
+
// Iterate over the keys of incomingRequest.headers - for every
|
|
96
|
+
// key, if the value is not present in newHeaders, we remove
|
|
97
|
+
// that value from proxyRequest's headers.
|
|
98
|
+
Object.keys(headers).forEach(key => {
|
|
99
|
+
// We delete the header on any falsy value, since
|
|
100
|
+
// there's no use case where we supply an empty header
|
|
101
|
+
// value.
|
|
102
|
+
if (!newHeaders[key]) {
|
|
103
|
+
proxyRequest.removeHeader(key);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Configure proxying for a path.
|
|
110
|
+
* @private
|
|
111
|
+
* @function
|
|
112
|
+
* @param appHostname {String} the hostname (host+port) under which the
|
|
113
|
+
* Express app is running (e.g. localhost:3443 for a local dev server)
|
|
114
|
+
* @param proxyPath {String} the path being proxied (e.g. /mobify/proxy/base/
|
|
115
|
+
* or /mobify/caching/base/)
|
|
116
|
+
* @param targetProtocol {String} the protocol to use to make requests to
|
|
117
|
+
* the target ('http' or 'https')
|
|
118
|
+
* @param targetHost {String} the target hostname (host+port)
|
|
119
|
+
* @param appProtocol {String} the protocol to use to make requests to
|
|
120
|
+
* the origin ('http' or 'https', defaults to 'https')
|
|
121
|
+
* @param caching {Boolean} true for a caching proxy, false for a
|
|
122
|
+
* standard proxy.
|
|
123
|
+
* @returns {middleware} function to pass to expressApp.use()
|
|
124
|
+
*/
|
|
125
|
+
exports.applyProxyRequestHeaders = applyProxyRequestHeaders;
|
|
126
|
+
const configureProxy = ({
|
|
127
|
+
appHostname,
|
|
128
|
+
proxyPath,
|
|
129
|
+
targetProtocol,
|
|
130
|
+
targetHost,
|
|
131
|
+
appProtocol = /* istanbul ignore next */'https',
|
|
132
|
+
caching
|
|
133
|
+
}) => {
|
|
134
|
+
// This configuration must match the behaviour of the proxying
|
|
135
|
+
// in CloudFront.
|
|
136
|
+
const targetOrigin = `${targetProtocol}://${targetHost}`;
|
|
137
|
+
const config = {
|
|
138
|
+
// The name of the changeOrigin option is misleading - it configures
|
|
139
|
+
// the proxying code in http-proxy to rewrite the Host header (not
|
|
140
|
+
// any Origin header) of the outgoing request. The Host header is
|
|
141
|
+
// also fixed up in rewriteProxyRequestHeaders, but that
|
|
142
|
+
// doesn't work correctly with http-proxy, because the https
|
|
143
|
+
// connection to the target is made *before* the request headers
|
|
144
|
+
// are modified by the onProxyReq event handler. So we set this
|
|
145
|
+
// flag true to get correct behaviour.
|
|
146
|
+
changeOrigin: true,
|
|
147
|
+
// Rewrite the domain in set-cookie headers in responses, if it
|
|
148
|
+
// matches the targetHost.
|
|
149
|
+
cookieDomainRewrite: {
|
|
150
|
+
targetHost: appHostname
|
|
151
|
+
},
|
|
152
|
+
// We don't do cookie *path* rewriting - it's complex.
|
|
153
|
+
cookiePathRewrite: false,
|
|
154
|
+
// Neither CloudFront nor the local Express app will follow redirect
|
|
155
|
+
// responses to proxy requests. The responses are returned to the
|
|
156
|
+
// client.
|
|
157
|
+
followRedirects: false,
|
|
158
|
+
logLevel: 'warn',
|
|
159
|
+
onError: (err, req, res) => {
|
|
160
|
+
/* istanbul ignore next */
|
|
161
|
+
if (!(0, _utils.isRemote)() && _utils.verboseProxyLogging) {
|
|
162
|
+
_loggerInstance.default.error(`Proxy: error ${err} for request ${proxyPath}/${req.url}`, {
|
|
163
|
+
namespace: 'configureProxy.onError',
|
|
164
|
+
additionalProperties: {
|
|
165
|
+
proxyPath,
|
|
166
|
+
url: req.url,
|
|
167
|
+
error: err
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
res.writeHead(500, {
|
|
172
|
+
'Content-Type': 'text/plain'
|
|
173
|
+
});
|
|
174
|
+
res.end(`Error in proxy request to ${req.url}: ${err}`);
|
|
175
|
+
},
|
|
176
|
+
/**
|
|
177
|
+
* Handler for all outgoing proxied requests. This is called
|
|
178
|
+
* irrespective of the source of the request (i.e., it could
|
|
179
|
+
* be from fetch, XmlHttpRequest or an external request to
|
|
180
|
+
* a /mobify/proxy path).
|
|
181
|
+
*
|
|
182
|
+
* Note also that this is called *after* a request is intercepted
|
|
183
|
+
* in outgoingRequestHook.
|
|
184
|
+
*
|
|
185
|
+
* @private
|
|
186
|
+
* @param proxyRequest {http.ClientRequest} the request that will be
|
|
187
|
+
* sent to the target host
|
|
188
|
+
* @param incomingRequest {http.IncomingMessage} the request made to
|
|
189
|
+
* this Express app that prompted the proxying
|
|
190
|
+
*/
|
|
191
|
+
onProxyReq: (proxyRequest, incomingRequest) => {
|
|
192
|
+
applyProxyRequestHeaders({
|
|
193
|
+
proxyRequest,
|
|
194
|
+
incomingRequest,
|
|
195
|
+
caching,
|
|
196
|
+
proxyPath,
|
|
197
|
+
targetHost,
|
|
198
|
+
targetProtocol
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
onProxyRes: (proxyResponse, req) => {
|
|
202
|
+
/* istanbul ignore next */
|
|
203
|
+
if (!(0, _utils.isRemote)() && _utils.verboseProxyLogging) {
|
|
204
|
+
_loggerInstance.default.info(`Proxy: ${proxyResponse.statusCode} response from ${proxyPath}${req.url}`, {
|
|
205
|
+
namespace: 'configureProxy.onProxyRes',
|
|
206
|
+
additionalProperties: {
|
|
207
|
+
statusCode: proxyResponse.statusCode,
|
|
208
|
+
proxyPath,
|
|
209
|
+
url: req.url
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// In this function, req.originalUrl is the path
|
|
215
|
+
// part of the original incoming request URL, containing
|
|
216
|
+
// the /mobify/proxy/.../ part. We need to strip that off
|
|
217
|
+
// before passing it to rewriteProxyResponseHeaders. If we
|
|
218
|
+
// match, group 2 is the full path on the target host, including
|
|
219
|
+
// query parameters.
|
|
220
|
+
const matchedUrl = generalProxyPathRE.exec(req.originalUrl);
|
|
221
|
+
|
|
222
|
+
// Rewrite key headers
|
|
223
|
+
proxyResponse.headers = (0, _ssrProxying.rewriteProxyResponseHeaders)({
|
|
224
|
+
appHostname,
|
|
225
|
+
caching,
|
|
226
|
+
targetHost,
|
|
227
|
+
targetProtocol,
|
|
228
|
+
appProtocol,
|
|
229
|
+
proxyPath,
|
|
230
|
+
statusCode: proxyResponse.statusCode,
|
|
231
|
+
headers: proxyResponse.headers,
|
|
232
|
+
headerFormat: 'http',
|
|
233
|
+
logging: !(0, _utils.isRemote)() && _utils.verboseProxyLogging,
|
|
234
|
+
requestUrl: matchedUrl && matchedUrl[2]
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Also handle binary responses
|
|
238
|
+
if ((0, _utils.isRemote)()) {
|
|
239
|
+
(0, _processExpressResponse.processExpressResponse)(proxyResponse);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
// Rewrite the request's path to remove the /mobify/proxy/...
|
|
243
|
+
// prefix.
|
|
244
|
+
pathRewrite: {
|
|
245
|
+
[`^${proxyPath}`]: ''
|
|
246
|
+
},
|
|
247
|
+
// The origin (protocol + host) to which we proxy
|
|
248
|
+
target: targetOrigin
|
|
249
|
+
};
|
|
250
|
+
const proxyFunc = (0, _httpProxyMiddleware.createProxyMiddleware)(config);
|
|
251
|
+
|
|
252
|
+
// For a standard proxy, we're done
|
|
253
|
+
if (!caching) {
|
|
254
|
+
return proxyFunc;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// For caching proxies, we need to validate the request method. We can't
|
|
258
|
+
// do that in the onProxyReq handler, because there's no way to send
|
|
259
|
+
// an HTTP error response from that function. Instead, we do it here,
|
|
260
|
+
// in a wrapper around the actual proxying function.
|
|
261
|
+
return (req, res, next) => {
|
|
262
|
+
// This function will only be called for requests for the
|
|
263
|
+
// current proxy config.
|
|
264
|
+
if (!ALLOWED_CACHING_PROXY_REQUEST_METHODS.includes(req.method)) {
|
|
265
|
+
return res.status(405).send(`Method ${req.method} not supported for caching proxy`).end();
|
|
266
|
+
}
|
|
267
|
+
return proxyFunc(req, res, next);
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Called by the Express app after updatePackageMobify has modified the
|
|
273
|
+
* proxyConfigs list, to create the actual proxying objects.
|
|
274
|
+
* @param {String} appHostname - the application hostname (the hostname
|
|
275
|
+
* to which requests are sent to the Express app)
|
|
276
|
+
* @param {String} appProtocol {String} the protocol to use to make requests to
|
|
277
|
+
* the origin ('http' or 'https', defaults to 'https')
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
exports.configureProxy = configureProxy;
|
|
281
|
+
const configureProxyConfigs = (appHostname, appProtocol) => {
|
|
282
|
+
(0, _utils.localDevLog)('');
|
|
283
|
+
_ssrShared.proxyConfigs.forEach(config => {
|
|
284
|
+
(0, _utils.localDevLog)(`Proxying ${config.proxyPath} and ${config.cachingPath} to ${config.protocol}://${config.host}`);
|
|
285
|
+
config.proxy = configureProxy({
|
|
286
|
+
proxyPath: config.proxyPath,
|
|
287
|
+
targetProtocol: config.protocol,
|
|
288
|
+
targetHost: config.host,
|
|
289
|
+
appProtocol,
|
|
290
|
+
appHostname,
|
|
291
|
+
caching: false
|
|
292
|
+
});
|
|
293
|
+
config.cachingProxy = configureProxy({
|
|
294
|
+
proxyPath: config.cachingPath,
|
|
295
|
+
targetProtocol: config.protocol,
|
|
296
|
+
targetHost: config.host,
|
|
297
|
+
appProtocol,
|
|
298
|
+
appHostname,
|
|
299
|
+
caching: true
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
(0, _utils.localDevLog)('');
|
|
303
|
+
};
|
|
304
|
+
exports.configureProxyConfigs = configureProxyConfigs;
|