Only accept non-objects passed to getDocument
in GENERIC builds
In general it's always recommended to pass a *parameter object* when calling the `getDocument`-function in the API, since that's the only way to provide additional options, and the fact that it also accepts a URL or TypedArray directly is now mostly for backwards compatibility reasons. Unfortunately we cannot really remove this, since that code has existed since "forever", however we can limit it to only the GENERIC build to avoid completely unnecessary checks in e.g. the Firefox PDF Viewer. Finally, note that the default-viewer always provides a *parameter object* when calling the `getDocument`-function and it's thus completely unaffected by these changes.
This commit is contained in:
parent
1b1ebf6a77
commit
4758e6649c
@ -232,11 +232,6 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
||||
* (see `web/debugger.js`). The default value is `false`.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef { string | URL | TypedArray | ArrayBuffer | DocumentInitParameters
|
||||
* } GetDocumentParameters
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is the main entry point for loading a PDF and interacting with it.
|
||||
*
|
||||
@ -244,48 +239,47 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
||||
* XHR as fallback) is used, which means it must follow same origin rules,
|
||||
* e.g. no cross-domain requests without CORS.
|
||||
*
|
||||
* @param {GetDocumentParameters}
|
||||
* @param {string | URL | TypedArray | ArrayBuffer | DocumentInitParameters}
|
||||
* src - Can be a URL where a PDF file is located, a typed array (Uint8Array)
|
||||
* already populated with data, or a parameter object.
|
||||
* @returns {PDFDocumentLoadingTask}
|
||||
*/
|
||||
function getDocument(src) {
|
||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||
if (typeof src === "string" || src instanceof URL) {
|
||||
src = { url: src };
|
||||
} else if (isArrayBuffer(src)) {
|
||||
src = { data: src };
|
||||
} else if (src instanceof PDFDataRangeTransport) {
|
||||
deprecated(
|
||||
"`PDFDataRangeTransport`-instance, " +
|
||||
"please use a parameter object with `range`-property instead."
|
||||
);
|
||||
src = { range: src };
|
||||
} else {
|
||||
if (typeof src !== "object") {
|
||||
throw new Error(
|
||||
"Invalid parameter in getDocument, " +
|
||||
"need either string, URL, TypedArray, or parameter object."
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (typeof src !== "object") {
|
||||
throw new Error("Invalid parameter in getDocument, need parameter object.");
|
||||
}
|
||||
if (!src.url && !src.data && !src.range) {
|
||||
throw new Error(
|
||||
"Invalid parameter object: need either .data, .range or .url"
|
||||
);
|
||||
}
|
||||
const task = new PDFDocumentLoadingTask();
|
||||
|
||||
let source;
|
||||
if (typeof src === "string" || src instanceof URL) {
|
||||
source = { url: src };
|
||||
} else if (isArrayBuffer(src)) {
|
||||
source = { data: src };
|
||||
} else if (
|
||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
|
||||
src instanceof PDFDataRangeTransport
|
||||
) {
|
||||
deprecated(
|
||||
"`PDFDataRangeTransport`-instance, " +
|
||||
"please use a parameter object with `range`-property instead."
|
||||
);
|
||||
source = { range: src };
|
||||
} else {
|
||||
if (typeof src !== "object") {
|
||||
throw new Error(
|
||||
"Invalid parameter in getDocument, " +
|
||||
"need either string, URL, TypedArray, or parameter object."
|
||||
);
|
||||
}
|
||||
if (!src.url && !src.data && !src.range) {
|
||||
throw new Error(
|
||||
"Invalid parameter object: need either .data, .range or .url"
|
||||
);
|
||||
}
|
||||
source = src;
|
||||
}
|
||||
const params = Object.create(null);
|
||||
let rangeTransport = null,
|
||||
worker = null;
|
||||
|
||||
for (const key in source) {
|
||||
const val = source[key];
|
||||
for (const key in src) {
|
||||
const val = src[key];
|
||||
|
||||
switch (key) {
|
||||
case "url":
|
||||
|
Loading…
x
Reference in New Issue
Block a user