diff --git a/src/display/api.js b/src/display/api.js index c999d1550..cc8357c6d 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -192,9 +192,7 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) { * @property {boolean} [transferPdfData] - Determines if we can transfer * TypedArrays used for loading the PDF file, utilized together with: * - The `data`-option, for the `getDocument` function. - * - The `initialData`-option, for the `PDFDataRangeTransport` constructor. - * - The `chunk`-option, for the `PDFDataTransportStream._onReceiveData` - * method. + * - The `PDFDataTransportStream` implementation. * This will help reduce main-thread memory usage, however it will take * ownership of the TypedArrays. The default value is `false`. * @property {boolean} [isEvalSupported] - Determines if we can evaluate strings @@ -2499,7 +2497,7 @@ class WorkerTransport { return; } assert( - isArrayBuffer(value), + value instanceof ArrayBuffer, "GetReader - expected an ArrayBuffer." ); // Enqueue data chunk into sink, and transfer it @@ -2585,7 +2583,7 @@ class WorkerTransport { return; } assert( - isArrayBuffer(value), + value instanceof ArrayBuffer, "GetRangeReader - expected an ArrayBuffer." ); sink.enqueue(new Uint8Array(value), 1, [value]); diff --git a/src/display/fetch_stream.js b/src/display/fetch_stream.js index a2d19f563..6680b067f 100644 --- a/src/display/fetch_stream.js +++ b/src/display/fetch_stream.js @@ -17,6 +17,7 @@ import { AbortException, assert, createPromiseCapability, + warn, } from "../shared/util.js"; import { createResponseStatusError, @@ -54,6 +55,17 @@ function createHeaders(httpHeaders) { return headers; } +function getArrayBuffer(val) { + if (val instanceof Uint8Array) { + return val.buffer; + } + if (val instanceof ArrayBuffer) { + return val; + } + warn(`getArrayBuffer - unexpected data format: ${val}`); + return new Uint8Array(val).buffer; +} + /** @implements {IPDFStream} */ class PDFFetchStream { constructor(source) { @@ -195,8 +207,7 @@ class PDFFetchStreamReader { total: this._contentLength, }); - const buffer = new Uint8Array(value).buffer; - return { value: buffer, done: false }; + return { value: getArrayBuffer(value), done: false }; } cancel(reason) { @@ -254,8 +265,7 @@ class PDFFetchStreamRangeReader { this._loaded += value.byteLength; this.onProgress?.({ loaded: this._loaded }); - const buffer = new Uint8Array(value).buffer; - return { value: buffer, done: false }; + return { value: getArrayBuffer(value), done: false }; } cancel(reason) { diff --git a/src/display/network.js b/src/display/network.js index 59d6bc933..00c895af3 100644 --- a/src/display/network.js +++ b/src/display/network.js @@ -38,8 +38,7 @@ function getArrayBuffer(xhr) { if (typeof data !== "string") { return data; } - const array = stringToBytes(data); - return array.buffer; + return stringToBytes(data).buffer; } class NetworkManager {