Skip to content

Commit 272de3e

Browse files
zburkekaladay
andauthored
STCLI-247 improve proxying by overwriting CORS headers, static alias for localhost (#351)
There are two features here: 1. provide new CLI option `--proxyUrl` to allow use of a hostname other than localhost, allowing the machine hosting the bundle to be accessed remotely (e.g. from a conference room, or by a colleague in another office, etc etc) 2. overwrite CORS headers between the proxy and browser, satisfying the browser that CORS requirements are being met (shhhhh) Details on Part 2: Overwrite the following CORS headers between the proxy and browser: ``` Access-Control-Allow-Origin: http://localhost:${PORT} Access-Control-Allow-Credentials: true ``` The ACAO value is commonly set to `*` for un-credentialed requests (i.e. those without cookies), but as MDN docs for CORS notes: > When responding to a credentialed requests request, the server must > specify an origin in the value of the `Access-Control-Allow-Origin` > header, instead of specifying the “*” wildcard. Likewise, the ACAC value > is commonly set to `""` for uncredentialed requests, but must be set to > `true` to allow cookies to pass through. (from https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) These CORS settings appear to have been in place before RTR was introduced, and may still be in place in some backend environments. Overriding these values in the local proxy is a prudent way to allow local development to continue while waiting for the backend settings to catch up. Refs STCLI-247 --------- Co-authored-by: Kevin Day <[email protected]>
1 parent 48e31cc commit 272de3e

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

doc/commands.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ Option | Description | Type | Notes
11321132
`--stripesConfig` | Stripes config JSON | string | supports stdin
11331133
`--tenant` | Specify a tenant ID | string |
11341134
`--startProxy` | Start a local proxy server between the platform and okapi | boolean | default: false
1135+
`--proxyHost` | Scheme and host name for the proxy server | string | default: http://localhost
11351136
`--proxyPort` | Port number for the proxy server | number | default: 3010
11361137

11371138
Examples:
@@ -1150,7 +1151,7 @@ $ stripes serve --existing-build output
11501151
```
11511152
Serve a platform with a local proxy server that points to a remote okapi server:
11521153
```
1153-
$ stripes serve --startProxy --proxyPort 3010 --okapi http://some-okapi-server.folio.org
1154+
$ stripes serve --startProxy --proxyHost http://localhost --proxyPort 3010 --okapi http://some-okapi-server.folio.org
11541155
```
11551156
Serve an app (in app context) with a mock backend server":
11561157
```

lib/commands/common-options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ module.exports.serverOptions = {
1111
default: false,
1212
group: 'Server Options:',
1313
},
14+
proxyHost: {
15+
type: 'string',
16+
describe: 'Proxy scheme and host',
17+
default: 'http://localhost',
18+
group: 'Server Options:',
19+
},
1420
proxyPort: {
1521
type: 'number',
1622
describe: 'Proxy server port',

lib/commands/serve.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ const serveBuildOptions = Object.assign({}, buildOptions);
1515
delete serveBuildOptions.publicPath;
1616

1717
function replaceArgvOkapiWithProxyURL(argv) {
18-
const proxyURL = `http://localhost:${argv.proxyPort}`;
19-
argv.okapi = proxyURL;
18+
argv.okapi = `${argv.proxyHost}:${argv.proxyPort}`;
2019

2120
if (argv.stripesConfig?.okapi) {
22-
argv.stripesConfig.okapi.url = proxyURL;
21+
argv.stripesConfig.okapi.url = argv.okapi;
2322
}
2423
}
2524

@@ -52,7 +51,7 @@ function serveCommand(argv) {
5251

5352
if (argv.startProxy) {
5453
console.info('starting proxy');
55-
childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.proxyPort]);
54+
childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.port, argv.proxyHost, argv.proxyPort]);
5655
// if we're using a proxy server - we need to pass the proxy host as okapi to Stripes platform
5756
replaceArgvOkapiWithProxyURL(argv);
5857
}

lib/run-proxy.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ const app = express();
55

66
const OKAPI = process.argv[2];
77
const PORT = process.argv[3];
8+
const PROXY_HOST = process.argv[4];
9+
const PROXY_PORT = process.argv[5];
810

911
app.use(
1012
'/',
1113
createProxyMiddleware({
1214
target: OKAPI,
1315
changeOrigin: true,
16+
on: {
17+
proxyRes: (proxyRes) => {
18+
proxyRes.headers['Access-Control-Allow-Origin'] = `${PROXY_HOST}:${PORT}`;
19+
proxyRes.headers['Access-Control-Allow-Credentials'] = 'true';
20+
},
21+
},
1422
}),
1523
);
1624

17-
app.listen(PORT);
25+
app.listen(PROXY_PORT);

0 commit comments

Comments
 (0)