Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapter-node): make compression optional #5491

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/adapter-node/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const expected = new Set([
'XFF_DEPTH',
'ADDRESS_HEADER',
'PROTOCOL_HEADER',
'HOST_HEADER'
'HOST_HEADER',
'COMPRESSION_ENABLED',
'COMPRESSION_THRESHOLD'
]);

if (ENV_PREFIX) {
Expand Down
23 changes: 17 additions & 6 deletions 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 @@ -6,13 +8,22 @@ import polka from 'polka';
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: compression_threshold })
: undefined;

const middlewares = [compression_middleware, handler].filter(Boolean);

const server = polka().use(
// https://github.com/lukeed/polka/issues/173
// @ts-ignore - nothing we can do about so just ignore it
compression({ threshold: 0 }),
handler
);
// https://github.com/lukeed/polka/issues/173
// @ts-ignore - nothing we can do about so just ignore it
const server = polka().use(...middlewares);

server.listen({ path, host, port }, () => {
console.log(`Listening on ${path ? path : host + ':' + port}`);
Expand Down