Add a new DOMFileReaderFactory
helper to the unit-tests, and re-factor NodeFileReaderFactory
to be asynchronous
This allows simplification of the 'creates pdf doc from URL and aborts loading after worker initialized' API unit-test. Note that the `DOMFileReaderFactory` uses the Fetch API, for simplicity, since it should be available in all browsers where we're running tests.
This commit is contained in:
parent
43491c19ee
commit
507e0a4907
@ -14,13 +14,12 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
buildGetDocumentParams, NodeCanvasFactory, NodeFileReaderFactory,
|
||||
TEST_PDFS_PATH
|
||||
buildGetDocumentParams, DOMFileReaderFactory, NodeCanvasFactory,
|
||||
NodeFileReaderFactory, TEST_PDFS_PATH
|
||||
} from './test_utils';
|
||||
import {
|
||||
createPromiseCapability, FontType, InvalidPDFException, MissingPDFException,
|
||||
OPS, PasswordException, PasswordResponses, PermissionFlag, StreamType,
|
||||
stringToBytes
|
||||
OPS, PasswordException, PasswordResponses, PermissionFlag, StreamType
|
||||
} from '../../src/shared/util';
|
||||
import {
|
||||
DOMCanvasFactory, RenderingCancelledException, StatTimer
|
||||
@ -110,50 +109,37 @@ describe('api', function() {
|
||||
}).catch(done.fail);
|
||||
});
|
||||
it('creates pdf doc from typed array', function(done) {
|
||||
var typedArrayPdf;
|
||||
let typedArrayPdfPromise;
|
||||
if (isNodeJS()) {
|
||||
typedArrayPdf = NodeFileReaderFactory.fetch({
|
||||
typedArrayPdfPromise = NodeFileReaderFactory.fetch({
|
||||
path: TEST_PDFS_PATH.node + basicApiFileName,
|
||||
});
|
||||
} else {
|
||||
let nonBinaryRequest = false;
|
||||
let request = new XMLHttpRequest();
|
||||
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);
|
||||
}
|
||||
typedArrayPdfPromise = DOMFileReaderFactory.fetch({
|
||||
path: TEST_PDFS_PATH.dom + basicApiFileName,
|
||||
});
|
||||
}
|
||||
// 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();
|
||||
loadingTask.onProgress = function(data) {
|
||||
progressReportedCapability.resolve(data);
|
||||
};
|
||||
const loadingTask = getDocument(typedArrayPdf);
|
||||
|
||||
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);
|
||||
const progressReportedCapability = createPromiseCapability();
|
||||
loadingTask.onProgress = function(data) {
|
||||
progressReportedCapability.resolve(data);
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
it('creates pdf doc from invalid PDF file', function(done) {
|
||||
|
@ -17,11 +17,29 @@ import { assert, CMapCompressionType } from '../../src/shared/util';
|
||||
import isNodeJS from '../../src/shared/is_node';
|
||||
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 {
|
||||
static fetch(params) {
|
||||
var fs = require('fs');
|
||||
var file = fs.readFileSync(params.path);
|
||||
return new Uint8Array(file);
|
||||
static async fetch(params) {
|
||||
const fs = require('fs');
|
||||
|
||||
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 {
|
||||
DOMFileReaderFactory,
|
||||
NodeFileReaderFactory,
|
||||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
|
Loading…
Reference in New Issue
Block a user