Merge pull request #15916 from Snuffleupagus/fetch-transfer
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
This commit is contained in:
commit
e09ad99973
@ -192,9 +192,7 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
|||||||
* @property {boolean} [transferPdfData] - Determines if we can transfer
|
* @property {boolean} [transferPdfData] - Determines if we can transfer
|
||||||
* TypedArrays used for loading the PDF file, utilized together with:
|
* TypedArrays used for loading the PDF file, utilized together with:
|
||||||
* - The `data`-option, for the `getDocument` function.
|
* - The `data`-option, for the `getDocument` function.
|
||||||
* - The `initialData`-option, for the `PDFDataRangeTransport` constructor.
|
* - The `PDFDataTransportStream` implementation.
|
||||||
* - The `chunk`-option, for the `PDFDataTransportStream._onReceiveData`
|
|
||||||
* method.
|
|
||||||
* This will help reduce main-thread memory usage, however it will take
|
* This will help reduce main-thread memory usage, however it will take
|
||||||
* ownership of the TypedArrays. The default value is `false`.
|
* ownership of the TypedArrays. The default value is `false`.
|
||||||
* @property {boolean} [isEvalSupported] - Determines if we can evaluate strings
|
* @property {boolean} [isEvalSupported] - Determines if we can evaluate strings
|
||||||
@ -2499,7 +2497,7 @@ class WorkerTransport {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assert(
|
assert(
|
||||||
isArrayBuffer(value),
|
value instanceof ArrayBuffer,
|
||||||
"GetReader - expected an ArrayBuffer."
|
"GetReader - expected an ArrayBuffer."
|
||||||
);
|
);
|
||||||
// Enqueue data chunk into sink, and transfer it
|
// Enqueue data chunk into sink, and transfer it
|
||||||
@ -2585,7 +2583,7 @@ class WorkerTransport {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assert(
|
assert(
|
||||||
isArrayBuffer(value),
|
value instanceof ArrayBuffer,
|
||||||
"GetRangeReader - expected an ArrayBuffer."
|
"GetRangeReader - expected an ArrayBuffer."
|
||||||
);
|
);
|
||||||
sink.enqueue(new Uint8Array(value), 1, [value]);
|
sink.enqueue(new Uint8Array(value), 1, [value]);
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
AbortException,
|
AbortException,
|
||||||
assert,
|
assert,
|
||||||
createPromiseCapability,
|
createPromiseCapability,
|
||||||
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
import {
|
import {
|
||||||
createResponseStatusError,
|
createResponseStatusError,
|
||||||
@ -54,6 +55,17 @@ function createHeaders(httpHeaders) {
|
|||||||
return headers;
|
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} */
|
/** @implements {IPDFStream} */
|
||||||
class PDFFetchStream {
|
class PDFFetchStream {
|
||||||
constructor(source) {
|
constructor(source) {
|
||||||
@ -195,8 +207,7 @@ class PDFFetchStreamReader {
|
|||||||
total: this._contentLength,
|
total: this._contentLength,
|
||||||
});
|
});
|
||||||
|
|
||||||
const buffer = new Uint8Array(value).buffer;
|
return { value: getArrayBuffer(value), done: false };
|
||||||
return { value: buffer, done: false };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel(reason) {
|
cancel(reason) {
|
||||||
@ -254,8 +265,7 @@ class PDFFetchStreamRangeReader {
|
|||||||
this._loaded += value.byteLength;
|
this._loaded += value.byteLength;
|
||||||
this.onProgress?.({ loaded: this._loaded });
|
this.onProgress?.({ loaded: this._loaded });
|
||||||
|
|
||||||
const buffer = new Uint8Array(value).buffer;
|
return { value: getArrayBuffer(value), done: false };
|
||||||
return { value: buffer, done: false };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel(reason) {
|
cancel(reason) {
|
||||||
|
@ -38,8 +38,7 @@ function getArrayBuffer(xhr) {
|
|||||||
if (typeof data !== "string") {
|
if (typeof data !== "string") {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
const array = stringToBytes(data);
|
return stringToBytes(data).buffer;
|
||||||
return array.buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user