Skip to content

Commit 1cd7e48

Browse files
committed
Ensure that getDocument is called with one of the data, range, or url parameters provided
Providing one of these parameters is necessary when calling `getDocument`, since otherwise there's nothing to actually load. However, we currently don't enforce that properly and if there's more than one of these parameters provided the behaviour isn't well defined.[1] The new behaviour is thus, in order: 1. Use the `data` parameter, since the PDF is already available and no additional loading is necessary. 2. Use the `range` parameter, for custom PDF loading (e.g. the Firefox PDF Viewer). 3. Use the `url` parameter, and have the PDF.js library load the PDF with a suitable `networkStream`. 4. Throw an error, since there's no way to load the PDF. --- [1] E.g. if both `data` and `range` is provided, we'd load the document directly (since it's available) and also initialize a pointless `PDFDataTransportStream` instance.
1 parent 0ee557c commit 1cd7e48

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/display/api.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,18 @@ function getDocument(src = {}) {
469469
);
470470

471471
let networkStream;
472-
if (rangeTransport) {
472+
if (data) {
473+
// The entire PDF was provided, no `networkStream` necessary.
474+
} else if (rangeTransport) {
473475
networkStream = new PDFDataTransportStream({
474476
pdfDataRangeTransport: rangeTransport,
475477
disableRange,
476478
disableStream,
477479
});
478-
} else if (!data) {
480+
} else if (url) {
479481
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
480482
throw new Error("Not implemented: NetworkStream");
481483
}
482-
if (!url) {
483-
throw new Error("getDocument - no `url` parameter provided.");
484-
}
485484
// eslint-disable-next-line no-nested-ternary
486485
const NetworkStream = isValidFetchUrl(url)
487486
? PDFFetchStream
@@ -499,6 +498,10 @@ function getDocument(src = {}) {
499498
disableRange,
500499
disableStream,
501500
});
501+
} else {
502+
throw new Error(
503+
"getDocument - expected either `data`, `range`, or `url` parameter."
504+
);
502505
}
503506

504507
return workerIdPromise.then(workerId => {

0 commit comments

Comments
 (0)