mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 08:08:07 +02:00
[api-minor] Support proper URL
-objects, in addition to URL-strings, in getDocument
Currently only URL-strings are officially supported by `getDocument`, however at this point in time I cannot really see any compelling reason to not support `URL`-objects as well. Most likely the reason that we've don't already support `URL`-objects, in `getDocument`, is that historically `URL` wasn't fully implemented across browsers and our old polyfill wasn't perfect; see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#browser_compatibility *Please note:* Because of how the `url` parameter is currently handled, there's actually *some* cases where passing a `URL`-object to `getDocument` already works. That, in my opinion, provides additional motivation for supporting `URL`-objects officially, since it makes the API more consistent. The following is an attempt to summarize the *current* situation, based on the actual code rather than the JSDocs: - `getDocument("url string")` works and is documented.[1] - `getDocument({ url: "url string", })` works and is documented.[1] - `getDocument(new URL(...))` throws immediately, since no supported parameters are found. - `getDocument({ url: new URL(...), })` actually works even though it's not documented.[1] Originally, when data was fetched on the worker-thread, this would likely have thrown since `URL` isn't clonable.[2] - `getDocument({ url: { abc: 123, }, })`, or some similarily meaningless input, will be "accepted" by `getDocument` and then throw a `MissingPDFException` when attempting to fetch the bogus data. With the changes in this patch, not only is `URL`-objects now officially supported and documented when calling `getDocument`, but we'll also do a much better job at actually validating any URL-data passed to `getDocument` (and instead fail early). --- [1] In *browsers*, we create a valid URL thus indirectly validating the input. In Node.js environments, on the other hand, no validation is done since obtaining a baseUrl is more difficult (and PDF.js is primarily written for browsers anyway). [2] https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types
This commit is contained in:
parent
27add0f1f3
commit
db1e1612df
2 changed files with 49 additions and 9 deletions
|
@ -73,6 +73,36 @@ describe("api", function () {
|
|||
}
|
||||
|
||||
describe("getDocument", function () {
|
||||
it("creates pdf doc from URL-string", async function () {
|
||||
const urlStr = TEST_PDFS_PATH + basicApiFileName;
|
||||
const loadingTask = getDocument(urlStr);
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
|
||||
expect(typeof urlStr).toEqual("string");
|
||||
expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true);
|
||||
expect(pdfDocument.numPages).toEqual(3);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
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 loadingTask = getDocument(urlObj);
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
|
||||
expect(urlObj instanceof URL).toEqual(true);
|
||||
expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true);
|
||||
expect(pdfDocument.numPages).toEqual(3);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
it("creates pdf doc from URL", function (done) {
|
||||
const loadingTask = getDocument(basicApiGetDocumentParams);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue