Skip to content

Fix createUploadRequest swallowing errors #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,53 @@ async function createUploadRequest(
]),
finalBoundary
]
const req = getHttpModule(baseUrl).request(`${baseUrl}${urlPath}`, {
const url = new URL(urlPath, baseUrl)
const req: ClientRequest = getHttpModule(baseUrl).request(url, {
method: 'POST',
...options,
headers: {
...options?.headers,
'Content-Type': `multipart/form-data; boundary=${boundary}`
}
},
signal: options.signal
})
let aborted = false
req.on('error', _err => {
aborted = true
})
req.on('close', () => {
aborted = true
})
try {
// Send the request body (headers + files).
for (const part of requestBody) {
if (aborted) {
break
}
if (typeof part === 'string') {
req.write(part)
} else if (typeof part?.pipe === 'function') {
part.pipe(req, { end: false })
// Wait for file streaming to complete.
// eslint-disable-next-line no-await-in-loop
await events.once(part, 'end')
// Ensure a new line after file content.
req.write('\r\n')
if (!aborted) {
// Ensure a new line after file content.
req.write('\r\n')
}
} else {
throw new TypeError(
'Socket API - Invalid multipart part, expected string or stream'
)
}
}
} catch (err) {
req.destroy(err as Error)
throw err
} finally {
// Close request after writing all data.
req.end()
if (!aborted) {
req.end()
}
}
return await getResponse(req)
}
Expand Down
Loading