Merge pull request 'fix: lazy-load pdf-parse to prevent startup crash if module is missing' (#4) from fix-lazyload-pdfparse-crash into main
Reviewed-on: #4
This commit was merged in pull request #4.
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
|
// 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(
|
export async function convertPdfToStatement(
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
@@ -80,7 +86,7 @@ export async function convertPdfToStatement(
|
|||||||
|
|
||||||
let text: string;
|
let text: string;
|
||||||
try {
|
try {
|
||||||
const result = await pdfParse(buffer);
|
const result = await getPdfParse()(buffer);
|
||||||
text = result.text || '';
|
text = result.text || '';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('PDF extraction error:', err);
|
console.error('PDF extraction error:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user