fix(api): use createRequire for pdf-parse (CJS-only, exports map blocks deep path)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,13 +3,13 @@ import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
|||||||
import { tmpdir } from 'node:os';
|
import { tmpdir } from 'node:os';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { promisify } from 'node:util';
|
import { promisify } from 'node:util';
|
||||||
|
import { createRequire } from 'node:module';
|
||||||
import mammoth from 'mammoth';
|
import mammoth from 'mammoth';
|
||||||
// pdf-parse имеет auto-test mode в index.js, ломающий ESM. Импортируем функцию напрямую из lib.
|
|
||||||
// @ts-expect-error пакет без declaration на этом пути
|
|
||||||
import pdfParseRaw from 'pdf-parse/lib/pdf-parse.js';
|
|
||||||
|
|
||||||
const pdfParse: (buf: Buffer) => Promise<{ text: string; numpages: number }> =
|
// pdf-parse — CJS-only. Прямой ESM-импорт ломается из-за auto-test mode в index.js.
|
||||||
pdfParseRaw as unknown as (buf: Buffer) => Promise<{ text: string; numpages: number }>;
|
// createRequire даёт CJS loader, в котором module.parent определён → test-mode не активен.
|
||||||
|
const require = createRequire(import.meta.url);
|
||||||
|
const pdfParse: (buf: Buffer) => Promise<{ text: string; numpages: number }> = require('pdf-parse');
|
||||||
|
|
||||||
const execFileP = promisify(execFile);
|
const execFileP = promisify(execFile);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user