From f89a06c6801bf7968b9f131f2a9b04833a6b01a3 Mon Sep 17 00:00:00 2001 From: OFF0 Date: Sat, 11 Mar 2023 12:11:08 +0100 Subject: [PATCH] utils: cleanup and move isvalidurl to utils/url --- src/utils/dom.ts | 28 ++-------------------------- src/utils/url.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/utils/dom.ts b/src/utils/dom.ts index 06b8cd0..81e9d7f 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -1,3 +1,5 @@ +import {isValidURL} from './url'; + type DataAttributes = { data: { [key: string]: string | number; @@ -62,32 +64,6 @@ export const lockScroll = () => document.body.style.overflow = 'hidden'; /** free global page scrolling */ export const unlockScroll = () => document.body.style.removeProperty('overflow'); -export const isValidURL = (url: URL) => { - if (!['http:', 'https:'].includes(url.protocol)) { - return false; - } - if (!['', '443', '80'].includes(url.port)) { - return false; - } - if (url.hostname === 'localhost') { - return false; - } - const lastDot = url.hostname.lastIndexOf('.'); - if (lastDot < 1) { - return false; - } - if (url.hostname.slice(lastDot) === '.local') { - return false; - } - if (url.hostname.slice(lastDot + 1).match(/^[\d]+$/)) { // there should be no tld with numbers, possible ipv4 - return false; - } - if (url.hostname.includes(':')) { // possibly an ipv6 addr; certainly an invalid hostname - return false; - } - return true; -} - /** * example usage: * diff --git a/src/utils/url.ts b/src/utils/url.ts index 0309cd9..4503605 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -37,3 +37,29 @@ export const getNoxyUrl = ( link.searchParams.set('url', url); return link; }; + +export const isValidURL = (url: URL) => { + if (!['http:', 'https:'].includes(url.protocol)) { + return false; + } + if (!['', '443', '80'].includes(url.port)) { + return false; + } + if (url.hostname === 'localhost') { + return false; + } + const lastDot = url.hostname.lastIndexOf('.'); + if (lastDot < 1) { + return false; + } + if (url.hostname.slice(lastDot) === '.local') { + return false; + } + if (url.hostname.slice(lastDot + 1).match(/^[\d]+$/)) { // there should be no tld with numbers, possible ipv4 + return false; + } + if (url.hostname.includes(':')) { // possibly an ipv6 addr; certainly an invalid hostname + return false; + } + return true; +};