Re-factor setPDFNetworkStreamFactory, in src/display/api.js, to also accept an asynchronous function

As part of trying to reduce the usage of SystemJS in the development viewer, this patch is a necessary step that will allow removal of some `require` statements.

Currently this uses `SystemJS.import` in non-PRODUCTION mode, but it should be possible to replace those with standard *dynamic* `import` calls in the future.
This commit is contained in:
Jonas Jenwald 2020-05-19 15:10:05 +02:00
parent 0960e6c0b5
commit d4d933538b
4 changed files with 72 additions and 32 deletions

View File

@ -65,7 +65,8 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
* @typedef {function} IPDFStreamFactory * @typedef {function} IPDFStreamFactory
* @param {DocumentInitParameters} params - The document initialization * @param {DocumentInitParameters} params - The document initialization
* parameters. The "url" key is always present. * parameters. The "url" key is always present.
* @returns {IPDFStream} * @returns {Promise} A promise, which is resolved with an instance of
* {IPDFStream}.
* @ignore * @ignore
*/ */
@ -80,7 +81,7 @@ let createPDFNetworkStream;
* data transport. * data transport.
* @param {IPDFStreamFactory} pdfNetworkStreamFactory - The factory function * @param {IPDFStreamFactory} pdfNetworkStreamFactory - The factory function
* that takes document initialization parameters (including a "url") and * that takes document initialization parameters (including a "url") and
* returns an instance of {IPDFStream}. * returns a promise which is resolved with an instance of {IPDFStream}.
* @ignore * @ignore
*/ */
function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
@ -313,12 +314,14 @@ function getDocument(src) {
if (task.destroyed) { if (task.destroyed) {
throw new Error("Loading aborted"); throw new Error("Loading aborted");
} }
return _fetchDocument(worker, params, rangeTransport, docId).then(
function (workerId) {
if (task.destroyed) {
throw new Error("Loading aborted");
}
const workerIdPromise = _fetchDocument(
worker,
params,
rangeTransport,
docId
);
const networkStreamPromise = new Promise(function (resolve) {
let networkStream; let networkStream;
if (rangeTransport) { if (rangeTransport) {
networkStream = new PDFDataTransportStream( networkStream = new PDFDataTransportStream(
@ -342,6 +345,14 @@ function getDocument(src) {
disableStream: params.disableStream, disableStream: params.disableStream,
}); });
} }
resolve(networkStream);
});
return Promise.all([workerIdPromise, networkStreamPromise]).then(
function ([workerId, networkStream]) {
if (task.destroyed) {
throw new Error("Loading aborted");
}
const messageHandler = new MessageHandler( const messageHandler = new MessageHandler(
docId, docId,

View File

@ -26,6 +26,12 @@ import {
validateResponseStatus, validateResponseStatus,
} from "./network_utils.js"; } from "./network_utils.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
'Module "./fetch_stream.js" shall not be used with MOZCENTRAL builds.'
);
}
function createFetchOptions(headers, withCredentials, abortController) { function createFetchOptions(headers, withCredentials, abortController) {
return { return {
method: "GET", method: "GET",

View File

@ -14,11 +14,6 @@
*/ */
/* globals __non_webpack_require__ */ /* globals __non_webpack_require__ */
const fs = __non_webpack_require__("fs");
const http = __non_webpack_require__("http");
const https = __non_webpack_require__("https");
const url = __non_webpack_require__("url");
import { import {
AbortException, AbortException,
assert, assert,
@ -30,6 +25,17 @@ import {
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
} from "./network_utils.js"; } from "./network_utils.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
'Module "./node_stream.js" shall not be used with MOZCENTRAL builds.'
);
}
const fs = __non_webpack_require__("fs");
const http = __non_webpack_require__("http");
const https = __non_webpack_require__("https");
const url = __non_webpack_require__("url");
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//; const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
function parseUrl(sourceUrl) { function parseUrl(sourceUrl) {

View File

@ -30,7 +30,24 @@ var pdfjsDisplaySVG = require("./display/svg.js");
const pdfjsDisplayWorkerOptions = require("./display/worker_options.js"); const pdfjsDisplayWorkerOptions = require("./display/worker_options.js");
const pdfjsDisplayAPICompatibility = require("./display/api_compatibility.js"); const pdfjsDisplayAPICompatibility = require("./display/api_compatibility.js");
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
const streamsPromise = Promise.all([
SystemJS.import("pdfjs/display/network.js"),
SystemJS.import("pdfjs/display/fetch_stream.js"),
]);
pdfjsDisplayAPI.setPDFNetworkStreamFactory(params => {
return streamsPromise.then(streams => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = streams;
if (
pdfjsDisplayDisplayUtils.isFetchSupported() &&
pdfjsDisplayDisplayUtils.isValidFetchUrl(params.url)
) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
});
} else if (PDFJSDev.test("GENERIC")) {
const { isNodeJS } = require("./shared/is_node.js"); const { isNodeJS } = require("./shared/is_node.js");
if (isNodeJS) { if (isNodeJS) {
const PDFNodeStream = require("./display/node_stream.js").PDFNodeStream; const PDFNodeStream = require("./display/node_stream.js").PDFNodeStream;