Remove url npm dependency and use Web URL constructor (#10930)

This commit is contained in:
Michael Telatynski 2023-05-17 12:50:00 +01:00 committed by GitHub
parent 7917d973e7
commit 2da199c41d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 101 additions and 53 deletions

View file

@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import url from "url";
/**
* If a url has no path component, etc. abbreviate it to just the hostname
*
@ -25,11 +23,16 @@ import url from "url";
export function abbreviateUrl(u?: string): string {
if (!u) return "";
const parsedUrl = url.parse(u);
// if it's something we can't parse as a url then just return it
if (!parsedUrl) return u;
let parsedUrl: URL;
try {
parsedUrl = parseUrl(u);
} catch (e) {
console.error(e);
// if it's something we can't parse as a url then just return it
return u;
}
if (parsedUrl.path === "/") {
if (parsedUrl.pathname === "/") {
// we ignore query / hash parts: these aren't relevant for IS server URLs
return parsedUrl.host || "";
}
@ -42,8 +45,15 @@ export function unabbreviateUrl(u?: string): string {
let longUrl = u;
if (!u.startsWith("https://")) longUrl = "https://" + u;
const parsed = url.parse(longUrl);
if (parsed.hostname === null) return u;
const parsed = parseUrl(longUrl);
if (!parsed.hostname) return u;
return longUrl;
}
export function parseUrl(u: string): URL {
if (!u.includes(":")) {
u = window.location.protocol + u;
}
return new URL(u);
}

View file

@ -15,7 +15,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import * as url from "url";
import { base32 } from "rfc4648";
import { IWidget, IWidgetData } from "matrix-widget-api";
import { Room } from "matrix-js-sdk/src/models/room";
@ -36,6 +35,7 @@ import { Jitsi } from "../widgets/Jitsi";
import { objectClone } from "./objects";
import { _t } from "../languageHandler";
import { IApp, isAppWidget } from "../stores/WidgetStore";
import { parseUrl } from "./UrlUtils";
// How long we wait for the state event echo to come back from the server
// before waitFor[Room/User]Widget rejects its promise
@ -106,7 +106,7 @@ export default class WidgetUtils {
return false;
}
const testUrl = url.parse(testUrlString);
const testUrl = parseUrl(testUrlString);
let scalarUrls = SdkConfig.get().integrations_widgets_urls;
if (!scalarUrls || scalarUrls.length === 0) {
const defaultManager = IntegrationManagers.sharedInstance().getPrimaryManager();
@ -118,7 +118,7 @@ export default class WidgetUtils {
}
for (let i = 0; i < scalarUrls.length; i++) {
const scalarUrl = url.parse(scalarUrls[i]);
const scalarUrl = parseUrl(scalarUrls[i]);
if (testUrl && scalarUrl) {
if (
testUrl.protocol === scalarUrl.protocol &&