From e4648bf1e335358d401b1a921d3e21555bfad14a Mon Sep 17 00:00:00 2001 From: "J-K. Solbakken" Date: Wed, 17 Jul 2024 13:51:50 +0200 Subject: [PATCH] just a little cleanup --- cli.js | 13 +++++-------- linkfollower.js | 29 ++++++++++++----------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/cli.js b/cli.js index cd58c72..260b9d6 100755 --- a/cli.js +++ b/cli.js @@ -9,14 +9,6 @@ const follow = async url => { console.log(`${result.value.url} ${result.value.status}`) result = await iterator.next() } - if (result.value?.status) { - console.log(result.value.status) - } -} - -if (process.argv.length != 3) { - console.log('Usage: follow ') - process.exit(1) } const prefixWithHttp = url => { @@ -28,5 +20,10 @@ const prefixWithHttp = url => { return url; } +if (process.argv.length != 3) { + console.log('Usage: follow ') + process.exit(1) +} + const url = new URL(prefixWithHttp(process.argv[2])) follow(url) diff --git a/linkfollower.js b/linkfollower.js index a494e29..b8789d9 100644 --- a/linkfollower.js +++ b/linkfollower.js @@ -17,7 +17,7 @@ export default async function* startFollowing(url) { let keepGoing = true while (keepGoing) { if (count > MAX_REDIRECT_DEPTH) { - return { url: url, status: `Max redirect depth of ${MAX_REDIRECT_DEPTH} exceeded` } + return { url: url, status: `Max redirect depth of ${MAX_REDIRECT_DEPTH} exceeded` } } try { const response = await visit(url) @@ -34,26 +34,21 @@ export default async function* startFollowing(url) { const visit = async url => { try { - const response = await fetch(url, fetchOptions) + const response = await fetch(url, fetchOptions) if (isRedirect(response.status)) { const locationHeader = response.headers.get('location').replaceAll(/\/$/g, "") - if (!locationHeader) { - return { status: `${url} responded with status ${response.status} but no location header` } - } - return { url: url, redirect: true, status: response.status, redirectUrl: addBaseTo(locationHeader, url.origin) } - } - + return locationHeader + ? { url: url, redirect: true, status: response.status, redirectUrl: addBaseTo(locationHeader, url.origin) } + : { status: `${url} responded with status ${response.status} but no location header` } + } + if (response.status === 200) { const html = await response.text() - for (const urlExtractor of extractors) { - const extracted = urlExtractor(html) - if (extracted) { - return { url: url, redirect: true, status: '200 + extracted', redirectUrl: addBaseTo(extracted, url.origin) } - } - } - - return { url: url, redirect: false, status: response.status } - } + const extracted = extractors.flatMap((extractor) => extractor(html)).filter((elem) => elem != null) + return extracted.length !== 0 + ? { url: url, redirect: true, status: '200 + extracted', redirectUrl: new URL(addBaseTo(extracted[0], url.origin)) } + : { url: url, redirect: false, status: response.status } + } } catch (error) { return { status: `${error.message}` } }