mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10:08 +02:00
[api-minor] Use the Fetch API, when supported, to load PDF documents in Node.js environments
Given that modern Node.js versions now implement support for a fair number of "browser" APIs, we can utilize the standard Fetch API to load PDF documents that are specified via http/https URLs. Please find compatibility information at: - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#browser_compatibility - https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch - https://developer.mozilla.org/en-US/docs/Web/API/Response#browser_compatibility - https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#response
This commit is contained in:
parent
72b8b29147
commit
eded037d06
6 changed files with 123 additions and 54 deletions
|
@ -32,6 +32,7 @@ import {
|
|||
import {
|
||||
buildGetDocumentParams,
|
||||
CMAP_URL,
|
||||
createTemporaryNodeServer,
|
||||
DefaultFileReaderFactory,
|
||||
TEST_PDFS_PATH,
|
||||
} from "./test_utils.js";
|
||||
|
@ -67,13 +68,27 @@ describe("api", function () {
|
|||
buildGetDocumentParams(tracemonkeyFileName);
|
||||
|
||||
let CanvasFactory;
|
||||
let tempServer = null;
|
||||
|
||||
beforeAll(function () {
|
||||
CanvasFactory = new DefaultCanvasFactory();
|
||||
|
||||
if (isNodeJS) {
|
||||
tempServer = createTemporaryNodeServer();
|
||||
}
|
||||
});
|
||||
|
||||
afterAll(function () {
|
||||
CanvasFactory = null;
|
||||
|
||||
if (isNodeJS) {
|
||||
// Close the server from accepting new connections after all test
|
||||
// finishes.
|
||||
const { server } = tempServer;
|
||||
server.close();
|
||||
|
||||
tempServer = null;
|
||||
}
|
||||
});
|
||||
|
||||
function waitSome(callback) {
|
||||
|
@ -119,13 +134,10 @@ describe("api", function () {
|
|||
});
|
||||
|
||||
it("creates pdf doc from URL-object", async function () {
|
||||
if (isNodeJS) {
|
||||
pending("window.location is not supported in Node.js.");
|
||||
}
|
||||
const urlObj = new URL(
|
||||
TEST_PDFS_PATH + basicApiFileName,
|
||||
window.location
|
||||
);
|
||||
const urlObj = isNodeJS
|
||||
? new URL(`http://127.0.0.1:${tempServer.port}/${basicApiFileName}`)
|
||||
: new URL(TEST_PDFS_PATH + basicApiFileName, window.location);
|
||||
|
||||
const loadingTask = getDocument(urlObj);
|
||||
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
|
@ -134,6 +146,9 @@ describe("api", function () {
|
|||
expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true);
|
||||
expect(pdfDocument.numPages).toEqual(3);
|
||||
|
||||
// Ensure that the Fetch API was used to load the PDF document.
|
||||
expect(pdfDocument.getNetworkStreamName()).toEqual("PDFFetchStream");
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue