Re-factor the source
parsing, in getDocument
, to use switch
rather than if...else
Given the number of parameters that we now need to parse here, this code is no longer as readable as one would like. Hence this re-factoring, which will improve overall readability and also help with the next patch.
This commit is contained in:
parent
9c6770748c
commit
27add0f1f3
@ -226,49 +226,53 @@ function getDocument(src) {
|
|||||||
worker = null;
|
worker = null;
|
||||||
|
|
||||||
for (const key in source) {
|
for (const key in source) {
|
||||||
if (key === "url" && typeof window !== "undefined") {
|
const value = source[key];
|
||||||
// The full path is required in the 'url' field.
|
|
||||||
params[key] = new URL(source[key], window.location).href;
|
switch (key) {
|
||||||
continue;
|
case "url":
|
||||||
} else if (key === "range") {
|
if (typeof window !== "undefined") {
|
||||||
rangeTransport = source[key];
|
// The full path is required in the 'url' field.
|
||||||
continue;
|
params[key] = new URL(value, window.location).href;
|
||||||
} else if (key === "worker") {
|
continue;
|
||||||
worker = source[key];
|
}
|
||||||
continue;
|
break;
|
||||||
} else if (key === "data") {
|
case "range":
|
||||||
// Converting string or array-like data to Uint8Array.
|
rangeTransport = value;
|
||||||
const pdfBytes = source[key];
|
continue;
|
||||||
if (
|
case "worker":
|
||||||
typeof PDFJSDev !== "undefined" &&
|
worker = value;
|
||||||
PDFJSDev.test("GENERIC") &&
|
continue;
|
||||||
isNodeJS &&
|
case "data":
|
||||||
typeof Buffer !== "undefined" && // eslint-disable-line no-undef
|
// Converting string or array-like data to Uint8Array.
|
||||||
pdfBytes instanceof Buffer // eslint-disable-line no-undef
|
if (
|
||||||
) {
|
typeof PDFJSDev !== "undefined" &&
|
||||||
params[key] = new Uint8Array(pdfBytes);
|
PDFJSDev.test("GENERIC") &&
|
||||||
} else if (pdfBytes instanceof Uint8Array) {
|
isNodeJS &&
|
||||||
// Use the data as-is when it's already a Uint8Array.
|
typeof Buffer !== "undefined" && // eslint-disable-line no-undef
|
||||||
params[key] = pdfBytes;
|
value instanceof Buffer // eslint-disable-line no-undef
|
||||||
} else if (typeof pdfBytes === "string") {
|
) {
|
||||||
params[key] = stringToBytes(pdfBytes);
|
params[key] = new Uint8Array(value);
|
||||||
} else if (
|
} else if (value instanceof Uint8Array) {
|
||||||
typeof pdfBytes === "object" &&
|
break; // Use the data as-is when it's already a Uint8Array.
|
||||||
pdfBytes !== null &&
|
} else if (typeof value === "string") {
|
||||||
!isNaN(pdfBytes.length)
|
params[key] = stringToBytes(value);
|
||||||
) {
|
} else if (
|
||||||
params[key] = new Uint8Array(pdfBytes);
|
typeof value === "object" &&
|
||||||
} else if (isArrayBuffer(pdfBytes)) {
|
value !== null &&
|
||||||
params[key] = new Uint8Array(pdfBytes);
|
!isNaN(value.length)
|
||||||
} else {
|
) {
|
||||||
throw new Error(
|
params[key] = new Uint8Array(value);
|
||||||
"Invalid PDF binary data: either typed array, " +
|
} else if (isArrayBuffer(value)) {
|
||||||
"string, or array-like object is expected in the data property."
|
params[key] = new Uint8Array(value);
|
||||||
);
|
} else {
|
||||||
}
|
throw new Error(
|
||||||
continue;
|
"Invalid PDF binary data: either typed array, " +
|
||||||
|
"string, or array-like object is expected in the data property."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
params[key] = source[key];
|
params[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
||||||
|
Loading…
Reference in New Issue
Block a user