best address logic

This commit is contained in:
Aiden McClelland
2025-08-07 17:15:23 -06:00
parent 4d5ff1a97b
commit 3845550e90
9 changed files with 119 additions and 107 deletions

View File

@@ -17,8 +17,7 @@ export type IpHostname =
}
| {
kind: "domain"
domain: string
subdomain: string | null
value: string
port: number | null
sslPort: number | null
}

View File

@@ -1,25 +0,0 @@
import { HostnameInfo } from "../types"
export function hostnameInfoToAddress(hostInfo: HostnameInfo): string {
if (hostInfo.kind === "onion") {
return `${hostInfo.hostname.value}`
}
if (hostInfo.kind !== "ip") {
throw Error("Expecting that the kind is ip.")
}
const hostname = hostInfo.hostname
if (hostname.kind === "domain") {
return `${hostname.subdomain ? `${hostname.subdomain}.` : ""}${hostname.domain}`
}
const port = hostname.sslPort || hostname.port
const portString = port ? `:${port}` : ""
if ("ipv4" === hostname.kind || "ipv6" === hostname.kind) {
return `${hostname.value}${portString}`
}
if ("local" === hostname.kind) {
return `${hostname.value}${portString}`
}
throw Error(
"Expecting to have a valid hostname kind." + JSON.stringify(hostname),
)
}

View File

@@ -3,6 +3,7 @@ import { knownProtocols } from "../interfaces/Host"
import { AddressInfo, Host, Hostname, HostnameInfo } from "../types"
import { Effects } from "../Effects"
import { DropGenerator, DropPromise } from "./Drop"
import { IPV6_LINK_LOCAL } from "./ip"
export type UrlString = string
export type HostId = string
@@ -95,9 +96,9 @@ export const addressHostToUrl = (
hostname = host.hostname.value
} else if (host.kind === "ip") {
if (host.hostname.kind === "domain") {
hostname = `${host.hostname.subdomain ? `${host.hostname.subdomain}.` : ""}${host.hostname.domain}`
hostname = host.hostname.value
} else if (host.hostname.kind === "ipv6") {
hostname = host.hostname.value.startsWith("fe80::")
hostname = IPV6_LINK_LOCAL.contains(host.hostname.value)
? `[${host.hostname.value}%${host.hostname.scopeId}]`
: `[${host.hostname.value}]`
} else {

View File

@@ -1,6 +1,7 @@
/// Currently being used
export { addressHostToUrl } from "./getServiceInterface"
export { getDefaultString } from "./getDefaultString"
export * from "./ip"
/// Not being used, but known to be browser compatible
export { GetServiceInterface, getServiceInterface } from "./getServiceInterface"
@@ -16,6 +17,5 @@ export { splitCommand } from "./splitCommand"
export { nullIfEmpty } from "./nullIfEmpty"
export { deepMerge, partialDiff } from "./deepMerge"
export { deepEqual } from "./deepEqual"
export { hostnameInfoToAddress } from "./Hostname"
export * as regexes from "./regexes"
export { stringFromStdErrOut } from "./stringFromStdErrOut"

View File

@@ -73,3 +73,5 @@ export const PRIVATE_IPV4_RANGES = [
new IpNet("172.16.0.0/12"),
new IpNet("192.168.0.0/16"),
]
export const IPV6_LINK_LOCAL = new IpNet("fe80::/10")