(input)
| 9 | |
| 10 | // Normalise a user-supplied target, stripping :port for DNS lookups |
| 11 | export const parseTarget = (input) => { |
| 12 | if (!input) throw new Error('No target provided'); |
| 13 | let normalised = /^https?:\/\//i.test(input) ? input : `https://${input}`; |
| 14 | normalised = bracketIPv6(normalised); |
| 15 | let u; |
| 16 | try { |
| 17 | u = new URL(normalised); |
| 18 | } catch { |
| 19 | throw new Error(`Invalid URL: ${input}`); |
| 20 | } |
| 21 | return { |
| 22 | hostname: u.hostname.replace(/^\[|]$/g, ''), |
| 23 | port: u.port || null, |
| 24 | protocol: u.protocol, |
| 25 | pathname: u.pathname || '/', |
| 26 | href: u.href, |
| 27 | }; |
| 28 | }; |
| 29 | |
| 30 | export default parseTarget; |
no test coverage detected