| Index: lib/url.js |
| =================================================================== |
| --- a/lib/url.js |
| +++ b/lib/url.js |
| @@ -35,16 +35,87 @@ |
| for (let key in publicSuffixes) |
| map.set(key, publicSuffixes[key]); |
| return map; |
| } |
| /** |
| + * A <code>URLInfo</code> object represents information about a URL. It is |
| + * returned by <code>{@link parseURL}</code>. |
| + */ |
| +class URLInfo |
| +{ |
| + /** |
| + * @private |
| + */ |
| + constructor(href, protocol = "", hostname = "") |
| + { |
| + this._href = href; |
| + this._protocol = protocol; |
| + this._hostname = hostname; |
| + } |
| + |
| + /** |
| + * The entire URL. |
| + * @type {string} |
| + */ |
| + get href() |
| + { |
| + return this._href; |
| + } |
| + |
| + /** |
| + * The protocol scheme of the URL, including the final <code>:</code>. |
| + * @type {string} |
| + */ |
| + get protocol() |
| + { |
| + return this._protocol; |
| + } |
| + |
| + /** |
| + * The hostname of the URL. |
| + * @type {string} |
| + */ |
| + get hostname() |
| + { |
| + return this._hostname; |
| + } |
| + |
| + /** |
| + * Returns the entire URL. |
| + * @returns {string} The entire URL. |
| + */ |
| + toString() |
| + { |
| + return this._href; |
| + } |
| +} |
| + |
| +/** |
| + * Parses a URL to extract the protocol and the hostname. This is a lightweight |
| + * alternative to the native <code>URL</code> object. Unlike the |
| + * <code>URL</code> object, this function is not robust and will give incorrect |
| + * results for invalid URLs. <em>Use this function with valid, normalized, |
| + * properly encoded (IDNA and percent-encoding) URLs only.</em> |
| + * |
| + * @param {string} url The URL to parse. |
| + * @returns {URLInfo} Information about the URL. |
| + */ |
| +function parseURL(url) |
| +{ |
| + let match = /^([^:]+:)(?:\/\/(?:[^/]*@)?(\[[^\]]*\]|[^:/]+))?/.exec(url); |
| + return match ? new URLInfo(url, match[1], match[2]) : new URLInfo(url); |
| +} |
| + |
| +exports.parseURL = parseURL; |
| + |
| +/** |
| * Normalizes a hostname. |
| * @param {string} hostname |
| * @returns {string} |
| */ |
| function normalizeHostname(hostname) |
| { |
| return (hostname[hostname.length - 1] == "." ? |
| hostname.replace(/\.+$/, "") : hostname).toLowerCase(); |