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,40 +226,44 @@ function getDocument(src) {
|
||||
worker = null;
|
||||
|
||||
for (const key in source) {
|
||||
if (key === "url" && typeof window !== "undefined") {
|
||||
const value = source[key];
|
||||
|
||||
switch (key) {
|
||||
case "url":
|
||||
if (typeof window !== "undefined") {
|
||||
// The full path is required in the 'url' field.
|
||||
params[key] = new URL(source[key], window.location).href;
|
||||
params[key] = new URL(value, window.location).href;
|
||||
continue;
|
||||
} else if (key === "range") {
|
||||
rangeTransport = source[key];
|
||||
}
|
||||
break;
|
||||
case "range":
|
||||
rangeTransport = value;
|
||||
continue;
|
||||
} else if (key === "worker") {
|
||||
worker = source[key];
|
||||
case "worker":
|
||||
worker = value;
|
||||
continue;
|
||||
} else if (key === "data") {
|
||||
case "data":
|
||||
// Converting string or array-like data to Uint8Array.
|
||||
const pdfBytes = source[key];
|
||||
if (
|
||||
typeof PDFJSDev !== "undefined" &&
|
||||
PDFJSDev.test("GENERIC") &&
|
||||
isNodeJS &&
|
||||
typeof Buffer !== "undefined" && // eslint-disable-line no-undef
|
||||
pdfBytes instanceof Buffer // eslint-disable-line no-undef
|
||||
value instanceof Buffer // eslint-disable-line no-undef
|
||||
) {
|
||||
params[key] = new Uint8Array(pdfBytes);
|
||||
} else if (pdfBytes instanceof Uint8Array) {
|
||||
// Use the data as-is when it's already a Uint8Array.
|
||||
params[key] = pdfBytes;
|
||||
} else if (typeof pdfBytes === "string") {
|
||||
params[key] = stringToBytes(pdfBytes);
|
||||
params[key] = new Uint8Array(value);
|
||||
} else if (value instanceof Uint8Array) {
|
||||
break; // Use the data as-is when it's already a Uint8Array.
|
||||
} else if (typeof value === "string") {
|
||||
params[key] = stringToBytes(value);
|
||||
} else if (
|
||||
typeof pdfBytes === "object" &&
|
||||
pdfBytes !== null &&
|
||||
!isNaN(pdfBytes.length)
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
!isNaN(value.length)
|
||||
) {
|
||||
params[key] = new Uint8Array(pdfBytes);
|
||||
} else if (isArrayBuffer(pdfBytes)) {
|
||||
params[key] = new Uint8Array(pdfBytes);
|
||||
params[key] = new Uint8Array(value);
|
||||
} else if (isArrayBuffer(value)) {
|
||||
params[key] = new Uint8Array(value);
|
||||
} else {
|
||||
throw new Error(
|
||||
"Invalid PDF binary data: either typed array, " +
|
||||
@ -268,7 +272,7 @@ function getDocument(src) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
params[key] = source[key];
|
||||
params[key] = value;
|
||||
}
|
||||
|
||||
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
||||
|
Loading…
Reference in New Issue
Block a user