Skip to content

Commit edf59f8

Browse files
handle tiny pdfs (#580)
Prevents throwing parsing small PDF files (where size <= 1350 bytes)
1 parent 1c76b4a commit edf59f8

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

core.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,17 +611,24 @@ class FileTypeParser {
611611
}
612612

613613
if (this.checkString('%PDF')) {
614-
await tokenizer.ignore(1350);
615-
const maxBufferSize = 10 * 1024 * 1024;
616-
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
617-
await tokenizer.readBuffer(buffer, {mayBeLess: true});
614+
try {
615+
await tokenizer.ignore(1350);
616+
const maxBufferSize = 10 * 1024 * 1024;
617+
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
618+
await tokenizer.readBuffer(buffer, {mayBeLess: true});
618619

619-
// Check if this is an Adobe Illustrator file
620-
if (buffer.includes(Buffer.from('AIPrivateData'))) {
621-
return {
622-
ext: 'ai',
623-
mime: 'application/postscript',
624-
};
620+
// Check if this is an Adobe Illustrator file
621+
if (buffer.includes(Buffer.from('AIPrivateData'))) {
622+
return {
623+
ext: 'ai',
624+
mime: 'application/postscript',
625+
};
626+
}
627+
} catch (error) {
628+
// Swallow end of stream error if file is too small for the Adobe AI check
629+
if (!(error instanceof strtok3.EndOfStreamError)) {
630+
throw error;
631+
}
625632
}
626633

627634
// Assume this is just a normal PDF

fixture/fixture-minimal.pdf

739 Bytes
Binary file not shown.

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ const names = {
219219
'fixture-smallest', // PDF saved from Adobe Illustrator, using the preset "smallest PDF"
220220
'fixture-fast-web', // PDF saved from Adobe Illustrator, using the default "[Illustrator Default"] preset, but enabling "Optimize for Fast Web View"
221221
'fixture-printed', // PDF printed from Adobe Illustrator, but with a PDF printer.
222+
'fixture-minimal', // PDF written to be as small as the spec allows
222223
],
223224
webm: [
224225
'fixture-null', // EBML DocType with trailing null character

0 commit comments

Comments
 (0)