Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

perf(api): compress exchange route responses (#5729)

Kris Oei755ff8cb00f6
diff --git a/apps/api/package.json b/apps/api/package.json
index 8b94886a5..9a8757cbf 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -48,6 +48,7 @@
"devDependencies": {
"@types/amqplib": "^0.10.7",
"@types/body-parser": "^1.19.2",
+ "@types/compression": "^1.8.1",
"@types/cors": "^2.8.13",
"@types/culori": "^4.0.1",
"@types/escape-html": "^1.0.4",
@@ -106,6 +107,7 @@
"bullmq": "^5.56.7",
"cacheable-lookup": "^6.1.0",
"cheerio": "^1.0.0-rc.12",
+ "compression": "^1.8.2",
"cors": "^2.8.5",
"culori": "^4.0.2",
"diff": "^8.0.3",
diff --git a/apps/api/pnpm-lock.yaml b/apps/api/pnpm-lock.yaml
index 55914df2f..cfb628b13 100644
--- a/apps/api/pnpm-lock.yaml
+++ b/apps/api/pnpm-lock.yaml
@@ -170,6 +170,9 @@ importers:
cheerio:
specifier: ^1.0.0-rc.12
version: 1.0.0-rc.12
+ compression:
+ specifier: ^1.8.2
+ version: 1.8.2
cors:
specifier: ^2.8.5
version: 2.8.5
@@ -321,6 +324,9 @@ importers:
'@types/body-parser':
specifier: ^1.19.2
version: 1.19.5
+ '@types/compression':
+ specifier: ^1.8.1
+ version: 1.8.1
'@types/cors':
specifier: ^2.8.13
version: 2.8.17
@@ -2034,6 +2040,9 @@ packages:
resolution: {integrity: sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-kCFuWS0ebDbmxs0AXYn6e2r2nrGAb5KwQhknjSPSPgJcGd8+HVSILlUyFhGqML2gk39HcG7D1ydW9/qpYkN00Q==}
+
resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==}
@@ -2565,6 +2574,14 @@ packages:
resolution: {integrity: sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==}
+ resolution: {integrity: sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==}
+ engines: {node: '>= 0.6'}
+
+ resolution: {integrity: sha512-o8vI5RE5A6EVVOd9o41jKp41aJom+QTEO/Bx8MYNjexMo/Bv2WOjUfZr+aL0WnYSgymUy6zeguqLTsIhV0gMvQ==}
+ engines: {node: '>= 0.8.0'}
+
resolution: {integrity: sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==}
engines: {'0': node >= 6.0}
@@ -3657,6 +3674,10 @@ packages:
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
hasBin: true
+ resolution: {integrity: sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w==}
+ engines: {node: '>= 0.6'}
+
resolution: {integrity: sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==}
engines: {node: '>= 0.6'}
@@ -6045,6 +6066,11 @@ snapshots:
'@types/deep-eql': 4.0.2
assertion-error: 2.0.1
+ '@types/[email protected]':
+ dependencies:
+ '@types/express': 5.0.6
+ '@types/node': 22.19.1
+
dependencies:
'@types/node': 24.10.1
@@ -6553,6 +6579,23 @@ snapshots:
+ dependencies:
+ mime-db: 1.54.0
+
+ dependencies:
+ bytes: 3.1.2
+ compressible: 2.0.18
+ debug: 2.6.9
+ destroy: 1.2.0
+ negotiator: 0.6.4
+ on-headers: 1.1.0
+ safe-buffer: 5.2.1
+ vary: 1.1.2
+ transitivePeerDependencies:
+ - supports-color
+
dependencies:
buffer-from: 1.1.2
@@ -7591,6 +7634,8 @@ snapshots:
+
diff --git a/apps/api/src/routes/exchange.ts b/apps/api/src/routes/exchange.ts
index 5482b05d5..3351db72f 100644
--- a/apps/api/src/routes/exchange.ts
+++ b/apps/api/src/routes/exchange.ts
@@ -6,7 +6,9 @@ import {
acceptProviderTerms,
acceptTermsSchema,
} from "../services/alexandria/terms";
+import compression from "compression";
import express, { Request, Response } from "express";
+import { constants as zlibConstants } from "node:zlib";
import { Agent, fetch, type Response as UpstreamResponse } from "undici";
import type { Logger } from "winston";
import { config } from "../config";
@@ -28,6 +30,7 @@ const INGEST_TIMEOUT_MS = 50_000;
const FORWARDED_REQUEST_HEADERS = ["accept", "x-request-id"];
const FORWARDED_RESPONSE_HEADERS = ["content-type", "x-request-id"];
+const NO_TRANSFORM = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
const FORWARDED_PATH_ROOTS = new Set([
"analytics",
"applications",
@@ -78,6 +81,10 @@ function sendUpstream(res: Response, upstream: UpstreamResponse, text: string) {
const value = upstream.headers.get(h);
if (value) res.setHeader(h, value);
}
+ // An upstream opt-out from transformation is the only cache directive kept.
+ if (NO_TRANSFORM.test(upstream.headers.get("cache-control") ?? "")) {
+ res.setHeader("Cache-Control", "no-transform");
+ }
let body: unknown;
try {
@@ -282,6 +289,26 @@ async function providerTermsAcceptController(req: Request, res: Response) {
export const exchangeRouter = express.Router();
+// Compresses responses that declare a Content-Length larger than one TCP
+// segment, as res.json and res.send do. A response without that header, or
+// with Cache-Control: no-transform, is not compressed.
+exchangeRouter.use(
+ compression({
+ threshold: 1400,
+ level: 6,
+ brotli: { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 4 } },
+ filter: (req, res) => {
+ // Express strips Content-Length from a 304, which still has to carry the
+ // Vary of the 200 it stands for.
+ if (res.statusCode === 304) {
+ res.vary("Accept-Encoding");
+ return false;
+ }
+ return res.hasHeader("Content-Length") && compression.filter(req, res);
+ },
+ }),
+);
+
// Discover is a flat 10,000/min catalogue read. Retrieve and records/fetch take
// the plan's scrape-equivalent budget (base 10/min, multiplier scaled).
// Dashboard and publisher routes below stay on the flat Labs limit.