Files
web-check/api/_common/parse-target.js
2026-05-04 21:03:52 +01:00

18 lines
516 B
JavaScript

// Normalise a user-supplied target, stripping :port for DNS lookups
export const parseTarget = (input) => {
if (!input) throw new Error('No target provided');
const normalised = /^https?:\/\//i.test(input) ? input : `https://${input}`;
let u;
try { u = new URL(normalised); }
catch { throw new Error(`Invalid URL: ${input}`); }
return {
hostname: u.hostname,
port: u.port || null,
protocol: u.protocol,
pathname: u.pathname || '/',
href: u.href,
};
};
export default parseTarget;