mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-14 17:06:39 +00:00
Add Smart Host Parsing
This commit is contained in:
15
src/lib/parseHostTarget.ts
Normal file
15
src/lib/parseHostTarget.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function parseHostTarget(input: string) {
|
||||
try {
|
||||
const normalized = input.match(/^https?:\/\//) ? input : `http://${input}`;
|
||||
const url = new URL(normalized);
|
||||
|
||||
const protocol = url.protocol.replace(":", ""); // http | https
|
||||
const host = url.hostname;
|
||||
const port = url.port ? parseInt(url.port, 10) : protocol === "https" ? 443 : 80;
|
||||
|
||||
return { protocol, host, port };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user