refactor(net): share hostname normalization

This commit is contained in:
Peter Steinberger
2026-02-16 01:01:22 +00:00
parent d5ee766afe
commit 4aaafe5322
3 changed files with 9 additions and 16 deletions

View File

@@ -0,0 +1,7 @@
export function normalizeHostname(hostname: string): string {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
if (normalized.startsWith("[") && normalized.endsWith("]")) {
return normalized.slice(1, -1);
}
return normalized;
}

View File

@@ -1,6 +1,7 @@
import { lookup as dnsLookupCb, type LookupAddress } from "node:dns";
import { lookup as dnsLookup } from "node:dns/promises";
import { Agent, type Dispatcher } from "undici";
import { normalizeHostname } from "./hostname.js";
type LookupCallback = (
err: NodeJS.ErrnoException | null,
@@ -25,14 +26,6 @@ export type SsrFPolicy = {
const BLOCKED_HOSTNAMES = new Set(["localhost", "metadata.google.internal"]);
function normalizeHostname(hostname: string): string {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
if (normalized.startsWith("[") && normalized.endsWith("]")) {
return normalized.slice(1, -1);
}
return normalized;
}
function normalizeHostnameSet(values?: string[]): Set<string> {
if (!values || values.length === 0) {
return new Set<string>();