@@ -111,6 +111,12 @@ async function readCappedResponseBuffer(res: Response, maxResponseBytes: number)
111111 } ) ;
112112}
113113
114+ async function releaseUnreadResponseBody ( res : Response | undefined ) : Promise < void > {
115+ if ( res ?. bodyUsed !== true ) {
116+ await res ?. body ?. cancel ( ) . catch ( ( ) => undefined ) ;
117+ }
118+ }
119+
114120/**
115121 * Check if bbernhard container REST API is available.
116122 */
@@ -120,8 +126,9 @@ export async function containerCheck(
120126 account ?: string ,
121127) : Promise < { ok : boolean ; status ?: number | null ; error ?: string | null } > {
122128 const normalized = normalizeBaseUrl ( baseUrl ) ;
129+ let res : Response | undefined ;
123130 try {
124- const res = await fetchWithTimeout ( `${ normalized } /v1/about` , { method : "GET" } , timeoutMs ) ;
131+ res = await fetchWithTimeout ( `${ normalized } /v1/about` , { method : "GET" } , timeoutMs ) ;
125132 if ( ! res . ok ) {
126133 return { ok : false , status : res . status , error : `HTTP ${ res . status } ` } ;
127134 }
@@ -136,6 +143,8 @@ export async function containerCheck(
136143 status : null ,
137144 error : err instanceof Error ? err . message : String ( err ) ,
138145 } ;
146+ } finally {
147+ await releaseUnreadResponseBody ( res ) ;
139148 }
140149}
141150
@@ -253,14 +262,19 @@ export async function containerFetchAttachment(
253262) : Promise < Buffer | null > {
254263 const baseUrl = normalizeBaseUrl ( opts . baseUrl ) ;
255264 const url = `${ baseUrl } /v1/attachments/${ encodeURIComponent ( attachmentId ) } ` ;
265+ let res : Response | undefined ;
256266
257- const res = await fetchWithTimeout ( url , { method : "GET" } , opts . timeoutMs ?? DEFAULT_TIMEOUT_MS ) ;
267+ try {
268+ res = await fetchWithTimeout ( url , { method : "GET" } , opts . timeoutMs ?? DEFAULT_TIMEOUT_MS ) ;
258269
259- if ( ! res . ok ) {
260- return null ;
261- }
270+ if ( ! res . ok ) {
271+ return null ;
272+ }
262273
263- return readCappedResponseBuffer ( res , normalizeMaxResponseBytes ( opts . maxResponseBytes ) ) ;
274+ return await readCappedResponseBuffer ( res , normalizeMaxResponseBytes ( opts . maxResponseBytes ) ) ;
275+ } finally {
276+ await releaseUnreadResponseBody ( res ) ;
277+ }
264278}
265279
266280/**
0 commit comments