UNPKG

2.49 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2017, Philip Walton <philip@philipwalton.com>
4 */
5Object.defineProperty(exports, "__esModule", { value: true });
6var core_1 = require("@aws-amplify/core");
7var HTTP_PORT = '80';
8var HTTPS_PORT = '443';
9var DEFAULT_PORT = RegExp(':(' + HTTP_PORT + '|' + HTTPS_PORT + ')$');
10var a = core_1.JS.browserOrNode().isBrowser ? document.createElement('a') : null;
11var cache = {};
12/**
13 * Parses the given url and returns an object mimicing a `Location` object.
14 * @param {string} url The url to parse.
15 * @return {!Object} An object with the same properties as a `Location`.
16 */
17function parseUrl(u) {
18 var url = u;
19 // All falsy values (as well as ".") should map to the current URL.
20 url = !url || url === '.' ? location.href : url;
21 if (cache[url])
22 return cache[url];
23 a.href = url;
24 // When parsing file relative paths (e.g. `../index.html`), IE will correctly
25 // resolve the `href` property but will keep the `..` in the `path` property.
26 // It will also not include the `host` or `hostname` properties. Furthermore,
27 // IE will sometimes return no protocol or just a colon, especially for things
28 // like relative protocol URLs (e.g. "//google.com").
29 // To workaround all of these issues, we reparse with the full URL from the
30 // `href` property.
31 if (url.charAt(0) === '.' || url.charAt(0) === '/')
32 return parseUrl(a.href);
33 // Don't include default ports.
34 var port = a.port === HTTP_PORT || a.port === HTTPS_PORT ? '' : a.port;
35 // PhantomJS sets the port to "0" when using the file: protocol.
36 port = port === '0' ? '' : port;
37 // Sometimes IE incorrectly includes a port for default ports
38 // (e.g. `:80` or `:443`) even when no port is specified in the URL.
39 // http://bit.ly/1rQNoMg
40 var host = a.host.replace(DEFAULT_PORT, '');
41 // Not all browser support `origin` so we have to build it.
42 var origin = a['origin'] ? a['origin'] : a.protocol + '//' + host;
43 // Sometimes IE doesn't include the leading slash for pathname.
44 // http://bit.ly/1rQNoMg
45 var pathname = a.pathname.charAt(0) === '/' ? a.pathname : '/' + a.pathname;
46 return (cache[url] = {
47 hash: a.hash,
48 host: host,
49 hostname: a.hostname,
50 href: a.href,
51 origin: origin,
52 pathname: pathname,
53 port: port,
54 protocol: a.protocol,
55 search: a.search,
56 });
57}
58exports.parseUrl = parseUrl;
59//# sourceMappingURL=parse-url.js.map
\No newline at end of file