@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,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MetricsSender = void 0;
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
var _loggerInstance = _interopRequireDefault(require("../logger-instance"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
/*
|
|
11
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
12
|
+
* All rights reserved.
|
|
13
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
14
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A class that will handle asynchronous sending of CloudWatch
|
|
19
|
+
* metrics.
|
|
20
|
+
*
|
|
21
|
+
* Use MetricsSender.getSender() to get the singleton instance.
|
|
22
|
+
*
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
class MetricsSender {
|
|
26
|
+
constructor() {
|
|
27
|
+
// CloudWatch client used to send metrics. For a local dev server,
|
|
28
|
+
// this will remain falsy, since a local dev server doesn't actually
|
|
29
|
+
// send metrics (unless SEND_CW_METRICS is defined for testing).
|
|
30
|
+
this._CW = null;
|
|
31
|
+
|
|
32
|
+
// A queue of metrics waiting to be sent. Each is a single
|
|
33
|
+
// name/value metric, and they accumulate on this queue
|
|
34
|
+
// until batched up into a putMetricData call.
|
|
35
|
+
this._queue = [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Return the number of metrics waiting to be sent
|
|
40
|
+
* @returns {number}
|
|
41
|
+
*/
|
|
42
|
+
get queueLength() {
|
|
43
|
+
return this._queue.length;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a CloudWatch AWS SDK client, or return a falsy value
|
|
48
|
+
* if this MetricsSender is not actually sending metrics.
|
|
49
|
+
*
|
|
50
|
+
* @private
|
|
51
|
+
* @returns {CloudWatch|null}
|
|
52
|
+
*/
|
|
53
|
+
_setup() {
|
|
54
|
+
/* istanbul ignore next */
|
|
55
|
+
if (!this._CW && ((0, _utils.isRemote)() || MetricsSender._override)) {
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
57
|
+
const Cloudwatch = require('aws-sdk/clients/cloudwatch');
|
|
58
|
+
this._CW = new Cloudwatch({
|
|
59
|
+
apiVersion: '2010-08-01',
|
|
60
|
+
// The AWS_REGION variable is defined by the Lambda
|
|
61
|
+
// environment.
|
|
62
|
+
region: process.env.AWS_REGION || 'us-east-1'
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return this._CW;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Call putMetricData as needed, including retrying.
|
|
70
|
+
*
|
|
71
|
+
* @private
|
|
72
|
+
* @param {CloudWatch} cw - Cloudwatch client
|
|
73
|
+
* @param {Array} metrics - Array of metrics to send
|
|
74
|
+
* @returns {Promise.<*>} resolved when the metrics are sent
|
|
75
|
+
* (or when they can't be sent).
|
|
76
|
+
*/
|
|
77
|
+
_putMetricData(cw, metrics) {
|
|
78
|
+
/* istanbul ignore next */
|
|
79
|
+
if (!cw) {
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
}
|
|
82
|
+
return new Promise(resolve => {
|
|
83
|
+
cw.putMetricData({
|
|
84
|
+
MetricData: metrics,
|
|
85
|
+
Namespace: 'ssr'
|
|
86
|
+
}, err => {
|
|
87
|
+
if (err) {
|
|
88
|
+
_loggerInstance.default.warn(`Metrics: error sending data: ${err}`, {
|
|
89
|
+
namespace: 'metrics-sender._putMetricData',
|
|
90
|
+
additionalProperties: {
|
|
91
|
+
metrics,
|
|
92
|
+
error: err
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
resolve();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Send any queued metrics. Returns a Promise that resolves when
|
|
103
|
+
* all sending is done.
|
|
104
|
+
* The Promise will never reject.
|
|
105
|
+
*/
|
|
106
|
+
flush() {
|
|
107
|
+
// Pending Promises for _putMetricData calls
|
|
108
|
+
const promises = [];
|
|
109
|
+
|
|
110
|
+
// Get a CloudWatch client (this will be falsy if we're not
|
|
111
|
+
// really sending)
|
|
112
|
+
const cw = this._setup();
|
|
113
|
+
while (this._queue.length) {
|
|
114
|
+
// Grab a batch of metrics from the queue - the maximum batch
|
|
115
|
+
// size is 20.
|
|
116
|
+
const queue = this._queue.slice(0, 20);
|
|
117
|
+
this._queue = this._queue.slice(20);
|
|
118
|
+
|
|
119
|
+
// Send the metrics
|
|
120
|
+
promises.push(this._putMetricData(cw, queue));
|
|
121
|
+
}
|
|
122
|
+
return Promise.all(promises).catch( /* istanbul ignore next */
|
|
123
|
+
err => _loggerInstance.default.warn(`Metrics: error during flush: ${err}`, {
|
|
124
|
+
namespace: 'metrics-sender.flush',
|
|
125
|
+
additionalProperties: {
|
|
126
|
+
error: err
|
|
127
|
+
}
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Add one or more custom metric values to the queue of those waiting
|
|
133
|
+
* to be sent. This function supports simple name-and-value metrics.
|
|
134
|
+
* It doesn't support more complex CloudWatch types.
|
|
135
|
+
*
|
|
136
|
+
* A metric is an object with at least 'name' (string) and 'value'
|
|
137
|
+
* (number, defaults to 0). It may also optionally include 'timestamp'
|
|
138
|
+
* (defaults to the time of the call to send()), and 'unit', which
|
|
139
|
+
* must be one of Seconds, Microseconds, Milliseconds, Bytes, Kilobytes,
|
|
140
|
+
* Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits,
|
|
141
|
+
* Terabits, Percent, Count, Bytes/Second, Kilobytes/Second,
|
|
142
|
+
* Megabytes/Second, Gigabytes/Second, Terabytes/Second,
|
|
143
|
+
* Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second,
|
|
144
|
+
* Terabits/Second, Count/Second or None (defaults to 'Count').
|
|
145
|
+
* There may also be a 'dimensions'
|
|
146
|
+
* object, which has dimension names as keys and dimension
|
|
147
|
+
* values as values.
|
|
148
|
+
*
|
|
149
|
+
* If called in the local development server, the metric information
|
|
150
|
+
* will be logged, but the code will not actually emit anything. This
|
|
151
|
+
* can be overridden by defining the SEND_CW_METRICS environment
|
|
152
|
+
* variable, for testing.
|
|
153
|
+
*
|
|
154
|
+
* The metrics are added to an internal queue so that they can be
|
|
155
|
+
* batched up to send more efficiently. They are only sent when
|
|
156
|
+
* flush() is called.
|
|
157
|
+
*
|
|
158
|
+
* @private
|
|
159
|
+
* @param metrics {Array<Object>} - array of name, value objects
|
|
160
|
+
*/
|
|
161
|
+
send(metrics) {
|
|
162
|
+
const now = new Date();
|
|
163
|
+
metrics.forEach(metric => {
|
|
164
|
+
const metricData = {
|
|
165
|
+
MetricName: metric.name,
|
|
166
|
+
Value: metric.value || 0,
|
|
167
|
+
// This value must be a string
|
|
168
|
+
Timestamp: (metric.timestamp instanceof Date ? metric.timestamp : now).toISOString(),
|
|
169
|
+
Unit: metric.unit || 'Count'
|
|
170
|
+
};
|
|
171
|
+
if (metric.dimensions) {
|
|
172
|
+
const dimensions = metricData.Dimensions = [];
|
|
173
|
+
Object.entries(metric.dimensions).forEach(([key, value]) => {
|
|
174
|
+
if (value) {
|
|
175
|
+
dimensions.push({
|
|
176
|
+
Name: key,
|
|
177
|
+
Value: value
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
this._queue.push(metricData);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Allow the presence of an environment variable to
|
|
188
|
+
// enable sending of CloudWatch metrics (for local
|
|
189
|
+
// integration testing)
|
|
190
|
+
exports.MetricsSender = MetricsSender;
|
|
191
|
+
MetricsSender._override = !!process.env.SEND_CW_METRICS;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Get the singleton MetricsSender
|
|
195
|
+
*
|
|
196
|
+
* @private
|
|
197
|
+
* @returns {MetricsSender}
|
|
198
|
+
*/
|
|
199
|
+
MetricsSender.getSender = () => {
|
|
200
|
+
if (!MetricsSender._instance) {
|
|
201
|
+
MetricsSender._instance = new MetricsSender();
|
|
202
|
+
}
|
|
203
|
+
return MetricsSender._instance;
|
|
204
|
+
};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.outgoingRequestHook = void 0;
|
|
7
|
+
var _http = _interopRequireDefault(require("http"));
|
|
8
|
+
var _https = _interopRequireDefault(require("https"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
11
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
12
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
13
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
14
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
|
|
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
|
+
let HTTP_AGENT, HTTPS_AGENT;
|
|
21
|
+
const KEEPALIVE_AGENT_OPTIONS = {
|
|
22
|
+
keepAlive: true
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the http and https agent singletons.
|
|
27
|
+
*
|
|
28
|
+
* @private
|
|
29
|
+
* @returns {object} -
|
|
30
|
+
*/
|
|
31
|
+
const getKeepAliveAgents = () => {
|
|
32
|
+
if (!HTTP_AGENT || !HTTPS_AGENT) {
|
|
33
|
+
HTTP_AGENT = new _http.default.Agent(KEEPALIVE_AGENT_OPTIONS);
|
|
34
|
+
HTTPS_AGENT = new _https.default.Agent(KEEPALIVE_AGENT_OPTIONS);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
httpAgent: HTTP_AGENT,
|
|
38
|
+
httpsAgent: HTTPS_AGENT
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const outgoingRequestHook = (wrapped, options) => {
|
|
42
|
+
return function () {
|
|
43
|
+
// Get the app hostname. If we can't, then just pass
|
|
44
|
+
// the call through to the wrapped function. We'll also
|
|
45
|
+
// do that if there's no access key.
|
|
46
|
+
const accessKey = process.env.X_MOBIFY_ACCESS_KEY;
|
|
47
|
+
const {
|
|
48
|
+
appHostname,
|
|
49
|
+
proxyKeepAliveAgent
|
|
50
|
+
} = options || {};
|
|
51
|
+
if (!(appHostname && accessKey)) {
|
|
52
|
+
return wrapped.apply(this, arguments);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// request and get can be called with (options[, callback])
|
|
56
|
+
// or (url[, options][, callback]).
|
|
57
|
+
let workingUrl = '';
|
|
58
|
+
let workingOptions;
|
|
59
|
+
let workingCallback;
|
|
60
|
+
const args = arguments;
|
|
61
|
+
|
|
62
|
+
// The options will be in the first 'object' argument
|
|
63
|
+
for (let i = 0; i < args.length; i++) {
|
|
64
|
+
const arg = args[i];
|
|
65
|
+
switch (typeof arg) {
|
|
66
|
+
case 'object':
|
|
67
|
+
{
|
|
68
|
+
// Assume this arg is the options
|
|
69
|
+
// We want to clone any options to avoid modifying
|
|
70
|
+
// the original object.
|
|
71
|
+
workingOptions = _objectSpread({}, arg);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case 'string':
|
|
75
|
+
{
|
|
76
|
+
// Assume this arg is the URL
|
|
77
|
+
workingUrl = arg;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
// Assume this is the callback
|
|
82
|
+
workingCallback = arg;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!workingOptions) {
|
|
87
|
+
// No options were supplied, so we add them
|
|
88
|
+
workingOptions = {
|
|
89
|
+
headers: {}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// We need to identify loopback requests: requests that are
|
|
94
|
+
// to the appHost (irrespective of protocol).
|
|
95
|
+
// The workingUrl value may be partial (the docs are very
|
|
96
|
+
// imprecise on permitted values). We check everywhere that
|
|
97
|
+
// might give us what we need. If this is not a loopback
|
|
98
|
+
// request, we just pass it through unmodified.
|
|
99
|
+
const isLoopback =
|
|
100
|
+
// Either hostname or host are allowed in the options. The docs
|
|
101
|
+
// say that 'hostname' is an alias for 'host', but that's not
|
|
102
|
+
// exactly true - host can include a port but hostname doesn't
|
|
103
|
+
// always. So we need to compare both.
|
|
104
|
+
workingOptions.host === appHostname || workingOptions.hostname === appHostname || workingUrl && workingUrl.includes(`//${appHostname}`);
|
|
105
|
+
if (!isLoopback) {
|
|
106
|
+
return wrapped.apply(this, arguments);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// We must inject the 'x-mobify-access-key' header into the
|
|
110
|
+
// request.
|
|
111
|
+
workingOptions.headers = workingOptions.headers ? _objectSpread({}, workingOptions.headers) : {};
|
|
112
|
+
|
|
113
|
+
// Inject the access key.
|
|
114
|
+
workingOptions.headers['x-mobify-access-key'] = accessKey;
|
|
115
|
+
|
|
116
|
+
// Create and add keep-alive agent to options for loop-back connection.
|
|
117
|
+
if (proxyKeepAliveAgent) {
|
|
118
|
+
var _workingOptions, _workingOptions2, _workingOptions2$head, _workingOptions3, _workingOptions3$head;
|
|
119
|
+
const {
|
|
120
|
+
httpAgent,
|
|
121
|
+
httpsAgent
|
|
122
|
+
} = getKeepAliveAgents();
|
|
123
|
+
|
|
124
|
+
// Add default agent to global connection reuse.
|
|
125
|
+
workingOptions.agent = workingUrl.startsWith('http:') || ((_workingOptions = workingOptions) === null || _workingOptions === void 0 ? void 0 : _workingOptions.protocol) === 'http:' ? httpAgent : httpsAgent;
|
|
126
|
+
|
|
127
|
+
// `node-fetch` and potentially other libraries add connection: close headers
|
|
128
|
+
// remove them to keep the connection alive. NOTE: There are variations in
|
|
129
|
+
// whether or not the connection header is upper or lower case, so handle both.
|
|
130
|
+
(_workingOptions2 = workingOptions) === null || _workingOptions2 === void 0 ? true : (_workingOptions2$head = _workingOptions2.headers) === null || _workingOptions2$head === void 0 ? true : delete _workingOptions2$head.connection;
|
|
131
|
+
(_workingOptions3 = workingOptions) === null || _workingOptions3 === void 0 ? true : (_workingOptions3$head = _workingOptions3.headers) === null || _workingOptions3$head === void 0 ? true : delete _workingOptions3$head.Connection;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Build the args, omitting any undefined values
|
|
135
|
+
const workingArgs = [workingUrl, workingOptions, workingCallback].filter(arg => !!arg);
|
|
136
|
+
return wrapped.apply(this, workingArgs);
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
exports.outgoingRequestHook = outgoingRequestHook;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseEndParameters = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
9
|
+
* All rights reserved.
|
|
10
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
11
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
12
|
+
*/
|
|
13
|
+
const parseEndParameters = params => {
|
|
14
|
+
// If data is specified in a call to end(), it is equivalent to
|
|
15
|
+
// calling response.write(data, encoding) followed by
|
|
16
|
+
// response.end(callback). We need the data to be sent before
|
|
17
|
+
// we call _storeResponseInCache, and we want to call end()
|
|
18
|
+
// after _storeResponseInCache is done. Therefore we have to
|
|
19
|
+
// parse the arguments, which is a little complex since end()
|
|
20
|
+
// may be called with a range of different patterns.
|
|
21
|
+
const result = {};
|
|
22
|
+
params === null || params === void 0 ? void 0 : params.forEach(value => {
|
|
23
|
+
// Any function parameter must be the callback
|
|
24
|
+
if (typeof value === 'function') {
|
|
25
|
+
result.callback = value;
|
|
26
|
+
} else {
|
|
27
|
+
// If we haven't assigned anything to data yet,
|
|
28
|
+
// this parameter must be the data.
|
|
29
|
+
if (result.data === undefined) {
|
|
30
|
+
result.data = value;
|
|
31
|
+
} else {
|
|
32
|
+
result.encoding = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
exports.parseEndParameters = parseEndParameters;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.responseSend = exports.processExpressResponse = 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
|
+
const processExpressResponse = response => {
|
|
16
|
+
if (!response) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Get a function that will return a header value for either
|
|
21
|
+
// response type.
|
|
22
|
+
const getHeader = response.getHeader ? response.getHeader.bind(response) : header => response.headers[header];
|
|
23
|
+
const contentType = getHeader(_constants.CONTENT_TYPE);
|
|
24
|
+
if (contentType && getHeader(_constants.CONTENT_ENCODING)) {
|
|
25
|
+
// It's technically possible to call send() more than once,
|
|
26
|
+
// so we only do this the first time that we pass through
|
|
27
|
+
// here and content-type and content-encoding are both set.
|
|
28
|
+
if (!getHeader(_constants.X_ORIGINAL_CONTENT_TYPE)) {
|
|
29
|
+
// Get a header-setting function that will work for either
|
|
30
|
+
// response type (for single-value headers only).
|
|
31
|
+
const setHeader = response.setHeader ? response.setHeader.bind(response) : (header, value) => {
|
|
32
|
+
response.headers[header] = value;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Copy the current content-type to a separate
|
|
36
|
+
// header. It can then be used in processLambdaResponse
|
|
37
|
+
// to restore the header.
|
|
38
|
+
setHeader(_constants.X_ORIGINAL_CONTENT_TYPE, contentType);
|
|
39
|
+
// Set the content-type header to application/octet-stream
|
|
40
|
+
// so that aws-serverless-express will treat this as
|
|
41
|
+
// binary.
|
|
42
|
+
setHeader(_constants.CONTENT_TYPE, _constants.APPLICATION_OCTET_STREAM);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
exports.processExpressResponse = processExpressResponse;
|
|
47
|
+
const responseSend = originalSend => {
|
|
48
|
+
return function wrappedSend(...args) {
|
|
49
|
+
// process response headers
|
|
50
|
+
processExpressResponse(this);
|
|
51
|
+
|
|
52
|
+
// Call the wrapped send() function
|
|
53
|
+
return originalSend.call(this, ...args);
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.responseSend = responseSend;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.processLambdaResponse = void 0;
|
|
7
|
+
var _constants = require("../../ssr/server/constants");
|
|
8
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
9
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
10
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
11
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
12
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
|
|
13
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
14
|
+
* All rights reserved.
|
|
15
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
16
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
17
|
+
*/
|
|
18
|
+
const processLambdaResponse = (response, event) => {
|
|
19
|
+
var _event$headers, _response$headers;
|
|
20
|
+
if (!response) return response;
|
|
21
|
+
|
|
22
|
+
// Retrieve the correlation ID from the event headers
|
|
23
|
+
const correlationId = (_event$headers = event.headers) === null || _event$headers === void 0 ? void 0 : _event$headers['x-correlation-id'];
|
|
24
|
+
const responseHeaders = _objectSpread({}, response.headers);
|
|
25
|
+
|
|
26
|
+
// Add the correlation ID to the response headers if it exists
|
|
27
|
+
if (correlationId) {
|
|
28
|
+
responseHeaders['x-correlation-id'] = correlationId;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// If the response contains an X_ORIGINAL_CONTENT_TYPE header,
|
|
32
|
+
// then replace the current CONTENT_TYPE header with it.
|
|
33
|
+
const originalContentType = (_response$headers = response.headers) === null || _response$headers === void 0 ? void 0 : _response$headers[_constants.X_ORIGINAL_CONTENT_TYPE];
|
|
34
|
+
if (originalContentType) {
|
|
35
|
+
responseHeaders[_constants.CONTENT_TYPE] = originalContentType;
|
|
36
|
+
delete responseHeaders[_constants.X_ORIGINAL_CONTENT_TYPE];
|
|
37
|
+
}
|
|
38
|
+
const result = _objectSpread(_objectSpread({}, response), {}, {
|
|
39
|
+
headers: responseHeaders
|
|
40
|
+
});
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
exports.processLambdaResponse = processLambdaResponse;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateGlobalAgentOptions = exports.AGENT_OPTIONS_TO_COPY = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
9
|
+
* All rights reserved.
|
|
10
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
11
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
12
|
+
*/
|
|
13
|
+
const AGENT_OPTIONS_TO_COPY = exports.AGENT_OPTIONS_TO_COPY = ['ca', 'cert', 'ciphers', 'clientCertEngine', 'crl', 'dhparam', 'ecdhCurve', 'honorCipherOrder', 'key', 'passphrase', 'pfx', 'rejectUnauthorized', 'secureOptions', 'secureProtocol', 'servername', 'sessionIdContext'];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Given an https.Agent (or an http.Agent) and the corresponding globalAgent,
|
|
17
|
+
* copy key fields from the options of the 'from' Agent to the
|
|
18
|
+
* options of the 'to' Agent. If 'from' is undefined, this function does nothing.
|
|
19
|
+
*
|
|
20
|
+
* The fields that we update are:
|
|
21
|
+
* ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve,
|
|
22
|
+
* honorCipherOrder, key, passphrase, pfx, rejectUnauthorized,
|
|
23
|
+
* secureOptions, secureProtocol, servername, sessionIdContext
|
|
24
|
+
*
|
|
25
|
+
* @private
|
|
26
|
+
* @param [from] {Agent} - source object (or undefined)
|
|
27
|
+
* @param to {Agent} - target object
|
|
28
|
+
*/
|
|
29
|
+
const updateGlobalAgentOptions = (from, to) => {
|
|
30
|
+
if (from && to && from.options) {
|
|
31
|
+
if (!to.options) {
|
|
32
|
+
to.options = {};
|
|
33
|
+
}
|
|
34
|
+
AGENT_OPTIONS_TO_COPY.forEach(key => {
|
|
35
|
+
if (key in from.options) {
|
|
36
|
+
to.options[key] = from.options[key];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.updateGlobalAgentOptions = updateGlobalAgentOptions;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _updateGlobalAgentOptions = require("./update-global-agent-options");
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (c) 2023, Salesforce, 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('update-global-agent-options', () => {
|
|
12
|
+
test('non copy option', () => {
|
|
13
|
+
let from = {
|
|
14
|
+
options: {
|
|
15
|
+
random: 'value'
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
let to = {
|
|
19
|
+
options: {}
|
|
20
|
+
};
|
|
21
|
+
_updateGlobalAgentOptions.AGENT_OPTIONS_TO_COPY.forEach(key => {
|
|
22
|
+
from.options[key] = key;
|
|
23
|
+
});
|
|
24
|
+
(0, _updateGlobalAgentOptions.updateGlobalAgentOptions)(from, to);
|
|
25
|
+
expect(Object.keys(to.options)).toHaveLength(_updateGlobalAgentOptions.AGENT_OPTIONS_TO_COPY.length);
|
|
26
|
+
expect(to.options).not.toHaveProperty('random');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.verboseProxyLogging = exports.setQuiet = exports.parseCacheControl = exports.localDevLog = exports.isRemote = exports.isQuiet = exports.infoLog = exports.getHashForString = exports.getFullRequestURL = exports.getBundleBaseUrl = exports.escapeJSText = exports.catchAndLog = void 0;
|
|
7
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
|
8
|
+
var _ssrShared = require("../ssr-shared");
|
|
9
|
+
var _ssrPaths = require("../ssr-paths");
|
|
10
|
+
var _loggerInstance = _interopRequireDefault(require("../logger-instance"));
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
/*
|
|
13
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
14
|
+
* All rights reserved.
|
|
15
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
16
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// To avoid an unwieldy file size, this file should be for utils that are less than ~20 lines.
|
|
20
|
+
// Larger utils should be placed in separate files.
|
|
21
|
+
// Also, ./utils/ssr-server/utils is a bit of a silly name, but these helpers can't live in
|
|
22
|
+
// ../ssr-server.js because it would create circular dependencies.
|
|
23
|
+
|
|
24
|
+
// TODO: Clean this up or provide a way to toggle
|
|
25
|
+
const verboseProxyLogging = exports.verboseProxyLogging = false;
|
|
26
|
+
const isRemote = () => Object.prototype.hasOwnProperty.call(process.env, 'AWS_LAMBDA_FUNCTION_NAME');
|
|
27
|
+
exports.isRemote = isRemote;
|
|
28
|
+
const getBundleBaseUrl = () => {
|
|
29
|
+
return `${(0, _ssrPaths.getBundlePath)()}/${isRemote() ? process.env.BUNDLE_ID : 'development'}/`;
|
|
30
|
+
};
|
|
31
|
+
exports.getBundleBaseUrl = getBundleBaseUrl;
|
|
32
|
+
let QUIET = false;
|
|
33
|
+
const setQuiet = quiet => {
|
|
34
|
+
QUIET = quiet;
|
|
35
|
+
};
|
|
36
|
+
exports.setQuiet = setQuiet;
|
|
37
|
+
const isQuiet = () => QUIET;
|
|
38
|
+
|
|
39
|
+
// Logs in local development server mode only
|
|
40
|
+
exports.isQuiet = isQuiet;
|
|
41
|
+
const localDevLog = (...args) => {
|
|
42
|
+
if (!isRemote() && !QUIET) {
|
|
43
|
+
/* istanbul ignore next */
|
|
44
|
+
console.log(...args);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Logs unless server is in quiet mode (used for tests)
|
|
49
|
+
exports.localDevLog = localDevLog;
|
|
50
|
+
const infoLog = (...args) => {
|
|
51
|
+
/* istanbul ignore next */
|
|
52
|
+
if (!QUIET) {
|
|
53
|
+
/* istanbul ignore next */
|
|
54
|
+
console.log(...args);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A global error catcher
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
exports.infoLog = infoLog;
|
|
63
|
+
const catchAndLog = (err, context) => {
|
|
64
|
+
/* istanbul ignore next */
|
|
65
|
+
const message = `${context || 'Uncaught exception'}: `;
|
|
66
|
+
_loggerInstance.default.error(message, /* istanbul ignore next */
|
|
67
|
+
{
|
|
68
|
+
namespace: 'catchAndLog',
|
|
69
|
+
additionalProperties: {
|
|
70
|
+
stack: err && (err.stack || err.message || err) || '(no error)'
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Given a piece of JavaScript or JSON as text, escape any
|
|
77
|
+
* '</' so that it can be embedded within HTML.
|
|
78
|
+
*
|
|
79
|
+
* @private
|
|
80
|
+
* @param {String} text
|
|
81
|
+
* @returns {String}
|
|
82
|
+
*/
|
|
83
|
+
exports.catchAndLog = catchAndLog;
|
|
84
|
+
const escapeJSText = text => text === null || text === void 0 ? void 0 : text.replace(/<\//gm, '\\x3c\\x2f');
|
|
85
|
+
exports.escapeJSText = escapeJSText;
|
|
86
|
+
const getHashForString = text => {
|
|
87
|
+
const hash = _crypto.default.createHash('sha256');
|
|
88
|
+
hash.update(text);
|
|
89
|
+
return hash.digest('hex');
|
|
90
|
+
};
|
|
91
|
+
exports.getHashForString = getHashForString;
|
|
92
|
+
const getFullRequestURL = url => {
|
|
93
|
+
// If it starts with a protocol (e.g. http(s)://, file://), then it's already a full URL
|
|
94
|
+
if (/^[a-zA-Z]+:\/\//.test(url)) return url;
|
|
95
|
+
const proxy = _ssrShared.proxyConfigs.find(({
|
|
96
|
+
path
|
|
97
|
+
}) => url.startsWith(`${(0, _ssrPaths.getProxyPath)()}/${path}/`));
|
|
98
|
+
if (proxy) {
|
|
99
|
+
return url.replace(`${(0, _ssrPaths.getProxyPath)()}/${proxy.path}`, `${proxy.protocol}://${proxy.host}`);
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`Unable to fetch ${url}, relative paths must begin with ${(0, _ssrPaths.getProxyPath)()} followed by a configured proxy path.`);
|
|
102
|
+
};
|
|
103
|
+
exports.getFullRequestURL = getFullRequestURL;
|
|
104
|
+
const CC_AGE_RE = /(s-maxage|max-age)\s*=\s*(\d+)/gi;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Perform limited parsing of a Cache-Control header value, to
|
|
108
|
+
* extract the s-maxage and max-age values and return them.
|
|
109
|
+
*
|
|
110
|
+
* @function
|
|
111
|
+
* @param value {String} the value to parse
|
|
112
|
+
* @returns {Object} with 'max-age' and 's-maxage' properties mapped
|
|
113
|
+
* to String or undefined values.
|
|
114
|
+
*/
|
|
115
|
+
const parseCacheControl = value => {
|
|
116
|
+
const result = {};
|
|
117
|
+
if (value) {
|
|
118
|
+
for (const match of value.matchAll(CC_AGE_RE)) {
|
|
119
|
+
result[match[1].toLowerCase()] = match[2];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
};
|
|
124
|
+
exports.parseCacheControl = parseCacheControl;
|