-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathfetch-file.ts
272 lines (240 loc) · 7.79 KB
/
fetch-file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import fs from "fs-extra"
import type { IncomingMessage } from "http"
import type { Headers, Options } from "got"
import type { GatsbyCache } from "gatsby"
// keeping the I for backward compatibility
export type IFetchRemoteFileOptions = {
url: string
auth?: {
htaccess_pass?: string
htaccess_user?: string
}
httpHeaders?: Headers
ext?: string
name?: string
cacheKey?: string
excludeDigest?: boolean
} & (
| {
directory: string
cache?: never
}
| {
directory?: never
cache: GatsbyCache
}
)
const STALL_RETRY_LIMIT = process.env.GATSBY_STALL_RETRY_LIMIT
? parseInt(process.env.GATSBY_STALL_RETRY_LIMIT, 10)
: 3
const STALL_TIMEOUT = process.env.GATSBY_STALL_TIMEOUT
? parseInt(process.env.GATSBY_STALL_TIMEOUT, 10)
: 30000
const CONNECTION_TIMEOUT = process.env.GATSBY_CONNECTION_TIMEOUT
? parseInt(process.env.GATSBY_CONNECTION_TIMEOUT, 10)
: 30000
const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT
? parseInt(process.env.GATSBY_INCOMPLETE_RETRY_LIMIT, 10)
: 3
// jest doesn't allow us to run all timings infinitely, so we set it 0 in tests
const BACKOFF_TIME = process.env.NODE_ENV === `test` ? 0 : 1000
function range(start: number, end: number): Array<number> {
return Array(end - start)
.fill(null)
.map((_, i) => start + i)
}
// Based on the defaults of https://github.com/JustinBeckwith/retry-axios
const STATUS_CODES_TO_RETRY = [...range(100, 200), 429, ...range(500, 600)]
const ERROR_CODES_TO_RETRY = [
`ETIMEDOUT`,
`ECONNRESET`,
`EADDRINUSE`,
`ECONNREFUSED`,
`EPIPE`,
`ENOTFOUND`,
`ENETUNREACH`,
`EAI_AGAIN`,
`ERR_NON_2XX_3XX_RESPONSE`,
`ERR_GOT_REQUEST_ERROR`,
]
/**
* requestRemoteNode
* --
* Download the requested file
*
* @param {String} url
* @param {Headers} headers
* @param {String} tmpFilename
* @param {Object} httpOptions
* @param {number} attempt
* @return {Promise<Object>} Resolves with the [http Result Object]{@link https://nodejs.org/api/http.html#http_class_http_serverresponse}
*/
export async function requestRemoteNode(
url: string | URL,
headers: Headers,
tmpFilename: string,
httpOptions?: Options,
attempt: number = 1
): Promise<IncomingMessage> {
// TODO(v5): use dynamic import syntax - it's currently blocked because older v4 versions have V8-compile-cache
// const { default: got, RequestError } = await import(`got`)
const { default: got, RequestError } = require(`got`)
return new Promise((resolve, reject) => {
let timeout: NodeJS.Timeout
const fsWriteStream = fs.createWriteStream(tmpFilename)
fsWriteStream.on(`error`, (error: unknown) => {
if (timeout) {
clearTimeout(timeout)
}
reject(error)
})
// Called if we stall for 30s without receiving any data
const handleTimeout = async (): Promise<void> => {
fsWriteStream.close()
await fs.remove(tmpFilename)
if (attempt < STALL_RETRY_LIMIT) {
// Retry by calling ourself recursively
resolve(
requestRemoteNode(url, headers, tmpFilename, httpOptions, attempt + 1)
)
} else {
// TODO move to new Error type
// eslint-disable-next-line prefer-promise-reject-errors
reject(`Failed to download ${url} after ${STALL_RETRY_LIMIT} attempts`)
}
}
const resetTimeout = (): void => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(handleTimeout, STALL_TIMEOUT)
}
const responseStream = got.stream(url, {
headers,
timeout: {
send: CONNECTION_TIMEOUT, // https://github.com/sindresorhus/got#timeout
},
...httpOptions,
isStream: true,
})
let haveAllBytesBeenWritten = false
// Fixes a bug in latest got where progress.total gets reset when stream ends, even if it wasn't complete.
let totalSize: number | null = null
responseStream.on(`downloadProgress`, progress => {
// reset the timeout on each progress event to make sure large files don't timeout
resetTimeout()
if (
progress.total != null &&
(!totalSize || totalSize < progress.total)
) {
totalSize = progress.total
}
if (progress.transferred === totalSize || totalSize === null) {
haveAllBytesBeenWritten = true
}
})
responseStream.pipe(fsWriteStream)
// If there's a 400/500 response or other error.
// it will trigger a finish event on fsWriteStream
responseStream.on(`error`, async error => {
if (timeout) {
clearTimeout(timeout)
}
fsWriteStream.close()
await fs.remove(tmpFilename)
if (!(error instanceof RequestError)) {
return reject(error)
}
// This is a replacement for the stream retry logic of got
// till we can update all got instances to v12
// https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md
// https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md#retry
const statusCode = error.response?.statusCode
const errorCode = error.code || error.message // got gives error.code, but msw/node returns the error codes in the message only
if (
// HTTP STATUS CODE ERRORS
(statusCode && STATUS_CODES_TO_RETRY.includes(statusCode)) ||
// GENERAL NETWORK ERRORS
(errorCode && ERROR_CODES_TO_RETRY.includes(errorCode))
) {
if (attempt < INCOMPLETE_RETRY_LIMIT) {
setTimeout(() => {
resolve(
requestRemoteNode(
url,
headers,
tmpFilename,
httpOptions,
attempt + 1
)
)
}, BACKOFF_TIME * attempt)
return undefined
}
// Throw user friendly error
error.message = [
`Unable to fetch:`,
url,
`---`,
`Reason: ${error.message}`,
`---`,
].join(`\n`)
// Gather details about what went wrong from the error object and the request
const details = Object.entries({
attempt,
method: error.options?.method,
errorCode: error.code,
responseStatusCode: error.response?.statusCode,
responseStatusMessage: error.response?.statusMessage,
requestHeaders: error.options?.headers,
responseHeaders: error.response?.headers,
})
// Remove undefined values from the details to keep it clean
.reduce((a, [k, v]) => (v === undefined ? a : ((a[k] = v), a)), {})
if (Object.keys(details).length) {
error.message = [
error.message,
`Fetch details:`,
JSON.stringify(details, null, 2),
`---`,
].join(`\n`)
}
}
return reject(error)
})
responseStream.on(`response`, response => {
resetTimeout()
fsWriteStream.once(`finish`, async () => {
if (timeout) {
clearTimeout(timeout)
}
// We have an incomplete download
if (!haveAllBytesBeenWritten) {
await fs.remove(tmpFilename)
if (attempt < INCOMPLETE_RETRY_LIMIT) {
// let's give node time to remove the file
setImmediate(() =>
resolve(
requestRemoteNode(
url,
headers,
tmpFilename,
httpOptions,
attempt + 1
)
)
)
return undefined
} else {
// TODO move to new Error type
// eslint-disable-next-line prefer-promise-reject-errors
return reject(
`Failed to download ${url} after ${INCOMPLETE_RETRY_LIMIT} attempts`
)
}
}
return resolve(response)
})
})
})
}