@salesforce/mrt-utilities 0.0.1 → 0.1.1
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/README.md +49 -45
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +10 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/metrics/index.d.ts +1 -0
- package/dist/cjs/metrics/index.js +7 -0
- package/dist/cjs/metrics/index.js.map +1 -0
- package/dist/cjs/metrics/metrics-sender.d.ts +136 -0
- package/dist/cjs/metrics/metrics-sender.js +240 -0
- package/dist/cjs/metrics/metrics-sender.js.map +1 -0
- package/dist/cjs/middleware/data-store.d.ts +56 -0
- package/dist/cjs/middleware/data-store.js +124 -0
- package/dist/cjs/middleware/data-store.js.map +1 -0
- package/dist/cjs/middleware/index.d.ts +3 -0
- package/dist/cjs/middleware/index.js +8 -0
- package/dist/cjs/middleware/index.js.map +1 -0
- package/dist/cjs/middleware/middleware.d.ts +126 -0
- package/dist/cjs/middleware/middleware.js +416 -0
- package/dist/cjs/middleware/middleware.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/streaming/create-lambda-adapter.d.ts +68 -0
- package/dist/cjs/streaming/create-lambda-adapter.js +797 -0
- package/dist/cjs/streaming/create-lambda-adapter.js.map +1 -0
- package/dist/cjs/streaming/index.d.ts +1 -0
- package/dist/cjs/streaming/index.js +7 -0
- package/dist/cjs/streaming/index.js.map +1 -0
- package/dist/cjs/utils/configure-proxying.d.ts +160 -0
- package/dist/cjs/utils/configure-proxying.js +204 -0
- package/dist/cjs/utils/configure-proxying.js.map +1 -0
- package/dist/cjs/utils/ssr-proxying.d.ts +300 -0
- package/dist/cjs/utils/ssr-proxying.js +713 -0
- package/dist/cjs/utils/ssr-proxying.js.map +1 -0
- package/dist/cjs/utils/utils.d.ts +27 -0
- package/dist/cjs/utils/utils.js +44 -0
- package/dist/cjs/utils/utils.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/metrics/index.d.ts +1 -0
- package/dist/esm/metrics/index.js +7 -0
- package/dist/esm/metrics/index.js.map +1 -0
- package/dist/esm/metrics/metrics-sender.d.ts +136 -0
- package/dist/esm/metrics/metrics-sender.js +240 -0
- package/dist/esm/metrics/metrics-sender.js.map +1 -0
- package/dist/esm/middleware/data-store.d.ts +56 -0
- package/dist/esm/middleware/data-store.js +124 -0
- package/dist/esm/middleware/data-store.js.map +1 -0
- package/dist/esm/middleware/index.d.ts +3 -0
- package/dist/esm/middleware/index.js +9 -0
- package/dist/esm/middleware/index.js.map +1 -0
- package/dist/esm/middleware/middleware.d.ts +126 -0
- package/dist/esm/middleware/middleware.js +416 -0
- package/dist/esm/middleware/middleware.js.map +1 -0
- package/dist/esm/streaming/create-lambda-adapter.d.ts +68 -0
- package/dist/esm/streaming/create-lambda-adapter.js +797 -0
- package/dist/esm/streaming/create-lambda-adapter.js.map +1 -0
- package/dist/esm/streaming/index.d.ts +1 -0
- package/dist/esm/streaming/index.js +7 -0
- package/dist/esm/streaming/index.js.map +1 -0
- package/dist/esm/utils/configure-proxying.d.ts +160 -0
- package/dist/esm/utils/configure-proxying.js +204 -0
- package/dist/esm/utils/configure-proxying.js.map +1 -0
- package/dist/esm/utils/ssr-proxying.d.ts +300 -0
- package/dist/esm/utils/ssr-proxying.js +713 -0
- package/dist/esm/utils/ssr-proxying.js.map +1 -0
- package/dist/esm/utils/utils.d.ts +27 -0
- package/dist/esm/utils/utils.js +44 -0
- package/dist/esm/utils/utils.js.map +1 -0
- package/package.json +130 -7
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type { IncomingHttpHeaders } from 'http';
|
|
2
|
+
declare const HEADER_FORMATS: readonly ["http", "aws"];
|
|
3
|
+
export declare const X_PROXY_REQUEST_URL = "x-proxy-request-url";
|
|
4
|
+
export declare const X_MOBIFY_REQUEST_CLASS = "x-mobify-request-class";
|
|
5
|
+
export declare const MAX_URL_LENGTH_BYTES = 8192;
|
|
6
|
+
type HeaderFormat = (typeof HEADER_FORMATS)[number];
|
|
7
|
+
/**
|
|
8
|
+
* Represents a parsed cookie object from set-cookie-parser
|
|
9
|
+
*/
|
|
10
|
+
interface ParsedCookie {
|
|
11
|
+
name: string;
|
|
12
|
+
value: string;
|
|
13
|
+
path?: string;
|
|
14
|
+
expires?: Date;
|
|
15
|
+
domain?: string;
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
secure?: boolean;
|
|
18
|
+
httpOnly?: boolean;
|
|
19
|
+
sameSite?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Represents a parsed host with its components
|
|
23
|
+
*/
|
|
24
|
+
export interface ParsedHost {
|
|
25
|
+
/** The host (10.10.10.10:port), which includes the port (if any) */
|
|
26
|
+
host: string;
|
|
27
|
+
/** The hostname (10.10.10.10), which excludes the port */
|
|
28
|
+
hostname: string;
|
|
29
|
+
/** The host's port */
|
|
30
|
+
port?: string;
|
|
31
|
+
/** Whether the hostname is an IP or localhost */
|
|
32
|
+
isIPOrLocalhost: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Represents AWS Lambda Event headers format
|
|
36
|
+
*/
|
|
37
|
+
interface AWSHeaderValue {
|
|
38
|
+
key: string;
|
|
39
|
+
value: string;
|
|
40
|
+
}
|
|
41
|
+
export type AWSHeaders = Record<string, AWSHeaderValue[]>;
|
|
42
|
+
/**
|
|
43
|
+
* Represents HTTP IncomingMessage headers format
|
|
44
|
+
*/
|
|
45
|
+
export type HTTPHeaders = Record<string, string | string[]>;
|
|
46
|
+
/**
|
|
47
|
+
* This class provides a representation of HTTP request or response
|
|
48
|
+
* headers, that operates in the same way in multiple contexts
|
|
49
|
+
* (i.e. within the Express app as well as the request-processor).
|
|
50
|
+
*
|
|
51
|
+
* Within a Headers instance, headers are referenced using lower-case
|
|
52
|
+
* names. Use getHeader to access the value for a header. If there
|
|
53
|
+
* are multiple values, this will return the first value. This class
|
|
54
|
+
* internally supports round-trip preservation of multi-value headers,
|
|
55
|
+
* but does not yet provide a way to access them.
|
|
56
|
+
*/
|
|
57
|
+
export declare class Headers {
|
|
58
|
+
private httpFormat;
|
|
59
|
+
private headers;
|
|
60
|
+
private _modified;
|
|
61
|
+
/**
|
|
62
|
+
* Construct a Headers object from either an AWS Lambda Event headers
|
|
63
|
+
* object, or an http.IncomingMessage headers object.
|
|
64
|
+
*
|
|
65
|
+
* Project code should never need to call this constructor.
|
|
66
|
+
*
|
|
67
|
+
* @private
|
|
68
|
+
* @param headers the input headers
|
|
69
|
+
* @param format either 'http' or 'aws'
|
|
70
|
+
*/
|
|
71
|
+
constructor(headers: AWSHeaders | HTTPHeaders | IncomingHttpHeaders, format: HeaderFormat);
|
|
72
|
+
/**
|
|
73
|
+
* Return true if and only if any set or delete methods were called on
|
|
74
|
+
* this instance after construction. This does not actually test if
|
|
75
|
+
* the headers values have been changed, just whether any mutating
|
|
76
|
+
* methods have been called.
|
|
77
|
+
* @returns {Boolean}
|
|
78
|
+
*/
|
|
79
|
+
get modified(): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Return an array of the header keys (all lower-case)
|
|
82
|
+
*/
|
|
83
|
+
keys(): string[];
|
|
84
|
+
/**
|
|
85
|
+
* Get the value of the set-cookie header(s), returning an array
|
|
86
|
+
* of strings. Always returns an array, even if it's empty.
|
|
87
|
+
*/
|
|
88
|
+
getSetCookie(): string[];
|
|
89
|
+
/**
|
|
90
|
+
* Set the value of the set-cookie header(s)
|
|
91
|
+
* @param values Array of set-cookie header values
|
|
92
|
+
*/
|
|
93
|
+
setSetCookie(values: string[]): void;
|
|
94
|
+
/**
|
|
95
|
+
* Return the FIRST value of the header with the given key.
|
|
96
|
+
* This is for single-value headers only: Location, Access-Control-*, etc
|
|
97
|
+
* If the header is not present, returns undefined.
|
|
98
|
+
* @param key header name
|
|
99
|
+
*/
|
|
100
|
+
getHeader(key: string): string | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Set the value of the header with the given key. This is for single-
|
|
103
|
+
* value headers only (see getHeader). Setting the value removes ALL other
|
|
104
|
+
* values for the given key.
|
|
105
|
+
* @param key header name
|
|
106
|
+
* @param value header value
|
|
107
|
+
*/
|
|
108
|
+
setHeader(key: string, value: string): void;
|
|
109
|
+
/**
|
|
110
|
+
* Remove any header with the given key
|
|
111
|
+
* @param key header name to remove
|
|
112
|
+
*/
|
|
113
|
+
deleteHeader(key: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Return the headers in AWS (Lambda event) format.
|
|
116
|
+
*
|
|
117
|
+
* Project code should never need to use this method.
|
|
118
|
+
*/
|
|
119
|
+
toAWSFormat(): AWSHeaders;
|
|
120
|
+
/**
|
|
121
|
+
* Return the headers in Express (http.IncomingMessage) format.
|
|
122
|
+
*
|
|
123
|
+
* RFC2616 allows some flexibility in how multiple values are
|
|
124
|
+
* combined into a single header value. We separate with ', '
|
|
125
|
+
* rather than just ',' to maintain previous behaviour.
|
|
126
|
+
*
|
|
127
|
+
* Project code should never need to use this method.
|
|
128
|
+
*/
|
|
129
|
+
toHTTPFormat(): HTTPHeaders;
|
|
130
|
+
/**
|
|
131
|
+
* Return the headers in the same format (aws or http) that was
|
|
132
|
+
* used to construct them.
|
|
133
|
+
*
|
|
134
|
+
* Project code should never need to use this method.
|
|
135
|
+
*/
|
|
136
|
+
toObject(): AWSHeaders | HTTPHeaders | IncomingHttpHeaders;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Return the given date as an RFC1123-format string, suitable for
|
|
140
|
+
* use in a Set-Cookie or Date header. The result is always in UTC.
|
|
141
|
+
* @function
|
|
142
|
+
* @param date Date object
|
|
143
|
+
* @returns RFC1123 formatted date string
|
|
144
|
+
*/
|
|
145
|
+
export declare const rfc1123: (date: Date) => string;
|
|
146
|
+
/**
|
|
147
|
+
* Given a cookie object parsed by set-cookie-parser,
|
|
148
|
+
* return a set-cookie header value for it.
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
export declare const cookieAsString: (cookie: ParsedCookie) => string;
|
|
152
|
+
/**
|
|
153
|
+
* Given a hostname that may be a hostname or ip address optionally
|
|
154
|
+
* followed by a port, return an object with 'host' being the ip address,
|
|
155
|
+
* the hostname, a port (if there is one), and 'isIPOrLocalhost' true for an ip
|
|
156
|
+
* address or localhost, false for a hostname
|
|
157
|
+
* @private
|
|
158
|
+
* @param host Can be localhost, an IP or domain name
|
|
159
|
+
* @return ParsedHost object
|
|
160
|
+
*/
|
|
161
|
+
export declare const parseHost: (host: string) => ParsedHost;
|
|
162
|
+
export declare const rewriteDomain: (domain: string, appHostname: string, targetHost: string) => string;
|
|
163
|
+
/**
|
|
164
|
+
* Parameters for rewriteSetCookies function
|
|
165
|
+
*/
|
|
166
|
+
interface RewriteSetCookiesParams {
|
|
167
|
+
/** the hostname (host+port) under which the Express app is running (e.g. localhost:3443 for a local dev server) */
|
|
168
|
+
appHostname: string;
|
|
169
|
+
/** Array of set-cookie header values */
|
|
170
|
+
setCookies: string[];
|
|
171
|
+
/** the target hostname (host+port) */
|
|
172
|
+
targetHost: string;
|
|
173
|
+
/** true to log operations */
|
|
174
|
+
logging?: boolean;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Given a headers object, rewrite any set-cookie headers in it
|
|
178
|
+
* so that they apply to the app hostname rather than the target
|
|
179
|
+
* hostname.
|
|
180
|
+
*
|
|
181
|
+
* @private
|
|
182
|
+
* @param params Configuration object for rewriting set-cookies
|
|
183
|
+
* @returns string[] of rewritten set-cookie header values
|
|
184
|
+
*/
|
|
185
|
+
export declare const rewriteSetCookies: ({ appHostname, setCookies, targetHost, logging, }: RewriteSetCookiesParams) => string[];
|
|
186
|
+
/**
|
|
187
|
+
* Parameters for rewriteProxyResponseHeaders function
|
|
188
|
+
*/
|
|
189
|
+
interface RewriteProxyResponseHeadersParams {
|
|
190
|
+
/** the hostname (host+port) under which the Express app is running (e.g. localhost:3443 for a local dev server) */
|
|
191
|
+
appHostname: string;
|
|
192
|
+
/** true for a caching proxy, false for a standard proxy */
|
|
193
|
+
caching: boolean;
|
|
194
|
+
/** the headers to be rewritten */
|
|
195
|
+
headers: AWSHeaders | HTTPHeaders | IncomingHttpHeaders;
|
|
196
|
+
/** 'aws' or 'http' - the format of the 'headers' parameter */
|
|
197
|
+
headerFormat?: HeaderFormat;
|
|
198
|
+
/** the path being proxied (e.g. /mobify/proxy/base/) */
|
|
199
|
+
proxyPath: string;
|
|
200
|
+
/** the URL from the request that prompted the response. If present, used to set the X-Proxy-Request-Url header. This should be the request URL sent to the target host, not containing any /mobify/proxy/... part. */
|
|
201
|
+
requestUrl?: string;
|
|
202
|
+
/** the protocol to use to make requests to the target ('http' or 'https') */
|
|
203
|
+
targetProtocol: string;
|
|
204
|
+
/** the target hostname (host+port) */
|
|
205
|
+
targetHost: string;
|
|
206
|
+
/** the protocol to use to make requests to the origin ('http' or 'https', defaults to 'https'), use of unencrypted protocol is only allowed in local development */
|
|
207
|
+
appProtocol?: string;
|
|
208
|
+
/** true to log operations */
|
|
209
|
+
logging?: boolean;
|
|
210
|
+
/** the response status code */
|
|
211
|
+
statusCode?: number;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Rewrite headers for a proxied response.
|
|
215
|
+
*
|
|
216
|
+
* 1. If the original domain appears in the
|
|
217
|
+
* Access-Control-Allow-Origin header, it's replaced with the
|
|
218
|
+
* appOrigin.
|
|
219
|
+
* 2. If the response is a 30x redirection and contains a Location
|
|
220
|
+
* header on the target host, that header is rewritten to use the
|
|
221
|
+
* app host and proxy path.
|
|
222
|
+
*
|
|
223
|
+
* For a caching proxy, we also remove any Set-Cookie headers - caching
|
|
224
|
+
* proxies don't pass Cookie headers for requests and don't allow Set-Cookie
|
|
225
|
+
* in responses, so that they may be cached independently of any cookie
|
|
226
|
+
* values.
|
|
227
|
+
*
|
|
228
|
+
* @private
|
|
229
|
+
* @param params Configuration object for rewriting proxy response headers
|
|
230
|
+
* @returns the modified response headers
|
|
231
|
+
*/
|
|
232
|
+
export declare const rewriteProxyResponseHeaders: ({ appHostname, caching, headers, headerFormat, proxyPath, requestUrl, statusCode, targetProtocol, targetHost, appProtocol, logging, }: RewriteProxyResponseHeadersParams) => AWSHeaders | HTTPHeaders | IncomingHttpHeaders;
|
|
233
|
+
/**
|
|
234
|
+
* List of x- headers that are removed from proxied requests.
|
|
235
|
+
* @private
|
|
236
|
+
*/
|
|
237
|
+
export declare const X_HEADERS_TO_REMOVE_PROXY: string[];
|
|
238
|
+
/**
|
|
239
|
+
* List of x- headers that are removed from origin requests.
|
|
240
|
+
* @private
|
|
241
|
+
*/
|
|
242
|
+
export declare const X_HEADERS_TO_REMOVE_ORIGIN: string[];
|
|
243
|
+
/**
|
|
244
|
+
* X-header key and values to add to proxied requests
|
|
245
|
+
* @private
|
|
246
|
+
*/
|
|
247
|
+
export declare const X_HEADERS_TO_ADD: Record<string, string>;
|
|
248
|
+
/**
|
|
249
|
+
* List of headers that are allowed for a caching proxy request.
|
|
250
|
+
* This must match the allowlist that CloudFront uses for a
|
|
251
|
+
* CacheBehavior that does not pass cookies and is not configured
|
|
252
|
+
* to cache based on headers.
|
|
253
|
+
*
|
|
254
|
+
* This is a map from lower-case header name to 'true' - we use an object
|
|
255
|
+
* to make lookups fast, since this mapping might be used for many requests.
|
|
256
|
+
*
|
|
257
|
+
* Also see what is configured in the SSR Manager (ssr-infrastructure repo),
|
|
258
|
+
* in the CloudFront configuration. This list is a superset of that list,
|
|
259
|
+
* since the proxying code must also allow headers that it adds, such as
|
|
260
|
+
* Host, Origin, etc.
|
|
261
|
+
*
|
|
262
|
+
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html
|
|
263
|
+
* See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Cookies.html
|
|
264
|
+
*
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
export declare const ALLOWED_CACHING_PROXY_REQUEST_HEADERS: Record<string, boolean>;
|
|
268
|
+
/**
|
|
269
|
+
* Parameters for rewriteProxyRequestHeaders function
|
|
270
|
+
*/
|
|
271
|
+
interface RewriteProxyRequestHeadersParams {
|
|
272
|
+
/** true for a caching proxy, false for a standard proxy */
|
|
273
|
+
caching?: boolean;
|
|
274
|
+
/** the headers to be rewritten */
|
|
275
|
+
headers?: AWSHeaders | HTTPHeaders | IncomingHttpHeaders;
|
|
276
|
+
/** 'aws' or 'http' - the format of the 'headers' parameter */
|
|
277
|
+
headerFormat?: HeaderFormat;
|
|
278
|
+
/** the protocol to use to make requests to the target ('http' or 'https') */
|
|
279
|
+
targetProtocol: string;
|
|
280
|
+
/** the target hostname (host+port) */
|
|
281
|
+
targetHost: string;
|
|
282
|
+
/** true to log operations */
|
|
283
|
+
logging?: boolean;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Rewrite headers for a request that is being proxied.
|
|
287
|
+
*
|
|
288
|
+
* 1. If the request contains a Host header, rewrite it so that the
|
|
289
|
+
* value is the target host.
|
|
290
|
+
* 2. If the request contains an Origin header, rewrite it so that the
|
|
291
|
+
* value is the target host.
|
|
292
|
+
* 3. ALL other header values are left unchanged. If they are multi-value
|
|
293
|
+
* headers whose values are stored as arrays, the values are left as arrays.
|
|
294
|
+
*
|
|
295
|
+
* @private
|
|
296
|
+
* @param params Configuration object for rewriting proxy request headers
|
|
297
|
+
* @returns the modified request headers
|
|
298
|
+
*/
|
|
299
|
+
export declare const rewriteProxyRequestHeaders: ({ caching, headers, headerFormat, targetProtocol, targetHost, logging, }: RewriteProxyRequestHeadersParams) => AWSHeaders | HTTPHeaders | IncomingHttpHeaders;
|
|
300
|
+
export {};
|