-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
120 lines (99 loc) · 3.29 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
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
'use strict';
const WritableStream = require('stream').Writable;
const got = require('got');
const isOnline = require('is-online');
const pkg = require('./package.json');
let isOnlineCache;
function isOnlineAsPromise() {
// Cache the isOnline() result up to `exports.connectivityCacheDuration` ms
if (isOnlineCache && isOnlineCache.cachedAt > Date.now() - module.exports.connectivityCacheDuration) {
return isOnlineCache;
}
isOnlineCache = new Promise((resolve, reject) => {
isOnline((err, online) => {
if (err) {
delete isOnlineCache.promise; // Do not cache errors
/* istanbul ignore next */
reject(err);
} else {
resolve(online);
}
});
});
isOnlineCache.cachedAt = Date.now();
return isOnlineCache;
}
function checkConnectivity(requestErr) {
return isOnlineAsPromise()
.catch(() => { throw requestErr; })
.then((online) => {
if (!online) {
throw requestErr;
}
return false;
});
}
class DevNull extends WritableStream {
_write(chunk, encoding, callback) {
callback();
}
}
function tryHead(link, gotOptions) {
return got.head(link, gotOptions)
.then(() => true);
}
function tryGet(link, options, gotOptions) {
return new Promise((resolve, reject) => {
let stream;
let req;
try {
stream = got.stream(link, gotOptions);
} catch (err) {
return resolve(false);
}
stream
.on('request', (req_) => { req = req_; })
.on('response', (res) => {
res.on('error', () => {}); // Swallow any response errors, because we are going to abort the request
setImmediate(() => req.abort());
resolve(true);
})
.on('error', (err, body, res) => {
res && res.on('error', () => {}); // Swallow any response errors, because we are going to abort the request
setImmediate(() => req.abort());
if (err instanceof got.MaxRedirectsError || err instanceof got.HTTPError || err instanceof got.UnsupportedProtocolError) {
return resolve(false);
}
/* istanbul ignore else */
if (err instanceof got.RequestError) {
return resolve(options.checkConnectivity ? checkConnectivity(err) : false);
}
/* istanbul ignore next */
reject(err);
})
.pipe(new DevNull());
});
}
// -------------------------------------------------------------------------
function isLinkWorking(link, options) {
options = Object.assign({
checkConnectivity: false,
followRedirect: true,
timeout: 15000,
retries: 3,
agent: null,
}, options);
const gotOptions = {
timeout: options.timeout,
followRedirect: options.followRedirect,
retries: options.retries,
agent: options.agent,
headers: {
'user-agent': `is-link-working/${pkg.version} (https://github.com/IndigoUnited/is-link-working)`,
},
};
return tryHead(link, gotOptions)
.catch(() => tryGet(link, options, gotOptions));
}
module.exports = isLinkWorking;
module.exports.connectivityCacheDuration = 5000;