96 lines
4.2 KiB
TypeScript
96 lines
4.2 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import { STREET_PREFIXES, STREET_SUFFIXES } from "./catalog";
|
|
|
|
export const HESSEN_STREET_SOURCE_URL = "https://www.geoportal.hessen.de/spatial-objects/706/collections/ad%3AThoroughfareName/items?f=json&limit=500";
|
|
|
|
type GeoJsonPage = {
|
|
numberMatched?: number;
|
|
features?: Array<{ properties?: { text?: unknown } }>;
|
|
};
|
|
|
|
export type GeodataValidationReport = {
|
|
sourceUrl: string;
|
|
sourceHashSha256: string;
|
|
officialStreetCount: number;
|
|
generatedStreetCount: number;
|
|
collisions: string[];
|
|
};
|
|
|
|
function normalizeStreet(value: string): string {
|
|
return value
|
|
.normalize("NFD")
|
|
.replace(/[\u0300-\u036f]/gu, "")
|
|
.replace(/ß/gu, "ss")
|
|
.replace(/�/gu, "ss")
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]/gu, "");
|
|
}
|
|
|
|
export function generatedStreetNames(): string[] {
|
|
const result = new Set<string>();
|
|
for (let index = 0; index < 60; index += 1) {
|
|
result.add(`${STREET_PREFIXES[(index * 11) % STREET_PREFIXES.length]!}${STREET_SUFFIXES[(index * 5) % STREET_SUFFIXES.length]!}`);
|
|
}
|
|
return [...result].sort((left, right) => left.localeCompare(right, "de"));
|
|
}
|
|
|
|
async function fetchPage(fetchImpl: typeof fetch, offset: number): Promise<{ raw: string; page: GeoJsonPage }> {
|
|
let lastError: unknown;
|
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
try {
|
|
const response = await fetchImpl(`${HESSEN_STREET_SOURCE_URL}&offset=${offset}`, {
|
|
headers: { accept: "application/geo+json" },
|
|
signal: AbortSignal.timeout(20_000),
|
|
});
|
|
if (!response.ok) throw new Error(`Hessen geodata request failed with HTTP ${response.status}.`);
|
|
const raw = await response.text();
|
|
const page = JSON.parse(raw) as GeoJsonPage;
|
|
if (!Array.isArray(page.features)) throw new Error(`Hessen geodata page at offset ${offset} has no feature array.`);
|
|
return { raw, page };
|
|
} catch (error) {
|
|
lastError = error;
|
|
}
|
|
}
|
|
throw lastError instanceof Error ? lastError : new Error(`Hessen geodata page at offset ${offset} failed.`);
|
|
}
|
|
|
|
/**
|
|
* Checks the synthetic street catalog against the official Hessen street-name
|
|
* snapshot. This is an explicit development-time gate and never runs during a
|
|
* staging seed, keeping the production seed independent from remote services.
|
|
*/
|
|
export async function validateGeneratedStreets(fetchImpl: typeof fetch = fetch): Promise<GeodataValidationReport> {
|
|
const first = await fetchPage(fetchImpl, 0);
|
|
const numberMatched = first.page.numberMatched ?? first.page.features!.length;
|
|
if (!Number.isSafeInteger(numberMatched) || numberMatched <= 0) throw new Error("Hessen geodata response has an invalid numberMatched value.");
|
|
const offsets = Array.from({ length: Math.max(0, Math.ceil(numberMatched / 500) - 1) }, (_, index) => (index + 1) * 500);
|
|
const pages = [first];
|
|
for (const batchStart of Array.from({ length: Math.ceil(offsets.length / 8) }, (_, index) => index * 8)) {
|
|
pages.push(...(await Promise.all(offsets.slice(batchStart, batchStart + 8).map((offset) => fetchPage(fetchImpl, offset)))));
|
|
}
|
|
for (const [pageIndex, { page }] of pages.entries()) {
|
|
if (page.numberMatched !== undefined && page.numberMatched !== numberMatched)
|
|
throw new Error("Hessen geodata pagination returned inconsistent numberMatched values.");
|
|
const expectedFeatures = Math.min(500, numberMatched - pageIndex * 500);
|
|
if (page.features!.length !== expectedFeatures) {
|
|
throw new Error(`Hessen geodata page at offset ${pageIndex * 500} is incomplete.`);
|
|
}
|
|
}
|
|
const officialNames = pages
|
|
.flatMap(({ page }) => page.features!)
|
|
.flatMap(({ properties }) => (typeof properties?.text === "string" ? [properties.text] : []));
|
|
if (officialNames.length !== numberMatched) throw new Error("Hessen geodata snapshot contains incomplete street-name properties.");
|
|
const officialNormalized = new Set(officialNames.map(normalizeStreet));
|
|
const generated = generatedStreetNames();
|
|
const hash = createHash("sha256");
|
|
for (const { raw } of pages) hash.update(raw);
|
|
return {
|
|
sourceUrl: HESSEN_STREET_SOURCE_URL,
|
|
sourceHashSha256: hash.digest("hex"),
|
|
officialStreetCount: officialNames.length,
|
|
generatedStreetCount: generated.length,
|
|
collisions: generated.filter((name) => officialNormalized.has(normalizeStreet(name))),
|
|
};
|
|
}
|