@@ -97,23 +97,33 @@ export class Http {
9797 } ) ;
9898 }
9999
100- /** @internal Reads `body` whole, or `undefined` once it holds more than `limit` bytes. */
100+ /**
101+ * @internal Reads `body` whole, or `undefined` once it holds more than `limit` bytes.
102+ * `signal` aborting cancels the body, so a stalled one is released.
103+ */
101104 static async readBody (
102105 body : ReadableStream < Uint8Array > | null ,
103- limit : number
106+ limit : number ,
107+ signal ?: AbortSignal
104108 ) : Promise < Uint8Array | undefined > {
105109 const chunks : Uint8Array [ ] = [ ] ;
106110 let length = 0 ;
107111 const reader = body ?. getReader ( ) ;
108- for ( ; ; ) {
109- const chunk = await reader ?. read ( ) ;
110- if ( chunk === undefined || chunk . done ) break ;
111- length += chunk . value . length ;
112- if ( length > limit ) {
113- await reader ?. cancel ( ) ;
114- return undefined ;
112+ const cancel = ( ) => void reader ?. cancel ( ) . catch ( ( ) => undefined ) ;
113+ signal ?. addEventListener ( 'abort' , cancel , { once : true } ) ;
114+ try {
115+ for ( ; ; ) {
116+ const chunk = await reader ?. read ( ) ;
117+ if ( chunk === undefined || chunk . done ) break ;
118+ length += chunk . value . length ;
119+ if ( length > limit ) {
120+ await reader ?. cancel ( ) ;
121+ return undefined ;
122+ }
123+ chunks . push ( chunk . value ) ;
115124 }
116- chunks . push ( chunk . value ) ;
125+ } finally {
126+ signal ?. removeEventListener ( 'abort' , cancel ) ;
117127 }
118128 const whole = new Uint8Array ( length ) ;
119129 let offset = 0 ;
@@ -165,16 +175,30 @@ export class Http {
165175 } catch ( error ) {
166176 throw failed ( error ) ;
167177 }
168- // A browser's `fetch` hides a manual redirect behind an opaque response.
169- if ( response . type === 'opaqueredirect' || ( response . status >= 300 && response . status < 400 ) ) {
178+ // A response that comes after the deadline, or that redirected, is not read: its
179+ // body is cancelled so the connection is released.
180+ const discard = ( ) => void response . body ?. cancel ( ) . catch ( ( ) => undefined ) ;
181+ if ( signal . aborted ) {
182+ discard ( ) ;
183+ throw failed ( signal . reason ) ;
184+ }
185+ // A browser's `fetch` hides a manual redirect behind an opaque response, and one
186+ // that ignored `redirect: 'manual'` marks the response it followed to.
187+ const status = response . status ;
188+ if (
189+ response . redirected ||
190+ response . type === 'opaqueredirect' ||
191+ ( status >= 300 && status < 400 )
192+ ) {
193+ discard ( ) ;
170194 throw new AuthError (
171195 { code : 'network' , url } ,
172- `${ url } redirected with HTTP ${ String ( response . status ) } `
196+ `${ url } redirected with HTTP ${ String ( status ) } `
173197 ) ;
174198 }
175199 let body : Uint8Array | undefined ;
176200 try {
177- body = await Http . readBody ( response . body , MAX_BODY_BYTES ) ;
201+ body = await Http . readBody ( response . body , MAX_BODY_BYTES , signal ) ;
178202 } catch ( error ) {
179203 throw failed ( error ) ;
180204 }
0 commit comments