File tree 1 file changed +24
-6
lines changed
1 file changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -164,35 +164,53 @@ async function createUploadRequest(
164
164
] ) ,
165
165
finalBoundary
166
166
]
167
- const req = getHttpModule ( baseUrl ) . request ( `${ baseUrl } ${ urlPath } ` , {
167
+ const url = new URL ( urlPath , baseUrl )
168
+ const req : ClientRequest = getHttpModule ( baseUrl ) . request ( url , {
168
169
method : 'POST' ,
169
170
...options ,
170
171
headers : {
171
172
...options ?. headers ,
172
173
'Content-Type' : `multipart/form-data; boundary=${ boundary } `
173
- }
174
+ } ,
175
+ signal : options . signal
176
+ } )
177
+ let aborted = false
178
+ req . on ( 'error' , _err => {
179
+ aborted = true
180
+ } )
181
+ req . on ( 'close' , ( ) => {
182
+ aborted = true
174
183
} )
175
184
try {
176
185
// Send the request body (headers + files).
177
186
for ( const part of requestBody ) {
187
+ if ( aborted ) {
188
+ break
189
+ }
178
190
if ( typeof part === 'string' ) {
179
191
req . write ( part )
180
192
} else if ( typeof part ?. pipe === 'function' ) {
181
193
part . pipe ( req , { end : false } )
182
194
// Wait for file streaming to complete.
183
195
// eslint-disable-next-line no-await-in-loop
184
196
await events . once ( part , 'end' )
185
- // Ensure a new line after file content.
186
- req . write ( '\r\n' )
197
+ if ( ! aborted ) {
198
+ // Ensure a new line after file content.
199
+ req . write ( '\r\n' )
200
+ }
187
201
} else {
188
202
throw new TypeError (
189
203
'Socket API - Invalid multipart part, expected string or stream'
190
204
)
191
205
}
192
206
}
207
+ } catch ( err ) {
208
+ req . destroy ( err as Error )
209
+ throw err
193
210
} finally {
194
- // Close request after writing all data.
195
- req . end ( )
211
+ if ( ! aborted ) {
212
+ req . end ( )
213
+ }
196
214
}
197
215
return await getResponse ( req )
198
216
}
You can’t perform that action at this time.
0 commit comments