Merge pull request #10558 from Snuffleupagus/PDFDataRangeTransport-tests
Re-factor the `PDFDataRangeTransport` unit-tests and enable them in Node.js/Travis
This commit is contained in:
commit
ff74a66672
@ -14,13 +14,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
buildGetDocumentParams, NodeCanvasFactory, NodeFileReaderFactory,
|
buildGetDocumentParams, DOMFileReaderFactory, NodeCanvasFactory,
|
||||||
TEST_PDFS_PATH
|
NodeFileReaderFactory, TEST_PDFS_PATH
|
||||||
} from './test_utils';
|
} from './test_utils';
|
||||||
import {
|
import {
|
||||||
createPromiseCapability, FontType, InvalidPDFException, MissingPDFException,
|
createPromiseCapability, FontType, InvalidPDFException, MissingPDFException,
|
||||||
OPS, PasswordException, PasswordResponses, PermissionFlag, StreamType,
|
OPS, PasswordException, PasswordResponses, PermissionFlag, StreamType
|
||||||
stringToBytes
|
|
||||||
} from '../../src/shared/util';
|
} from '../../src/shared/util';
|
||||||
import {
|
import {
|
||||||
DOMCanvasFactory, RenderingCancelledException, StatTimer
|
DOMCanvasFactory, RenderingCancelledException, StatTimer
|
||||||
@ -110,50 +109,37 @@ describe('api', function() {
|
|||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
it('creates pdf doc from typed array', function(done) {
|
it('creates pdf doc from typed array', function(done) {
|
||||||
var typedArrayPdf;
|
let typedArrayPdfPromise;
|
||||||
if (isNodeJS()) {
|
if (isNodeJS()) {
|
||||||
typedArrayPdf = NodeFileReaderFactory.fetch({
|
typedArrayPdfPromise = NodeFileReaderFactory.fetch({
|
||||||
path: TEST_PDFS_PATH.node + basicApiFileName,
|
path: TEST_PDFS_PATH.node + basicApiFileName,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let nonBinaryRequest = false;
|
typedArrayPdfPromise = DOMFileReaderFactory.fetch({
|
||||||
let request = new XMLHttpRequest();
|
path: TEST_PDFS_PATH.dom + basicApiFileName,
|
||||||
request.open('GET', TEST_PDFS_PATH.dom + basicApiFileName, false);
|
});
|
||||||
try {
|
|
||||||
request.responseType = 'arraybuffer';
|
|
||||||
nonBinaryRequest = request.responseType !== 'arraybuffer';
|
|
||||||
} catch (e) {
|
|
||||||
nonBinaryRequest = true;
|
|
||||||
}
|
|
||||||
if (nonBinaryRequest && request.overrideMimeType) {
|
|
||||||
request.overrideMimeType('text/plain; charset=x-user-defined');
|
|
||||||
}
|
|
||||||
request.send(null);
|
|
||||||
|
|
||||||
if (nonBinaryRequest) {
|
|
||||||
typedArrayPdf = stringToBytes(request.responseText);
|
|
||||||
} else {
|
|
||||||
typedArrayPdf = new Uint8Array(request.response);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Sanity check to make sure that we fetched the entire PDF file.
|
|
||||||
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
|
||||||
|
|
||||||
const loadingTask = getDocument(typedArrayPdf);
|
typedArrayPdfPromise.then((typedArrayPdf) => {
|
||||||
|
// Sanity check to make sure that we fetched the entire PDF file.
|
||||||
|
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
||||||
|
|
||||||
const progressReportedCapability = createPromiseCapability();
|
const loadingTask = getDocument(typedArrayPdf);
|
||||||
loadingTask.onProgress = function(data) {
|
|
||||||
progressReportedCapability.resolve(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
Promise.all([
|
const progressReportedCapability = createPromiseCapability();
|
||||||
loadingTask.promise,
|
loadingTask.onProgress = function(data) {
|
||||||
progressReportedCapability.promise,
|
progressReportedCapability.resolve(data);
|
||||||
]).then(function(data) {
|
};
|
||||||
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
|
|
||||||
expect(data[1].loaded / data[1].total).toEqual(1);
|
|
||||||
|
|
||||||
loadingTask.destroy().then(done);
|
return Promise.all([
|
||||||
|
loadingTask.promise,
|
||||||
|
progressReportedCapability.promise,
|
||||||
|
]).then(function(data) {
|
||||||
|
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
|
||||||
|
expect(data[1].loaded / data[1].total).toEqual(1);
|
||||||
|
|
||||||
|
loadingTask.destroy().then(done);
|
||||||
|
});
|
||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
it('creates pdf doc from invalid PDF file', function(done) {
|
it('creates pdf doc from invalid PDF file', function(done) {
|
||||||
@ -1467,99 +1453,87 @@ describe('api', function() {
|
|||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('PDFDataRangeTransport', function () {
|
|
||||||
var loadPromise;
|
|
||||||
function getDocumentData() {
|
|
||||||
const pdfPath = new URL('../pdfs/tracemonkey.pdf', window.location).href;
|
|
||||||
if (loadPromise) {
|
|
||||||
return loadPromise;
|
|
||||||
}
|
|
||||||
loadPromise = new Promise(function (resolve, reject) {
|
|
||||||
var xhr = new XMLHttpRequest(pdfPath);
|
|
||||||
xhr.open('GET', pdfPath);
|
|
||||||
xhr.responseType = 'arraybuffer';
|
|
||||||
xhr.onload = function () {
|
|
||||||
resolve(new Uint8Array(xhr.response));
|
|
||||||
};
|
|
||||||
xhr.onerror = function () {
|
|
||||||
reject(new Error('PDF is not loaded'));
|
|
||||||
};
|
|
||||||
xhr.send();
|
|
||||||
});
|
|
||||||
return loadPromise;
|
|
||||||
}
|
|
||||||
it('should fetch document info and page using ranges', function (done) {
|
|
||||||
if (isNodeJS()) {
|
|
||||||
pending('XMLHttpRequest is not supported in Node.js.');
|
|
||||||
}
|
|
||||||
|
|
||||||
var transport;
|
describe('PDFDataRangeTransport', function() {
|
||||||
var initialDataLength = 4000;
|
let dataPromise;
|
||||||
var fetches = 0;
|
|
||||||
var getDocumentPromise = getDocumentData().then(function (data) {
|
beforeAll(function(done) {
|
||||||
var initialData = data.subarray(0, initialDataLength);
|
const fileName = 'tracemonkey.pdf';
|
||||||
transport = new PDFDataRangeTransport(data.length, initialData);
|
if (isNodeJS()) {
|
||||||
transport.requestDataRange = function (begin, end) {
|
dataPromise = NodeFileReaderFactory.fetch({
|
||||||
|
path: TEST_PDFS_PATH.node + fileName,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
dataPromise = DOMFileReaderFactory.fetch({
|
||||||
|
path: TEST_PDFS_PATH.dom + fileName,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(function() {
|
||||||
|
dataPromise = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch document info and page using ranges', function(done) {
|
||||||
|
const initialDataLength = 4000;
|
||||||
|
let fetches = 0, loadingTask;
|
||||||
|
|
||||||
|
dataPromise.then(function(data) {
|
||||||
|
const initialData = data.subarray(0, initialDataLength);
|
||||||
|
const transport = new PDFDataRangeTransport(data.length, initialData);
|
||||||
|
transport.requestDataRange = function(begin, end) {
|
||||||
fetches++;
|
fetches++;
|
||||||
waitSome(function () {
|
waitSome(function() {
|
||||||
transport.onDataProgress(4000);
|
transport.onDataProgress(4000);
|
||||||
transport.onDataRange(begin, data.subarray(begin, end));
|
transport.onDataRange(begin, data.subarray(begin, end));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
var loadingTask = getDocument(transport);
|
loadingTask = getDocument(transport);
|
||||||
return loadingTask.promise;
|
return loadingTask.promise;
|
||||||
});
|
}).then(function(pdfDocument) {
|
||||||
var pdfDocument;
|
|
||||||
var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
|
|
||||||
pdfDocument = pdfDocument_;
|
|
||||||
var pagePromise = pdfDocument.getPage(10);
|
|
||||||
return pagePromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
getPagePromise.then(function (page) {
|
|
||||||
expect(pdfDocument.numPages).toEqual(14);
|
expect(pdfDocument.numPages).toEqual(14);
|
||||||
expect(page.rotate).toEqual(0);
|
|
||||||
|
return pdfDocument.getPage(10);
|
||||||
|
}).then(function(pdfPage) {
|
||||||
|
expect(pdfPage.rotate).toEqual(0);
|
||||||
expect(fetches).toBeGreaterThan(2);
|
expect(fetches).toBeGreaterThan(2);
|
||||||
done();
|
|
||||||
|
loadingTask.destroy().then(done);
|
||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
it('should fetch document info and page using range and streaming',
|
|
||||||
function (done) {
|
|
||||||
if (isNodeJS()) {
|
|
||||||
pending('XMLHttpRequest is not supported in Node.js.');
|
|
||||||
}
|
|
||||||
|
|
||||||
var transport;
|
it('should fetch document info and page using range and streaming',
|
||||||
var initialDataLength = 4000;
|
function(done) {
|
||||||
var fetches = 0;
|
const initialDataLength = 4000;
|
||||||
var getDocumentPromise = getDocumentData().then(function (data) {
|
let fetches = 0, loadingTask;
|
||||||
var initialData = data.subarray(0, initialDataLength);
|
|
||||||
transport = new PDFDataRangeTransport(data.length, initialData);
|
dataPromise.then(function(data) {
|
||||||
transport.requestDataRange = function (begin, end) {
|
const initialData = data.subarray(0, initialDataLength);
|
||||||
|
const transport = new PDFDataRangeTransport(data.length, initialData);
|
||||||
|
transport.requestDataRange = function(begin, end) {
|
||||||
fetches++;
|
fetches++;
|
||||||
if (fetches === 1) {
|
if (fetches === 1) {
|
||||||
// send rest of the data on first range request.
|
// Send rest of the data on first range request.
|
||||||
transport.onDataProgressiveRead(data.subarray(initialDataLength));
|
transport.onDataProgressiveRead(data.subarray(initialDataLength));
|
||||||
}
|
}
|
||||||
waitSome(function () {
|
waitSome(function() {
|
||||||
transport.onDataRange(begin, data.subarray(begin, end));
|
transport.onDataRange(begin, data.subarray(begin, end));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
var loadingTask = getDocument(transport);
|
loadingTask = getDocument(transport);
|
||||||
return loadingTask.promise;
|
return loadingTask.promise;
|
||||||
});
|
}).then(function(pdfDocument) {
|
||||||
var pdfDocument;
|
|
||||||
var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
|
|
||||||
pdfDocument = pdfDocument_;
|
|
||||||
var pagePromise = pdfDocument.getPage(10);
|
|
||||||
return pagePromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
getPagePromise.then(function (page) {
|
|
||||||
expect(pdfDocument.numPages).toEqual(14);
|
expect(pdfDocument.numPages).toEqual(14);
|
||||||
expect(page.rotate).toEqual(0);
|
|
||||||
|
return pdfDocument.getPage(10);
|
||||||
|
}).then(function(pdfPage) {
|
||||||
|
expect(pdfPage.rotate).toEqual(0);
|
||||||
expect(fetches).toEqual(1);
|
expect(fetches).toEqual(1);
|
||||||
done();
|
|
||||||
|
waitSome(function() {
|
||||||
|
loadingTask.destroy().then(done);
|
||||||
|
});
|
||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -17,11 +17,29 @@ import { assert, CMapCompressionType } from '../../src/shared/util';
|
|||||||
import isNodeJS from '../../src/shared/is_node';
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { isRef } from '../../src/core/primitives';
|
import { isRef } from '../../src/core/primitives';
|
||||||
|
|
||||||
|
class DOMFileReaderFactory {
|
||||||
|
static async fetch(params) {
|
||||||
|
const response = await fetch(params.path);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
return new Uint8Array(await response.arrayBuffer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class NodeFileReaderFactory {
|
class NodeFileReaderFactory {
|
||||||
static fetch(params) {
|
static async fetch(params) {
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
var file = fs.readFileSync(params.path);
|
|
||||||
return new Uint8Array(file);
|
return new Promise((resolve, reject) => {
|
||||||
|
fs.readFile(params.path, (error, data) => {
|
||||||
|
if (error || !data) {
|
||||||
|
reject(error || new Error(`Empty file for: ${params.path}`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve(new Uint8Array(data));
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,6 +160,7 @@ class XRefMock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
DOMFileReaderFactory,
|
||||||
NodeFileReaderFactory,
|
NodeFileReaderFactory,
|
||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
|
Loading…
Reference in New Issue
Block a user