fix: stream large project texts and scope API capabilities
Analyze UTF-8 text files as bounded batches of complete lines and reassemble only approved spans without loading entire files. Preserve line endings, BOM, Unicode offsets and source-change checks. Scope capability checks to selected file types, report missing features and HTTP failures, and retain the required Office image-policy handling. Cover large files, chunk boundaries, mappings and failure cleanup with regression tests.
This commit is contained in:
38
src/lib/project-capabilities.ts
Normal file
38
src/lib/project-capabilities.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/** Requirements follow the files that will actually use the API. */
|
||||
export function requiredProjectCapabilities(documents: boolean): string[] {
|
||||
return ["request_force_masks", ...(documents ? [
|
||||
"document_hit_review", "document_review_mapping", "document_restoration",
|
||||
"comment_author_review", "revision_author_review", "document_image_policy",
|
||||
] : [])];
|
||||
}
|
||||
|
||||
export async function checkProjectCapabilities(
|
||||
url: string, documents: boolean,
|
||||
fetcher: (url: string, init: RequestInit) => Promise<Response>,
|
||||
): Promise<void> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetcher(url, { signal: AbortSignal.timeout(15_000) });
|
||||
} catch (error) {
|
||||
const timeout = error instanceof Error && /timeout|abort/i.test(error.name);
|
||||
throw new Error(`Velum: ${timeout ? "Zeitlimit bei der Prüfung der Projektfähigkeiten" : "Projekt-API nicht erreichbar"}: ${url}.`, { cause: error });
|
||||
}
|
||||
if (response.status === 404 || response.status === 405) {
|
||||
throw new Error(`Velum: Projekt-API-Endpunkt fehlt (HTTP ${response.status}): ${url}. Backend aktualisieren oder Basis-URL prüfen.`);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`Velum: Projektfähigkeiten nicht abrufbar (HTTP ${response.status}): ${url}. Anmeldung, Proxy und Backend prüfen.`);
|
||||
}
|
||||
if (!(response.headers.get("content-type") ?? "").includes("application/json")) {
|
||||
throw new Error(`Velum: Keine JSON-Antwort für ${url}. Anmeldung oder API-Weiterleitung prüfen.`);
|
||||
}
|
||||
const capabilities: unknown = await response.json();
|
||||
if (!capabilities || typeof capabilities !== "object" || Array.isArray(capabilities)) {
|
||||
throw new Error(`Velum: Ungültige Projektfähigkeiten von ${url}.`);
|
||||
}
|
||||
const missing = requiredProjectCapabilities(documents)
|
||||
.filter((key) => (capabilities as Record<string, unknown>)[key] !== true);
|
||||
if (missing.length) {
|
||||
throw new Error(`Velum: Backend unterstützt die ausgewählten ${documents ? "Office-/PDF-Dateien" : "Textdateien"} noch nicht vollständig. Fehlende API-Fähigkeiten: ${missing.join(", ")}. API: ${url}. Backend aktualisieren.`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user