lib/url.js
var request = require("request"); var url = module.exports;

Check a URL meets basic validity requirements. Return true if the URL is of the form: http://domain.tld[/other/parts] OR https://... ftp://... Otherwise, raise an Error.

Parameters:

  • theUrl must be a String.
    (the URL to validate)
url.checkUrl = function(theUrl) { log.debug('function checkUrl: ' + theUrl); var protocol = /^(f|ht)tps?:\/\//i.test(theUrl); var domain = /:\/\/\w+(\.\w+)*([:\/].+)*$/i.test(theUrl); if (!protocol || !domain) {

not a valid URL

var msg = 'malformed URL: ' + theUrl + '; ' if (!protocol) { msg += 'protocol missing (must include http(s):// or ftp(s)://)' } if (!domain) { if (!protocol) { msg += ', ' } msg += 'domain missing' } var e = new Error(msg); throw e; } return true; }

Convert a file:/// url to an absolute remote URL. Rendering pages locally sometimes adds a spurious 'file:///' to the beginning of relative resource paths. This function strips the 'file:///' and constructs an absolute url.

Parameters:

  • path must be a String.
    (resource path to clean)

  • pageUrl must be a String.
    (URL of the page the resource was linked from)

url.cleanResourcePath = function(path, pageUrl) { if (/^(f|ht)tps?:\/\//i.test(path)) {

already absolute

return path; } else if (/^file:\/\/\/?/i.test(path) || (/^\//.test(path))) {

root relative path

var relative = path.replace(/^(file:)?\/+/gi, ''); var root = url.getRoot(pageUrl); return [root, relative].join('/'); } else { return url.relativeToAbsolute(path, pageUrl); } }

Get the base URL from a URL

Parameters:

  • fullUrl must be a String.
    (the URL)

Returns a String
(the base URL)

url.getBase = function(fullUrl) { var splitUrl = fullUrl.split('://'); if (splitUrl.length > 1 && splitUrl[1].split('/').length == 1) {

naked domain - return unchanged

return fullUrl } splitUrl = fullUrl.split('/'); return splitUrl.slice(0, splitUrl.length - 1).join('/'); }

Get the root URL from a URL

Parameters:

  • fullUrl must be a String.
    (the URL)

Returns a String
(the root URL)

url.getRoot = function(fullUrl) { var splitUrl = fullUrl.split('/'); return splitUrl.slice(0, 3).join('/'); }

Convert a relative URL to an absolute

Parameters:

  • relative must be a String.
    (the relative URL to convert)

  • current must be a String.
    (the URL to which relative is relative)

Returns a String
(the converted URL)

url.relativeToAbsolute = function(relative, current) { return [url.getBase(current), relative].join('/'); }

Resolve HTTP redirects

url.resolveRedirects = function(url, callback) { request({ url: url, method: 'HEAD' }, function(err, response, body){ callback(err, response.request.href); }); }