Inline the setPDFNetworkStreamFactory functionality in src/display/api.js

Given that this is internal functionality, not exposed in the official API, it's not entirely clear (at least to me) why we can't just initialize this directly in `src/display/api.js` instead.
When testing both the development viewer and all the ways in which we run tests, everthing still appears to work just fine with this patch.
This commit is contained in:
Jonas Jenwald 2023-01-02 18:35:11 +01:00
parent e36564668b
commit 1d5de9f4f4
4 changed files with 33 additions and 80 deletions

View file

@ -47,6 +47,7 @@ import {
DOMCMapReaderFactory,
DOMStandardFontDataFactory,
isDataScheme,
isValidFetchUrl,
loadScript,
PageViewport,
RenderingCancelledException,
@ -81,32 +82,37 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS) {
DefaultStandardFontDataFactory = NodeStandardFontDataFactory;
}
/**
* @typedef {function} IPDFStreamFactory
* @param {DocumentInitParameters} params - The document initialization
* parameters. The "url" key is always present.
* @returns {Promise} A promise, which is resolved with an instance of
* {IPDFStream}.
* @ignore
*/
/**
* @type {IPDFStreamFactory}
* @private
*/
let createPDFNetworkStream;
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
const streamsPromise = Promise.all([
import("./network.js"),
import("./fetch_stream.js"),
]);
/**
* Sets the function that instantiates an {IPDFStream} as an alternative PDF
* data transport.
*
* @param {IPDFStreamFactory} pdfNetworkStreamFactory - The factory function
* that takes document initialization parameters (including a "url") and
* returns a promise which is resolved with an instance of {IPDFStream}.
* @ignore
*/
function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
createPDFNetworkStream = pdfNetworkStreamFactory;
createPDFNetworkStream = async params => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = await streamsPromise;
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
} else if (PDFJSDev.test("GENERIC || CHROME")) {
if (PDFJSDev.test("GENERIC") && isNodeJS) {
const { PDFNodeStream } = require("./node_stream.js");
createPDFNetworkStream = params => {
return new PDFNodeStream(params);
};
} else {
const { PDFNetworkStream } = require("./network.js");
const { PDFFetchStream } = require("./fetch_stream.js");
createPDFNetworkStream = params => {
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
}
}
/**
@ -438,6 +444,9 @@ function getDocument(src) {
rangeTransport
);
} else if (!params.data) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream");
}
networkStream = createPDFNetworkStream({
url: params.url,
length: params.length,
@ -3349,6 +3358,5 @@ export {
PDFWorker,
PDFWorkerUtil,
RenderTask,
setPDFNetworkStreamFactory,
version,
};