Merge pull request #15916 from Snuffleupagus/fetch-transfer

[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
This commit is contained in:
Jonas Jenwald 2023-01-13 13:28:12 +01:00 committed by GitHub
commit e09ad99973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -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]);

View File

@ -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) {

View File

@ -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 {