Skip to content

Commit

Permalink
Merge pull request #9769 from Snuffleupagus/node-unittest-rm-console-…
Browse files Browse the repository at this point in the history
…errors

Reduce the amount of errors logged, primarily in Node.js/Travis, when running the unit-tests
  • Loading branch information
timvandermeij authored Jun 3, 2018
2 parents 3859c63 + 11b4613 commit 8ce2474
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
10 changes: 2 additions & 8 deletions examples/node/pdf2png/pdf2png.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,8 @@ var pdfURL = '../../../web/compressed.tracemonkey-pldi-09.pdf';
// Read the PDF file into a typed array so PDF.js can load it.
var rawData = new Uint8Array(fs.readFileSync(pdfURL));

// Load the PDF file. The `disableFontFace` and `nativeImageDecoderSupport`
// options must be passed because Node.js has no native `@font-face` and
// `Image` support.
pdfjsLib.getDocument({
data: rawData,
disableFontFace: true,
nativeImageDecoderSupport: 'none',
}).then(function (pdfDocument) {
// Load the PDF file.
pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
console.log('# PDF document loaded.');

// Get the first page.
Expand Down
4 changes: 2 additions & 2 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ var WorkerMessageHandler = {
finishWorkerTask(task);
pdfManager.updatePassword(data.password);
pdfManagerReady();
}).catch(function (ex) {
}).catch(function (boundException) {
finishWorkerTask(task);
handler.send('PasswordException', ex);
handler.send('PasswordException', boundException);
}.bind(null, e));
} else if (e instanceof InvalidPDFException) {
handler.send('InvalidPDF', e);
Expand Down
14 changes: 11 additions & 3 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ function getDocument(src) {
const NativeImageDecoderValues = Object.values(NativeImageDecoding);
if (params.nativeImageDecoderSupport === undefined ||
!NativeImageDecoderValues.includes(params.nativeImageDecoderSupport)) {
params.nativeImageDecoderSupport = NativeImageDecoding.DECODE;
params.nativeImageDecoderSupport =
(apiCompatibilityParams.nativeImageDecoderSupport ||
NativeImageDecoding.DECODE);
}
if (!Number.isInteger(params.maxImageSize)) {
params.maxImageSize = -1;
Expand All @@ -283,7 +285,7 @@ function getDocument(src) {
params.isEvalSupported = true;
}
if (typeof params.disableFontFace !== 'boolean') {
params.disableFontFace = false;
params.disableFontFace = apiCompatibilityParams.disableFontFace || false;
}

if (typeof params.disableRange !== 'boolean') {
Expand Down Expand Up @@ -1786,7 +1788,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
password,
});
};
loadingTask.onPassword(updatePassword, exception.code);
try {
loadingTask.onPassword(updatePassword, exception.code);
} catch (ex) {
this._passwordCapability.reject(ex);
}
} else {
this._passwordCapability.reject(
new PasswordException(exception.message, exception.code));
Expand Down Expand Up @@ -2168,6 +2174,8 @@ var WorkerTransport = (function WorkerTransportClosure() {
disableStream: params.disableStream,
disableAutoFetch: params.disableAutoFetch,
disableCreateObjectURL: params.disableCreateObjectURL,
disableFontFace: params.disableFontFace,
nativeImageDecoderSupport: params.nativeImageDecoderSupport,
});
},
};
Expand Down
16 changes: 12 additions & 4 deletions src/display/api_compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

let compatibilityParams = Object.create(null);
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
const isNodeJS = require('../shared/is_node');

const userAgent =
(typeof navigator !== 'undefined' && navigator.userAgent) || '';
const isIE = /Trident/.test(userAgent);
Expand Down Expand Up @@ -42,9 +44,15 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
compatibilityParams.disableStream = true;
}
})();

// Support: Node.js
(function checkFontFaceAndImage() {
// Node.js is missing native support for `@font-face` and `Image`.
if (isNodeJS()) {
compatibilityParams.disableFontFace = true;
compatibilityParams.nativeImageDecoderSupport = 'none';
}
})();
}
const apiCompatibilityParams = Object.freeze(compatibilityParams);

export {
apiCompatibilityParams,
};
exports.apiCompatibilityParams = Object.freeze(compatibilityParams);
5 changes: 5 additions & 0 deletions test/unit/clitests_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

import { setVerbosityLevel, VerbosityLevel } from '../../src/shared/util';
import isNodeJS from '../../src/shared/is_node';
import { PDFNodeStream } from '../../src/display/node_stream';
import { setPDFNetworkStreamFactory } from '../../src/display/api';
Expand All @@ -23,6 +24,10 @@ if (!isNodeJS()) {
'Node.js environments.');
}

// Reduce the amount of console "spam", by ignoring `info`/`warn` calls,
// when running the unit-tests in Node.js/Travis.
setVerbosityLevel(VerbosityLevel.ERRORS);

// Set the network stream factory for the unit-tests.
setPDFNetworkStreamFactory(function(params) {
return new PDFNodeStream(params);
Expand Down
7 changes: 7 additions & 0 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,13 @@ function waitOnEventOrTimeout({ target, name, delay = 0, }) {
* Promise that is resolved when DOM window becomes visible.
*/
let animationStarted = new Promise(function (resolve) {
if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) &&
typeof window === 'undefined') {
// Prevent "ReferenceError: window is not defined" errors when running the
// unit-tests in Node.js/Travis.
setTimeout(resolve, 20);
return;
}
window.requestAnimationFrame(resolve);
});

Expand Down
5 changes: 1 addition & 4 deletions web/viewer_compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
}
})();
}
const viewerCompatibilityParams = Object.freeze(compatibilityParams);

export {
viewerCompatibilityParams,
};
exports.viewerCompatibilityParams = Object.freeze(compatibilityParams);

0 comments on commit 8ce2474

Please sign in to comment.