From aebb8534f376930b6fc9552724527065e3492854 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 12 Aug 2024 11:59:13 +0200 Subject: [PATCH] Limit base-class initialization checks to development and TESTING modes We have a number of base-classes that are only intended to be extended, but never to be used directly. To help enforce this during development these base-class constructors will check for direct usage, however that code is obviously not needed in the actual builds. *Note:* This patch reduces the size of the `gulp mozcentral` output by `~2.7` kilo-bytes, which isn't a lot but still cannot hurt. --- src/core/base_stream.js | 5 ++++- src/core/colorspace.js | 5 ++++- src/core/crypto.js | 5 ++++- src/core/font_renderer.js | 5 ++++- src/core/image_utils.js | 5 ++++- src/core/name_number_tree.js | 5 ++++- src/core/pattern.js | 5 ++++- src/core/pdf_manager.js | 5 ++++- src/display/base_factory.js | 25 ++++++++++++++++++++----- src/display/editor/editor.js | 5 ++++- src/display/pattern_helper.js | 5 ++++- src/shared/util.js | 5 ++++- web/app_options.js | 4 +++- web/base_tree_viewer.js | 5 ++++- web/external_services.js | 5 ++++- web/preferences.js | 5 ++++- 16 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/core/base_stream.js b/src/core/base_stream.js index 717b2b41b15fd..7f79a147c1f13 100644 --- a/src/core/base_stream.js +++ b/src/core/base_stream.js @@ -17,7 +17,10 @@ import { bytesToString, shadow, unreachable } from "../shared/util.js"; class BaseStream { constructor() { - if (this.constructor === BaseStream) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseStream + ) { unreachable("Cannot initialize BaseStream."); } } diff --git a/src/core/colorspace.js b/src/core/colorspace.js index 1b66ab6d501bf..149cdf9840d23 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -62,7 +62,10 @@ function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) { class ColorSpace { constructor(name, numComps) { - if (this.constructor === ColorSpace) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === ColorSpace + ) { unreachable("Cannot initialize ColorSpace."); } this.name = name; diff --git a/src/core/crypto.js b/src/core/crypto.js index ee2625c3a60b8..1eee1e6fc8a49 100644 --- a/src/core/crypto.js +++ b/src/core/crypto.js @@ -693,7 +693,10 @@ class NullCipher { class AESBaseCipher { constructor() { - if (this.constructor === AESBaseCipher) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === AESBaseCipher + ) { unreachable("Cannot initialize AESBaseCipher."); } diff --git a/src/core/font_renderer.js b/src/core/font_renderer.js index 5608e83d3e33c..572cda50995bd 100644 --- a/src/core/font_renderer.js +++ b/src/core/font_renderer.js @@ -769,7 +769,10 @@ class Commands { class CompiledFont { constructor(fontMatrix) { - if (this.constructor === CompiledFont) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === CompiledFont + ) { unreachable("Cannot initialize CompiledFont."); } this.fontMatrix = fontMatrix; diff --git a/src/core/image_utils.js b/src/core/image_utils.js index 110277c9e4f16..9638d37efa3ee 100644 --- a/src/core/image_utils.js +++ b/src/core/image_utils.js @@ -23,7 +23,10 @@ import { RefSet, RefSetCache } from "./primitives.js"; class BaseLocalCache { constructor(options) { - if (this.constructor === BaseLocalCache) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseLocalCache + ) { unreachable("Cannot initialize BaseLocalCache."); } this._onlyRefs = options?.onlyRefs === true; diff --git a/src/core/name_number_tree.js b/src/core/name_number_tree.js index 1462675b3ca0a..9f814a55439ee 100644 --- a/src/core/name_number_tree.js +++ b/src/core/name_number_tree.js @@ -23,7 +23,10 @@ import { FormatError, unreachable, warn } from "../shared/util.js"; */ class NameOrNumberTree { constructor(root, xref, type) { - if (this.constructor === NameOrNumberTree) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === NameOrNumberTree + ) { unreachable("Cannot initialize NameOrNumberTree."); } this.root = root; diff --git a/src/core/pattern.js b/src/core/pattern.js index 37fdc525711af..38fb16c20370a 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -98,7 +98,10 @@ class BaseShading { static SMALL_NUMBER = 1e-6; constructor() { - if (this.constructor === BaseShading) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseShading + ) { unreachable("Cannot initialize BaseShading."); } } diff --git a/src/core/pdf_manager.js b/src/core/pdf_manager.js index aee66820ab15d..35d34cb537095 100644 --- a/src/core/pdf_manager.js +++ b/src/core/pdf_manager.js @@ -37,7 +37,10 @@ function parseDocBaseUrl(url) { class BasePdfManager { constructor(args) { - if (this.constructor === BasePdfManager) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BasePdfManager + ) { unreachable("Cannot initialize BasePdfManager."); } this._docBaseUrl = parseDocBaseUrl(args.docBaseUrl); diff --git a/src/display/base_factory.js b/src/display/base_factory.js index 5a5863ff45e89..5f5b2b0e9d338 100644 --- a/src/display/base_factory.js +++ b/src/display/base_factory.js @@ -17,7 +17,10 @@ import { CMapCompressionType, unreachable } from "../shared/util.js"; class BaseFilterFactory { constructor() { - if (this.constructor === BaseFilterFactory) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseFilterFactory + ) { unreachable("Cannot initialize BaseFilterFactory."); } } @@ -49,7 +52,10 @@ class BaseCanvasFactory { #enableHWA = false; constructor({ enableHWA = false } = {}) { - if (this.constructor === BaseCanvasFactory) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseCanvasFactory + ) { unreachable("Cannot initialize BaseCanvasFactory."); } this.#enableHWA = enableHWA; @@ -101,7 +107,10 @@ class BaseCanvasFactory { class BaseCMapReaderFactory { constructor({ baseUrl = null, isCompressed = true }) { - if (this.constructor === BaseCMapReaderFactory) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseCMapReaderFactory + ) { unreachable("Cannot initialize BaseCMapReaderFactory."); } this.baseUrl = baseUrl; @@ -139,7 +148,10 @@ class BaseCMapReaderFactory { class BaseStandardFontDataFactory { constructor({ baseUrl = null }) { - if (this.constructor === BaseStandardFontDataFactory) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseStandardFontDataFactory + ) { unreachable("Cannot initialize BaseStandardFontDataFactory."); } this.baseUrl = baseUrl; @@ -171,7 +183,10 @@ class BaseStandardFontDataFactory { class BaseSVGFactory { constructor() { - if (this.constructor === BaseSVGFactory) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseSVGFactory + ) { unreachable("Cannot initialize BaseSVGFactory."); } } diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index 47f502974d292..dfee06d90d48c 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -140,7 +140,10 @@ class AnnotationEditor { * @param {AnnotationEditorParameters} parameters */ constructor(parameters) { - if (this.constructor === AnnotationEditor) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === AnnotationEditor + ) { unreachable("Cannot initialize AnnotationEditor."); } diff --git a/src/display/pattern_helper.js b/src/display/pattern_helper.js index 781449fbb7fad..7433215b8ae12 100644 --- a/src/display/pattern_helper.js +++ b/src/display/pattern_helper.js @@ -35,7 +35,10 @@ function applyBoundingBox(ctx, bbox) { class BaseShadingPattern { constructor() { - if (this.constructor === BaseShadingPattern) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseShadingPattern + ) { unreachable("Cannot initialize BaseShadingPattern."); } } diff --git a/src/shared/util.js b/src/shared/util.js index 8d2cad1a17eb6..19be8031ec8b1 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -468,7 +468,10 @@ function shadow(obj, prop, value, nonSerializable = false) { const BaseException = (function BaseExceptionClosure() { // eslint-disable-next-line no-shadow function BaseException(message, name) { - if (this.constructor === BaseException) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseException + ) { unreachable("Cannot initialize BaseException."); } this.message = message; diff --git a/web/app_options.js b/web/app_options.js index 53c4b5325dcc9..a3372dbfc77a0 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -583,7 +583,9 @@ class AppOptions { } constructor() { - throw new Error("Cannot initialize AppOptions."); + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + throw new Error("Cannot initialize AppOptions."); + } } static get(name) { diff --git a/web/base_tree_viewer.js b/web/base_tree_viewer.js index b5875e14d7ebc..a3ef2c21d8671 100644 --- a/web/base_tree_viewer.js +++ b/web/base_tree_viewer.js @@ -20,7 +20,10 @@ const TREEITEM_SELECTED_CLASS = "selected"; class BaseTreeViewer { constructor(options) { - if (this.constructor === BaseTreeViewer) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseTreeViewer + ) { throw new Error("Cannot initialize BaseTreeViewer."); } this.container = options.container; diff --git a/web/external_services.js b/web/external_services.js index 55fbb707d1d1d..772a15ef81405 100644 --- a/web/external_services.js +++ b/web/external_services.js @@ -17,7 +17,10 @@ class BaseExternalServices { constructor() { - if (this.constructor === BaseExternalServices) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseExternalServices + ) { throw new Error("Cannot initialize BaseExternalServices."); } } diff --git a/web/preferences.js b/web/preferences.js index d44e4a39a74f0..f290ed9d77fa1 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -30,7 +30,10 @@ class BasePreferences { #initializedPromise = null; constructor() { - if (this.constructor === BasePreferences) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BasePreferences + ) { throw new Error("Cannot initialize BasePreferences."); }