Skip to content

Commit

Permalink
feat(compression): make threshold configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolguevara committed Jul 13, 2022
1 parent f9832a4 commit ace0b24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/adapter-node/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const expected = new Set([
'ADDRESS_HEADER',
'PROTOCOL_HEADER',
'HOST_HEADER',
'COMPRESSION_ENABLED'
'COMPRESSION_ENABLED',
'COMPRESSION_THRESHOLD'
]);

if (ENV_PREFIX) {
Expand Down
11 changes: 10 additions & 1 deletion packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global ENV_PREFIX */

import { handler } from './handler.js';
import { env } from './env.js';
import compression from 'compression';
Expand All @@ -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);

Expand Down

0 comments on commit ace0b24

Please sign in to comment.