forked from sholladay/await-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.11 KB
/
index.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
'use strict'
const got = require('got')
const awaitUrl = (url, option) => {
const config = Object.assign(
{
tries: 150,
interval: 1200,
logTries: false,
},
option
)
return new Promise((resolve, reject) => {
const attempt = async tries => {
let error = undefined
let success = false
for (var attempt = 1; attempt <= tries && !success; attempt++) {
error = undefined
if (config.logTries) {
console.log(`Attempt ${attempt}`)
}
try {
const res = await got(url, {
followRedirect: false,
timeout: {
connect: 10000,
socket: 10000,
request: 10000,
},
})
if (res.statusCode === 200) {
success = true
} else {
error = new RangeError(`Expected 200 response but got ${res.statusCode}`)
}
} catch (err) {
error = err
}
}
if (error) {
throw error
}
}
attempt(config.tries).then(resolve, reject)
})
}
module.exports = awaitUrl