Re-factor the setPDFNetworkStreamFactory usage for the unit-tests (PR 13549 follow-up)

This should have been part of PR 13549, since we no longer support browsers without native Fetch API and ReadableStream implementations.
This commit is contained in:
Jonas Jenwald 2021-08-29 18:27:53 +02:00
parent 954e1a1694
commit e69afc6f3d

View File

@ -42,6 +42,7 @@
import { GlobalWorkerOptions } from "pdfjs/display/worker_options.js"; import { GlobalWorkerOptions } from "pdfjs/display/worker_options.js";
import { isNodeJS } from "pdfjs/shared/is_node.js"; import { isNodeJS } from "pdfjs/shared/is_node.js";
import { isValidFetchUrl } from "pdfjs/display/display_utils.js";
import { PDFFetchStream } from "pdfjs/display/fetch_stream.js"; import { PDFFetchStream } from "pdfjs/display/fetch_stream.js";
import { PDFNetworkStream } from "pdfjs/display/network.js"; import { PDFNetworkStream } from "pdfjs/display/network.js";
import { setPDFNetworkStreamFactory } from "pdfjs/display/api.js"; import { setPDFNetworkStreamFactory } from "pdfjs/display/api.js";
@ -102,20 +103,13 @@ async function initializePDFJS(callback) {
"The `gulp unittest` command cannot be used in Node.js environments." "The `gulp unittest` command cannot be used in Node.js environments."
); );
} }
// Set the network stream factory for unit-tests. // Set the network stream factory for the unit-tests.
if ( setPDFNetworkStreamFactory(params => {
typeof Response !== "undefined" && if (isValidFetchUrl(params.url)) {
"body" in Response.prototype &&
typeof ReadableStream !== "undefined"
) {
setPDFNetworkStreamFactory(function (params) {
return new PDFFetchStream(params); return new PDFFetchStream(params);
}); }
} else { return new PDFNetworkStream(params);
setPDFNetworkStreamFactory(function (params) { });
return new PDFNetworkStream(params);
});
}
// Configure the worker. // Configure the worker.
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js"; GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";