Skip to content

Commit 1aa1d1d

Browse files
committed
perf(server): gzip large thread snapshots
1 parent b64ae88 commit 1aa1d1d

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎apps/server/src/http.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { expect, it } from "@effect/vitest";
2+
import * as NodeZlib from "node:zlib";
3+
import * as Effect from "effect/Effect";
4+
import * as Stream from "effect/Stream";
5+
import { HttpServerResponse } from "effect/unstable/http";
6+
import { describe } from "vite-plus/test";
27

3-
import { isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
8+
import { compressHttpResponse, isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
49

510
describe("http dev routing", () => {
611
it("treats localhost and loopback addresses as local", () => {
@@ -25,3 +30,35 @@ describe("http dev routing", () => {
2530
);
2631
});
2732
});
33+
34+
describe("http compression", () => {
35+
it.effect("gzips large JSON responses when the client accepts it", () =>
36+
Effect.gen(function* () {
37+
const body = `{"value":"${"compressible".repeat(1_000)}"}`;
38+
const response = compressHttpResponse(
39+
HttpServerResponse.text(body, { contentType: "application/json" }),
40+
"br, gzip, deflate",
41+
);
42+
43+
expect(response.headers["content-encoding"]).toBe("gzip");
44+
expect(response.headers["content-length"]).toBeUndefined();
45+
expect(response.headers.vary).toBe("Accept-Encoding");
46+
expect(response.body._tag).toBe("Stream");
47+
if (response.body._tag === "Stream") {
48+
const chunks = yield* Stream.runCollect(Stream.orDie(response.body.stream));
49+
expect(NodeZlib.gunzipSync(Buffer.concat(Array.from(chunks))).toString()).toBe(body);
50+
}
51+
}),
52+
);
53+
54+
it("keeps the original body when gzip is declined", () => {
55+
const response = compressHttpResponse(
56+
HttpServerResponse.text("x".repeat(2_000), { contentType: "application/json" }),
57+
"gzip;q=0, *;q=1",
58+
);
59+
60+
expect(response.headers["content-encoding"]).toBeUndefined();
61+
expect(response.headers["content-length"]).toBe("2000");
62+
expect(response.headers.vary).toBe("Accept-Encoding");
63+
});
64+
});

‎apps/server/src/http.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import * as FileSystem from "effect/FileSystem";
1212
import * as Layer from "effect/Layer";
1313
import * as Option from "effect/Option";
1414
import * as Path from "effect/Path";
15+
import * as Stream from "effect/Stream";
1516
import { cast } from "effect/Function";
1617
import {
18+
Headers,
1719
HttpBody,
1820
HttpClient,
1921
HttpClientResponse,
@@ -42,6 +44,71 @@ import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./ht
4244
const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
4345
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
4446
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];
47+
const GZIP_MIN_BYTES = 1024;
48+
49+
function acceptsGzip(value: string | undefined): boolean {
50+
if (!value) return false;
51+
52+
const accepted = new Map(
53+
value.split(",").map((entry) => {
54+
const [coding = "", ...parameters] = entry.trim().toLowerCase().split(";");
55+
const quality = parameters
56+
.map((parameter) => parameter.trim().match(/^q=(.+)$/)?.[1])
57+
.find((parameter) => parameter !== undefined);
58+
return [coding, quality === undefined ? 1 : Number(quality)] as const;
59+
}),
60+
);
61+
return (accepted.get("gzip") ?? accepted.get("*") ?? 0) > 0;
62+
}
63+
64+
export function compressHttpResponse(
65+
response: HttpServerResponse.HttpServerResponse,
66+
acceptEncoding: string | undefined,
67+
): HttpServerResponse.HttpServerResponse {
68+
const body = response.body;
69+
if (
70+
body._tag !== "Uint8Array" ||
71+
body.contentLength < GZIP_MIN_BYTES ||
72+
!body.contentType.startsWith("application/json") ||
73+
response.headers["content-encoding"]
74+
) {
75+
return response;
76+
}
77+
78+
const vary = response.headers.vary;
79+
const variedResponse = HttpServerResponse.setHeader(
80+
response,
81+
"vary",
82+
vary ? `${vary}, Accept-Encoding` : "Accept-Encoding",
83+
);
84+
if (!acceptsGzip(acceptEncoding)) return variedResponse;
85+
86+
const compressedBody = Stream.fromReadableStream({
87+
evaluate: () => new Response(body.body).body!.pipeThrough(new CompressionStream("gzip")),
88+
onError: (cause) => cause,
89+
});
90+
const headers = Headers.set(
91+
Headers.remove(variedResponse.headers, "content-length"),
92+
"content-encoding",
93+
"gzip",
94+
);
95+
return HttpServerResponse.stream(compressedBody, {
96+
status: response.status,
97+
statusText: response.statusText,
98+
headers,
99+
cookies: response.cookies,
100+
contentType: body.contentType,
101+
});
102+
}
103+
104+
export const httpCompressionLayer = HttpRouter.middleware(
105+
(httpEffect) =>
106+
Effect.gen(function* () {
107+
const request = yield* HttpServerRequest.HttpServerRequest;
108+
return compressHttpResponse(yield* httpEffect, request.headers["accept-encoding"]);
109+
}),
110+
{ global: true },
111+
);
45112

46113
export const browserApiCorsLayer = Layer.unwrap(
47114
Effect.gen(function* () {

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
serverEnvironmentHttpApiLayer,
1212
staticAndDevRouteLayer,
1313
browserApiCorsLayer,
14+
httpCompressionLayer,
1415
} from "./http.ts";
1516
import { fixPath } from "./os-jank.ts";
1617
import { websocketRpcRouteLayer } from "./ws.ts";
@@ -372,6 +373,7 @@ export const makeRoutesLayer = Layer.mergeAll(
372373
Layer.provide(PreviewAutomationBroker.layer),
373374
Layer.provide(ServerSelfUpdate.layer),
374375
Layer.provide(browserApiCorsLayer),
376+
Layer.provide(httpCompressionLayer),
375377
);
376378

377379
export const makeServerLayer = Layer.unwrap(

0 commit comments

Comments
 (0)