- Removed deprecated functions related to document parsing and blob storage in blobstore.ts. - Introduced new PDF rendering logic in pdf-preview-renderer.ts and pdf-preview-pdfjs-runtime.ts. - Updated previews-render.ts to utilize the new PDF rendering functions. - Refactored user-whisper-align-job.ts to use the compute-worker client for alignment requests. - Enhanced artifact.ts and operation.ts to validate parsed PDF artifacts and resolve current PDF parses. - Updated snapshot.ts to align with new worker operation types. - Adjusted runtime-config.ts to check for compute-worker availability. - Modified types in parsed-pdf.ts and tts.ts to reflect changes in the compute-worker protocol. - Added unit tests for PDF artifact validation and compute-worker client contract. - Removed obsolete pdf-op-key.vitest.spec.ts test file.
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
let cachedStandardFontDataUrl: string | null = null;
|
|
const pdfjsPackageName = 'pdfjs-dist';
|
|
|
|
function candidatePackageRoots(): string[] {
|
|
const roots = new Set<string>();
|
|
let dir = process.cwd();
|
|
|
|
for (let i = 0; i < 8; i += 1) {
|
|
roots.add(path.join(dir, 'node_modules', pdfjsPackageName));
|
|
const next = path.dirname(dir);
|
|
if (next === dir) break;
|
|
dir = next;
|
|
}
|
|
|
|
return [...roots];
|
|
}
|
|
|
|
function resolvePdfjsPackageFile(relativePath: string): string {
|
|
const normalizedRelativePath = relativePath.replace(/^\/+/, '');
|
|
const candidates = candidatePackageRoots().map((root) => path.join(root, normalizedRelativePath));
|
|
const found = candidates.find((candidate) => fs.existsSync(candidate));
|
|
if (found) return found;
|
|
|
|
throw new Error(
|
|
`pdfjs-dist file not found: ${normalizedRelativePath}; checked ${candidates.join(', ')}`,
|
|
);
|
|
}
|
|
|
|
export function resolvePdfjsStandardFontDataUrl(): string {
|
|
if (cachedStandardFontDataUrl) return cachedStandardFontDataUrl;
|
|
|
|
const pdfjsPackageDir = path.dirname(resolvePdfjsPackageFile('package.json'));
|
|
const standardFontDir = path.join(pdfjsPackageDir, 'standard_fonts');
|
|
|
|
if (!fs.existsSync(standardFontDir)) {
|
|
throw new Error(`pdfjs-dist standard_fonts directory not found at ${standardFontDir}`);
|
|
}
|
|
|
|
cachedStandardFontDataUrl = `${standardFontDir.replace(/\/?$/, '/')}`;
|
|
return cachedStandardFontDataUrl;
|
|
}
|
|
|
|
export function resolvePdfjsWorkerSrc(): string {
|
|
return pathToFileURL(resolvePdfjsPackageFile('legacy/build/pdf.worker.mjs')).href;
|
|
}
|
|
|
|
export function configurePdfjsNodeRuntime(pdfjs: {
|
|
GlobalWorkerOptions?: {
|
|
workerSrc?: string;
|
|
workerPort?: unknown;
|
|
};
|
|
}): void {
|
|
if (!pdfjs.GlobalWorkerOptions) return;
|
|
pdfjs.GlobalWorkerOptions.workerSrc = resolvePdfjsWorkerSrc();
|
|
pdfjs.GlobalWorkerOptions.workerPort = null;
|
|
}
|