From 6a78f20b17b39be0b17aea9eeb11498b82ff9b88 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 8 Mar 2022 17:13:47 +0100 Subject: [PATCH] Simplify the `PDFDocument` constructor Originally the code in the `src/`-folder was shared between the main/worker-threads, and back then it probably made sense that the `PDFDocument` constructor accepted different arguments. However, for many years we've not been passing anything *except* Streams to `PDFDocument` and we should thus be able to slightly simplify that code. Note that for e.g. unit-tests of this code, using either a `NullStream` or a `StringStream` works just fine. --- src/core/document.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index bfbb30af2..b129409d1 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -18,7 +18,6 @@ import { FormatError, info, InvalidPDFException, - isArrayBuffer, isArrayEqual, OPS, PageActionEventType, @@ -43,13 +42,13 @@ import { } from "./core_utils.js"; import { Dict, isName, Name, Ref } from "./primitives.js"; import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js"; -import { NullStream, Stream } from "./stream.js"; import { AnnotationFactory } from "./annotation.js"; import { BaseStream } from "./base_stream.js"; import { calculateMD5 } from "./crypto.js"; import { Catalog } from "./catalog.js"; import { clearGlobalCaches } from "./cleanup_helper.js"; import { Linearization } from "./parser.js"; +import { NullStream } from "./stream.js"; import { ObjectLoader } from "./object_loader.js"; import { OperatorList } from "./operator_list.js"; import { PartialEvaluator } from "./evaluator.js"; @@ -625,14 +624,15 @@ function find(stream, signature, limit = 1024, backwards = false) { * The `PDFDocument` class holds all the (worker-thread) data of the PDF file. */ class PDFDocument { - constructor(pdfManager, arg) { - let stream; - if (arg instanceof BaseStream) { - stream = arg; - } else if (isArrayBuffer(arg)) { - stream = new Stream(arg); - } else { - throw new Error("PDFDocument: Unknown argument type"); + constructor(pdfManager, stream) { + if ( + typeof PDFJSDev === "undefined" || + PDFJSDev.test("!PRODUCTION || TESTING") + ) { + assert( + stream instanceof BaseStream, + 'PDFDocument: Invalid "stream" argument.' + ); } if (stream.length <= 0) { throw new InvalidPDFException(