-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinger.js
97 lines (90 loc) · 2.61 KB
/
pinger.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
// Copyright 2024 Joseph P Medley
'use strict';
const https = require('https');
const RECOVERABLE_ERRORS = 'ECONNRESET,EPROTO,ETIMEDOUT';
let REQUEST_OPTIONS = {
hostname: 'developer.mozilla.org',
path: ''
}
const MDN = "https://" + REQUEST_OPTIONS.hostname;
const RETRY_COUNT = 5;
const STATUS_NEEDS_RETRY = 0;
const STATUS_COMPLETE = 1;
class Pinger {
constructor(records, httpOptions) {
this._records = records;
this._httpOptions = httpOptions;
}
async pingRecords(verboseOutput = false) {
for (let r of this._records) {
if (!r.mdn_url) { continue; }
if (r.mdn_url === "No URL found in compatibility data") { continue; }
if (verboseOutput) { console.log(r.key); }
let retryCount = RETRY_COUNT;
while (retryCount > 0) {
let status = await this._ping(r)
.catch(e => {
console.log(e);
});
if (status == STATUS_NEEDS_RETRY) {
retryCount--;
if (retryCount == 0) { r.mdn_exists = false; }
}
else if (status == STATUS_COMPLETE) { retryCount = 0; }
}
}
return this._records;
}
_ping(record) {
return new Promise((resolve, reject) => {
try {
REQUEST_OPTIONS.path = record.mdn_url.replace(MDN, '/en-US');
https.get(REQUEST_OPTIONS, (res) => {
const status = res.statusCode.toString();
let code;
if (status.match(/3\d\d/)) {
record.mdn_exists = false;
record.redirect = true;
code = STATUS_COMPLETE;
resolve(code);
}
if (status.match(/4\d\d/)) {
record.mdn_exists = false;
// For some reason the const doesn't survive resolve().
code = STATUS_COMPLETE;
resolve(code);
}
if (status.match(/5\d\d/)) {
code = STATUS_NEEDS_RETRY;
resolve(code);
}
res.on('data', (chunk) => {
res.resume();
});
res.on('end', () => {
if (status.match(/2\d\d/)) {
record.mdn_exists = true;
record.redirect = false;
code = STATUS_COMPLETE;
resolve(code);
}
});
res.on('error', (e) => {
global.__logger.error(e);
if (RECOVERABLE_ERRORS.includes(e.code)) {
// resolve(Status.needsretry);
code = STATUS_NEEDS_RETRY;
resolve(code);
}
else {
reject(e);
}
});
});
} catch(e) {
reject(e);
}
})
}
}
module.exports.Pinger = Pinger;