utils: cleanup and move isvalidurl to utils/url

OFF0 2 years ago
parent fcbf1c4272
commit f89a06c680
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -1,3 +1,5 @@
import {isValidURL} from './url';
type DataAttributes = { type DataAttributes = {
data: { data: {
[key: string]: string | number; [key: string]: string | number;
@ -62,32 +64,6 @@ export const lockScroll = () => document.body.style.overflow = 'hidden';
/** free global page scrolling */ /** free global page scrolling */
export const unlockScroll = () => document.body.style.removeProperty('overflow'); 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: * example usage:
* *

@ -37,3 +37,29 @@ export const getNoxyUrl = (
link.searchParams.set('url', url); link.searchParams.set('url', url);
return link; 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;
};

Loading…
Cancel
Save