linkfollower
Advanced tools
Comparing version 1.1.3 to 1.1.4
24
cli.js
@@ -6,5 +6,11 @@ #!/usr/bin/env node | ||
const follow = async url => { | ||
for await (const response of startFollowing(url)) { | ||
console.log(`${response.url} ${response.status}`) | ||
} | ||
const iterator = startFollowing(url) | ||
let result = await iterator.next() | ||
while (!result.done) { | ||
console.log(`${result.value.url} ${result.value.status}`) | ||
result = await iterator.next() | ||
} | ||
if (result.value?.status) { | ||
console.log(result.value.status) | ||
} | ||
} | ||
@@ -17,2 +23,12 @@ | ||
follow(process.argv[2]) | ||
const prefixWithHttp = url => { | ||
let pattern = new RegExp('^http'); | ||
if (!pattern.test(url)) { | ||
return 'http://' + url; | ||
} | ||
return url; | ||
} | ||
const url = new URL(prefixWithHttp(process.argv[2])) | ||
follow(url) |
@@ -28,3 +28,3 @@ import fetch from 'node-fetch' | ||
keepGoing = response.redirect | ||
url = response.redirectUrl | ||
url = keepGoing ? new URL(response.redirectUrl) : null | ||
yield response | ||
@@ -39,17 +39,16 @@ } catch (err) { | ||
const visit = async url => { | ||
url = prefixWithHttp(url) | ||
try { | ||
const response = await fetch(url, fetchOptions) | ||
if (isRedirect(response.status)) { | ||
const location = response.headers.get('location').replaceAll(/\/$/g, "") | ||
if (!location) { | ||
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: location } | ||
return { url: url, redirect: true, status: response.status, redirectUrl: addPathTo(locationHeader, url.origin) } | ||
} | ||
if (response.status == 200) { | ||
const redirectUrl = extractMetaRefreshUrl(await response.text()) | ||
return redirectUrl ? | ||
{ url: url, redirect: true, status: '200 + META REFRESH', redirectUrl: redirectUrl } : | ||
const metaRefreshUrl = extractMetaRefreshUrl(await response.text()) | ||
return metaRefreshUrl ? | ||
{ url: url, redirect: true, status: '200 + META REFRESH', redirectUrl: addPathTo(metaRefreshUrl, url.origin) } : | ||
{ url: url, redirect: false, status: response.status } | ||
@@ -75,11 +74,2 @@ } | ||
const prefixWithHttp = url => { | ||
let pattern = new RegExp('^http'); | ||
if (!pattern.test(url)) { | ||
return 'http://' + url; | ||
} | ||
return url; | ||
} | ||
const stripUnwantedCharsFrom = (url) => url | ||
@@ -89,2 +79,10 @@ .replaceAll("'", "") | ||
.replaceAll(" ", "") | ||
.replaceAll(/\/$/g, "") | ||
.replaceAll(/\/$/g, "") | ||
const addPathTo = (locationResponse, base) => { | ||
if (locationResponse.startsWith('http')) { | ||
new URL(locationResponse) | ||
} | ||
return new URL(locationResponse, base) | ||
} |
{ | ||
"name": "linkfollower", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"type": "module", | ||
@@ -11,3 +11,3 @@ "description": "Node.js based command line utility that lets you follow redirects to see where http URLs end up. Useful for shortened URLs.", | ||
"devDependencies": { | ||
"chai": "4.3.7", | ||
"chai": "5.0.0", | ||
"express": "4.18.2", | ||
@@ -14,0 +14,0 @@ "mocha": "10.2.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4781
98