Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle invalid url #26

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:[email protected]">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:[email protected]">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>