@@ -12,12 +12,9 @@ export class HostedOutboundRequestBlocked extends Schema.TaggedErrorClass<Hosted
1212export interface HostedHttpClientOptions {
1313 readonly allowLocalNetwork ?: boolean ;
1414 readonly maxRedirects ?: number ;
15- readonly maxResponseBytes ?: number ;
1615 readonly fetch ?: typeof globalThis . fetch ;
1716}
1817
19- const DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024 ;
20-
2118const parseIpv4 = ( hostname : string ) : readonly [ number , number , number , number ] | null => {
2219 const parts = hostname . split ( "." ) ;
2320 if ( parts . length !== 4 ) return null ;
@@ -158,69 +155,11 @@ const guardFetch = (
158155 current = next . toString ( ) ;
159156 continue ;
160157 }
161- return guardResponseBody (
162- response ,
163- url ,
164- options . maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES ,
165- ) ;
158+ return response ;
166159 }
167- const url = current instanceof Request ? current . url : String ( current ) ;
168- const response = await underlying ( current , { ...init , redirect : "manual" } ) ;
169- return guardResponseBody ( response , url , options . maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES ) ;
160+ return await underlying ( current , { ...init , redirect : "manual" } ) ;
170161 } ) as typeof globalThis . fetch ;
171162
172- const guardResponseBody = ( response : Response , url : string , maxResponseBytes : number ) : Response => {
173- if ( ! Number . isFinite ( maxResponseBytes ) || maxResponseBytes <= 0 || ! response . body ) {
174- return response ;
175- }
176-
177- const contentLength = response . headers . get ( "content-length" ) ;
178- if ( contentLength !== null ) {
179- const parsed = Number ( contentLength ) ;
180- if ( Number . isFinite ( parsed ) && parsed > maxResponseBytes ) {
181- // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: fetch-compatible response adapter must reject oversized responses
182- throw new HostedOutboundRequestBlocked ( {
183- url,
184- reason : "Response body is too large" ,
185- } ) ;
186- }
187- }
188-
189- let total = 0 ;
190- const reader = response . body . getReader ( ) ;
191- const limitedBody = new ReadableStream < Uint8Array > ( {
192- async pull ( controller ) {
193- const next = await reader . read ( ) ;
194- if ( next . done ) {
195- controller . close ( ) ;
196- return ;
197- }
198-
199- total += next . value . byteLength ;
200- if ( total > maxResponseBytes ) {
201- controller . error (
202- new HostedOutboundRequestBlocked ( {
203- url,
204- reason : "Response body is too large" ,
205- } ) ,
206- ) ;
207- return ;
208- }
209-
210- controller . enqueue ( next . value ) ;
211- } ,
212- cancel ( reason ) {
213- return reader . cancel ( reason ) ;
214- } ,
215- } ) ;
216-
217- return new Response ( limitedBody , {
218- status : response . status ,
219- statusText : response . statusText ,
220- headers : response . headers ,
221- } ) ;
222- } ;
223-
224163export const makeHostedHttpClientLayer = (
225164 options : HostedHttpClientOptions = { } ,
226165) : Layer . Layer < HttpClient . HttpClient > =>
0 commit comments