Skip to content
Draft
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions packages/cli-kit/src/public/node/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ describe('downloadFile', () => {
await expect(fileExists(to)).resolves.toBe(false)
})
})

test('Fails and cleans up if server returns 500', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const url = 'https://shopify.example/500.txt'
const filename = '/bin/500.txt'
const to = joinPath(tmpDir, filename)

// When
const result = downloadFile(url, to)

// Then
await expect(result).rejects.toThrow(
'Failed to download file from https://shopify.example/500.txt: 500 Internal Server Error',
)
await expect(fileExists(to)).resolves.toBe(false)
})
})
})

describe('requestMode', () => {
Expand Down
64 changes: 39 additions & 25 deletions packages/cli-kit/src/public/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,38 +226,52 @@ export function downloadFile(url: string, to: string): Promise<string> {

return runWithTimer('cmd_all_timing_network_ms')(() => {
return new Promise<string>((resolve, reject) => {
if (!fileExistsSync(dirname(to))) {
mkdirSync(dirname(to))
}
nodeFetch(url, {redirect: 'follow'})
.then((res) => {
if (!res.ok) {
return reject(new Error(`Failed to download file from ${sanitizedUrl}: ${res.status} ${res.statusText}`))
}
if (!res.body) {
return reject(new Error(`Failed to download file from ${sanitizedUrl}: Empty response body`))
}

const file = createFileWriteStream(to)
if (!fileExistsSync(dirname(to))) {
mkdirSync(dirname(to))
}

// if we can't remove the file for some reason (seen on windows), that's ok -- it's in a temporary directory
const tryToRemoveFile = () => {
try {
unlinkFileSync(to)
// eslint-disable-next-line no-catch-all/no-catch-all
} catch (err: unknown) {
outputDebug(outputContent`Failed to remove file ${outputToken.path(to)}: ${outputToken.raw(String(err))}`)
}
}
const file = createFileWriteStream(to)

file.on('finish', () => {
file.close()
resolve(to)
})
// if we can't remove the file for some reason (seen on windows), that's ok -- it's in a temporary directory
const tryToRemoveFile = () => {
try {
if (!file.destroyed) {
file.destroy()
}
unlinkFileSync(to)
// eslint-disable-next-line no-catch-all/no-catch-all
} catch (err: unknown) {
outputDebug(outputContent`Failed to remove file ${outputToken.path(to)}: ${outputToken.raw(String(err))}`)
}
}

file.on('error', (err) => {
tryToRemoveFile()
reject(err)
})
file.on('finish', () => {
file.close()
resolve(to)
})

nodeFetch(url, {redirect: 'follow'})
.then((res) => {
res.body?.pipe(file)
file.on('error', (err) => {
tryToRemoveFile()
reject(err instanceof Error ? err : new Error(String(err)))
})

res.body.on('error', (err) => {
tryToRemoveFile()
reject(err instanceof Error ? err : new Error(String(err)))
})

res.body.pipe(file)
})
.catch((err) => {
tryToRemoveFile()
reject(err instanceof Error ? err : new Error(String(err)))
})
})
Expand Down
Loading