From 64689dc0ec0bbe12e8fe740a3657d4dda8715627 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Mon, 23 Sep 2024 22:34:19 +0900 Subject: [PATCH 1/2] 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 { From 5fcba261f30679c68502c4f968e3ddf0052d0405 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Mon, 23 Sep 2024 22:38:55 +0900 Subject: [PATCH 2/2] ci: test using Node.JS v18.0 This would have caught like https://github.com/braintree/sanitize-url/issues/78, as `URL.canParse()` was only added in Node.JS v18.17 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0de48b1..2241b38 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,6 @@ jobs: - name: Use Node.js uses: actions/setup-node@v1 with: - node-version: "18.x" + node-version: "18.0" - run: npm install - run: npm test