-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
82 lines (65 loc) · 1.88 KB
/
index.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
import { parse } from "url";
import http, { RequestOptions } from "http";
import https from "https";
// taken from https://stackoverflow.com/a/68415816/5748481
/**
Check if a URL is reachable
@param {string} url - URL of the website
@param {number} timeout - Cancel request after given time
@param {RequestOptions} requestOptions - Request options for HTTP Request
@returns {Promise} Promise object with status code or error
@example
try {
const statusCode = await isReachable("https://learnaws.io");
// statusCode = 200
} catch (error) {
const { message } = error;
// Unable to resolve given URL, got status 404 or
// Request timed out while requesting the provided URL
}
*/
const isReachable = (
url: string,
timeout: number = 2000,
requestOptions?: RequestOptions
): Promise<string | number | Error> => {
return new Promise((resolve, reject) => {
const { host, protocol, pathname } = parse(url);
if (!host) {
reject(Error("Invalid URL"));
}
const isHttps = protocol === "https:";
const options: RequestOptions = {
host,
timeout,
method: "HEAD",
path: pathname,
port: isHttps ? 443 : 80,
...requestOptions,
};
const network = isHttps ? https : http;
const req = network.request(options, (res) => {
const statusCode = res.statusCode ?? 404;
const isStatusOk = statusCode < 400;
if (isStatusOk) {
return resolve(statusCode);
}
reject(
Error(`Unable to resolve given URL, got status ${res.statusCode}`)
);
});
req.end();
req.on("timeout", () => {
req.destroy(Error("Request timed out while requesting the provided URL"));
});
req.on("error", (err) => {
reject(err);
});
});
};
export default isReachable;
console.time("took");
isReachable("https://learnaws.io").then((d) => {
console.log(d);
console.timeEnd("took");
});