Skip to content

Commit 3474ae3

Browse files
fix(gatsby-source-filesystem): Retry a download if a file is incomplete (#28547)
1 parent b712950 commit 3474ae3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

packages/gatsby-source-filesystem/src/create-remote-file-node.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const STALL_TIMEOUT = process.env.GATSBY_STALL_TIMEOUT || 30000
6161

6262
const CONNECTION_TIMEOUT = process.env.GATSBY_CONNECTION_TIMEOUT || 30000
6363

64+
const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT || 3
65+
6466
/********************
6567
* Queue Management *
6668
********************/
@@ -178,8 +180,31 @@ const requestRemoteNode = (url, headers, tmpFilename, httpOpts, attempt = 1) =>
178180

179181
responseStream.on(`response`, response => {
180182
resetTimeout()
183+
const contentLength =
184+
response.headers && Number(response.headers[`content-length`])
181185

182186
fsWriteStream.on(`finish`, () => {
187+
// We have an incomplete download
188+
if (contentLength && contentLength !== fsWriteStream.bytesWritten) {
189+
fs.removeSync(tmpFilename)
190+
191+
if (attempt < INCOMPLETE_RETRY_LIMIT) {
192+
resolve(
193+
requestRemoteNode(
194+
url,
195+
headers,
196+
tmpFilename,
197+
httpOpts,
198+
attempt + 1
199+
)
200+
)
201+
} else {
202+
reject(
203+
`Failed to download ${url} after ${INCOMPLETE_RETRY_LIMIT} attempts`
204+
)
205+
}
206+
}
207+
183208
if (timeout) {
184209
clearTimeout(timeout)
185210
}

0 commit comments

Comments
 (0)