mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[api-minor] Add a new transferPdfData
option to allow transferring more data to the worker-thread (bug 1809164)
Also, removes the `initialData`-parameter JSDocs for the `getDocument`-function given that this parameter has been completely unused since PR 8982 (over five years ago). Note that the `initialData`-parameter is, and always was, intended to be provided when initializing a `PDFDataRangeTransport`-instance.
This commit is contained in:
parent
d6f63b5fb7
commit
bbe629018d
4 changed files with 130 additions and 23 deletions
|
@ -193,6 +193,45 @@ describe("api", function () {
|
|||
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
|
||||
expect(data[1].loaded / data[1].total).toEqual(1);
|
||||
|
||||
// Check that the TypedArray wasn't transferred.
|
||||
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
it("creates pdf doc from TypedArray, with `transferPdfData` set", async function () {
|
||||
if (isNodeJS) {
|
||||
pending("Worker is not supported in Node.js.");
|
||||
}
|
||||
const typedArrayPdf = await DefaultFileReaderFactory.fetch({
|
||||
path: TEST_PDFS_PATH + basicApiFileName,
|
||||
});
|
||||
|
||||
// Sanity check to make sure that we fetched the entire PDF file.
|
||||
expect(typedArrayPdf instanceof Uint8Array).toEqual(true);
|
||||
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
||||
|
||||
const loadingTask = getDocument({
|
||||
data: typedArrayPdf,
|
||||
transferPdfData: true,
|
||||
});
|
||||
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
|
||||
|
||||
const progressReportedCapability = createPromiseCapability();
|
||||
loadingTask.onProgress = function (data) {
|
||||
progressReportedCapability.resolve(data);
|
||||
};
|
||||
|
||||
const data = await Promise.all([
|
||||
loadingTask.promise,
|
||||
progressReportedCapability.promise,
|
||||
]);
|
||||
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
|
||||
expect(data[1].loaded / data[1].total).toEqual(1);
|
||||
|
||||
// Check that the TypedArray was transferred.
|
||||
expect(typedArrayPdf.length).toEqual(0);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
|
@ -3257,6 +3296,47 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||
expect(pdfPage.rotate).toEqual(0);
|
||||
expect(fetches).toBeGreaterThan(2);
|
||||
|
||||
// Check that the TypedArray wasn't transferred.
|
||||
expect(initialData.length).toEqual(initialDataLength);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
it("should fetch document info and page using ranges, with `transferPdfData` set", async function () {
|
||||
if (isNodeJS) {
|
||||
pending("Worker is not supported in Node.js.");
|
||||
}
|
||||
const initialDataLength = 4000;
|
||||
let fetches = 0;
|
||||
|
||||
const data = await dataPromise;
|
||||
const initialData = new Uint8Array(data.subarray(0, initialDataLength));
|
||||
const transport = new PDFDataRangeTransport(data.length, initialData);
|
||||
transport.requestDataRange = function (begin, end) {
|
||||
fetches++;
|
||||
waitSome(function () {
|
||||
transport.onDataProgress(4000);
|
||||
transport.onDataRange(
|
||||
begin,
|
||||
new Uint8Array(data.subarray(begin, end))
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const loadingTask = getDocument({
|
||||
range: transport,
|
||||
transferPdfData: true,
|
||||
});
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
expect(pdfDocument.numPages).toEqual(14);
|
||||
|
||||
const pdfPage = await pdfDocument.getPage(10);
|
||||
expect(pdfPage.rotate).toEqual(0);
|
||||
expect(fetches).toBeGreaterThan(2);
|
||||
|
||||
// Check that the TypedArray was transferred.
|
||||
expect(initialData.length).toEqual(0);
|
||||
|
||||
await loadingTask.destroy();
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue