fix: lazy-load pdf-parse to prevent startup crash if module is missing
This commit is contained in:
@@ -63,9 +63,15 @@ export function isPdfConversionError(r: unknown): r is PdfConversionError {
|
||||
);
|
||||
}
|
||||
|
||||
// pdf-parse v1 default export: (buffer, options?) => Promise<{ text, numpages, info }>
|
||||
// Lazy-loaded so the app starts even if pdf-parse is missing from node_modules.
|
||||
let _pdfParse: ((buf: Buffer) => Promise<{ text: string }>) | undefined;
|
||||
function getPdfParse() {
|
||||
if (!_pdfParse) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const pdfParse: (buf: Buffer) => Promise<{ text: string }> = require('pdf-parse');
|
||||
_pdfParse = require('pdf-parse') as (buf: Buffer) => Promise<{ text: string }>;
|
||||
}
|
||||
return _pdfParse;
|
||||
}
|
||||
|
||||
export async function convertPdfToStatement(
|
||||
buffer: Buffer,
|
||||
@@ -80,7 +86,7 @@ export async function convertPdfToStatement(
|
||||
|
||||
let text: string;
|
||||
try {
|
||||
const result = await pdfParse(buffer);
|
||||
const result = await getPdfParse()(buffer);
|
||||
text = result.text || '';
|
||||
} catch (err) {
|
||||
console.error('PDF extraction error:', err);
|
||||
|
||||
Reference in New Issue
Block a user