-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttps-worker.js
executable file
·49 lines (44 loc) · 1.09 KB
/
https-worker.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
async function test(url, name) {
return new Promise((resolve, reject) => {
require((url.protocol === 'https:') ? 'https' : 'http').get(url, res => {
if (res.statusCode === 301 && res.headers.location) {
test(new URL(res.headers.location, url), name).then(bool => {
resolve(bool);
}).catch(error => {
reject(error);
});
} else {
if (res.statusCode !== 200) {
resolve(false);
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', chunk => {
rawData += chunk;
});
res.on('end', () => {
rawData = rawData.replace(/(<([^>]+)>)/ig, '');
if (rawData.includes(name) && !rawData.includes('Not Found') && !rawData.includes('Not found')) {
resolve(true);
} else {
resolve(false);
}
});
}
}).on('error', e => {
reject(e);
});
});
}
process.on('message', msg => {
const dis = msg.split(' ');
const url = dis[0];
const name = dis[1];
test(new URL(url), name).then(bool => {
process.send(bool);
}).catch(error => {
process.send(error.message);
}).finally(() => {
process.exit(0);
});
});