Skip to content

Commit

Permalink
Hopding#1032 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Nov 6, 2021
1 parent a1abda4 commit fbdfbd9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 105 deletions.
20 changes: 12 additions & 8 deletions src/api/PDFImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class PDFImage implements Embeddable {
readonly height: number;

private embedder: ImageEmbedder | undefined;
private embedderTask: Promise<PDFRef> | undefined;
private embedTask: Promise<PDFRef> | undefined;

private constructor(ref: PDFRef, doc: PDFDocument, embedder: ImageEmbedder) {
assertIs(ref, 'ref', [[PDFRef, 'PDFRef']]);
Expand Down Expand Up @@ -125,15 +125,19 @@ export default class PDFImage implements Embeddable {
* @returns Resolves when the embedding is complete.
*/
async embed(): Promise<void> {
const embedder = this.embedder;
if (!embedder) return;
if (!this.embedder) return;

let embedderTask = this.embedderTask;
if (!embedderTask) {
embedderTask = embedder.embedIntoContext(this.doc.context, this.ref);
this.embedderTask = embedderTask;
// The image should only be embedded once. If there's a pending embed
// operation then wait on it. Otherwise we need to start the embed.
if (!this.embedTask) {
const { doc, ref } = this;
this.embedTask = this.embedder.embedIntoContext(doc.context, ref);
}
await embedderTask;
await this.embedTask;

// We clear `this.embedder` so that the indirectly referenced image data
// can be garbage collected, thus avoiding a memory leak.
// See https://github.com/Hopding/pdf-lib/pull/1032/files.
this.embedder = undefined;
}
}
129 changes: 62 additions & 67 deletions tests/api/PDFDocument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
ViewerPreferences,
} from 'src/index';

const examplePngImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TxaoVBzuIdMhQnSyIijhKFYtgobQVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi5uak6CIl/i8ptIjx4Lg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hb4g+B2AyAwiwY7NGOXijviS9vBeYh6CEP4edBLDADCAAAQhAAAIQgAAEIAABCDAUAFF/GIN1DM+PBYCo/ohMXDQ1WPjoeUZH1mMBEEh0oqLGvsHCy0S4NzWVWotJBogbvZB+brDwQT7UWSmXy5sxyQB9HQEROdVv4HQ+vx+QmS4iXsWmCK7Usu8AhOqAXMzlcn3VgWTbugQgEYrxMkZ/gyUPgnuhe2C6/Stxvdeg2ezMJERvhOuoZ+JBrNYBRuDdBtDuXkDM25nCHLbZSv9X6A4VHU+DpwCcbvbjcetLtTaOANtuirrux08HM0euisjDEMKC7RQuq+C+pVJqpzx3NZ3+eeBza9I0rWJgyHnxg2sAJrqnaHUzFcyN60Jox13hprv8aNopZBS4GcqWWVHM+lAkN0zY7ncgkYBukRoKLPpiXVj9UFkfV4Bdl8Jf60u3IMZZAG/6iLuhkDvaSZ74VqtUx3kp3NN7gUZt8RmA43a2eEY1OCfQ04AcBpAGkAKwpkBLIG8BfQE/eNJsvG/G4VlARj0BfjDBx2ECEIAABCAAAQhAAAIQgAAE+P/tN8YvpvbTDBOlAAAAAElFTkSuQmCC';

const unencryptedPdfBytes = fs.readFileSync('assets/pdfs/normal.pdf');
const oldEncryptedPdfBytes1 = fs.readFileSync('assets/pdfs/encrypted_old.pdf');
Expand Down Expand Up @@ -434,7 +435,7 @@ describe(`PDFDocument`, () => {
});
});

