Simplify PDF utils dynamic imports for Bun runtime.
Remove pkg bytecode workarounds that are no longer needed outside Node pkg builds. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
7d8031e009
commit
07cbd2e07c
@ -2,53 +2,39 @@
|
|||||||
* PDF conversion utilities loaded on demand so headless startup
|
* PDF conversion utilities loaded on demand so headless startup
|
||||||
* does not require ESM-only pdf-to-img at module load time.
|
* does not require ESM-only pdf-to-img at module load time.
|
||||||
*/
|
*/
|
||||||
import { createRequire } from 'node:module'
|
import { pathToFileURL } from "node:url";
|
||||||
import { pathToFileURL } from 'node:url'
|
import { createRequire } from "node:module";
|
||||||
|
|
||||||
const require = createRequire(import.meta.url)
|
const require = createRequire(import.meta.url);
|
||||||
|
|
||||||
// Dynamic import() from pkg snapshot bytecode throws "Invalid host defined options"
|
|
||||||
// (nodejs/node#43663). Use require() for CJS packages in pkg; ESM packages need
|
|
||||||
// --no-bytecode on the pkg build so file-URL import() works.
|
|
||||||
async function importPackage(name) {
|
async function importPackage(name) {
|
||||||
const packagePath = require.resolve(name)
|
const packagePath = require.resolve(name);
|
||||||
|
return import(pathToFileURL(packagePath).href);
|
||||||
if (typeof process.pkg !== 'undefined') {
|
|
||||||
try {
|
|
||||||
return require(packagePath)
|
|
||||||
} catch (error) {
|
|
||||||
if (error?.code !== 'ERR_REQUIRE_ESM') {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return import(pathToFileURL(packagePath).href)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveDefault(module) {
|
function resolveDefault(module) {
|
||||||
return module?.default ?? module
|
return module?.default ?? module;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function convertPDFToImage(pdfInput, options = {}) {
|
export async function convertPDFToImage(pdfInput, options = {}) {
|
||||||
const [pdfToImgModule, sharpModule] = await Promise.all([
|
const [pdfToImgModule, sharpModule] = await Promise.all([
|
||||||
importPackage('pdf-to-img'),
|
importPackage("pdf-to-img"),
|
||||||
importPackage('sharp')
|
importPackage("sharp"),
|
||||||
])
|
]);
|
||||||
const { pdf } = pdfToImgModule
|
const { pdf } = pdfToImgModule;
|
||||||
const sharp = resolveDefault(sharpModule)
|
const sharp = resolveDefault(sharpModule);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { width, height, page_numbers, scale = 2, ...pdfOnlyOptions } =
|
const { width, height, page_numbers, scale = 2, ...pdfOnlyOptions } =
|
||||||
options
|
options;
|
||||||
|
|
||||||
const pdfOptions = {
|
const pdfOptions = {
|
||||||
scale,
|
scale,
|
||||||
...pdfOnlyOptions
|
...pdfOnlyOptions,
|
||||||
}
|
};
|
||||||
|
|
||||||
const document = await pdf(pdfInput, pdfOptions)
|
const document = await pdf(pdfInput, pdfOptions);
|
||||||
const outputImages = []
|
const outputImages = [];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
page_numbers &&
|
page_numbers &&
|
||||||
@ -56,37 +42,37 @@ export async function convertPDFToImage(pdfInput, options = {}) {
|
|||||||
page_numbers.length > 0
|
page_numbers.length > 0
|
||||||
) {
|
) {
|
||||||
for (const pageNum of page_numbers) {
|
for (const pageNum of page_numbers) {
|
||||||
let image = await document.getPage(pageNum)
|
let image = await document.getPage(pageNum);
|
||||||
|
|
||||||
if (width || height) {
|
if (width || height) {
|
||||||
const resizeOptions = {}
|
const resizeOptions = {};
|
||||||
if (width) resizeOptions.width = width
|
if (width) resizeOptions.width = width;
|
||||||
if (height) resizeOptions.height = height
|
if (height) resizeOptions.height = height;
|
||||||
image = await sharp(image).resize(resizeOptions).toBuffer()
|
image = await sharp(image).resize(resizeOptions).toBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
outputImages.push(image)
|
outputImages.push(image);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for await (const image of document) {
|
for await (const image of document) {
|
||||||
let processedImage = image
|
let processedImage = image;
|
||||||
|
|
||||||
if (width || height) {
|
if (width || height) {
|
||||||
const resizeOptions = {}
|
const resizeOptions = {};
|
||||||
if (width) resizeOptions.width = width
|
if (width) resizeOptions.width = width;
|
||||||
if (height) resizeOptions.height = height
|
if (height) resizeOptions.height = height;
|
||||||
processedImage = await sharp(image)
|
processedImage = await sharp(image)
|
||||||
.resize(resizeOptions)
|
.resize(resizeOptions)
|
||||||
.toBuffer()
|
.toBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
outputImages.push(processedImage)
|
outputImages.push(processedImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputImages
|
return outputImages;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error converting PDF to image:', error)
|
console.error("Error converting PDF to image:", error);
|
||||||
throw error
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user