Skip to content

Commit 5a26f85

Browse files
Convert compression middleware to route-specific
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
1 parent 956ca97 commit 5a26f85

6 files changed

Lines changed: 21 additions & 5 deletions

File tree

.jules/bolt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,9 @@ In Express applications handling cross-origin traffic, placing the `cors()` midd
224224

225225
Action:
226226
Moved the `cors()` middleware before `helmet()` in the global middleware stack. This allows `OPTIONS` preflight requests to be intercepted and resolved immediately by `cors`, bypassing unnecessary security header processing and improving baseline latency. Consolidated the `res.setHeader` calls in the JSON error handler into a single global setter.
227+
228+
## 2026-05-12 — Compression Middleware Overhead
229+
Learning:
230+
Global `compression()` middleware introduces significant CPU and memory allocation overhead on unhandled routes (404s) and lightweight responses.
231+
Action:
232+
Always apply `compression()` as a route-specific middleware only to endpoints that return large payloads.

.jules/warden.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,9 @@ Observation / Pruned:
202202
Assessed repository state following previous optimizations. Since no new functional or architectural changes were introduced by the prior agent run, no new release cut or version bump is warranted. Maintained semantic integrity by preserving the existing v1.1.31 state. Zero dead code identified and pruned.
203203
Alignment / Deferred:
204204
Release deferred. Repository state verified and stable.
205+
206+
2026-05-12 — Assessment & Lifecycle
207+
Observation / Pruned:
208+
Assessed BOLT's optimization converting `compression()` to a route-specific middleware. This prevents unhandled routes and simple endpoints from undergoing redundant compression overhead. Tests verified. Checked for unused dependencies and dead code. Zero dead code or unused files found.
209+
Alignment / Deferred:
210+
Appended release notes. Version bumped to 1.1.32.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.1.32] - 2026-05-12
2+
### Changed
3+
* **[Performance]:** Converted `compression()` from a global middleware to a route-specific middleware on the `/v1/chat/completions` endpoint. This prevents unhandled routes (404s) and lightweight responses from incurring unnecessary CPU overhead and memory allocation for compression.
4+
15
## [1.1.31] - 2026-05-04
26
### Changed
37
* **[Performance]:** Moved the `cors()` middleware to be above `helmet()` in the global middleware stack. This allows `OPTIONS` preflight requests to be intercepted and resolved immediately by `cors`, bypassing unnecessary security header processing. Also, consolidated the `res.setHeader` calls in the JSON error handler.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "one-api",
3-
"version": "1.1.31",
3+
"version": "1.1.32",
44
"description": "One API to rule them all. Unified gateway for 20+ LLM providers. OpenAI-compatible, single binary, zero config.",
55
"main": "src/index.js",
66
"scripts": {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ app.use((req, res, next) => {
5050

5151

5252
// Compress all responses to reduce bandwidth and latency
53-
app.use(compression());
53+
const compressMiddleware = compression();
5454

5555
const ERROR_INVALID_JSON = Buffer.from(JSON.stringify({ error: 'Invalid JSON payload' }));
5656
const ERROR_PAYLOAD_TOO_LARGE = Buffer.from(JSON.stringify({ error: 'Payload too large' }));
@@ -96,7 +96,7 @@ const ERROR_MALFORMED_MESSAGE = Buffer.from(JSON.stringify({ error: 'Malformed m
9696
// Set a larger JSON limit since LLM contexts can be quite large
9797
const jsonParser = express.json({ limit: '10mb' });
9898

99-
app.post('/v1/chat/completions', jsonParser, (req, res) => {
99+
app.post('/v1/chat/completions', jsonParser, compressMiddleware, (req, res) => {
100100
const body = req.body;
101101
const model = body ? body.model : undefined;
102102
const messages = body ? body.messages : undefined;

0 commit comments

Comments
 (0)