Merge pull request #18756 from Snuffleupagus/api-NetworkStream

Simplify the code that picks the appropriate NetworkStream-implementation
This commit is contained in:
Tim van der Meij 2024-09-17 19:19:19 +02:00 committed by GitHub
commit a58cd6851e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -413,34 +413,34 @@ function getDocument(src = {}) {
}); });
} else if (!data) { } else if (!data) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream"); throw new Error("Not implemented: NetworkStream");
} }
if (!url) { if (!url) {
throw new Error("getDocument - no `url` parameter provided."); throw new Error("getDocument - no `url` parameter provided.");
} }
const createPDFNetworkStream = params => { let NetworkStream;
if ( if (
typeof PDFJSDev !== "undefined" && typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") && PDFJSDev.test("GENERIC") &&
isNodeJS isNodeJS
) { ) {
const isFetchSupported = function () { const isFetchSupported =
return (
typeof fetch !== "undefined" && typeof fetch !== "undefined" &&
typeof Response !== "undefined" && typeof Response !== "undefined" &&
"body" in Response.prototype "body" in Response.prototype;
);
};
return isFetchSupported() && isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
networkStream = createPDFNetworkStream({ NetworkStream =
isFetchSupported && isValidFetchUrl(url)
? PDFFetchStream
: PDFNodeStream;
} else {
NetworkStream = isValidFetchUrl(url)
? PDFFetchStream
: PDFNetworkStream;
}
networkStream = new NetworkStream({
url, url,
length, length,
httpHeaders, httpHeaders,