@lwrjs/dev-proxy-server 0.15.0-alpha.9 → 0.15.0
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/build/cjs/middleware.cjs
CHANGED
|
@@ -38,18 +38,23 @@ var ProxyLogLevels;
|
|
|
38
38
|
ProxyLogLevels2["Error"] = "error";
|
|
39
39
|
ProxyLogLevels2["Silent"] = "silent";
|
|
40
40
|
})(ProxyLogLevels || (ProxyLogLevels = {}));
|
|
41
|
-
function
|
|
41
|
+
function getForwardedHeader(incomingRequest, remoteOrigin) {
|
|
42
|
+
const remoteHost = remoteOrigin?.replace(/^([a-z][a-z0-9+\-.]*):\/\//, "");
|
|
43
|
+
const hostHeader = remoteHost || incomingRequest.headers.host;
|
|
44
|
+
const hostProto = remoteHost ? "https" : incomingRequest.protocol;
|
|
45
|
+
return `host=${hostHeader};proto=${hostProto}`;
|
|
46
|
+
}
|
|
47
|
+
function addLocalProxyMiddleware(app, defaultHost, remoteOrigin) {
|
|
42
48
|
const domainAndPort = defaultHost.replace(/^([a-z][a-z0-9+\-.]*):\/\//, "");
|
|
43
|
-
app.use("/", (0, import_http_proxy_middleware.
|
|
49
|
+
app.use("/", (0, import_http_proxy_middleware.createProxyMiddleware)({
|
|
44
50
|
target: defaultHost,
|
|
45
51
|
changeOrigin: true,
|
|
46
52
|
onProxyReq: (proxyRequest, incomingRequest) => {
|
|
47
|
-
const forwarded = incomingRequest.headers.forwarded;
|
|
48
53
|
const hostHeader = incomingRequest.headers.host;
|
|
49
|
-
const hostProto = incomingRequest.protocol;
|
|
50
54
|
if (hostHeader !== domainAndPort) {
|
|
51
55
|
proxyRequest.setHeader(HOST, domainAndPort);
|
|
52
|
-
|
|
56
|
+
const forwarded = incomingRequest.headers.forwarded;
|
|
57
|
+
proxyRequest.setHeader("Forwarded", forwarded ? forwarded : getForwardedHeader(incomingRequest, remoteOrigin));
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
}));
|
|
@@ -61,7 +66,7 @@ function addRemoteProxyMiddleware(app, proxyConfig) {
|
|
|
61
66
|
const [path, target] = entry;
|
|
62
67
|
const targetHost = target.replace(/^([a-z][a-z0-9+\-.]*):\/\//, "");
|
|
63
68
|
const pathRewrite = mobifyRules && (path.indexOf("/mobify/proxy") === 0 || path.indexOf("/mobify/caching") === 0) ? {[path]: ""} : void 0;
|
|
64
|
-
app.use(path, (0, import_http_proxy_middleware.
|
|
69
|
+
app.use(path, (0, import_http_proxy_middleware.createProxyMiddleware)({
|
|
65
70
|
target,
|
|
66
71
|
pathRewrite,
|
|
67
72
|
logLevel: resolveLogLevel(import_diagnostics.logger.getLogLevel()),
|
|
@@ -80,6 +85,23 @@ function addRemoteProxyMiddleware(app, proxyConfig) {
|
|
|
80
85
|
proxyRequest.setHeader("Forwarded", `host=${hostHeader}`);
|
|
81
86
|
const originalPath = incomingRequest.url;
|
|
82
87
|
proxyRequest.path = originalPath;
|
|
88
|
+
if (process.env.AUTH_TOKEN) {
|
|
89
|
+
let cookies = proxyRequest.getHeader("cookie") || "";
|
|
90
|
+
const sidCookie = `__Secure-has-sid=1; sid=${process.env.AUTH_TOKEN};`;
|
|
91
|
+
if (cookies) {
|
|
92
|
+
cookies += `; ${sidCookie}`;
|
|
93
|
+
} else {
|
|
94
|
+
cookies = sidCookie;
|
|
95
|
+
}
|
|
96
|
+
proxyRequest.setHeader("cookie", cookies);
|
|
97
|
+
if (process.env.PROXY_AS_GUEST !== "true") {
|
|
98
|
+
const reqUrl = new URL(incomingRequest.url || "", `http://${incomingRequest.headers.host}`);
|
|
99
|
+
if (reqUrl.searchParams.get("asGuest") === "true") {
|
|
100
|
+
reqUrl.searchParams.set("asGuest", "false");
|
|
101
|
+
proxyRequest.path = reqUrl.pathname + reqUrl.search;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
83
105
|
}
|
|
84
106
|
}));
|
|
85
107
|
}
|
|
@@ -33,7 +33,7 @@ var LwrProxyServer = class {
|
|
|
33
33
|
this.proxyServer = (0, import_express.default)();
|
|
34
34
|
this.config = config;
|
|
35
35
|
(0, import_middleware.addRemoteProxyMiddleware)(this.proxyServer, this.config);
|
|
36
|
-
(0, import_middleware.addLocalProxyMiddleware)(this.proxyServer, this.config.defaultHost);
|
|
36
|
+
(0, import_middleware.addLocalProxyMiddleware)(this.proxyServer, this.config.defaultHost, this.config.remoteOrigin);
|
|
37
37
|
}
|
|
38
38
|
async listen(callback) {
|
|
39
39
|
const {proxyServer, config} = this;
|
package/build/es/middleware.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { LwrProxyConfiguration } from './proxy-server.js';
|
|
|
5
5
|
* @param app
|
|
6
6
|
* @param defaultHost
|
|
7
7
|
*/
|
|
8
|
-
export declare function addLocalProxyMiddleware(app: ExpressApp, defaultHost: string): void;
|
|
8
|
+
export declare function addLocalProxyMiddleware(app: ExpressApp, defaultHost: string, remoteOrigin?: string): void;
|
|
9
9
|
/**
|
|
10
10
|
* The Proxy middleware responsible for proxying requests to a configured remote origin.
|
|
11
11
|
* @param app
|
package/build/es/middleware.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
2
2
|
import { logger } from '@lwrjs/diagnostics';
|
|
3
3
|
// Header constants
|
|
4
4
|
const HOST = 'host';
|
|
@@ -11,26 +11,31 @@ var ProxyLogLevels;
|
|
|
11
11
|
ProxyLogLevels["Error"] = "error";
|
|
12
12
|
ProxyLogLevels["Silent"] = "silent";
|
|
13
13
|
})(ProxyLogLevels || (ProxyLogLevels = {}));
|
|
14
|
+
function getForwardedHeader(incomingRequest, remoteOrigin) {
|
|
15
|
+
// In prod, the Forwarded header is added by the CDN and is set to the Core org
|
|
16
|
+
const remoteHost = remoteOrigin?.replace(/^([a-z][a-z0-9+\-.]*):\/\//, '');
|
|
17
|
+
const hostHeader = remoteHost || incomingRequest.headers.host;
|
|
18
|
+
const hostProto = remoteHost ? 'https' : incomingRequest.protocol;
|
|
19
|
+
return `host=${hostHeader};proto=${hostProto}`;
|
|
20
|
+
}
|
|
14
21
|
/**
|
|
15
22
|
* The Proxy middleware responsible for proxying all other requests not configured to external systems.
|
|
16
23
|
* @param app
|
|
17
24
|
* @param defaultHost
|
|
18
25
|
*/
|
|
19
|
-
export function addLocalProxyMiddleware(app, defaultHost) {
|
|
26
|
+
export function addLocalProxyMiddleware(app, defaultHost, remoteOrigin) {
|
|
20
27
|
const domainAndPort = defaultHost.replace(/^([a-z][a-z0-9+\-.]*):\/\//, '');
|
|
21
|
-
app.use('/',
|
|
28
|
+
app.use('/', createProxyMiddleware({
|
|
22
29
|
target: defaultHost,
|
|
23
30
|
changeOrigin: true,
|
|
24
31
|
onProxyReq: (proxyRequest, incomingRequest) => {
|
|
25
|
-
//
|
|
26
|
-
const forwarded = incomingRequest.headers.forwarded;
|
|
27
|
-
// Host: <host>:<port>
|
|
32
|
+
// Set the Host header to the target
|
|
28
33
|
const hostHeader = incomingRequest.headers.host;
|
|
29
|
-
const hostProto = incomingRequest.protocol;
|
|
30
34
|
if (hostHeader !== domainAndPort) {
|
|
31
35
|
proxyRequest.setHeader(HOST, domainAndPort);
|
|
32
|
-
//
|
|
33
|
-
|
|
36
|
+
// Set the Forwarded header if it's not defined
|
|
37
|
+
const forwarded = incomingRequest.headers.forwarded;
|
|
38
|
+
proxyRequest.setHeader('Forwarded', forwarded ? forwarded : getForwardedHeader(incomingRequest, remoteOrigin));
|
|
34
39
|
}
|
|
35
40
|
},
|
|
36
41
|
}));
|
|
@@ -53,7 +58,7 @@ export function addRemoteProxyMiddleware(app, proxyConfig) {
|
|
|
53
58
|
const pathRewrite = mobifyRules && (path.indexOf('/mobify/proxy') === 0 || path.indexOf('/mobify/caching') === 0)
|
|
54
59
|
? { [path]: '' }
|
|
55
60
|
: undefined;
|
|
56
|
-
app.use(path,
|
|
61
|
+
app.use(path, createProxyMiddleware({
|
|
57
62
|
target,
|
|
58
63
|
pathRewrite,
|
|
59
64
|
logLevel: resolveLogLevel(logger.getLogLevel()),
|
|
@@ -77,6 +82,32 @@ export function addRemoteProxyMiddleware(app, proxyConfig) {
|
|
|
77
82
|
// Preserve the original path, including double slashes
|
|
78
83
|
const originalPath = incomingRequest.url;
|
|
79
84
|
proxyRequest.path = originalPath;
|
|
85
|
+
// Add auth token in cookies
|
|
86
|
+
// Retrieve the current cookie header
|
|
87
|
+
if (process.env.AUTH_TOKEN) {
|
|
88
|
+
let cookies = proxyRequest.getHeader('cookie') || '';
|
|
89
|
+
// Append the SID token to the cookies
|
|
90
|
+
const sidCookie = `__Secure-has-sid=1; sid=${process.env.AUTH_TOKEN};`;
|
|
91
|
+
if (cookies) {
|
|
92
|
+
cookies += `; ${sidCookie}`;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
cookies = sidCookie;
|
|
96
|
+
}
|
|
97
|
+
// Set our auth cookies on the proxied request
|
|
98
|
+
proxyRequest.setHeader('cookie', cookies);
|
|
99
|
+
// TODO once local-dev bundles are updated appropriately we can remove this
|
|
100
|
+
// Remove asGuest from query parameters as that can cause issues with certain requests
|
|
101
|
+
// Example Request for CMS image that fails with asGuest=true
|
|
102
|
+
// /services/data/v62.0/connect/sites/0DMSB000000jBc24AE/cms/delivery/contents?includeContentBody=true&contentKeys=MCB6JD6GWNRVFYHN2CAP5WQNOXQE&language=en-US&asGuest=true&htmlEncode=false
|
|
103
|
+
if (process.env.PROXY_AS_GUEST !== 'true') {
|
|
104
|
+
const reqUrl = new URL(incomingRequest.url || '', `http://${incomingRequest.headers.host}`);
|
|
105
|
+
if (reqUrl.searchParams.get('asGuest') === 'true') {
|
|
106
|
+
reqUrl.searchParams.set('asGuest', 'false');
|
|
107
|
+
proxyRequest.path = reqUrl.pathname + reqUrl.search;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
80
111
|
},
|
|
81
112
|
}));
|
|
82
113
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Application as ExpressApp } from 'express';
|
|
2
2
|
export interface LwrProxyConfiguration {
|
|
3
3
|
proxyConfigs: ProxyConfigDefinition[];
|
|
4
|
+
remoteOrigin?: string;
|
|
4
5
|
}
|
|
5
6
|
type ProxyConfigDefinition = Array<string>;
|
|
6
7
|
export interface LwrProxyServerConfig extends LwrProxyConfiguration {
|
package/build/es/proxy-server.js
CHANGED
|
@@ -8,7 +8,7 @@ export class LwrProxyServer {
|
|
|
8
8
|
this.proxyServer = express();
|
|
9
9
|
this.config = config;
|
|
10
10
|
addRemoteProxyMiddleware(this.proxyServer, this.config);
|
|
11
|
-
addLocalProxyMiddleware(this.proxyServer, this.config.defaultHost);
|
|
11
|
+
addLocalProxyMiddleware(this.proxyServer, this.config.defaultHost, this.config.remoteOrigin);
|
|
12
12
|
}
|
|
13
13
|
async listen(callback) {
|
|
14
14
|
const { proxyServer, config } = this;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.15.0
|
|
7
|
+
"version": "0.15.0",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
"build": "tsc -b"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@lwrjs/diagnostics": "0.15.0
|
|
37
|
-
"@lwrjs/shared-utils": "0.15.0
|
|
38
|
-
"express": "^4.
|
|
39
|
-
"http-proxy-middleware": "0.
|
|
36
|
+
"@lwrjs/diagnostics": "0.15.0",
|
|
37
|
+
"@lwrjs/shared-utils": "0.15.0",
|
|
38
|
+
"express": "^4.20.0",
|
|
39
|
+
"http-proxy-middleware": "2.0.7"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@lwrjs/types": "0.15.0
|
|
42
|
+
"@lwrjs/types": "0.15.0"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=18.0.0"
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"volta": {
|
|
48
48
|
"extends": "../../../package.json"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "ee374df435d5342f63e4da126a09461e761837f3"
|
|
51
51
|
}
|