import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; import { extname } from 'node:path'; import type { ImageRecognizer } from './pipeline.js'; const run = promisify(execFile); // Optional adapter: requires a separately installed Tesseract executable. // Passes arguments directly (no shell), enforces a timeout and output cap. export const tesseractAdapter: ImageRecognizer = async imagePath => { if (!['.png', '.jpg', '.jpeg', '.tif', '.tiff'].includes(extname(imagePath).toLowerCase())) { throw new Error('Only image files can be sent to the OCR adapter'); } const { stdout } = await run('tesseract', [imagePath, 'stdout', '-l', 'eng'], { timeout: 30_000, maxBuffer: 2_000_000, windowsHide: true, }); return stdout; };