describe(`addJavaScript method`, () => {
describe(`addJavaScript() method`, () => {
it(`adds the script to the catalog`, async () => {
const pdfDoc = await PDFDocument.create();
pdfDoc.addJavaScript(
Expand Down Expand Up @@ -473,17 +474,17 @@ describe(`PDFDocument`, () => {
});

describe(`embedPng() method`, () => {
it(`It should still be possible to modify the PDFDocument even if a image has already been embedded`, async () => {
it(`does not prevent the PDFDocument from being modified after embedding an image`, async () => {
const pdfDoc = await PDFDocument.create();
const pdfPage = pdfDoc.addPage();

const noErrorFunc = async () => {
const embeddedImage = await pdfDoc.embedPng(examplePngImage);
await pdfPage.drawImage(embeddedImage);
pdfPage.drawImage(embeddedImage);
await embeddedImage.embed();

const pdfPage2 = pdfDoc.addPage();
await pdfPage2.drawImage(embeddedImage);
pdfPage2.drawImage(embeddedImage);

pdfDoc.setTitle('Unit Test');
};
Expand All @@ -493,89 +494,83 @@ describe(`PDFDocument`, () => {
});

describe(`save() method`, () => {

it(`The save() function may be called several times with different changes`, async () => {
it(`can called multiple times on the same PDFDocument with different changes`, async () => {
const pdfDoc = await PDFDocument.create();
const pdfPage = pdfDoc.addPage();
const embeddedImage = await pdfDoc.embedPng(examplePngImage);

const noErrorFunc = async () => {
await pdfPage.drawImage(embeddedImage);
await embeddedImage.embed();

const pdfPage2 = pdfDoc.addPage();
await pdfPage2.drawImage(embeddedImage);

pdfDoc.setTitle('Unit Test');
const page1 = pdfDoc.addPage();
page1.drawImage(embeddedImage);

const pdfBytes1 = await pdfDoc.save();
expect(pdfBytes1.byteLength).toBeGreaterThan(0);

const pdfPage3 = pdfDoc.addPage();
await pdfPage3.drawImage(embeddedImage);
const page2 = pdfDoc.addPage();
page2.drawImage(embeddedImage);

pdfDoc.setTitle('Unit Test 2. change');
pdfDoc.setTitle('Unit Test');

const pdfBytes2 = await pdfDoc.save();
expect(pdfBytes2.byteLength).toBeGreaterThan(0);
expect(pdfBytes1.byteLength).not.toEqual(pdfBytes2.byteLength);
expect(pdfBytes2.byteLength).not.toEqual(pdfBytes1.byteLength);

const pdfPage3 = pdfDoc.addPage();
pdfPage3.drawImage(embeddedImage);

const pdf1 = await PDFDocument.load(pdfBytes1);
expect(pdf1.getTitle()).toEqual('Unit Test');
expect(pdf1.getPageCount()).toEqual(2);
pdfDoc.setTitle('Unit Test 2. change');

const pdf2 = await PDFDocument.load(pdfBytes2);
expect(pdf2.getTitle()).toEqual('Unit Test 2. change');
expect(pdf2.getPageCount()).toEqual(3);
const pdfBytes3 = await pdfDoc.save();
expect(pdfBytes3.byteLength).toBeGreaterThan(0);
expect(pdfBytes3.byteLength).not.toEqual(pdfBytes2.byteLength);
};

await expect(noErrorFunc()).resolves.not.toThrowError();
});
});
});

describe(`copy() method`, () => {
let pdfDoc: PDFDocument;
let srcDoc: PDFDocument;
beforeAll(async () => {
const parseSpeed = ParseSpeeds.Fastest;
srcDoc = await PDFDocument.load(unencryptedPdfBytes, { parseSpeed });
const title = '🥚 The Life of an Egg 🍳';
const author = 'Humpty Dumpty';
const subject = '📘 An Epic Tale of Woe 📖';
const keywords = ['eggs', 'wall', 'fall', 'king', 'horses', 'men', '🥚'];
const producer = 'PDF App 9000 🤖';
const creator = 'PDF App 8000 🤖';

// Milliseconds will not get saved, so these dates do not have milliseconds.
const creationDate = new Date('1997-08-15T01:58:37Z');
const modificationDate = new Date('2018-12-21T07:00:11Z');

srcDoc.setTitle(title);
srcDoc.setAuthor(author);
srcDoc.setSubject(subject);
srcDoc.setKeywords(keywords);
srcDoc.setProducer(producer);
srcDoc.setCreator(creator);
srcDoc.setCreationDate(creationDate);
srcDoc.setModificationDate(modificationDate);
pdfDoc = await srcDoc.copy();
});
describe(`copy() method`, () => {
let pdfDoc: PDFDocument;
let srcDoc: PDFDocument;
beforeAll(async () => {
const parseSpeed = ParseSpeeds.Fastest;
srcDoc = await PDFDocument.load(unencryptedPdfBytes, { parseSpeed });
const title = '🥚 The Life of an Egg 🍳';
const author = 'Humpty Dumpty';
const subject = '📘 An Epic Tale of Woe 📖';
const keywords = ['eggs', 'wall', 'fall', 'king', 'horses', 'men', '🥚'];
const producer = 'PDF App 9000 🤖';
const creator = 'PDF App 8000 🤖';

it(`Returns a pdf with the same number of pages`, async () => {
expect(pdfDoc.getPageCount()).toBe(srcDoc.getPageCount());
});
// Milliseconds will not get saved, so these dates do not have milliseconds.
const creationDate = new Date('1997-08-15T01:58:37Z');
const modificationDate = new Date('2018-12-21T07:00:11Z');

srcDoc.setTitle(title);
srcDoc.setAuthor(author);
srcDoc.setSubject(subject);
srcDoc.setKeywords(keywords);
srcDoc.setProducer(producer);
srcDoc.setCreator(creator);
srcDoc.setCreationDate(creationDate);
srcDoc.setModificationDate(modificationDate);
pdfDoc = await srcDoc.copy();
});

it(`Can copy author, creationDate, creator, producer, subject, title, defaultWordBreaks`, async () => {
expect(pdfDoc.getAuthor()).toBe(srcDoc.getAuthor());
expect(pdfDoc.getCreationDate()).toStrictEqual(srcDoc.getCreationDate());
expect(pdfDoc.getCreator()).toBe(srcDoc.getCreator());
expect(pdfDoc.getModificationDate()).toStrictEqual(
srcDoc.getModificationDate(),
);
expect(pdfDoc.getProducer()).toBe(srcDoc.getProducer());
expect(pdfDoc.getSubject()).toBe(srcDoc.getSubject());
expect(pdfDoc.getTitle()).toBe(srcDoc.getTitle());
expect(pdfDoc.defaultWordBreaks).toEqual(srcDoc.defaultWordBreaks);
it(`Returns a pdf with the same number of pages`, async () => {
expect(pdfDoc.getPageCount()).toBe(srcDoc.getPageCount());
});

it(`Can copy author, creationDate, creator, producer, subject, title, defaultWordBreaks`, async () => {
expect(pdfDoc.getAuthor()).toBe(srcDoc.getAuthor());
expect(pdfDoc.getCreationDate()).toStrictEqual(srcDoc.getCreationDate());
expect(pdfDoc.getCreator()).toBe(srcDoc.getCreator());
expect(pdfDoc.getModificationDate()).toStrictEqual(
srcDoc.getModificationDate(),
);
expect(pdfDoc.getProducer()).toBe(srcDoc.getProducer());
expect(pdfDoc.getSubject()).toBe(srcDoc.getSubject());
expect(pdfDoc.getTitle()).toBe(srcDoc.getTitle());
expect(pdfDoc.defaultWordBreaks).toEqual(srcDoc.defaultWordBreaks);
});
});
});
57 changes: 27 additions & 30 deletions tests/api/PDFImage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
import { PDFDocument, PDFImage } from "src/api";
import { PngEmbedder } from "src/core";
import { toUint8Array } from "src/utils";
import { PDFDocument, PDFImage } from 'src/api';
import { PngEmbedder } from 'src/core';
import { toUint8Array } from 'src/utils';


const examplePngImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAABhGlDQ1BJQ0MgcH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sHNN8ngQbQL70RZqG/Z9b29JnvflkydP37whghG3ZaegoxzfwB5vBCAAAQhAAAIQgAAEIAABCEAAAhCAAAQgwB5rstWPtnP0LqBX/vZNyLF6vVrpN/hucewhb4g+B2AyAwiwY7NGOXijviS9vBeYh6CEP4edBLDADCAAAQhAAAIQgAAEIAABCDAUAFF/GIN1DM+PBYCo/ohMXDQ1WPjoeUZH1mMBEEh0oqLGvsHCy0S4NzWVWotJBogbvZB+brDwQT7UWSmXy5sxyQB9HQEROdVv4HQ+vx+QmS4iXsWmCK7Usu8AhOqAXMzlcn3VgWTbugQgEYrxMkZ/gyUPgnuhe2C6/Stxvdeg2ezMJERvhOuoZ+JBrNYBRuDdBtDuXkDM25nCHLbZSv9X6A4VHU+DpwCcbvbjcetLtTaOANtuirrux08HM0euisjDEMKC7RQuq+C+pVJqpzx3NZ3+eeBza9I0rWJgyHnxg2sAJrqnaHUzFcyN60Jox13hprv8aNopZBS4GcqWWVHM+lAkN0zY7ncgkYBukRoKLPpiXVj9UFkfV4Bdl8Jf60u3IMZZAG/6iLuhkDvaSZ74VqtUx3kp3NN7gUZt8RmA43a2eEY1OCfQ04AcBpAGkAKwpkBLIG8BfQE/eNJsvG/G4VlARj0BfjDBx2ECEIAABCAAAQhAAAIQgAAE+P/tN8YvpvbTDBOlAAAAAElFTkSuQmCC';

describe(`PDFImage`, () => {

describe(`embed() method`, () => {
it(`The embedder field should be cleared after the first call to embed()`, async () => {
it(`clears the 'embedder' field after the first call`, async () => {
const pdfDoc = await PDFDocument.create();

const bytes = toUint8Array(examplePngImage);
const embedder = await PngEmbedder.for(bytes);
const ref = pdfDoc.context.nextRef();
const pdfImage = PDFImage.of(ref, pdfDoc, embedder);

const embedderVariable = "embedder";
const embedderVariable = 'embedder';
expect(pdfImage[embedderVariable]).toBeDefined();
await pdfImage.embed();
expect(pdfImage[embedderVariable]).toBeUndefined();
});
it(`The method embed() may be called several times without an error`, async () => {

it(`may be called multiple times without causing an error`, async () => {
const pdfDoc = await PDFDocument.create();

const bytes = toUint8Array(examplePngImage);
const embedder = await PngEmbedder.for(bytes);
const ref = pdfDoc.context.nextRef();
const pdfImage = PDFImage.of(ref, pdfDoc, embedder);

const noErrorFunc = async () => {
await pdfImage.embed();
await pdfImage.embed();
}

await expect(noErrorFunc()).resolves.not.toThrowError();
await expect(pdfImage.embed()).resolves.not.toThrowError();
await expect(pdfImage.embed()).resolves.not.toThrowError();
});
it(`The method embed() may be called parallel without causing an error`, async () => {

it(`may be called in parallel without causing an error`, async () => {
const pdfDoc = await PDFDocument.create();

const bytes = toUint8Array(examplePngImage);
const embedder = await PngEmbedder.for(bytes);
const ref = pdfDoc.context.nextRef();
const pdfImage = PDFImage.of(ref, pdfDoc, embedder);

const noErrorFunc = async () => {
const embedderTaskVariable = "embedderTask";
const undefinedTask = pdfImage[embedderTaskVariable];
expect(undefinedTask).toBeUndefined();
const task1 = pdfImage.embed();
const firstTask = pdfImage[embedderTaskVariable];
const task2 = pdfImage.embed();
const secondTask = pdfImage[embedderTaskVariable];
await Promise.all([task1, task2]);
expect(firstTask).toEqual(secondTask);
}

await expect(noErrorFunc()).resolves.not.toThrowError();
const task = () => pdfImage['embedTask'];

expect(task()).toBeUndefined();

const task1 = pdfImage.embed();
const firstTask = task();

const task2 = pdfImage.embed();
const secondTask = task();

await Promise.all([task1, task2]);

expect(firstTask).toEqual(secondTask);
});
});
});
});

0 comments on commit fbdfbd9

Please sign in to comment.