diff --git a/packages/adapter-node/src/env.js b/packages/adapter-node/src/env.js index dda5e10201af..259814fc2947 100644 --- a/packages/adapter-node/src/env.js +++ b/packages/adapter-node/src/env.js @@ -9,7 +9,8 @@ const expected = new Set([ 'ADDRESS_HEADER', 'PROTOCOL_HEADER', 'HOST_HEADER', - 'COMPRESSION_ENABLED' + 'COMPRESSION_ENABLED', + 'COMPRESSION_THRESHOLD' ]); if (ENV_PREFIX) { diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index 76a4c3159d13..9b0a481bd8ae 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -1,3 +1,5 @@ +/* global ENV_PREFIX */ + import { handler } from './handler.js'; import { env } from './env.js'; import compression from 'compression'; @@ -7,8 +9,15 @@ export const path = env('SOCKET_PATH', false); export const host = env('HOST', '0.0.0.0'); export const port = env('PORT', !path && '3000'); export const compression_enabled = env('COMPRESSION_ENABLED', 'true') === 'true'; +export const compression_threshold = parseInt(env('COMPRESSION_THRESHOLD', '0'), 10); + +if (isNaN(compression_threshold) || compression_threshold < 0) { + throw Error(`${ENV_PREFIX}COMPRESSION_THRESHOLD should be a positve number`); +} -const compression_middleware = compression_enabled ? compression({ threshold: 0 }) : undefined; +const compression_middleware = compression_enabled + ? compression({ threshold: compression_threshold }) + : undefined; const middlewares = [compression_middleware, handler].filter(Boolean);