import process from "node:process"; import { resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { pdfToPng } from "pdf-to-png-converter"; import sharp from "sharp"; import { assertPreviewOutputSize, assertPreviewRasterMetadata, assertPreviewSourceSize, FILE_PREVIEW_MAX_INTERMEDIATE_BYTES, FILE_PREVIEW_MAX_OUTPUT_BYTES, FILE_PREVIEW_MAX_PAGES, FILE_PREVIEW_MAX_PIXELS, FILE_PREVIEW_MAX_SOURCE_BYTES, FILE_PREVIEW_MAX_THUMBNAIL_BYTES, type FilePreviewDecoderRequest, type FilePreviewDecoderResponse, type FilePreviewDecoderSuccess, } from "./file-preview-decoder-contract.js"; const PREVIEW_MAX_DIMENSION = 2_048; const THUMBNAIL_MAX_DIMENSION = 480; const SHARP_TIMEOUT_SECONDS = 15; // This module runs in a disposable child process. Keeping libvips to one // thread and disabling its cache makes peak native memory predictable; the // parent enforces a separate hard wall-clock timeout and kills this process. sharp.cache(false); sharp.concurrency(1); function asExactUint8Array(buffer: Buffer): Uint8Array { return Uint8Array.from(buffer); } async function renderRaster(input: Buffer, format: "jpeg" | "png" | "webp", maxDimension: number): Promise { const pipeline = sharp(input, { failOn: "warning", limitInputPixels: FILE_PREVIEW_MAX_PIXELS, page: 0, pages: 1, sequentialRead: true, }); const metadata = await pipeline.metadata(); assertPreviewRasterMetadata(metadata); const resized = pipeline .timeout({ seconds: SHARP_TIMEOUT_SECONDS }) .rotate() .resize({ width: maxDimension, height: maxDimension, fit: "inside", withoutEnlargement: true }); if (format === "jpeg") return resized.jpeg({ quality: 82, mozjpeg: true }).toBuffer(); if (format === "png") return resized.png({ compressionLevel: 9 }).toBuffer(); return resized.webp({ quality: 80 }).toBuffer(); } async function inspectRaster(input: Buffer): Promise { const metadata = await sharp(input, { failOn: "warning", limitInputPixels: FILE_PREVIEW_MAX_PIXELS, page: 0, pages: 1, sequentialRead: true, }) .timeout({ seconds: SHARP_TIMEOUT_SECONDS }) .metadata(); assertPreviewRasterMetadata(metadata); } async function convertHeicToJpeg(input: Buffer): Promise { const moduleName = "heic-convert"; const loaded = (await import(/* @vite-ignore */ moduleName)) as | { default: (options: { buffer: Buffer; format: "JPEG"; quality: number }) => Promise } | ((options: { buffer: Buffer; format: "JPEG"; quality: number }) => Promise); const heicConvert = typeof loaded === "function" ? loaded : loaded.default; const converted = await heicConvert({ buffer: input, format: "JPEG", quality: 0.85 }); const output = Buffer.isBuffer(converted) ? Buffer.from(converted) : Buffer.from(new Uint8Array(converted)); assertPreviewOutputSize(output.byteLength, FILE_PREVIEW_MAX_INTERMEDIATE_BYTES, "HEIC intermediate image"); return output; } async function renderPdfFirstPage(input: Buffer): Promise { const safeOptions = { concurrencyLimit: 1, disableFontFace: true, enableXfa: false, maxInputBytes: FILE_PREVIEW_MAX_SOURCE_BYTES, processPagesInParallel: false, useSystemFonts: false, verbosityLevel: 0 as const, viewportScale: 2, }; // Inspect page 1 and the first forbidden page without creating a canvas. // Presence of page MAX+1 proves the document exceeds the page limit. const metadata = await pdfToPng(input, { ...safeOptions, pagesToProcess: [1, FILE_PREVIEW_MAX_PAGES + 1], returnMetadataOnly: true, }); const firstPage = metadata.find((page) => page.pageNumber === 1); if (!firstPage) { throw new Error("PDF rendered no pages"); } if (metadata.some((page) => page.pageNumber === FILE_PREVIEW_MAX_PAGES + 1)) { throw new Error(`Preview source exceeds the ${FILE_PREVIEW_MAX_PAGES}-page limit`); } assertPreviewRasterMetadata({ width: firstPage.width, height: firstPage.height, pages: 1 }); const pages = await pdfToPng(input, { ...safeOptions, pagesToProcess: [1], returnPageContent: true, }); const content = pages[0]?.content; if (!content) { throw new Error("PDF rendered no page content"); } const output = Buffer.from(content); assertPreviewOutputSize(output.byteLength, FILE_PREVIEW_MAX_INTERMEDIATE_BYTES, "PDF intermediate image"); return output; } export async function decodeFilePreview(request: FilePreviewDecoderRequest): Promise { assertPreviewSourceSize(request.input.byteLength); const input = Buffer.from(request.input.buffer, request.input.byteOffset, request.input.byteLength); let preview: Buffer; let previewExtension: FilePreviewDecoderSuccess["previewExtension"]; let previewContentType: FilePreviewDecoderSuccess["previewContentType"]; if (request.mime === "image/heic" || request.mime === "image/heif") { // Parse HEIF container dimensions through libvips before heic-convert can // allocate the decoded bitmap. Failure is fail-closed even if the fallback // decoder might otherwise accept the file. await inspectRaster(input); const jpeg = await convertHeicToJpeg(input); preview = await renderRaster(jpeg, "jpeg", PREVIEW_MAX_DIMENSION); previewExtension = "jpg"; previewContentType = "image/jpeg"; } else if (request.mime === "application/pdf") { const png = await renderPdfFirstPage(input); preview = await renderRaster(png, "png", PREVIEW_MAX_DIMENSION); previewExtension = "png"; previewContentType = "image/png"; } else { preview = await renderRaster(input, "webp", PREVIEW_MAX_DIMENSION); previewExtension = "webp"; previewContentType = "image/webp"; } assertPreviewOutputSize(preview.byteLength, FILE_PREVIEW_MAX_OUTPUT_BYTES, "Preview image"); const thumbnail = await renderRaster(preview, "webp", THUMBNAIL_MAX_DIMENSION); assertPreviewOutputSize(thumbnail.byteLength, FILE_PREVIEW_MAX_THUMBNAIL_BYTES, "Preview thumbnail"); return { ok: true, preview: asExactUint8Array(preview), thumbnail: asExactUint8Array(thumbnail), previewExtension, previewContentType, }; } function errorMessage(error: unknown): string { const message = error instanceof Error ? error.message : String(error); return message.slice(0, 1_024); } const isMainProcess = process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url); if (isMainProcess && typeof process.send === "function") { process.once("message", (message: FilePreviewDecoderRequest) => { void decodeFilePreview(message) .then((result) => { process.send?.(result, () => process.exit(0)); }) .catch((error: unknown) => { const result: FilePreviewDecoderResponse = { ok: false, error: errorMessage(error) }; process.send?.(result, () => process.exit(1)); }); }); }