@lwrjs/dev-proxy-server 0.17.2-alpha.3 → 0.17.2-alpha.31

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.
@@ -63,15 +63,23 @@ function parseHostAndProto(urlString) {
63
63
  proto: "https"
64
64
  };
65
65
  }
66
- function addLocalProxyMiddleware(app, defaultHost, remoteOrigin) {
66
+ function addLocalProxyMiddleware(app, config) {
67
+ const {defaultHost, basePath, remoteOrigin} = config;
67
68
  const domainAndPort = parseHostAndProto(defaultHost).host;
68
- app.use("/", (0, import_http_proxy_middleware.createProxyMiddleware)({
69
+ const hasBasePath = basePath && basePath !== "/";
70
+ app.use(basePath || "/", (0, import_http_proxy_middleware.createProxyMiddleware)({
69
71
  target: defaultHost,
70
72
  changeOrigin: true,
73
+ pathRewrite: (path, req) => {
74
+ return hasBasePath ? path.replace(basePath, "") : path;
75
+ },
71
76
  onProxyReq: (proxyRequest, incomingRequest) => {
72
77
  const hostHeader = incomingRequest.headers.host;
73
78
  if (hostHeader !== domainAndPort) {
74
79
  proxyRequest.setHeader(HOST, domainAndPort);
80
+ if (hasBasePath) {
81
+ proxyRequest.setHeader("X-Mobify-Request-Class", `basePath=${basePath}`);
82
+ }
75
83
  const forwarded = incomingRequest.headers.forwarded;
76
84
  proxyRequest.setHeader("Forwarded", forwarded ? forwarded : getForwardedHeader(incomingRequest, remoteOrigin));
77
85
  if (process.env.AUTH_TOKEN && !proxyRequest.path.startsWith("/mobify/bundle/")) {
@@ -88,7 +96,7 @@ function addRemoteProxyMiddleware(app, proxyConfig) {
88
96
  const [path, target] = entry;
89
97
  const targetHost = parseHostAndProto(target).host;
90
98
  const pathRewrite = mobifyRules && (path.indexOf("/mobify/proxy") === 0 || path.indexOf("/mobify/caching") === 0) ? {[path]: ""} : void 0;
91
- app.use(path, (0, import_http_proxy_middleware.createProxyMiddleware)({
99
+ const middleware = (0, import_http_proxy_middleware.createProxyMiddleware)({
92
100
  target,
93
101
  pathRewrite,
94
102
  logLevel: resolveLogLevel(import_diagnostics.logger.getLogLevel()),
@@ -109,16 +117,13 @@ function addRemoteProxyMiddleware(app, proxyConfig) {
109
117
  proxyRequest.path = originalPath;
110
118
  if (process.env.AUTH_TOKEN) {
111
119
  addAuthCookie(proxyRequest, process.env.AUTH_TOKEN);
112
- if (process.env.PROXY_AS_GUEST !== "true") {
113
- const reqUrl = new URL(incomingRequest.url || "", `http://${incomingRequest.headers.host}`);
114
- if (reqUrl.searchParams.get("asGuest") === "true") {
115
- reqUrl.searchParams.set("asGuest", "false");
116
- proxyRequest.path = reqUrl.pathname + reqUrl.search;
117
- }
118
- }
119
120
  }
120
121
  }
121
- }));
122
+ });
123
+ if (proxyConfig.basePath) {
124
+ app.use(proxyConfig.basePath + path, middleware);
125
+ }
126
+ app.use(path, middleware);
122
127
  }
123
128
  }
124
129
  function resolveLogLevel(currentLevel) {
@@ -31,9 +31,11 @@ var import_middleware = __toModule(require("./middleware.cjs"));
31
31
  var LwrProxyServer = class {
32
32
  constructor(config) {
33
33
  this.proxyServer = (0, import_express.default)();
34
+ if (config.basePath && !config.basePath.startsWith("/"))
35
+ config.basePath = "/" + config.basePath;
34
36
  this.config = config;
35
37
  (0, import_middleware.addRemoteProxyMiddleware)(this.proxyServer, this.config);
36
- (0, import_middleware.addLocalProxyMiddleware)(this.proxyServer, this.config.defaultHost, this.config.remoteOrigin);
38
+ (0, import_middleware.addLocalProxyMiddleware)(this.proxyServer, this.config);
37
39
  }
38
40
  async listen(callback) {
39
41
  const {proxyServer, config} = this;
@@ -1,5 +1,5 @@
1
1
  import type { Application as ExpressApp } from 'express';
2
- import type { LwrProxyConfiguration } from './proxy-server.js';
2
+ import type { LocalProxyServerConfig, LwrProxyConfiguration } from './proxy-server.js';
3
3
  /**
4
4
  * Proxy middleware for handling requests that are served by the LWR server or lambda.
5
5
  *
@@ -7,7 +7,7 @@ import type { LwrProxyConfiguration } from './proxy-server.js';
7
7
  * @param defaultHost - A URL string pointing to the local proxy server for handling requests.
8
8
  * @param remoteOrigin - A URL string representing the value of the first entry in the `_proxy` configuration file.
9
9
  */
10
- export declare function addLocalProxyMiddleware(app: ExpressApp, defaultHost: string, remoteOrigin?: string): void;
10
+ export declare function addLocalProxyMiddleware(app: ExpressApp, config: LocalProxyServerConfig): void;
11
11
  /**
12
12
  * Middleware to handle API request proxying to a configured remote origin.
13
13
  *
@@ -77,12 +77,18 @@ function parseHostAndProto(urlString) {
77
77
  * @param defaultHost - A URL string pointing to the local proxy server for handling requests.
78
78
  * @param remoteOrigin - A URL string representing the value of the first entry in the `_proxy` configuration file.
79
79
  */
80
- export function addLocalProxyMiddleware(app, defaultHost, remoteOrigin) {
80
+ export function addLocalProxyMiddleware(app, config) {
81
+ const { defaultHost, basePath, remoteOrigin } = config;
81
82
  // Extract the domain and port from the defaultHost URL string.
82
83
  const domainAndPort = parseHostAndProto(defaultHost).host;
83
- app.use('/', createProxyMiddleware({
84
+ const hasBasePath = basePath && basePath !== '/';
85
+ app.use(basePath || '/', createProxyMiddleware({
84
86
  target: defaultHost,
85
87
  changeOrigin: true,
88
+ pathRewrite: (path, req) => {
89
+ // Strip off the basePath from the request URL
90
+ return hasBasePath ? path.replace(basePath, '') : path;
91
+ },
86
92
  onProxyReq: (proxyRequest, incomingRequest) => {
87
93
  // Set the Host header to the target
88
94
  const hostHeader = incomingRequest.headers.host;
@@ -90,6 +96,10 @@ export function addLocalProxyMiddleware(app, defaultHost, remoteOrigin) {
90
96
  if (hostHeader !== domainAndPort) {
91
97
  // Setting the Host Header to the domain and port for the proxy server URL
92
98
  proxyRequest.setHeader(HOST, domainAndPort);
99
+ if (hasBasePath) {
100
+ // Add X-Mobify-Request-Class header with basePath value
101
+ proxyRequest.setHeader('X-Mobify-Request-Class', `basePath=${basePath}`);
102
+ }
93
103
  // Add the `Forwarded` header if it is not already set.
94
104
  const forwarded = incomingRequest.headers.forwarded;
95
105
  proxyRequest.setHeader('Forwarded',
@@ -125,7 +135,7 @@ export function addRemoteProxyMiddleware(app, proxyConfig) {
125
135
  const pathRewrite = mobifyRules && (path.indexOf('/mobify/proxy') === 0 || path.indexOf('/mobify/caching') === 0)
126
136
  ? { [path]: '' }
127
137
  : undefined;
128
- app.use(path, createProxyMiddleware({
138
+ const middleware = createProxyMiddleware({
129
139
  target,
130
140
  pathRewrite,
131
141
  logLevel: resolveLogLevel(logger.getLogLevel()),
@@ -154,20 +164,16 @@ export function addRemoteProxyMiddleware(app, proxyConfig) {
154
164
  // Retrieve the current cookie header
155
165
  if (process.env.AUTH_TOKEN) {
156
166
  addAuthCookie(proxyRequest, process.env.AUTH_TOKEN);
157
- // TODO once local-dev bundles are updated appropriately we can remove this
158
- // Remove asGuest from query parameters as that can cause issues with certain requests
159
- // Example Request for CMS image that fails with asGuest=true
160
- // /services/data/v62.0/connect/sites/0DMSB000000jBc24AE/cms/delivery/contents?includeContentBody=true&contentKeys=MCB6JD6GWNRVFYHN2CAP5WQNOXQE&language=en-US&asGuest=true&htmlEncode=false
161
- if (process.env.PROXY_AS_GUEST !== 'true') {
162
- const reqUrl = new URL(incomingRequest.url || '', `http://${incomingRequest.headers.host}`);
163
- if (reqUrl.searchParams.get('asGuest') === 'true') {
164
- reqUrl.searchParams.set('asGuest', 'false');
165
- proxyRequest.path = reqUrl.pathname + reqUrl.search;
166
- }
167
- }
168
167
  }
169
168
  },
170
- }));
169
+ });
170
+ // Proxy all configured routes at the basePath path (if one exists)
171
+ if (proxyConfig.basePath) {
172
+ app.use(proxyConfig.basePath + path, middleware);
173
+ }
174
+ // Also proxy the same routes at the root (probably not needed for all routes but doesn't hurt)
175
+ // this covers the /cdn-cgi scenario (which seems to always be requested at the root)
176
+ app.use(path, middleware);
171
177
  }
172
178
  }
173
179
  function resolveLogLevel(currentLevel) {
@@ -2,12 +2,18 @@ import { Application as ExpressApp } from 'express';
2
2
  export interface LwrProxyConfiguration {
3
3
  proxyConfigs: ProxyConfigDefinition[];
4
4
  remoteOrigin?: string;
5
+ basePath?: string;
5
6
  }
6
7
  type ProxyConfigDefinition = Array<string>;
7
8
  export interface LwrProxyServerConfig extends LwrProxyConfiguration {
8
9
  port: number;
9
10
  defaultHost: string;
10
11
  }
12
+ export interface LocalProxyServerConfig {
13
+ defaultHost: string;
14
+ basePath?: string;
15
+ remoteOrigin?: string;
16
+ }
11
17
  /**
12
18
  * A simple configuration-enabled local development proxy server.
13
19
  */
@@ -4,11 +4,15 @@ import { addLocalProxyMiddleware, addRemoteProxyMiddleware } from './middleware.
4
4
  * A simple configuration-enabled local development proxy server.
5
5
  */
6
6
  export class LwrProxyServer {
7
+ proxyServer;
8
+ config;
7
9
  constructor(config) {
8
10
  this.proxyServer = express();
11
+ if (config.basePath && !config.basePath.startsWith('/'))
12
+ config.basePath = '/' + config.basePath;
9
13
  this.config = config;
10
14
  addRemoteProxyMiddleware(this.proxyServer, this.config);
11
- addLocalProxyMiddleware(this.proxyServer, this.config.defaultHost, this.config.remoteOrigin);
15
+ addLocalProxyMiddleware(this.proxyServer, this.config);
12
16
  }
13
17
  async listen(callback) {
14
18
  const { proxyServer, config } = this;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.17.2-alpha.3",
7
+ "version": "0.17.2-alpha.31",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -33,19 +33,19 @@
33
33
  "build": "tsc -b"
34
34
  },
35
35
  "dependencies": {
36
- "@lwrjs/diagnostics": "0.17.2-alpha.3",
37
- "@lwrjs/shared-utils": "0.17.2-alpha.3",
36
+ "@lwrjs/diagnostics": "0.17.2-alpha.31",
37
+ "@lwrjs/shared-utils": "0.17.2-alpha.31",
38
38
  "express": "^4.20.0",
39
39
  "http-proxy-middleware": "2.0.7"
40
40
  },
41
41
  "devDependencies": {
42
- "@lwrjs/types": "0.17.2-alpha.3"
42
+ "@lwrjs/types": "0.17.2-alpha.31"
43
43
  },
44
44
  "engines": {
45
- "node": ">=18.0.0"
45
+ "node": ">=20.0.0"
46
46
  },
47
47
  "volta": {
48
48
  "extends": "../../../package.json"
49
49
  },
50
- "gitHead": "43757693dfca356cff105d4896a7a3cbf11ac017"
50
+ "gitHead": "bbb087ab080321047b10bbcda40e88e69b6a69a5"
51
51
  }