From 98f083dc9a11ca4f29db2590ca0045804766d215 Mon Sep 17 00:00:00 2001 From: Marcin Jakuszko <137152553+mjakuszko-punks@users.noreply.github.com> Date: Mon, 6 Jan 2025 21:54:36 +0100 Subject: [PATCH] feat: Add configuration parameter for request body size (#1565) --- README.md | 1 + src/App.ts | 3 +-- src/SidecarConfig.ts | 1 + src/Specs.ts | 8 ++++++++ src/main.ts | 2 +- src/types/sidecar-config/CONFIG.ts | 1 + src/types/sidecar-config/SidecarConfig.ts | 1 + 7 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dcbc2e815..28634e4d2 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ For more information on our configuration manager visit its readme [here](https: - `SAS_EXPRESS_BIND_HOST`: address on which the server will be listening, defaults to `127.0.0.1`. - `SAS_EXPRESS_PORT`: port on which the server will be listening, defaults to `8080`. - `SAS_EXPRESS_KEEP_ALIVE_TIMEOUT`: Set the `keepAliveTimeout` in express. +- `SAS_EXPRESS_MAX_BODY`: Set the size of request body payload, defaults to `100kb` ### Substrate node diff --git a/src/App.ts b/src/App.ts index 32f540fd9..f80c2abbe 100644 --- a/src/App.ts +++ b/src/App.ts @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -import express from 'express'; -import { Application, ErrorRequestHandler, Request, RequestHandler, Response } from 'express'; +import express, { Application, ErrorRequestHandler, Request, RequestHandler, Response } from 'express'; import { Server } from 'http'; import packageJson from '../package.json'; diff --git a/src/SidecarConfig.ts b/src/SidecarConfig.ts index 25374fa7d..c22aa6c52 100644 --- a/src/SidecarConfig.ts +++ b/src/SidecarConfig.ts @@ -54,6 +54,7 @@ export class SidecarConfig { HOST: config.Get(MODULES.EXPRESS, CONFIG.BIND_HOST) as string, PORT: config.Get(MODULES.EXPRESS, CONFIG.PORT) as number, KEEP_ALIVE_TIMEOUT: config.Get(MODULES.EXPRESS, CONFIG.KEEP_ALIVE_TIMEOUT) as number, + MAX_BODY: config.Get(MODULES.EXPRESS, CONFIG.MAX_BODY) as string, }, SUBSTRATE: { URL: config.Get(MODULES.SUBSTRATE, CONFIG.URL) as string, diff --git a/src/Specs.ts b/src/Specs.ts index 8e057b20b..21d054ce7 100644 --- a/src/Specs.ts +++ b/src/Specs.ts @@ -87,6 +87,14 @@ export class Specs { type: 'number', }), ); + + this._specs.appendSpec( + MODULES.EXPRESS, + this._specs.getSpec(CONFIG.MAX_BODY, 'Max size of request payload body. It will default to 100kb.', { + default: '100kb', + type: 'string', + }), + ); } /** diff --git a/src/main.ts b/src/main.ts index 9a80334dc..f2253f5a6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,7 +65,7 @@ async function main() { startUpPrompt(config.SUBSTRATE.URL, chainName.toString(), implName.toString()); - const preMiddlewares = [json(), middleware.httpLoggerCreate(logger)]; + const preMiddlewares = [json({ limit: config.EXPRESS.MAX_BODY }), middleware.httpLoggerCreate(logger)]; if (config.METRICS.ENABLED) { // Create Metrics App diff --git a/src/types/sidecar-config/CONFIG.ts b/src/types/sidecar-config/CONFIG.ts index fe5f0bc6a..8e197a214 100644 --- a/src/types/sidecar-config/CONFIG.ts +++ b/src/types/sidecar-config/CONFIG.ts @@ -41,4 +41,5 @@ export enum CONFIG { PROM_HOST = 'PROM_HOST', LOKI_PORT = 'LOKI_PORT', INCLUDE_QUERYPARAMS = 'INCLUDE_QUERYPARAMS', + MAX_BODY = 'MAX_BODY', } diff --git a/src/types/sidecar-config/SidecarConfig.ts b/src/types/sidecar-config/SidecarConfig.ts index 3c13f4f21..c1693f5b3 100644 --- a/src/types/sidecar-config/SidecarConfig.ts +++ b/src/types/sidecar-config/SidecarConfig.ts @@ -37,6 +37,7 @@ interface ISidecarConfigExpress { HOST: string; PORT: number; KEEP_ALIVE_TIMEOUT: number; + MAX_BODY: string; } interface ISidecarConfigLog {