Skip to content

A fast, helpful, and open-source document parser

fix: return null from extension detection when format is unrecognizable

AdemBoukhris4572416c1826a50
diff --git a/src/conversion/README.md b/src/conversion/README.md
index 73e7bc1..6ad096b 100644
--- a/src/conversion/README.md
+++ b/src/conversion/README.md
@@ -50,9 +50,11 @@ LiteParse's core parsing works on PDFs. This module extends support to 50+ forma
`guessFileExtension(filePath)` - Detects format from extension or magic bytes
- Checks file extension first
- Falls back to magic byte detection (PDF, PNG, JPEG, ZIP-based)
+- Returns `null` when format cannot be determined
`guessExtensionFromBuffer(data)` - Detects format from raw bytes using magic bytes
- Supports PDF, PNG, JPEG, TIFF (both endians), and ZIP-based formats
+- Returns `null` when format cannot be determined (e.g. plain text)
- Used by `convertBufferToPdf` to determine the temp file extension
**Design Decisions:**
diff --git a/src/conversion/convertToPdf.test.ts b/src/conversion/convertToPdf.test.ts
index 5f9814b..d751a30 100644
--- a/src/conversion/convertToPdf.test.ts
+++ b/src/conversion/convertToPdf.test.ts
@@ -105,9 +105,9 @@ describe("test guessFileExtension", () => {
expect(await guessFileExtension("/some/file")).toBe(".png");
});
- it("defaults to .pdf when file-type returns undefined", async () => {
+ it("returns null when file-type returns undefined", async () => {
mockFileTypeFromFile.mockResolvedValue(undefined);
- expect(await guessFileExtension("/some/file")).toBe(".pdf");
+ expect(await guessFileExtension("/some/file")).toBeNull();
});
it("returns extension directly if present", async () => {
@@ -300,9 +300,9 @@ describe("test guessExtensionFromBuffer", () => {
expect(await guessExtensionFromBuffer(zipBytes)).toBe(".zip");
});
- it("defaults to .pdf for unknown bytes", async () => {
+ it("returns null for unknown bytes", async () => {
const unknownBytes = Buffer.from([0x00, 0x01, 0x02, 0x03]);
- expect(await guessExtensionFromBuffer(unknownBytes)).toBe(".pdf");
+ expect(await guessExtensionFromBuffer(unknownBytes)).toBeNull();
});
it("works with Uint8Array input", async () => {
diff --git a/src/conversion/convertToPdf.ts b/src/conversion/convertToPdf.ts
index eda5327..d556732 100644
--- a/src/conversion/convertToPdf.ts
+++ b/src/conversion/convertToPdf.ts
@@ -79,7 +79,7 @@ export const htmlExtensions = [".htm", ".html", ".xhtml"];
* Guess file extension from file content using file-type magic byte detection.
* Returns the path's own extension if present, otherwise inspects file bytes.
*/
-export async function guessFileExtension(filePath: string): Promise<string> {
+export async function guessFileExtension(filePath: string): Promise<string | null> {
const ext = path.extname(filePath).toLowerCase();
if (ext) {
return ext;
@@ -90,7 +90,7 @@ export async function guessFileExtension(filePath: string): Promise<string> {
return `.${result.ext}`;
}
- return ".pdf"; // Default assumption for extensionless files
+ return null;
}
/**
@@ -366,6 +366,12 @@ export async function convertToPdf(
};
}
+ // Unknown format or text-based — pass through as text
+ if (!extension) {
+ const content = await fs.readFile(filePath, "utf-8");
+ return { content };
+ }
+
// Create temp directory for output
const tmpDir = await fs.mkdtemp(path.join(getTmpDir(), "liteparse-"));
@@ -379,12 +385,8 @@ export async function convertToPdf(
} else if (imageExtensions.includes(extension)) {
pdfPath = await convertImageToPdf(filePath, tmpDir);
} else {
- // Unsupported format
- // Assume its a text-based format and pass through text directly
const content = await fs.readFile(filePath, "utf-8");
- return {
- content,
- };
+ return { content };
}
return {
@@ -417,12 +419,12 @@ export async function cleanupConversionFiles(pdfPath: string): Promise<void> {
/**
* Guess file extension from raw bytes using file-type magic byte detection.
*/
-export async function guessExtensionFromBuffer(data: Buffer | Uint8Array): Promise<string> {
+export async function guessExtensionFromBuffer(data: Buffer | Uint8Array): Promise<string | null> {
const result = await fileTypeFromBuffer(data);
if (result) {
return `.${result.ext}`;
}
- return ".pdf"; // Default assumption
+ return null;
}
/**
@@ -435,9 +437,9 @@ export async function convertBufferToPdf(
): Promise<ConversionResult | ConversionPassthrough | ConversionError> {
const ext = await guessExtensionFromBuffer(data);
- // Write buffer to temp file with detected extension
+ // Write buffer to temp file with detected extension (use .bin for unknown)
const tmpDir = await fs.mkdtemp(path.join(getTmpDir(), "liteparse-"));
- const tmpPath = path.join(tmpDir, `input${ext}`);
+ const tmpPath = path.join(tmpDir, `input${ext || ".bin"}`);
await fs.writeFile(tmpPath, data);
return convertToPdf(tmpPath, password);