-
Notifications
You must be signed in to change notification settings - Fork 391
/
requestStream.js
69 lines (58 loc) · 1.68 KB
/
requestStream.js
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
const simpleGet = require('simple-get')
const HttpAgent = require('agentkeepalive')
const debug = require('./debug')
const HttpsAgent = HttpAgent.HttpsAgent
const httpAgent = new HttpAgent()
const httpsAgent = new HttpsAgent()
const CONNECTION_TIMEOUT = 15 * 1000 // 15 seconds
const READ_TIMEOUT = 3 * 60 * 1000 // 3 minutes
const MAX_RETRIES = 5
// Just a promisified simpleGet
function getStream(options) {
return new Promise((resolve, reject) => {
let rejected = false
const openTimeout = setTimeout(() => {
rejected = true
reject(new Error(`Connection timed out after ${CONNECTION_TIMEOUT} ms`))
}, CONNECTION_TIMEOUT)
simpleGet(options, (err, res) => {
clearTimeout(openTimeout)
if (rejected) {
return
}
if (err) {
reject(err)
return
}
resolve(res)
})
})
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/* eslint-disable no-await-in-loop, max-depth */
module.exports = async options => {
const agent = options.url.startsWith('https:') ? httpsAgent : httpAgent
const reqOptions = {...options, followRedirects: false, agent}
let error
for (let i = 0; i < MAX_RETRIES; i++) {
try {
const response = await getStream(reqOptions)
response.connection.setTimeout(READ_TIMEOUT, () => {
response.destroy(
new Error(`Read timeout: No data received on socket for ${READ_TIMEOUT} ms`)
)
})
return response
} catch (err) {
error = err
if (err.statusCode && err.statusCode < 500) {
break
}
debug('Error, retrying after 1500ms: %s', err.message)
await delay(1500)
}
}
throw error
}