feat: add response compression middleware to reduce bandwidth (#928) (#935)

* feat: add response compression middleware for HTTP responses

Add compression middleware to Express server with SSE route exclusion
and 1KB threshold. Reduces bandwidth for non-streaming API responses
(history, sessions, files, static assets) by 60-80%.

Closes #928

* fix: harden proxy compression and proxy docs

---------

Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
jwcrystal
2026-04-17 16:26:56 +03:00
committed by GitHub
co-authored by Bohdan Triapitsyn
parent 71eac0d2f6
commit 304b14b4b1
7 changed files with 646 additions and 1 deletions
+29
View File
@@ -1,5 +1,6 @@
import 'reflect-metadata';
import express from 'express';
import compression from 'compression';
import path from 'path';
import { spawn, spawnSync } from 'child_process';
import fs from 'fs';
@@ -95,6 +96,27 @@ const TUNNEL_BOOTSTRAP_TTL_MAX_MS = 24 * 60 * 60 * 1000;
const TUNNEL_SESSION_TTL_DEFAULT_MS = 8 * 60 * 60 * 1000;
const TUNNEL_SESSION_TTL_MIN_MS = 5 * 60 * 1000;
const TUNNEL_SESSION_TTL_MAX_MS = 30 * 24 * 60 * 60 * 1000;
function headerIncludesEventStream(value) {
if (typeof value === 'string') {
return value.toLowerCase().includes('text/event-stream');
}
if (Array.isArray(value)) {
return value.some((entry) => typeof entry === 'string' && entry.toLowerCase().includes('text/event-stream'));
}
return false;
}
function shouldSkipCompression(req, res) {
if (headerIncludesEventStream(req.headers.accept)) {
return true;
}
return headerIncludesEventStream(res.getHeader('Content-Type'));
}
const OPENCHAMBER_VERSION = (() => {
try {
const packagePath = path.resolve(__dirname, '..', 'package.json');
@@ -970,6 +992,13 @@ async function main(options = {}) {
const app = express();
const serverStartedAt = new Date().toISOString();
app.set('trust proxy', true);
app.use(compression({
filter: (req, res) => {
if (shouldSkipCompression(req, res)) return false;
return compression.filter(req, res);
},
threshold: 1024,
}));
expressApp = app;
server = http.createServer(app);