Skip to content

Commit

Permalink
fix: do not crash on unresolved domain (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster authored and JustinBeckwith committed Feb 13, 2019
1 parent 25e1882 commit 629b83c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
"@types/mocha": "^5.2.5",
"@types/nock": "^9.3.1",
"@types/node": "^11.9.3",
"@types/sinon": "^7.0.5",
"@types/update-notifier": "^2.5.0",
"codecov": "^3.1.0",
"gts": "^0.9.0",
"mocha": "^5.0.0",
"nock": "^10.0.6",
"nyc": "^13.2.0",
"semantic-release": "^15.13.3",
"sinon": "^7.2.3",
"source-map-support": "^0.5.10",
"typescript": "^3.3.3"
},
Expand Down
33 changes: 20 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,32 @@ export class LinkChecker extends EventEmitter {
}

// Perform a HEAD or GET request based on the need to crawl
const res = await gaxios.request<string>({
method: 'GET',
url: opts.url,
responseType: opts.crawl ? 'text' : 'stream',
validateStatus: () => true
});
const result: LinkResult = {
url: opts.url,
status: res.status,
state: (res.status >= 200 && res.status < 300) ? LinkState.OK :
LinkState.BROKEN
};
let status = 0;
let state = LinkState.BROKEN;
let data = '';
try {
const res = await gaxios.request<string>({
method: 'GET',
url: opts.url,
responseType: opts.crawl ? 'text' : 'stream',
validateStatus: () => true
});
status = res.status;
if (res.status >= 200 && res.status < 300) {
state = LinkState.OK;
}
data = res.data;
} catch (err) {
// request failure: invalid domain name, etc.
}
const result: LinkResult = {url: opts.url, status, state};
opts.results.push(result);
this.emit('link', result);

// If we need to go deeper, scan the next level of depth for links and crawl
if (opts.crawl) {
this.emit('pagestart', opts.url);
const urls = getLinks(res.data, opts.checkOptions.path);
const urls = getLinks(data, opts.checkOptions.path);
for (const url of urls) {
// only crawl links that start with the same host
const crawl = url.startsWith(opts.checkOptions.path);
Expand Down
13 changes: 13 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as assert from 'assert';
import * as gaxios from 'gaxios';
import * as nock from 'nock';
import * as sinon from 'sinon';

import {check, LinkState} from '../src';

nock.disableNetConnect();
Expand Down Expand Up @@ -48,6 +51,16 @@ describe('linkinator', () => {
assert.strictEqual(results.links.length, 2);
});

it('should handle fetch exceptions', async () => {
const requestStub = sinon.stub(gaxios, 'request');
requestStub.throws('Fetch error');
const results = await check({path: 'test/fixtures/basic'});
assert.ok(!results.passed);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length, 1);
requestStub.restore();
});

it('should skip mailto: links', async () => {
const results = await check({path: 'test/fixtures/mailto'});
assert.ok(results.passed);
Expand Down

0 comments on commit 629b83c

Please sign in to comment.