From d92a752b66ba2e4e432ac7787a42daafd9bd1666 Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Wed, 18 Oct 2023 12:25:10 +0200 Subject: [PATCH] fix: PR comments, unneeded end() call and variable naming --- src/lib/create-config.ts | 24 +++++++++---------- src/lib/routes/client-api/metrics.ts | 2 +- src/lib/routes/client-api/register.ts | 2 +- src/lib/routes/proxy-api/index.ts | 5 ++-- src/lib/routes/util.ts | 8 ++++--- src/lib/types/option.ts | 8 +++---- .../reference/deploy/configuring-unleash.md | 8 +++---- 7 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 8c419ceb029c..a845d913c2e3 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -103,27 +103,27 @@ function loadClientCachingOptions( function loadMetricsRateLimitingConfig( options: IUnleashOptions, ): IMetricsRateLimiting { - const clientMetricsMax = parseEnvVarNumber( - process.env.REGISTER_CLIENT_RATE_LIMIT, + const clientMetricsMaxPerMinute = parseEnvVarNumber( + process.env.REGISTER_CLIENT_RATE_LIMIT_PER_MINUTE, 6000, ); - const clientRegisterMax: number = parseEnvVarNumber( - process.env.CLIENT_METRICS_RATE_LIMIT, + const clientRegisterMaxPerMinute = parseEnvVarNumber( + process.env.CLIENT_METRICS_RATE_LIMIT_PER_MINUTE, 6000, ); - const frontendRegisterMax = parseEnvVarNumber( - process.env.REGISTER_FRONTEND_RATE_LIMIT, + const frontendRegisterMaxPerMinute = parseEnvVarNumber( + process.env.REGISTER_FRONTEND_RATE_LIMIT_PER_MINUTE, 6000, ); - const frontendMetricsMax = parseEnvVarNumber( - process.env.FRONTEND_METRICS_RATE_LIMIT, + const frontendMetricsMaxPerMinute = parseEnvVarNumber( + process.env.FRONTEND_METRICS_RATE_LIMIT_PER_MINUTE, 6000, ); const defaultRateLimitOptions: IMetricsRateLimiting = { - clientMetricsMax, - clientRegisterMax, - frontendRegisterMax, - frontendMetricsMax, + clientMetricsMaxPerMinute: clientMetricsMaxPerMinute, + clientRegisterMaxPerMinute: clientRegisterMaxPerMinute, + frontendRegisterMaxPerMinute: frontendRegisterMaxPerMinute, + frontendMetricsMaxPerMinute: frontendMetricsMaxPerMinute, }; return mergeAll([ diff --git a/src/lib/routes/client-api/metrics.ts b/src/lib/routes/client-api/metrics.ts index bd2eff30c159..191419819c03 100644 --- a/src/lib/routes/client-api/metrics.ts +++ b/src/lib/routes/client-api/metrics.ts @@ -65,7 +65,7 @@ export default class ClientMetricsController extends Controller { }), rateLimit({ windowMs: minutesToMilliseconds(1), - max: config.metricsRateLimiting.clientMetricsMax, + max: config.metricsRateLimiting.clientMetricsMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, diff --git a/src/lib/routes/client-api/register.ts b/src/lib/routes/client-api/register.ts index 02e1aa8997e8..7f65844c1caa 100644 --- a/src/lib/routes/client-api/register.ts +++ b/src/lib/routes/client-api/register.ts @@ -52,7 +52,7 @@ export default class RegisterController extends Controller { }), rateLimit({ windowMs: minutesToMilliseconds(1), - max: config.metricsRateLimiting.clientRegisterMax, + max: config.metricsRateLimiting.clientRegisterMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, diff --git a/src/lib/routes/proxy-api/index.ts b/src/lib/routes/proxy-api/index.ts index bbf3e007d452..9cf758617fe4 100644 --- a/src/lib/routes/proxy-api/index.ts +++ b/src/lib/routes/proxy-api/index.ts @@ -116,7 +116,7 @@ export default class ProxyController extends Controller { }), rateLimit({ windowMs: minutesToMilliseconds(1), - max: config.metricsRateLimiting.frontendMetricsMax, + max: config.metricsRateLimiting.frontendMetricsMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, @@ -144,7 +144,8 @@ export default class ProxyController extends Controller { }), rateLimit({ windowMs: minutesToMilliseconds(1), - max: config.metricsRateLimiting.frontendRegisterMax, + max: config.metricsRateLimiting + .frontendRegisterMaxPerMinute, validate: false, standardHeaders: true, legacyHeaders: false, diff --git a/src/lib/routes/util.ts b/src/lib/routes/util.ts index d4ee2278a103..789092116932 100644 --- a/src/lib/routes/util.ts +++ b/src/lib/routes/util.ts @@ -30,9 +30,11 @@ export const handleErrors: ( ) => void = (res, logger, error) => { if (createError.isHttpError(error)) { return res - .status(error.status ?? 400) - .json({ message: error.message }) - .end(); + .status( + // @ts-expect-error - The error object here is not guaranteed to contain status + error.status ?? 400, + ) + .json({ message: error.message }); } const finalError = diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index f50d819983f3..ba39760e422a 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -187,10 +187,10 @@ interface IFrontendApi { } export interface IMetricsRateLimiting { - clientMetricsMax: number; - clientRegisterMax: number; - frontendMetricsMax: number; - frontendRegisterMax: number; + clientMetricsMaxPerMinute: number; + clientRegisterMaxPerMinute: number; + frontendMetricsMaxPerMinute: number; + frontendRegisterMaxPerMinute: number; } export interface IUnleashConfig { diff --git a/website/docs/reference/deploy/configuring-unleash.md b/website/docs/reference/deploy/configuring-unleash.md index e64087086d2b..790a3b13e7c7 100644 --- a/website/docs/reference/deploy/configuring-unleash.md +++ b/website/docs/reference/deploy/configuring-unleash.md @@ -140,10 +140,10 @@ unleash.start(unleashOptions); - **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 - - `clientMetricsMax` - How many requests per minute is allowed against POST `/api/client/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_CLIENT_RATE_LIMIT` environment variable - - `clientRegisterMax` - How many requests per minute is allowed against POST `/api/client/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `CLIENT_METRICS_RATE_LIMIT` environment variable - - `frontendMetricsMax` - How many requests per minute is allowed against POST `/api/frontend/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `FRONTEND_METRICS_RATE_LIMIT` environment variable - - `frontendRegisterMax` - How many requests per minute is allowed against POST `/api/frontend/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_FRONTEND_RATE_LIMIT` environment variable + - `clientMetricsMaxPerMinute` - How many requests per minute is allowed against POST `/api/client/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_CLIENT_RATE_LIMIT_PER_MINUTE` environment variable + - `clientRegisterMaxPerMinute` - How many requests per minute is allowed against POST `/api/client/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `CLIENT_METRICS_RATE_LIMIT_PER_MINUTE` environment variable + - `frontendMetricsMaxPerMinute` - How many requests per minute is allowed against POST `/api/frontend/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `FRONTEND_METRICS_RATE_LIMIT_PER_MINUTE` environment variable + - `frontendRegisterMaxPerMinute` - How many requests per minute is allowed against POST `/api/frontend/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_FRONTEND_RATE_LIMIT_PER_MINUTE` environment variable ### Disabling Auto-Start {#disabling-auto-start} If you're using Unleash as part of a larger express app, you can disable the automatic server start by calling `server.create`. It takes the same options as `server.start`, but will not begin listening for connections.