Skip to content

Commit

Permalink
just a little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jksolbakken committed Jul 17, 2024
1 parent 6ce22de commit e4648bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
13 changes: 5 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <URL>')
process.exit(1)
}

const prefixWithHttp = url => {
Expand All @@ -28,5 +20,10 @@ const prefixWithHttp = url => {
return url;
}

if (process.argv.length != 3) {
console.log('Usage: follow <URL>')
process.exit(1)
}

const url = new URL(prefixWithHttp(process.argv[2]))
follow(url)
29 changes: 12 additions & 17 deletions linkfollower.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}` }
}
Expand Down

0 comments on commit e4648bf

Please sign in to comment.