1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 15:48:06 +02:00

[api-major] Completely remove the global PDFJS object

This commit is contained in:
Jonas Jenwald 2018-02-17 23:51:24 +01:00
parent 4b4fcecf70
commit b8606abbc1
18 changed files with 323 additions and 426 deletions

View file

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS, pdfjsDistBuildPdf */
/* globals pdfjsDistBuildPdf, pdfjsDistWebPdfViewer */
'use strict';
@ -69,7 +69,7 @@ var rasterizeTextLayer = (function rasterizeTextLayerClosure() {
foreignObject.appendChild(div);
// Rendering text layer as HTML.
var task = PDFJS.renderTextLayer({
var task = pdfjsDistBuildPdf.renderTextLayer({
textContent,
container: div,
viewport,
@ -204,11 +204,11 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
div,
annotations,
page,
linkService: new PDFJS.SimpleLinkService(),
linkService: new pdfjsDistWebPdfViewer.SimpleLinkService(),
imageResourcesPath,
renderInteractiveForms,
};
PDFJS.AnnotationLayer.render(parameters);
pdfjsDistBuildPdf.AnnotationLayer.render(parameters);
// Inline SVG images from text annotations.
var images = div.getElementsByTagName('img');
@ -271,7 +271,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
*/
function Driver(options) {
// Configure the global worker options.
PDFJS.GlobalWorkerOptions.workerSrc = WORKER_SRC;
pdfjsDistBuildPdf.GlobalWorkerOptions.workerSrc = WORKER_SRC;
// Set the passed options
this.inflight = options.inflight;
@ -358,7 +358,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
let absoluteUrl = new URL(task.file, window.location).href;
try {
PDFJS.getDocument({
pdfjsDistBuildPdf.getDocument({
url: absoluteUrl,
password: task.password,
nativeImageDecoderSupport: task.nativeImageDecoderSupport,

View file

@ -58,266 +58,265 @@ describe('api', function() {
}, WAIT_TIMEOUT);
}
describe('PDFJS', function() {
describe('getDocument', function() {
it('creates pdf doc from URL', function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
describe('getDocument', function() {
it('creates pdf doc from URL', function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
var isProgressReportedResolved = false;
var progressReportedCapability = createPromiseCapability();
var isProgressReportedResolved = false;
var progressReportedCapability = createPromiseCapability();
// Attach the callback that is used to report loading progress;
// similarly to how viewer.js works.
loadingTask.onProgress = function (progressData) {
if (!isProgressReportedResolved) {
isProgressReportedResolved = true;
progressReportedCapability.resolve(progressData);
}
};
// Attach the callback that is used to report loading progress;
// similarly to how viewer.js works.
loadingTask.onProgress = function (progressData) {
if (!isProgressReportedResolved) {
isProgressReportedResolved = true;
progressReportedCapability.resolve(progressData);
}
};
var promises = [
progressReportedCapability.promise,
loadingTask.promise
];
Promise.all(promises).then(function (data) {
expect((data[0].loaded / data[0].total) > 0).toEqual(true);
expect(data[1] instanceof PDFDocumentProxy).toEqual(true);
expect(loadingTask).toEqual(data[1].loadingTask);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
var promises = [
progressReportedCapability.promise,
loadingTask.promise
];
Promise.all(promises).then(function (data) {
expect((data[0].loaded / data[0].total) > 0).toEqual(true);
expect(data[1] instanceof PDFDocumentProxy).toEqual(true);
expect(loadingTask).toEqual(data[1].loadingTask);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
it('creates pdf doc from URL and aborts before worker initialized',
function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
let destroyed = loadingTask.destroy();
});
it('creates pdf doc from URL and aborts before worker initialized',
function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
let destroyed = loadingTask.destroy();
loadingTask.promise.then(function(reason) {
done.fail('shall fail loading');
}).catch(function (reason) {
expect(true).toEqual(true);
destroyed.then(done);
});
loadingTask.promise.then(function(reason) {
done.fail('shall fail loading');
}).catch(function (reason) {
expect(true).toEqual(true);
destroyed.then(done);
});
it('creates pdf doc from URL and aborts loading after worker initialized',
function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
// This can be somewhat random -- we cannot guarantee perfect
// 'Terminate' message to the worker before/after setting up pdfManager.
var destroyed = loadingTask._worker.promise.then(function () {
return loadingTask.destroy();
});
destroyed.then(function (data) {
expect(true).toEqual(true);
done();
}).catch(function (reason) {
done.fail(reason);
});
});
it('creates pdf doc from URL and aborts loading after worker initialized',
function(done) {
var loadingTask = getDocument(basicApiGetDocumentParams);
// This can be somewhat random -- we cannot guarantee perfect
// 'Terminate' message to the worker before/after setting up pdfManager.
var destroyed = loadingTask._worker.promise.then(function () {
return loadingTask.destroy();
});
it('creates pdf doc from typed array', function(done) {
var typedArrayPdf;
if (isNodeJS()) {
typedArrayPdf = NodeFileReaderFactory.fetch({
path: TEST_PDFS_PATH.node + basicApiFileName,
});
destroyed.then(function (data) {
expect(true).toEqual(true);
done();
}).catch(function (reason) {
done.fail(reason);
});
});
it('creates pdf doc from typed array', function(done) {
var typedArrayPdf;
if (isNodeJS()) {
typedArrayPdf = 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 {
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);
}
typedArrayPdf = new Uint8Array(request.response);
}
// Sanity check to make sure that we fetched the entire PDF file.
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
}
// Sanity check to make sure that we fetched the entire PDF file.
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
var loadingTask = getDocument(typedArrayPdf);
loadingTask.promise.then(function(data) {
expect(data instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
var loadingTask = getDocument(typedArrayPdf);
loadingTask.promise.then(function(data) {
expect(data instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
it('creates pdf doc from invalid PDF file', function(done) {
// A severely corrupt PDF file (even Adobe Reader fails to open it).
var loadingTask = getDocument(buildGetDocumentParams('bug1020226.pdf'));
loadingTask.promise.then(function () {
done.fail('shall fail loading');
}).catch(function (error) {
expect(error instanceof InvalidPDFException).toEqual(true);
loadingTask.destroy().then(done);
});
});
it('creates pdf doc from invalid PDF file', function(done) {
// A severely corrupt PDF file (even Adobe Reader fails to open it).
var loadingTask = getDocument(buildGetDocumentParams('bug1020226.pdf'));
loadingTask.promise.then(function () {
done.fail('shall fail loading');
}).catch(function (error) {
expect(error instanceof InvalidPDFException).toEqual(true);
loadingTask.destroy().then(done);
});
it('creates pdf doc from non-existent URL', function(done) {
if (isNodeJS()) {
pending('XMLHttpRequest is not supported in Node.js.');
});
it('creates pdf doc from non-existent URL', function(done) {
if (isNodeJS()) {
pending('XMLHttpRequest is not supported in Node.js.');
}
var loadingTask = getDocument(
buildGetDocumentParams('non-existent.pdf'));
loadingTask.promise.then(function(error) {
done.fail('shall fail loading');
}).catch(function (error) {
expect(error instanceof MissingPDFException).toEqual(true);
loadingTask.destroy().then(done);
});
});
it('creates pdf doc from PDF file protected with user and owner password',
function (done) {
var loadingTask = getDocument(buildGetDocumentParams('pr6531_1.pdf'));
var isPasswordNeededResolved = false;
var passwordNeededCapability = createPromiseCapability();
var isPasswordIncorrectResolved = false;
var passwordIncorrectCapability = createPromiseCapability();
// Attach the callback that is used to request a password;
// similarly to how viewer.js handles passwords.
loadingTask.onPassword = function (updatePassword, reason) {
if (reason === PasswordResponses.NEED_PASSWORD &&
!isPasswordNeededResolved) {
isPasswordNeededResolved = true;
passwordNeededCapability.resolve();
updatePassword('qwerty'); // Provide an incorrect password.
return;
}
var loadingTask = getDocument(
buildGetDocumentParams('non-existent.pdf'));
loadingTask.promise.then(function(error) {
done.fail('shall fail loading');
}).catch(function (error) {
expect(error instanceof MissingPDFException).toEqual(true);
loadingTask.destroy().then(done);
});
if (reason === PasswordResponses.INCORRECT_PASSWORD &&
!isPasswordIncorrectResolved) {
isPasswordIncorrectResolved = true;
passwordIncorrectCapability.resolve();
updatePassword('asdfasdf'); // Provide the correct password.
return;
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var promises = [
passwordNeededCapability.promise,
passwordIncorrectCapability.promise,
loadingTask.promise
];
Promise.all(promises).then(function (data) {
expect(data[2] instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
it('creates pdf doc from PDF file protected with user and owner password',
function (done) {
var loadingTask = getDocument(buildGetDocumentParams('pr6531_1.pdf'));
});
it('creates pdf doc from PDF file protected with only a user password',
function (done) {
var filename = 'pr6531_2.pdf';
var isPasswordNeededResolved = false;
var passwordNeededCapability = createPromiseCapability();
var isPasswordIncorrectResolved = false;
var passwordIncorrectCapability = createPromiseCapability();
// Attach the callback that is used to request a password;
// similarly to how viewer.js handles passwords.
loadingTask.onPassword = function (updatePassword, reason) {
if (reason === PasswordResponses.NEED_PASSWORD &&
!isPasswordNeededResolved) {
isPasswordNeededResolved = true;
passwordNeededCapability.resolve();
updatePassword('qwerty'); // Provide an incorrect password.
return;
}
if (reason === PasswordResponses.INCORRECT_PASSWORD &&
!isPasswordIncorrectResolved) {
isPasswordIncorrectResolved = true;
passwordIncorrectCapability.resolve();
updatePassword('asdfasdf'); // Provide the correct password.
return;
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var promises = [
passwordNeededCapability.promise,
passwordIncorrectCapability.promise,
loadingTask.promise
];
Promise.all(promises).then(function (data) {
expect(data[2] instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy().then(done);
}).catch(function (reason) {
done.fail(reason);
});
});
it('creates pdf doc from PDF file protected with only a user password',
function (done) {
var filename = 'pr6531_2.pdf';
var passwordNeededLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: '',
}));
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail with no password');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
return passwordNeededLoadingTask.destroy();
});
var passwordIncorrectLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'qwerty',
}));
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail with wrong password');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
return passwordIncorrectLoadingTask.destroy();
});
var passwordAcceptedLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'asdfasdf',
}));
var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
expect(data instanceof PDFDocumentProxy).toEqual(true);
return passwordAcceptedLoadingTask.destroy();
});
Promise.all([result1, result2, result3]).then(function () {
done();
}).catch(function (reason) {
done.fail(reason);
});
var passwordNeededLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: '',
}));
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail with no password');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
return passwordNeededLoadingTask.destroy();
});
it('creates pdf doc from password protected PDF file and aborts/throws ' +
'in the onPassword callback (issue 7806)', function (done) {
var filename = 'issue3371.pdf';
var passwordIncorrectLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'qwerty',
}));
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail with wrong password');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
return passwordIncorrectLoadingTask.destroy();
});
var passwordNeededLoadingTask = getDocument(
buildGetDocumentParams(filename));
var passwordIncorrectLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'qwerty',
}));
var passwordAcceptedLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'asdfasdf',
}));
var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
expect(data instanceof PDFDocumentProxy).toEqual(true);
return passwordAcceptedLoadingTask.destroy();
});
Promise.all([result1, result2, result3]).then(function () {
done();
}).catch(function (reason) {
done.fail(reason);
});
});
let passwordNeededDestroyed;
passwordNeededLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.NEED_PASSWORD) {
passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
return;
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail since the loadingTask should be destroyed');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD);
return passwordNeededDestroyed;
});
it('creates pdf doc from password protected PDF file and aborts/throws ' +
'in the onPassword callback (issue 7806)', function (done) {
var filename = 'issue3371.pdf';
passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.INCORRECT_PASSWORD) {
throw new Error('Incorrect password');
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail since the onPassword callback should throw');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
return passwordIncorrectLoadingTask.destroy();
});
var passwordNeededLoadingTask = getDocument(
buildGetDocumentParams(filename));
var passwordIncorrectLoadingTask = getDocument(
buildGetDocumentParams(filename, {
password: 'qwerty',
}));
Promise.all([result1, result2]).then(function () {
done();
}).catch(function (reason) {
done.fail(reason);
});
let passwordNeededDestroyed;
passwordNeededLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.NEED_PASSWORD) {
passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
return;
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail since the loadingTask should be destroyed');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD);
return passwordNeededDestroyed;
});
passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.INCORRECT_PASSWORD) {
throw new Error('Incorrect password');
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail since the onPassword callback should throw');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
return passwordIncorrectLoadingTask.destroy();
});
Promise.all([result1, result2]).then(function () {
done();
}).catch(function (reason) {
done.fail(reason);
});
});
});
describe('PDFWorker', function() {
if (isNodeJS()) {
pending('Worker is not supported in Node.js.');