@@ -193,8 +193,20 @@ async function createUploadRequest(
193193 // Wait for file streaming to complete.
194194 // eslint-disable-next-line no-await-in-loop
195195 await new Promise < void > ( ( resolve , reject ) => {
196- part . once ( 'end' , resolve )
197- part . once ( 'error' , reject )
196+ const cleanup = ( ) => {
197+ part . off ( 'end' , onEnd )
198+ part . off ( 'error' , onError )
199+ }
200+ const onEnd = ( ) => {
201+ cleanup ( )
202+ resolve ( )
203+ }
204+ const onError = ( e : Error ) => {
205+ cleanup ( )
206+ reject ( e )
207+ }
208+ part . on ( 'end' , onEnd )
209+ part . on ( 'error' , onError )
198210 } )
199211 if ( ! aborted ) {
200212 // Ensure a new line after file content.
@@ -225,8 +237,20 @@ async function getErrorResponseBody(
225237 response . on ( 'data' , ( chunk : Buffer ) => chunks . push ( chunk ) )
226238 try {
227239 await new Promise < void > ( ( resolve , reject ) => {
228- response . once ( 'end' , resolve )
229- response . once ( 'error' , reject )
240+ const cleanup = ( ) => {
241+ response . off ( 'end' , onEnd )
242+ response . off ( 'error' , onError )
243+ }
244+ const onEnd = ( ) => {
245+ cleanup ( )
246+ resolve ( )
247+ }
248+ const onError = ( e : Error ) => {
249+ cleanup ( )
250+ reject ( e )
251+ }
252+ response . on ( 'end' , onEnd )
253+ response . on ( 'error' , onError )
230254 } )
231255 return Buffer . concat ( chunks ) . toString ( 'utf8' )
232256 } catch {
0 commit comments