@lwrjs/dev-proxy-server 0.16.10 → 0.16.12

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()),
@@ -111,7 +119,11 @@ function addRemoteProxyMiddleware(app, proxyConfig) {
111
119
  addAuthCookie(proxyRequest, process.env.AUTH_TOKEN);
112
120
  }
113
121
  }
114
- }));
122
+ });
123
+ if (proxyConfig.basePath) {
124
+ app.use(proxyConfig.basePath + path, middleware);
125
+ }
126
+ app.use(path, middleware);
115
127
  }
116
128
  }
117
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()),
@@ -156,7 +166,14 @@ export function addRemoteProxyMiddleware(app, proxyConfig) {
156
166
  addAuthCookie(proxyRequest, process.env.AUTH_TOKEN);
157
167
  }
158
168
  },
159
- }));
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);
160
177
  }
161
178
  }
162
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
  */
@@ -6,9 +6,11 @@ import { addLocalProxyMiddleware, addRemoteProxyMiddleware } from './middleware.
6
6
  export class LwrProxyServer {
7
7
  constructor(config) {
8
8
  this.proxyServer = express();
9
+ if (config.basePath && !config.basePath.startsWith('/'))
10
+ config.basePath = '/' + config.basePath;
9
11
  this.config = config;
10
12
  addRemoteProxyMiddleware(this.proxyServer, this.config);
11
- addLocalProxyMiddleware(this.proxyServer, this.config.defaultHost, this.config.remoteOrigin);
13
+ addLocalProxyMiddleware(this.proxyServer, this.config);
12
14
  }
13
15
  async listen(callback) {
14
16
  const { proxyServer, config } = this;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.16.10",
7
+ "version": "0.16.12",
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.16.10",
37
- "@lwrjs/shared-utils": "0.16.10",
36
+ "@lwrjs/diagnostics": "0.16.12",
37
+ "@lwrjs/shared-utils": "0.16.12",
38
38
  "express": "^4.20.0",
39
39
  "http-proxy-middleware": "2.0.7"
40
40
  },
41
41
  "devDependencies": {
42
- "@lwrjs/types": "0.16.10"
42
+ "@lwrjs/types": "0.16.12"
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": "03ff25c3eb0445af45adeec09d0295115156724b"
50
+ "gitHead": "53e1f3a51a839e565a77d9f7a9a781fc5a515147"
51
51
  }