From 64689dc0ec0bbe12e8fe740a3657d4dda8715627 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Mon, 23 Sep 2024 22:34:19 +0900 Subject: [PATCH] fix: replace `URL.canParse()` [URL.canParse][1] was only pretty recently created, and is not available in many environments. We can instead wrap a `URL` creation in a `try..catch`, as listed in MDN. Unfortunately, this is a bit slower, but at least it works almost everywhere. [1]: https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static Fixes: https://github.com/braintree/sanitize-url/issues/78 --- src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 83faca9..0612916 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,13 @@ function decodeHtmlCharacters(str: string) { } function isValidUrl(url: string): boolean { - return URL.canParse(url); + try { + // We can replace this with URL.canParse() once it's more widely available + new URL(url); // eslint-disable-line no-new -- We're only checking for exceptions + return true; + } catch (error) { + return false; + } } function decodeURI(uri: string): string {