| [9af201e] | 1 | /**
|
|---|
| 2 | * @param {{ protocol?: string, auth?: string, hostname?: string, port?: string, pathname?: string, search?: string, hash?: string, slashes?: boolean }} objURL
|
|---|
| 3 | * @returns {string}
|
|---|
| 4 | */
|
|---|
| 5 | function format(objURL) {
|
|---|
| 6 | var protocol = objURL.protocol || "";
|
|---|
| 7 | if (protocol && protocol.substr(-1) !== ":") {
|
|---|
| 8 | protocol += ":";
|
|---|
| 9 | }
|
|---|
| 10 | var auth = objURL.auth || "";
|
|---|
| 11 | if (auth) {
|
|---|
| 12 | auth = encodeURIComponent(auth);
|
|---|
| 13 | auth = auth.replace(/%3A/i, ":");
|
|---|
| 14 | auth += "@";
|
|---|
| 15 | }
|
|---|
| 16 | var host = "";
|
|---|
| 17 | if (objURL.hostname) {
|
|---|
| 18 | host = auth + (objURL.hostname.indexOf(":") === -1 ? objURL.hostname : "[".concat(objURL.hostname, "]"));
|
|---|
| 19 | if (objURL.port) {
|
|---|
| 20 | host += ":".concat(objURL.port);
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | var pathname = objURL.pathname || "";
|
|---|
| 24 | if (objURL.slashes) {
|
|---|
| 25 | host = "//".concat(host || "");
|
|---|
| 26 | if (pathname && pathname.charAt(0) !== "/") {
|
|---|
| 27 | pathname = "/".concat(pathname);
|
|---|
| 28 | }
|
|---|
| 29 | } else if (!host) {
|
|---|
| 30 | host = "";
|
|---|
| 31 | }
|
|---|
| 32 | var search = objURL.search || "";
|
|---|
| 33 | if (search && search.charAt(0) !== "?") {
|
|---|
| 34 | search = "?".concat(search);
|
|---|
| 35 | }
|
|---|
| 36 | var hash = objURL.hash || "";
|
|---|
| 37 | if (hash && hash.charAt(0) !== "#") {
|
|---|
| 38 | hash = "#".concat(hash);
|
|---|
| 39 | }
|
|---|
| 40 | pathname = pathname.replace(/[?#]/g,
|
|---|
| 41 | /**
|
|---|
| 42 | * @param {string} match
|
|---|
| 43 | * @returns {string}
|
|---|
| 44 | */
|
|---|
| 45 | function (match) {
|
|---|
| 46 | return encodeURIComponent(match);
|
|---|
| 47 | });
|
|---|
| 48 | search = search.replace("#", "%23");
|
|---|
| 49 | return "".concat(protocol).concat(host).concat(pathname).concat(search).concat(hash);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * @param {URL & { fromCurrentScript?: boolean }} parsedURL
|
|---|
| 54 | * @returns {string}
|
|---|
| 55 | */
|
|---|
| 56 | function createSocketURL(parsedURL) {
|
|---|
| 57 | var hostname = parsedURL.hostname;
|
|---|
| 58 |
|
|---|
| 59 | // Node.js module parses it as `::`
|
|---|
| 60 | // `new URL(urlString, [baseURLString])` parses it as '[::]'
|
|---|
| 61 | var isInAddrAny = hostname === "0.0.0.0" || hostname === "::" || hostname === "[::]";
|
|---|
| 62 |
|
|---|
| 63 | // why do we need this check?
|
|---|
| 64 | // hostname n/a for file protocol (example, when using electron, ionic)
|
|---|
| 65 | // see: https://github.com/webpack/webpack-dev-server/pull/384
|
|---|
| 66 | if (isInAddrAny && self.location.hostname && self.location.protocol.indexOf("http") === 0) {
|
|---|
| 67 | hostname = self.location.hostname;
|
|---|
| 68 | }
|
|---|
| 69 | var socketURLProtocol = parsedURL.protocol || self.location.protocol;
|
|---|
| 70 |
|
|---|
| 71 | // When https is used in the app, secure web sockets are always necessary because the browser doesn't accept non-secure web sockets.
|
|---|
| 72 | if (socketURLProtocol === "auto:" || hostname && isInAddrAny && self.location.protocol === "https:") {
|
|---|
| 73 | socketURLProtocol = self.location.protocol;
|
|---|
| 74 | }
|
|---|
| 75 | socketURLProtocol = socketURLProtocol.replace(/^(?:http|.+-extension|file)/i, "ws");
|
|---|
| 76 | var socketURLAuth = "";
|
|---|
| 77 |
|
|---|
| 78 | // `new URL(urlString, [baseURLstring])` doesn't have `auth` property
|
|---|
| 79 | // Parse authentication credentials in case we need them
|
|---|
| 80 | if (parsedURL.username) {
|
|---|
| 81 | socketURLAuth = parsedURL.username;
|
|---|
| 82 |
|
|---|
| 83 | // Since HTTP basic authentication does not allow empty username,
|
|---|
| 84 | // we only include password if the username is not empty.
|
|---|
| 85 | if (parsedURL.password) {
|
|---|
| 86 | // Result: <username>:<password>
|
|---|
| 87 | socketURLAuth = socketURLAuth.concat(":", parsedURL.password);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | // In case the host is a raw IPv6 address, it can be enclosed in
|
|---|
| 92 | // the brackets as the brackets are needed in the final URL string.
|
|---|
| 93 | // Need to remove those as url.format blindly adds its own set of brackets
|
|---|
| 94 | // if the host string contains colons. That would lead to non-working
|
|---|
| 95 | // double brackets (e.g. [[::]]) host
|
|---|
| 96 | //
|
|---|
| 97 | // All of these web socket url params are optionally passed in through resourceQuery,
|
|---|
| 98 | // so we need to fall back to the default if they are not provided
|
|---|
| 99 | var socketURLHostname = (hostname || self.location.hostname || "localhost").replace(/^\[(.*)\]$/, "$1");
|
|---|
| 100 | var socketURLPort = parsedURL.port;
|
|---|
| 101 | if (!socketURLPort || socketURLPort === "0") {
|
|---|
| 102 | socketURLPort = self.location.port;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // If path is provided it'll be passed in via the resourceQuery as a
|
|---|
| 106 | // query param so it has to be parsed out of the querystring in order for the
|
|---|
| 107 | // client to open the socket to the correct location.
|
|---|
| 108 | var socketURLPathname = "/ws";
|
|---|
| 109 | if (parsedURL.pathname && !parsedURL.fromCurrentScript) {
|
|---|
| 110 | socketURLPathname = parsedURL.pathname;
|
|---|
| 111 | }
|
|---|
| 112 | return format({
|
|---|
| 113 | protocol: socketURLProtocol,
|
|---|
| 114 | auth: socketURLAuth,
|
|---|
| 115 | hostname: socketURLHostname,
|
|---|
| 116 | port: socketURLPort,
|
|---|
| 117 | pathname: socketURLPathname,
|
|---|
| 118 | slashes: true
|
|---|
| 119 | });
|
|---|
| 120 | }
|
|---|
| 121 | export default createSocketURL; |
|---|