@@ -12,8 +12,10 @@ import * as FileSystem from "effect/FileSystem";
1212import * as Layer from "effect/Layer" ;
1313import * as Option from "effect/Option" ;
1414import * as Path from "effect/Path" ;
15+ import * as Stream from "effect/Stream" ;
1516import { cast } from "effect/Function" ;
1617import {
18+ Headers ,
1719 HttpBody ,
1820 HttpClient ,
1921 HttpClientResponse ,
@@ -42,6 +44,71 @@ import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./ht
4244const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces" ;
4345const LOOPBACK_HOSTNAMES = new Set ( [ "127.0.0.1" , "::1" , "localhost" ] ) ;
4446const 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
46113export const browserApiCorsLayer = Layer . unwrap (
47114 Effect . gen ( function * ( ) {
0 commit comments