Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
"file-type": "^19.6.0",
"tmp": "^0.2.3",
"tslib": "^2.8.1",
"undici": "^6.23.0",
"node-poppler": "^7.2.4"
"undici": "^6.23.0"
},
"optionalDependencies": {
"sharp": "~0.34.5",
"@cantoo/pdf-lib": "^2.3.2",
"pdf.js-extract": "^0.2.1"
"pdf.js-extract": "^0.2.1",
"node-poppler": "^7.2.4"
},
"keywords": [
"typescript",
Expand Down
12 changes: 8 additions & 4 deletions src/image/extractedImage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Buffer } from "node:buffer";
import { MindeeError } from "@/errors/index.js";
import { writeFileSync } from "node:fs";
import { writeFile } from "fs/promises";
import path from "node:path";
import { logger } from "@/logger.js";
import { BufferInput, MIMETYPES } from "@/input/index.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type * as popplerTypes from "node-poppler";
import { writeFile } from "fs/promises";
import { BufferInput, MIMETYPES } from "@/input/index.js";
import { logger } from "@/logger.js";
import { loadOptionalDependency } from "@/dependency/index.js";

/**
Expand Down Expand Up @@ -34,7 +36,9 @@ export class ExtractedImage {
try {
let outputBuffer: Buffer = this.buffer;
if (fileExt !== ".pdf") {
const popplerImport = await loadOptionalDependency<typeof popplerTypes>("node-poppler", "Image Processing");
const popplerImport = await loadOptionalDependency<typeof popplerTypes>(
"node-poppler", "Image Processing"
);
const poppler = (popplerImport as any).default || popplerImport;
const popplerInstance = new poppler.Poppler();
const options: Record<string, unknown> = {
Expand Down
21 changes: 15 additions & 6 deletions src/pdf/pdfCompressor.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { logger } from "@/logger.js";
import tmp from "tmp";
import { ExtractedPdfInfo, extractTextFromPdf, hasSourceText } from "./pdfUtils.js";
import * as fs from "node:fs";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type * as popplerTypes from "node-poppler";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type * as pdfLibTypes from "@cantoo/pdf-lib";
import { compressImage } from "@/image/index.js";
import { loadOptionalDependency } from "@/dependency/index.js";
import { ExtractedPdfInfo, extractTextFromPdf, hasSourceText } from "./pdfUtils.js";

let pdfLib: typeof pdfLibTypes | null = null;

async function getPdfLib(): Promise<typeof pdfLibTypes> {
if (!pdfLib) {
const pdfLibImport = await loadOptionalDependency<typeof pdfLibTypes>("@cantoo/pdf-lib", "Text Embedding");
const pdfLibImport = await loadOptionalDependency<typeof pdfLibTypes>(
"@cantoo/pdf-lib", "Text Embedding"
);
pdfLib = (pdfLibImport as any).default || pdfLibImport;
}
return pdfLib!;
Expand Down Expand Up @@ -43,8 +47,9 @@ export async function compressPdf(
"is set to false. Resulting file will not contain any embedded text.");
}
} else {
logger.warn("Found text inside of the provided PDF file. Compression operation aborted since disableSourceText "
+ "is set to 'true'."
logger.warn(
"Found text inside of the provided PDF file. " +
"Compression operation aborted since disableSourceText is set to 'true'."
);
return pdfData;
}
Expand Down Expand Up @@ -74,7 +79,9 @@ export async function compressPdf(
* @param forceSourceTextCompression If true, attempts to re-write detected text.
* @param disableSourceText If true, doesn't re-apply source text to the output PDF.
*/
function handleCompressionWarnings(forceSourceTextCompression: boolean, disableSourceText: boolean): void {
function handleCompressionWarnings(
forceSourceTextCompression: boolean, disableSourceText: boolean
): void {
if (forceSourceTextCompression) {
if (!disableSourceText) {
logger.warn("Re-writing PDF source-text is an EXPERIMENTAL feature.");
Expand Down Expand Up @@ -170,7 +177,9 @@ async function compressPagesWithQuality(
* @returns The total size of compressed pages.
*/
function calculateTotalCompressedSize(compressedPages: Buffer[]): number {
return compressedPages.reduce((sum, page) => sum + page.length, 0);
return compressedPages.reduce(
(sum, page) => sum + page.length, 0
);
}

/**
Expand Down
52 changes: 20 additions & 32 deletions tests/dependency/missingDependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,26 @@ import { describe, it } from "node:test";

describe("MindeeV1 - Optional Dependencies #OptionalDepsRemoved", function () {

it("should NOT have sharp installed", async function () {
try {
const moduleName = "sharp";
await import(moduleName);
assert.fail("sharp should not be installed in this environment, but it was found!");
} catch (error: any) {
const isModuleNotFound = error.code === "ERR_MODULE_NOT_FOUND";
const isSharpBinaryMissing = error.message && error.message.includes("Could not load the \"sharp\" module");
const modules = [
"sharp",
"pdf.js-extract",
"@cantoo/pdf-lib",
"node-poppler",
];

if (!isModuleNotFound && !isSharpBinaryMissing) {
throw error;
for (const moduleName of modules) {
it(`should NOT have ${moduleName} installed`, async function () {
try {
await import(moduleName);
assert.fail("sharp should not be installed in this environment, but it was found!");
} catch (error: any) {
const binaryMissing = error.message
&& error.code === "ERR_MODULE_NOT_FOUND"
&& error.message.includes(`Could not load the "${moduleName}" module`);
if (!binaryMissing) {
throw error;
}
}
}
});

it("should NOT have pdf.js-extract installed", async function () {
try {
const moduleName = "pdf.js-extract";
await import(moduleName);
assert.fail("pdf.js-extract should not be installed, but it was found!");
} catch (error: any) {
assert.strictEqual(error.code, "ERR_MODULE_NOT_FOUND");
}
});

it("should NOT have @cantoo/pdf-lib installed", async function () {
try {
const moduleName = "@cantoo/pdf-lib";
await import(moduleName);
assert.fail("@cantoo/pdf-lib should not be installed, but it was found!");
} catch (error: any) {
assert.strictEqual(error.code, "ERR_MODULE_NOT_FOUND");
}
});
});
}
});