Skip to content

Commit

Permalink
fix: handle invalid url (#26)
Browse files Browse the repository at this point in the history
- Fixes #25
  • Loading branch information
styfle committed Sep 4, 2023
1 parent 400fd6f commit 2d8ccb0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"scripts": {
"build": "tsc",
"format": "prettier \".\" --write --ignore-path \".lintignore\"",
"lint:eslint": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".lintignore\"",
"lint:prettier": "prettier \".\" --check --ignore-path \".lintignore\"",
"test": "node --test test/*.test.js"
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ export async function awaken({
// absolute/external href so set depth=0 to stop crawling deeper
newDepth = 0;
} catch {
absoluteUrl = new URL(href, url);
// relative/internal href so decrement depth and continue crawling
newDepth = depth - 1;
try {
absoluteUrl = new URL(href, url);
// relative/internal href so decrement depth and continue crawling
newDepth = depth - 1;
} catch {
return; // ignore invalid urls
}
}
promises.push(
awaken({
Expand Down
38 changes: 38 additions & 0 deletions test/awaken-protocols.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import assert from 'node:assert';
import test from 'node:test';
import { awaken } from '../dist/index.js';
import { serveLocalFixture } from './server-helper.js';

test('should awaken protocols fixture', async () => {
const { server, initUrl } = await serveLocalFixture(
'./fixture-protocols/',
5353,
);
try {
const results = new Map();
let count = 0;
const onAwaken = () => {
count++;
};
const start = Date.now();
const returnValue = await awaken({ url: initUrl, onAwaken, results });
const end = Date.now();
const ms = end - start;
assert.strictEqual(returnValue, undefined);
assert.strictEqual(count, 1);
assert.strictEqual(results.size, 1);
assert.ok(
ms < (process.env.CI ? 200 : 75),
`should be quick, but took ${ms}ms`,
);
assert.deepStrictEqual(Array.from(results.values()), [
{
referer: '',
status: 200,
url: 'http://127.0.0.1:5353/home.html',
},
]);
} finally {
server.close();
}
});
10 changes: 10 additions & 0 deletions test/fixture-protocols/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Strange protocols</h1>
<ul>
<li><a href="mailto:me@example.com">Mail To Protocol</a></li>
<li><a href="file:///Users/me/example.html">File Protocol</a></li>
<li><a href="ftp://example.com/path/file.txt:21">FTP Protocol</a></li>
<li><a href="telnet:towel.blinkenlights.nl:23">Telnet Protocol</a></li>
<li><a href="tel:+1-201-555-0123">Tel Protocol</a></li>
<li><a href="data:image/png;base64,iVBORw0KGgoAAAA==">Data Protocol</a></li>
<li><a href="http://tel:04993195xx">Broken url</a></li>
</ul>
3 changes: 3 additions & 0 deletions test/fixture/about.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<h1>About</h1>
<ul>
<li><a href="./sub/nested.html">Sub Nested</a></li>

<li><a href="mailto:me@example.com">Mail To Protocol</a></li>
<li><a href="file:///Users/me/example.html">File Protocol</a></li>
<li><a href="ftp://example.com/path/file.txt:21">FTP Protocol</a></li>
<li><a href="telnet:towel.blinkenlights.nl:23">Telnet Protocol</a></li>
<li><a href="tel:+1-201-555-0123">Tel Protocol</a></li>
<li><a href="data:image/png;base64,iVBORw0KGgoAAAA==">Data Protocol</a></li>
<li><a href="http://tel:04993195xx">Broken url</a></li>
</ul>

0 comments on commit 2d8ccb0

Please sign in to comment.