From cb2ae021ca8094aa5b4d2e9038f535c3a1c3b53c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 22 Mar 2026 11:05:43 +0100 Subject: [PATCH 1/2] Avoid resolving an `objId` more than once in the `PDFObjects` class Trying to resolve the same `objId` more than once would be a bug elsewhere in the code-base, since that should never happen, hence update the `resolve` method to prevent that. --- src/display/pdf_objects.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/display/pdf_objects.js b/src/display/pdf_objects.js index eccbe6a72..5f8edd82c 100644 --- a/src/display/pdf_objects.js +++ b/src/display/pdf_objects.js @@ -100,6 +100,9 @@ class PDFObjects { */ resolve(objId, data = null) { const obj = this.#ensureObj(objId); + if (obj.data !== INITIAL_DATA) { + throw new Error(`Object already resolved ${objId}.`); + } obj.data = data; obj.resolve(); } From e3564deefafca8231b78897ba4f20d85ce280d28 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 22 Mar 2026 11:10:15 +0100 Subject: [PATCH 2/2] Remove the internal `#ensureObj` method in the `PDFObjects` class With the introduction of `Map.prototype.getOrInsertComputed()` usage this method is no longer necessary, and the code can just be inlined instead. --- src/display/pdf_objects.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/display/pdf_objects.js b/src/display/pdf_objects.js index 5f8edd82c..7c54b01e4 100644 --- a/src/display/pdf_objects.js +++ b/src/display/pdf_objects.js @@ -28,16 +28,6 @@ const dataObj = () => ({ class PDFObjects { #objs = new Map(); - /** - * Ensures there is an object defined for `objId`. - * - * @param {string} objId - * @returns {Object} - */ - #ensureObj(objId) { - return this.#objs.getOrInsertComputed(objId, dataObj); - } - /** * If called *without* callback, this returns the data of `objId` but the * object needs to be resolved. If it isn't, this method throws. @@ -54,7 +44,7 @@ class PDFObjects { // If there is a callback, then the get can be async and the object is // not required to be resolved right now. if (callback) { - const obj = this.#ensureObj(objId); + const obj = this.#objs.getOrInsertComputed(objId, dataObj); obj.promise.then(() => callback(obj.data)); return null; } @@ -99,7 +89,7 @@ class PDFObjects { * @param {any} [data] */ resolve(objId, data = null) { - const obj = this.#ensureObj(objId); + const obj = this.#objs.getOrInsertComputed(objId, dataObj); if (obj.data !== INITIAL_DATA) { throw new Error(`Object already resolved ${objId}.`); }