Remove the isFetchSupported function since the Fetch API is available in all supported browsers

The currently supported browsers, note the minimum versions [listed here](5a4e06af2d/gulpfile.js (L78-L88)), should now have native support for all of the features checked in the `isFetchSupported` function:

 - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#browser_compatibility
 - https://developer.mozilla.org/en-US/docs/Web/API/Response#browser_compatibility
 - https://developer.mozilla.org/en-US/docs/Web/API/Body/body#browser_compatibility
 - https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility

Hence this function can now be removed, and the code can thus be simplified a little bit.
This commit is contained in:
Jonas Jenwald 2021-06-11 22:53:09 +02:00
parent 5a4e06af2d
commit ddea90b8f6
2 changed files with 24 additions and 62 deletions

View File

@ -49,7 +49,7 @@ class DOMCanvasFactory extends BaseCanvasFactory {
async function fetchData(url, asTypedArray = false) {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
isValidFetchUrl(url, document.baseURI)
) {
const response = await fetch(url);
if (!response.ok) {
@ -483,15 +483,6 @@ class StatTimer {
}
}
function isFetchSupported() {
return (
typeof fetch !== "undefined" &&
typeof Response !== "undefined" &&
"body" in Response.prototype &&
typeof ReadableStream !== "undefined"
);
}
function isValidFetchUrl(url, baseUrl) {
try {
const { protocol } = baseUrl ? new URL(url, baseUrl) : new URL(url);
@ -626,7 +617,6 @@ export {
getFilenameFromUrl,
getPdfFilenameFromUrl,
isDataScheme,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,

View File

@ -18,7 +18,6 @@ import {
addLinkAttributes,
getFilenameFromUrl,
getPdfFilenameFromUrl,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,
@ -54,6 +53,7 @@ import {
} from "./shared/util.js";
import { AnnotationLayer } from "./display/annotation_layer.js";
import { GlobalWorkerOptions } from "./display/worker_options.js";
import { isNodeJS } from "./shared/is_node.js";
import { renderTextLayer } from "./display/text_layer.js";
import { SVGGraphics } from "./display/svg.js";
import { XfaLayer } from "./display/xfa_layer.js";
@ -70,60 +70,32 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
import("pdfjs/display/network.js"),
import("pdfjs/display/fetch_stream.js"),
]);
setPDFNetworkStreamFactory(params => {
return streamsPromise.then(streams => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = streams;
if (isFetchSupported() && isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
});
} else if (PDFJSDev.test("GENERIC")) {
const { isNodeJS } = require("./shared/is_node.js");
if (isNodeJS) {
const PDFNodeStream = require("./display/node_stream.js").PDFNodeStream;
setPDFNetworkStreamFactory(params => {
return new PDFNodeStream(params);
});
} else {
const PDFNetworkStream = require("./display/network.js").PDFNetworkStream;
let PDFFetchStream;
if (isFetchSupported()) {
PDFFetchStream = require("./display/fetch_stream.js").PDFFetchStream;
}
setPDFNetworkStreamFactory(params => {
if (PDFFetchStream && isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
}
} else if (PDFJSDev.test("CHROME")) {
const PDFNetworkStream = require("./display/network.js").PDFNetworkStream;
let PDFFetchStream;
const isChromeWithFetchCredentials = function () {
// fetch does not include credentials until Chrome 61.0.3138.0 and later.
// https://chromium.googlesource.com/chromium/src/+/2e231cf052ca5e68e22baf0008ac9e5e29121707
try {
// Indexed properties on window are read-only in Chrome 61.0.3151.0+
// https://chromium.googlesource.com/chromium/src.git/+/58ab4a971b06dec13e4edf9de8382ca6847f6190
window[999] = 123; // should throw. Note: JS strict mode MUST be enabled.
delete window[999];
return false;
} catch (e) {
return true;
}
};
if (isFetchSupported() && isChromeWithFetchCredentials()) {
PDFFetchStream = require("./display/fetch_stream.js").PDFFetchStream;
}
setPDFNetworkStreamFactory(params => {
if (PDFFetchStream && isValidFetchUrl(params.url)) {
setPDFNetworkStreamFactory(async params => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = await streamsPromise;
if (isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
} else if (PDFJSDev.test("GENERIC || CHROME")) {
if (PDFJSDev.test("GENERIC") && isNodeJS) {
const { PDFNodeStream } = require("./display/node_stream.js");
setPDFNetworkStreamFactory(params => {
return new PDFNodeStream(params);
});
} else {
const { PDFNetworkStream } = require("./display/network.js");
const { PDFFetchStream } = require("./display/fetch_stream.js");
setPDFNetworkStreamFactory(params => {
if (isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
}
}
export {