Migrate url tooling to twenty-shared (#10440)
Migrate and unify URL tooling in twenty-shared. We now have: - isValidHostname which follows our own business rules - a zod schema that can be re-used in different context and leverages is isValidHostname - isValidUrl on top of the zod schema - a getAbsoluteURl and getHostname on top of the zod schema I have added a LOT of tests to cover all the cases I've found Also fixes: https://github.com/twentyhq/twenty/issues/10147
This commit is contained in:
26
packages/twenty-shared/src/utils/url/isValidHostname.ts
Normal file
26
packages/twenty-shared/src/utils/url/isValidHostname.ts
Normal file
@ -0,0 +1,26 @@
|
||||
export const isValidHostname = (
|
||||
url: string,
|
||||
options?: {
|
||||
allowLocalhost?: boolean;
|
||||
allowIp?: boolean;
|
||||
},
|
||||
): boolean => {
|
||||
const allowIp = options?.allowIp ?? true;
|
||||
const allowLocalhost = options?.allowLocalhost ?? true;
|
||||
|
||||
const regex =
|
||||
/^(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]{1,1}\.){1,10}(xn--)?([a-z0-9][a-z0-9-]{0,60}|[a-z0-9-]{1,30}\.[a-z]{2,})$/;
|
||||
const isMatch = regex.test(url);
|
||||
const isIp = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(url);
|
||||
const isLocalhost = url === 'localhost' || url === '127.0.0.1';
|
||||
|
||||
if (isLocalhost && !allowLocalhost) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isIp && !allowIp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isMatch || isLocalhost || isIp;
|
||||
};
|
||||
Reference in New Issue
Block a user