File object to parse
Parsed ImportedMatchData
export async function parseImportFile(file: File): Promise<ImportedMatchData> {
// Validate file type
if (!file.name.endsWith(".json")) {
throw createError(
ErrorType.IMPORT,
"Only JSON files are supported for import",
new Error("Invalid file type"),
"INVALID_FILE_TYPE",
);
}
// Validate file size (max 10MB)
const maxSizeBytes = 10 * 1024 * 1024;
if (file.size > maxSizeBytes) {
throw createError(
ErrorType.IMPORT,
`File too large. Maximum size is 10MB, but received ${(file.size / 1024 / 1024).toFixed(2)}MB`,
new Error(`File size: ${file.size} bytes`),
"FILE_TOO_LARGE",
);
}
// Read file as text
let fileContent: string;
try {
fileContent = await file.text();
} catch (error) {
throw createError(
ErrorType.IMPORT,
`Failed to read file: ${error instanceof Error ? error.message : "Unknown error"}`,
error,
"FILE_READ_FAILED",
);
}
// Parse JSON
let data: unknown;
try {
const pool = getJSONSerializationWorkerPool();
const { data: parsedData } = await pool.deserialize(fileContent);
data = parsedData;
console.info(
`[Import] 📦 Deserialized JSON using worker pool: ${fileContent.length} bytes`,
);
} catch (error) {
throw createError(
ErrorType.IMPORT,
`Invalid JSON format: ${error instanceof Error ? error.message : "JSON parsing failed"}`,
error,
"INVALID_JSON",
);
}
// Validate structure
const validation = validateImportedMatchData(data);
if (!validation.valid) {
throw createError(
ErrorType.IMPORT,
`Invalid import file structure:\n${validation.errors.join("\n")}`,
new Error(validation.errors.join("; ")),
"INVALID_STRUCTURE",
);
}
return data as ImportedMatchData;
}
Parses and validates an import file (JSON only). Reads file as text, parses JSON, and validates structure.