linkfollower
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -5,2 +5,5 @@ const http = require('http'); | ||
const metaRefreshPattern = '(CONTENT|content)=["\']0;[ ]*(URL|url)=(.*?)(["\']\s*>)'; | ||
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'; | ||
const MAX_REDIRECT_DEPTH = 10; | ||
@@ -12,3 +15,3 @@ | ||
let parsedUrl = url.parse(link); | ||
if (parsedUrl.host == null) { | ||
if (!parsedUrl.host) { | ||
reject(link + ' is not a valid URL'); | ||
@@ -39,3 +42,3 @@ } | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246' | ||
'User-Agent': userAgent | ||
} | ||
@@ -45,16 +48,23 @@ }; | ||
requestor.get(options, response => { | ||
response.on('data', d => {/* don't care */}); | ||
let html = []; | ||
response.on('data', d => { | ||
html.push(d.toString()); | ||
}); | ||
response.on('end', () => { | ||
result.push({ url: location.href, status: response.statusCode }); | ||
if (isRedirect(response)) { | ||
if (!response.headers.location) { | ||
failure(new Error(location.href + ' returned a redirect but no Location header')); | ||
let processedResponse = processResponse(response, "".concat(...html)); | ||
let statusCode = response.statusCode; | ||
let statusTxt = (statusCode === 200 && processedResponse.url) ? '200 + META REFRESH' : statusCode; | ||
result.push({ url: location.href, status: statusTxt}); | ||
if (processedResponse.redirect) { | ||
if (!processedResponse.url) { | ||
failure(new Error(location.href + ' returned a redirect but no URL')); | ||
return; | ||
} | ||
depth++; | ||
visit(location.parse(response.headers.location)); | ||
visit(location.parse(processedResponse.url)); | ||
} else { | ||
success(result); | ||
} | ||
}); | ||
@@ -69,5 +79,6 @@ }).on('error', function (err) { | ||
let isRedirect = response => { | ||
let processResponse = (response, html) => { | ||
let statusCode = response.statusCode; | ||
return statusCode === 301 | ||
let httpRedirect = | ||
statusCode === 301 | ||
|| statusCode === 302 | ||
@@ -77,5 +88,15 @@ || statusCode === 303 | ||
|| statusCode === 308; | ||
if (httpRedirect) { | ||
return { redirect: true, url: response.headers.location } | ||
} | ||
let htmlRefreshUrl = extractMetaRefreshUrl(html); | ||
if (htmlRefreshUrl) { | ||
return { redirect: true, url: htmlRefreshUrl } | ||
} | ||
return { redirect: false } | ||
} | ||
let prefixWithHttp = function(link) { | ||
let prefixWithHttp = link => { | ||
let pattern = new RegExp('^http'); | ||
@@ -89,2 +110,7 @@ if (!pattern.test(link)) { | ||
let extractMetaRefreshUrl = html => { | ||
let match = html.match(metaRefreshPattern); | ||
return match && match.length == 5 ? match[3] : null; | ||
} | ||
exports.follow = follow; |
{ | ||
"name": "linkfollower", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Node.js based command line utility that lets you follow redirects to see where http URLs end up. Useful for shortened URLs.", | ||
@@ -9,5 +9,5 @@ "main": "cli.js", | ||
"chai": "^3.5.0", | ||
"chai-as-promised": "^5.3.0", | ||
"express": "^4.14.0", | ||
"mocha": "^3.0.2" | ||
"chai-as-promised": "^6.0.0", | ||
"express": "^4.15.2", | ||
"mocha": "^3.2.0" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": { |
@@ -5,4 +5,6 @@ # linkfollower | ||
[![npm version](https://badge.fury.io/js/linkfollower.png)](https://badge.fury.io/js/linkfollower) | ||
Node.js based command line utility for finding out where a shortened (or any other) HTTP URL end up. | ||
Follows up to 10 redirects. Now also adds Ms Edge User-Agent header to requests since Facebook won't redirect | ||
Follows up to 10 redirects. Now also adds Chrome User-Agent header to requests since Facebook won't redirect | ||
unsupported browsers from fb.me addresses. | ||
@@ -9,0 +11,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
5086
105
27