Skip to content

Commit

Permalink
Use HTTP check even for HTTP URLs with non-default ports (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
solarispika committed May 5, 2022
1 parent dd7e83d commit d753f49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -38,10 +38,11 @@ const checkHttp = async (url, timeout) => {
const getAddress = async hostname => net.isIP(hostname) ? hostname : (await dnsLookupP(hostname)).address;

const isTargetReachable = timeout => async target => {
const isHTTP = target.startsWith('https://') || target.startsWith('http://');
const url = new URL(prependHttp(target));

if (!url.port) {
url.port = url.protocol === 'http:' ? 80 : 443;
if (!url.port && !isHTTP) {
url.port = 443;
}

let address;
Expand All @@ -55,7 +56,7 @@ const isTargetReachable = timeout => async target => {
return false;
}

if ([80, 443].includes(url.port)) {
if (isHTTP) {
return checkHttp(url.toString(), timeout);
}

Expand Down
13 changes: 13 additions & 0 deletions test.js
@@ -1,5 +1,6 @@
import {promisify} from 'util';
import dns from 'dns';
import http from 'http';
import test from 'ava';
import isReachable from '.';

Expand All @@ -26,6 +27,18 @@ test('multiple https urls', async t => {
t.true(await isReachable(['https://google.com', 'https://baidu.com']));
});

test('http server on custom port', async t => {
const server = http.createServer((_, response) => {
response.writeHead(200).end();
}).listen(8080);
t.true(await isReachable('http://localhost:8080'));
server.close();
});

test('unreachable http server on custom port', async t => {
t.false(await isReachable('http://localhost:8081'));
});

test('imap host and port', async t => {
t.true(await isReachable('imap.gmail.com:995'));
});
Expand Down

0 comments on commit d753f49

Please sign in to comment.