From cbb8748a221f3afec93becc5e0160c803fbace0f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 May 2024 16:27:48 +0200 Subject: [PATCH 1/2] Inline the `_fetchDocument` helper function in `getDocument` This function has been modified a number of times over the years, and at this point it's small/simple enough that we can just inline the code instead. --- src/display/api.js | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index aaedfcf3befaf..c0e54f2a873de 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -357,7 +357,7 @@ function getDocument(src) { task._worker = worker; } - const fetchDocParams = { + const docParams = { docId, apiVersion: typeof PDFJSDev !== "undefined" && !PDFJSDev.test("TESTING") @@ -398,9 +398,16 @@ function getDocument(src) { if (task.destroyed) { throw new Error("Loading aborted"); } + if (worker.destroyed) { + throw new Error("Worker was destroyed"); + } - const workerIdPromise = _fetchDocument(worker, fetchDocParams); - const networkStreamPromise = new Promise(function (resolve) { + const workerIdPromise = worker.messageHandler.sendWithPromise( + "GetDocRequest", + docParams, + data ? [data.buffer] : null + ); + const networkStreamPromise = new Promise(resolve => { let networkStream; if (rangeTransport) { networkStream = new PDFDataTransportStream(rangeTransport, { @@ -451,6 +458,9 @@ function getDocument(src) { if (task.destroyed) { throw new Error("Loading aborted"); } + if (worker.destroyed) { + throw new Error("Worker was destroyed"); + } const messageHandler = new MessageHandler( docId, @@ -474,31 +484,6 @@ function getDocument(src) { return task; } -/** - * Starts fetching of specified PDF document/data. - * - * @param {PDFWorker} worker - * @param {Object} source - * @returns {Promise} A promise that is resolved when the worker ID of - * the `MessageHandler` is known. - * @private - */ -async function _fetchDocument(worker, source) { - if (worker.destroyed) { - throw new Error("Worker was destroyed"); - } - const workerId = await worker.messageHandler.sendWithPromise( - "GetDocRequest", - source, - source.data ? [source.data.buffer] : null - ); - - if (worker.destroyed) { - throw new Error("Worker was destroyed"); - } - return workerId; -} - function getUrlProp(val) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { return null; // The 'url' is unused with `PDFDataRangeTransport`. From 6b171540b7ce83f11ce77f1b26a393d74baa7462 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 May 2024 17:04:25 +0200 Subject: [PATCH 2/2] Initialize the `networkStream` synchronously in `getDocument` This is fairly old code, and at some point the need for this to be asynchronous disappeared. --- src/display/api.js | 124 +++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 66 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index c0e54f2a873de..8430e0f3a5c55 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -407,77 +407,69 @@ function getDocument(src) { docParams, data ? [data.buffer] : null ); - const networkStreamPromise = new Promise(resolve => { - let networkStream; - if (rangeTransport) { - networkStream = new PDFDataTransportStream(rangeTransport, { - disableRange, - disableStream, - }); - } else if (!data) { - if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { - throw new Error("Not implemented: createPDFNetworkStream"); - } - const createPDFNetworkStream = params => { - if ( - typeof PDFJSDev !== "undefined" && - PDFJSDev.test("GENERIC") && - isNodeJS - ) { - const isFetchSupported = function () { - return ( - typeof fetch !== "undefined" && - typeof Response !== "undefined" && - "body" in Response.prototype - ); - }; - return isFetchSupported() && isValidFetchUrl(params.url) - ? new PDFFetchStream(params) - : new PDFNodeStream(params); - } - return isValidFetchUrl(params.url) - ? new PDFFetchStream(params) - : new PDFNetworkStream(params); - }; - networkStream = createPDFNetworkStream({ - url, - length, - httpHeaders, - withCredentials, - rangeChunkSize, - disableRange, - disableStream, - }); + let networkStream; + if (rangeTransport) { + networkStream = new PDFDataTransportStream(rangeTransport, { + disableRange, + disableStream, + }); + } else if (!data) { + if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { + throw new Error("Not implemented: createPDFNetworkStream"); } - resolve(networkStream); - }); - - return Promise.all([workerIdPromise, networkStreamPromise]).then( - function ([workerId, networkStream]) { - if (task.destroyed) { - throw new Error("Loading aborted"); - } - if (worker.destroyed) { - throw new Error("Worker was destroyed"); + const createPDFNetworkStream = params => { + if ( + typeof PDFJSDev !== "undefined" && + PDFJSDev.test("GENERIC") && + isNodeJS + ) { + const isFetchSupported = function () { + return ( + typeof fetch !== "undefined" && + typeof Response !== "undefined" && + "body" in Response.prototype + ); + }; + return isFetchSupported() && isValidFetchUrl(params.url) + ? new PDFFetchStream(params) + : new PDFNodeStream(params); } + return isValidFetchUrl(params.url) + ? new PDFFetchStream(params) + : new PDFNetworkStream(params); + }; - const messageHandler = new MessageHandler( - docId, - workerId, - worker.port - ); - const transport = new WorkerTransport( - messageHandler, - task, - networkStream, - transportParams, - transportFactory - ); - task._transport = transport; - messageHandler.send("Ready", null); + networkStream = createPDFNetworkStream({ + url, + length, + httpHeaders, + withCredentials, + rangeChunkSize, + disableRange, + disableStream, + }); + } + + return workerIdPromise.then(workerId => { + if (task.destroyed) { + throw new Error("Loading aborted"); } - ); + if (worker.destroyed) { + throw new Error("Worker was destroyed"); + } + + const messageHandler = new MessageHandler(docId, workerId, worker.port); + const transport = new WorkerTransport( + messageHandler, + task, + networkStream, + transportParams, + transportFactory + ); + task._transport = transport; + messageHandler.send("Ready", null); + }); }) .catch(task._capability.reject);