From 69c8c462eb2d3e063c9ceb4fd63d4bf36f452a56 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 22 Dec 2021 15:41:40 +0300 Subject: [PATCH 1/5] fix: drop `url` package --- client-src/utils/createSocketURL.js | 65 +++++++++++++-- client-src/utils/getCurrentScriptSource.js | 3 + client-src/utils/parseURL.js | 38 +++++---- package-lock.json | 93 ++++++---------------- package.json | 1 - 5 files changed, 105 insertions(+), 95 deletions(-) diff --git a/client-src/utils/createSocketURL.js b/client-src/utils/createSocketURL.js index 168c3b6c9e..9f55c54805 100644 --- a/client-src/utils/createSocketURL.js +++ b/client-src/utils/createSocketURL.js @@ -1,12 +1,67 @@ -import url from "url"; +function format(objURL) { + let protocol = objURL.protocol || ""; + + if (protocol && protocol.substr(-1) !== ":") { + protocol += ":"; + } + + let auth = objURL.auth || ""; + + if (auth) { + auth = encodeURIComponent(auth); + auth = auth.replace(/%3A/i, ":"); + auth += "@"; + } + + let host = false; + + if (objURL.hostname) { + host = + auth + + (objURL.hostname.indexOf(":") === -1 + ? objURL.hostname + : `[${objURL.hostname}]`); + + if (objURL.port) { + host += `:${objURL.port}`; + } + } + + let pathname = objURL.pathname || ""; + + if (objURL.slashes) { + host = `//${host || ""}`; + + if (pathname && pathname.charAt(0) !== "/") { + pathname = `/${pathname}`; + } + } else if (!host) { + host = ""; + } + + let search = objURL.search || ""; + + if (search && search.charAt(0) !== "?") { + search = `?${search}`; + } + + let hash = objURL.hash || ""; + + if (hash && hash.charAt(0) !== "#") { + hash = `#${hash}`; + } + + pathname = pathname.replace(/[?#]/g, (match) => encodeURIComponent(match)); + search = search.replace("#", "%23"); + + return `${protocol}${host}${pathname}${search}${hash}`; +} -// We handle legacy API that is Node.js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers -// Please look at https://nodejs.org/api/url.html#url_url_strings_and_url_objects function createSocketURL(parsedURL) { let { hostname } = parsedURL; // Node.js module parses it as `::` - // `new URL(urlString, [baseURLstring])` parses it as '[::]' + // `new URL(urlString, [baseURLString])` parses it as '[::]' const isInAddrAny = hostname === "0.0.0.0" || hostname === "::" || hostname === "[::]"; @@ -80,7 +135,7 @@ function createSocketURL(parsedURL) { socketURLPathname = parsedURL.pathname; } - return url.format({ + return format({ protocol: socketURLProtocol, auth: socketURLAuth, hostname: socketURLHostname, diff --git a/client-src/utils/getCurrentScriptSource.js b/client-src/utils/getCurrentScriptSource.js index 01b05cb66d..2c16610389 100644 --- a/client-src/utils/getCurrentScriptSource.js +++ b/client-src/utils/getCurrentScriptSource.js @@ -1,3 +1,6 @@ +/** + * @returns {string} + */ function getCurrentScriptSource() { // `document.currentScript` is the most accurate way to find the current script, // but is not supported in all browsers. diff --git a/client-src/utils/parseURL.js b/client-src/utils/parseURL.js index 9be4077d25..df4288f19d 100644 --- a/client-src/utils/parseURL.js +++ b/client-src/utils/parseURL.js @@ -1,6 +1,9 @@ -import url from "url"; import getCurrentScriptSource from "./getCurrentScriptSource.js"; +/** + * @param {string} resourceQuery + * @returns {URL} + */ function parseURL(resourceQuery) { let options = {}; @@ -16,25 +19,20 @@ function parseURL(resourceQuery) { // Else, get the url from the