Skip to content

Commit

Permalink
fix(gatsby-source-filesystem): Retry a download if a file is incomple…
Browse files Browse the repository at this point in the history
…te (#28547)
  • Loading branch information
sidharthachatterjee authored Dec 17, 2020
1 parent b712950 commit 3474ae3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const STALL_TIMEOUT = process.env.GATSBY_STALL_TIMEOUT || 30000

const CONNECTION_TIMEOUT = process.env.GATSBY_CONNECTION_TIMEOUT || 30000

const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT || 3

/********************
* Queue Management *
********************/
Expand Down Expand Up @@ -178,8 +180,31 @@ const requestRemoteNode = (url, headers, tmpFilename, httpOpts, attempt = 1) =>

responseStream.on(`response`, response => {
resetTimeout()
const contentLength =
response.headers && Number(response.headers[`content-length`])

fsWriteStream.on(`finish`, () => {
// We have an incomplete download
if (contentLength && contentLength !== fsWriteStream.bytesWritten) {
fs.removeSync(tmpFilename)

if (attempt < INCOMPLETE_RETRY_LIMIT) {
resolve(
requestRemoteNode(
url,
headers,
tmpFilename,
httpOpts,
attempt + 1
)
)
} else {
reject(
`Failed to download ${url} after ${INCOMPLETE_RETRY_LIMIT} attempts`
)
}
}

if (timeout) {
clearTimeout(timeout)
}
Expand Down

0 comments on commit 3474ae3

Please sign in to comment.