1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: Make compression middleware optional (#5306)

## Why
Currently AWS API Gateway doesn't have compression enabled by default,
this PR will make it easier to for example deploy Unleash over to AWS
Lambda without further configuration in API Gateway, frameworks like
Serverless requires a bit more work to set up compression and some times
one might not need compression at all.

## How
Create a new config flag called `disableCompression` which will not
include `compression` middleware in express' instance when set as true.
This commit is contained in:
Pedro Papadopolis 2023-12-16 18:06:26 +11:00 committed by GitHub
parent d41e86771f
commit 24c2a70138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 1 deletions

View File

@ -148,6 +148,7 @@ exports[`should create default config 1`] = `
"server": {
"baseUriPath": "",
"cdnPrefix": undefined,
"disableCompression": false,
"enableHeapSnapshotEnpoint": false,
"enableRequestLogger": false,
"gracefulShutdownEnable": true,

View File

@ -1,5 +1,12 @@
import express from 'express';
import { createTestConfig } from '../test/config/test-config';
import compression from 'compression';
jest.mock('compression', () =>
jest.fn().mockImplementation(() => (req, res, next) => {
next();
}),
);
jest.mock(
'./routes',
@ -40,3 +47,43 @@ test('should call preRouterHook', async () => {
await getApp(config, {}, {});
expect(called).toBe(1);
});
describe('compression middleware', () => {
beforeAll(() => {
(compression as jest.Mock).mockClear();
});
afterEach(() => {
(compression as jest.Mock).mockClear();
});
test.each([
{
disableCompression: true,
expectCompressionEnabled: false,
},
{
disableCompression: false,
expectCompressionEnabled: true,
},
{
disableCompression: null,
expectCompressionEnabled: true,
},
{
disableCompression: undefined,
expectCompressionEnabled: true,
},
])(
`should expect the compression middleware to be $expectCompressionEnabled when configInput.server.disableCompression is $disableCompression`,
async ({ disableCompression, expectCompressionEnabled }) => {
const config = createTestConfig({
server: {
disableCompression: disableCompression as any,
},
});
await getApp(config, {}, {});
expect(compression).toBeCalledTimes(+expectCompressionEnabled);
},
);
});

View File

@ -63,7 +63,10 @@ export default async function getApp(
config.preHook(app, config, services, db);
}
app.use(compression());
if (!config.server.disableCompression) {
app.use(compression());
}
app.use(cookieParser());
app.use((req, res, next) => {

View File

@ -217,6 +217,10 @@ const defaultServerOption: IServerOption = {
process.env.ENABLE_HEAP_SNAPSHOT_ENPOINT,
false,
),
disableCompression: parseEnvVarBoolean(
process.env.SERVER_DISABLE_COMPRESSION,
false,
),
keepAliveTimeout: secondsToMilliseconds(
parseEnvVarNumber(process.env.SERVER_KEEPALIVE_TIMEOUT, 15),
),

View File

@ -83,6 +83,7 @@ export interface IServerOption {
port?: number;
host?: string;
pipe?: string;
disableCompression?: boolean;
keepAliveTimeout: number;
headersTimeout: number;
baseUriPath: string;

View File

@ -137,6 +137,7 @@ unleash.start(unleashOptions);
- `refreshIntervalInMs` - how often (in milliseconds) front-end clients should refresh their data from the cache. Overridable with the `FRONTEND_API_REFRESH_INTERVAL_MS` environment variable.
- **accessControlMaxAge** - You can configure the max-age of the Access-Control-Max-Age header. Defaults to 86400 seconds. Overridable with the `ACCESS_CONTROL_MAX_AGE` environment variable.
- **responseTimeWithAppNameKillSwitch** - use this to disable metrics with app names. This is enabled by default but may increase the cardinality of metrics causing Unleash memory usage to grow if your app name is randomly generated (which is not recommended). Overridable with the `UNLEASH_RESPONSE_TIME_WITH_APP_NAME_KILL_SWITCH` environment variable.
- **disableCompression** - Disables Express compression middleware, useful when configuring the application in a serverless environment. Defaults to `false`. Overridable with the `SERVER_DISABLE_COMPRESSION` environment variable.
- **keepAliveTimeout** - Use this to tweak connection keepalive timeout in seconds. Useful for hosted situations where you need to make sure your connections are closed before terminating the instance. Defaults to `15`. Overridable with the `SERVER_KEEPALIVE_TIMEOUT` environment variable.
You can also set the environment variable `ENABLED_ENVIRONMENTS` to a comma delimited string of environment names to override environments.
- **metricsRateLimiting** - Use the following to tweak the rate limits for `/api/client/register`, `/api/client/metrics`, `/api/frontend/register` and `/api/frontend/metrics` POST endpoints