Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -626,7 +617,6 @@ export {
getFilenameFromUrl,
getPdfFilenameFromUrl,
isDataScheme,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,
Expand Down
60 changes: 16 additions & 44 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
addLinkAttributes,
getFilenameFromUrl,
getPdfFilenameFromUrl,
isFetchSupported,
isPdfFile,
isValidFetchUrl,
LinkTarget,
Expand Down Expand Up @@ -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";
Expand All @@ -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);
});

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")) {
const { isNodeJS } = require("./shared/is_node.js");
if (isNodeJS) {
const PDFNodeStream = require("./display/node_stream.js").PDFNodeStream;
} 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").PDFNetworkStream;
let PDFFetchStream;
if (isFetchSupported()) {
PDFFetchStream = require("./display/fetch_stream.js").PDFFetchStream;
}
const { PDFNetworkStream } = require("./display/network.js");
const { PDFFetchStream } = require("./display/fetch_stream.js");

setPDFNetworkStreamFactory(params => {
if (PDFFetchStream && isValidFetchUrl(params.url)) {
if (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)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
}

export {
Expand Down