diff --git a/CHANGELOG.md b/CHANGELOG.md index f8152f3c..6c961038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 3.2.0 IN PROGRESS +* Add a proxy server to overcome issues with cookies SameSite policy. Refs STCLI-246. + ## [3.1.0](https://github.com/folio-org/stripes-cli/tree/v3.1.0) (2024-03-12) [Full Changelog](https://github.com/folio-org/stripes-cli/compare/v3.0.0...v3.1.0) diff --git a/doc/commands.md b/doc/commands.md index fd41059a..94f1e6e7 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -1131,6 +1131,8 @@ Option | Description | Type | Notes `--port` | Development server port | number | default: 3000 `--stripesConfig` | Stripes config JSON | string | supports stdin `--tenant` | Specify a tenant ID | string | +`--startProxy` | Start a local proxy server between the platform and okapi | boolean | default: false +`--proxyPort` | Port number for the proxy server | number | default: 3010 Examples: @@ -1146,6 +1148,10 @@ Serve a build previously created with "stripes build": ``` $ stripes serve --existing-build output ``` +Serve a platform with a local proxy server that points to a remote okapi server: +``` +$ stripes serve --startProxy --proxyPort 3010 --okapi http://some-okapi-server.folio.org +``` Serve an app (in app context) with a mock backend server": ``` $ stripes serve --mirage diff --git a/lib/commands/common-options.js b/lib/commands/common-options.js index 6d9177f3..00b8bbb6 100644 --- a/lib/commands/common-options.js +++ b/lib/commands/common-options.js @@ -5,6 +5,18 @@ module.exports.serverOptions = { default: 3000, group: 'Server Options:', }, + startProxy: { + type: 'boolean', + describe: 'Start a proxy server', + default: false, + group: 'Server Options:', + }, + proxyPort: { + type: 'number', + describe: 'Proxy server port', + default: 3010, + group: 'Server Options:', + }, host: { type: 'string', describe: 'Development server host', diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 4d9e6bd0..00300e28 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -1,4 +1,6 @@ const importLazy = require('import-lazy')(require); +const childProcess = require('child_process'); +const path = require('path'); const { contextMiddleware } = importLazy('../cli/context-middleware'); const { stripesConfigMiddleware } = importLazy('../cli/stripes-config-middleware'); @@ -12,6 +14,15 @@ const server = importLazy('../server'); const serveBuildOptions = Object.assign({}, buildOptions); delete serveBuildOptions.publicPath; +function replaceArgvOkapiWithProxyURL(argv) { + const proxyURL = `http://localhost:${argv.proxyPort}`; + argv.okapi = proxyURL; + + if (argv.stripesConfig?.okapi) { + argv.stripesConfig.okapi.url = proxyURL; + } +} + function serveCommand(argv) { const context = argv.context; // Default serve command to development env @@ -39,6 +50,13 @@ function serveCommand(argv) { return; } + if (argv.startProxy) { + console.info('starting proxy'); + childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.proxyPort]); + // if we're using a proxy server - we need to pass the proxy host as okapi to Stripes platform + replaceArgvOkapiWithProxyURL(argv); + } + const platform = new StripesPlatform(argv.stripesConfig, context, argv); const webpackOverrides = platform.getWebpackOverrides(context); diff --git a/lib/platform/tenant-config.js b/lib/platform/tenant-config.js index 370c969e..ee6f4f65 100644 --- a/lib/platform/tenant-config.js +++ b/lib/platform/tenant-config.js @@ -11,6 +11,7 @@ const defaultConfig = { showPerms: false, hasAllPerms: false, languages: ['en'], + useSecureTokens: true, }, modules: { }, diff --git a/lib/run-proxy.js b/lib/run-proxy.js new file mode 100644 index 00000000..31e62a17 --- /dev/null +++ b/lib/run-proxy.js @@ -0,0 +1,17 @@ +const express = require('express'); +const { createProxyMiddleware } = require('http-proxy-middleware'); + +const app = express(); + +const OKAPI = process.argv[2]; +const PORT = process.argv[3]; + +app.use( + '/', + createProxyMiddleware({ + target: OKAPI, + changeOrigin: true, + }), +); + +app.listen(PORT); diff --git a/package.json b/package.json index 2a8ac27a..57aed750 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "fs-extra": "^11.1.1", "get-stdin": "^6.0.0", "global-dirs": "^0.1.1", + "http-proxy-middleware": "^3.0.0", "import-lazy": "^3.1.0", "inquirer": "^8.2.0", "is-installed-globally": "^0.4.0",