Restore the btoa
/atob
polyfills for Node.js
These were removed in PR 9170, since they were unused in the browsers that we'll support in PDF.js version `2.0`. However looking at the output of Travis, where a subset of the unit-tests are run using Node.js, there's warnings about `btoa` being undefined. This doesn't appear to cause any errors, which probably explains why we didn't notice this before (despite PR 9201).
This commit is contained in:
parent
ba0a3aebd0
commit
0e1b5589e7
@ -197,10 +197,6 @@ DOMElementSerializer.prototype = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function btoa (chars) {
|
|
||||||
return Buffer.from(chars, 'binary').toString('base64');
|
|
||||||
}
|
|
||||||
|
|
||||||
const document = {
|
const document = {
|
||||||
childNodes : [],
|
childNodes : [],
|
||||||
|
|
||||||
@ -245,7 +241,6 @@ Image.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.btoa = btoa;
|
|
||||||
exports.document = document;
|
exports.document = document;
|
||||||
exports.Image = Image;
|
exports.Image = Image;
|
||||||
|
|
||||||
|
@ -1015,10 +1015,17 @@ gulp.task('lib', ['buildnumber'], function () {
|
|||||||
var licenseHeaderLibre =
|
var licenseHeaderLibre =
|
||||||
fs.readFileSync('./src/license_header_libre.js').toString();
|
fs.readFileSync('./src/license_header_libre.js').toString();
|
||||||
var preprocessor2 = require('./external/builder/preprocessor2.js');
|
var preprocessor2 = require('./external/builder/preprocessor2.js');
|
||||||
|
var sharedFiles = [
|
||||||
|
'compatibility',
|
||||||
|
'global_scope',
|
||||||
|
'is_node',
|
||||||
|
'streams_polyfill',
|
||||||
|
'util',
|
||||||
|
];
|
||||||
var buildLib = merge([
|
var buildLib = merge([
|
||||||
gulp.src([
|
gulp.src([
|
||||||
'src/{core,display}/*.js',
|
'src/{core,display}/*.js',
|
||||||
'src/shared/{compatibility,util,streams_polyfill,global_scope}.js',
|
'src/shared/{' + sharedFiles.join() + '}.js',
|
||||||
'src/{pdf,pdf.worker}.js',
|
'src/{pdf,pdf.worker}.js',
|
||||||
], { base: 'src/', }),
|
], { base: 'src/', }),
|
||||||
gulp.src([
|
gulp.src([
|
||||||
|
@ -15,11 +15,12 @@
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
arrayByteLength, arraysToBytes, assert, createPromiseCapability, info,
|
arrayByteLength, arraysToBytes, assert, createPromiseCapability, info,
|
||||||
InvalidPDFException, isNodeJS, MessageHandler, MissingPDFException,
|
InvalidPDFException, MessageHandler, MissingPDFException, PasswordException,
|
||||||
PasswordException, setVerbosityLevel, UnexpectedResponseException,
|
setVerbosityLevel, UnexpectedResponseException, UnknownErrorException,
|
||||||
UnknownErrorException, UNSUPPORTED_FEATURES, warn, XRefParseException
|
UNSUPPORTED_FEATURES, warn, XRefParseException
|
||||||
} from '../shared/util';
|
} from '../shared/util';
|
||||||
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
|
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
|
||||||
|
import isNodeJS from '../shared/is_node';
|
||||||
import { Ref } from './primitives';
|
import { Ref } from './primitives';
|
||||||
|
|
||||||
var WorkerTask = (function WorkerTaskClosure() {
|
var WorkerTask = (function WorkerTaskClosure() {
|
||||||
|
@ -15,10 +15,11 @@
|
|||||||
/* globals __non_webpack_require__ */
|
/* globals __non_webpack_require__ */
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createObjectURL, FONT_IDENTITY_MATRIX, IDENTITY_MATRIX, ImageKind, isNodeJS,
|
createObjectURL, FONT_IDENTITY_MATRIX, IDENTITY_MATRIX, ImageKind, isNum, OPS,
|
||||||
isNum, OPS, Util, warn
|
Util, warn
|
||||||
} from '../shared/util';
|
} from '../shared/util';
|
||||||
import { DOMSVGFactory } from './dom_utils';
|
import { DOMSVGFactory } from './dom_utils';
|
||||||
|
import isNodeJS from '../shared/is_node';
|
||||||
|
|
||||||
var SVGGraphics = function() {
|
var SVGGraphics = function() {
|
||||||
throw new Error('Not implemented: SVGGraphics');
|
throw new Error('Not implemented: SVGGraphics');
|
||||||
|
@ -31,7 +31,8 @@ var pdfjsDisplaySVG = require('./display/svg.js');
|
|||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' ||
|
if (typeof PDFJSDev === 'undefined' ||
|
||||||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
||||||
if (pdfjsSharedUtil.isNodeJS()) {
|
const isNodeJS = require('./shared/is_node.js');
|
||||||
|
if (isNodeJS()) {
|
||||||
var PDFNodeStream = require('./display/node_stream.js').PDFNodeStream;
|
var PDFNodeStream = require('./display/node_stream.js').PDFNodeStream;
|
||||||
pdfjsDisplayAPI.setPDFNetworkStreamClass(PDFNodeStream);
|
pdfjsDisplayAPI.setPDFNetworkStreamClass(PDFNodeStream);
|
||||||
} else if (typeof Response !== 'undefined' && 'body' in Response.prototype &&
|
} else if (typeof Response !== 'undefined' && 'body' in Response.prototype &&
|
||||||
|
@ -22,6 +22,7 @@ if ((typeof PDFJSDev === 'undefined' ||
|
|||||||
(typeof PDFJS === 'undefined' || !PDFJS.compatibilityChecked)) {
|
(typeof PDFJS === 'undefined' || !PDFJS.compatibilityChecked)) {
|
||||||
|
|
||||||
var globalScope = require('./global_scope');
|
var globalScope = require('./global_scope');
|
||||||
|
const isNodeJS = require('./is_node');
|
||||||
|
|
||||||
var userAgent = (typeof navigator !== 'undefined' && navigator.userAgent) || '';
|
var userAgent = (typeof navigator !== 'undefined' && navigator.userAgent) || '';
|
||||||
var isAndroid = /Android/.test(userAgent);
|
var isAndroid = /Android/.test(userAgent);
|
||||||
@ -41,6 +42,28 @@ if (typeof PDFJS === 'undefined') {
|
|||||||
|
|
||||||
PDFJS.compatibilityChecked = true;
|
PDFJS.compatibilityChecked = true;
|
||||||
|
|
||||||
|
// Support: Node.js
|
||||||
|
(function checkNodeBtoa() {
|
||||||
|
if (globalScope.btoa || !isNodeJS()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
globalScope.btoa = function(chars) {
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
return Buffer.from(chars, 'binary').toString('base64');
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Support: Node.js
|
||||||
|
(function checkNodeAtob() {
|
||||||
|
if (globalScope.atob || !isNodeJS()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
globalScope.atob = function(input) {
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
return Buffer.from(input, 'base64').toString('binary');
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
// Checks if possible to use URL.createObjectURL()
|
// Checks if possible to use URL.createObjectURL()
|
||||||
// Support: IE, Chrome on iOS
|
// Support: IE, Chrome on iOS
|
||||||
(function checkOnBlobSupport() {
|
(function checkOnBlobSupport() {
|
||||||
|
19
src/shared/is_node.js
Normal file
19
src/shared/is_node.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* Copyright 2018 Mozilla Foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
/* globals module, process */
|
||||||
|
|
||||||
|
module.exports = function isNodeJS() {
|
||||||
|
return typeof process === 'object' && process + '' === '[object process]';
|
||||||
|
};
|
@ -1089,11 +1089,6 @@ function isSpace(ch) {
|
|||||||
return (ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A);
|
return (ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNodeJS() {
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
return typeof process === 'object' && process + '' === '[object process]';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promise Capability object.
|
* Promise Capability object.
|
||||||
*
|
*
|
||||||
@ -1633,7 +1628,6 @@ export {
|
|||||||
isNum,
|
isNum,
|
||||||
isString,
|
isString,
|
||||||
isSpace,
|
isSpace,
|
||||||
isNodeJS,
|
|
||||||
isSameOrigin,
|
isSameOrigin,
|
||||||
createValidAbsoluteUrl,
|
createValidAbsoluteUrl,
|
||||||
isLittleEndian,
|
isLittleEndian,
|
||||||
|
@ -17,9 +17,8 @@ import {
|
|||||||
buildGetDocumentParams, NodeFileReaderFactory, TEST_PDFS_PATH
|
buildGetDocumentParams, NodeFileReaderFactory, TEST_PDFS_PATH
|
||||||
} from './test_utils';
|
} from './test_utils';
|
||||||
import {
|
import {
|
||||||
createPromiseCapability, FontType, InvalidPDFException, isNodeJS,
|
createPromiseCapability, FontType, InvalidPDFException, MissingPDFException,
|
||||||
MissingPDFException, PasswordException, PasswordResponses, StreamType,
|
PasswordException, PasswordResponses, StreamType, stringToBytes
|
||||||
stringToBytes
|
|
||||||
} from '../../src/shared/util';
|
} from '../../src/shared/util';
|
||||||
import {
|
import {
|
||||||
DOMCanvasFactory, RenderingCancelledException
|
DOMCanvasFactory, RenderingCancelledException
|
||||||
@ -27,6 +26,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
getDocument, PDFDocumentProxy, PDFPageProxy
|
getDocument, PDFDocumentProxy, PDFPageProxy
|
||||||
} from '../../src/display/api';
|
} from '../../src/display/api';
|
||||||
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { PDFJS } from '../../src/display/global';
|
import { PDFJS } from '../../src/display/global';
|
||||||
|
|
||||||
describe('api', function() {
|
describe('api', function() {
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
import { CMap, CMapFactory, IdentityCMap } from '../../src/core/cmap';
|
import { CMap, CMapFactory, IdentityCMap } from '../../src/core/cmap';
|
||||||
import { DOMCMapReaderFactory } from '../../src/display/dom_utils';
|
import { DOMCMapReaderFactory } from '../../src/display/dom_utils';
|
||||||
import { isNodeJS } from '../../src/shared/util';
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { Name } from '../../src/core/primitives';
|
import { Name } from '../../src/core/primitives';
|
||||||
import { NodeCMapReaderFactory } from './test_utils';
|
import { NodeCMapReaderFactory } from './test_utils';
|
||||||
import { StringStream } from '../../src/core/stream';
|
import { StringStream } from '../../src/core/stream';
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import { buildGetDocumentParams } from './test_utils';
|
import { buildGetDocumentParams } from './test_utils';
|
||||||
import { DOMCanvasFactory } from '../../src/display/dom_utils';
|
import { DOMCanvasFactory } from '../../src/display/dom_utils';
|
||||||
import { getDocument } from '../../src/display/api';
|
import { getDocument } from '../../src/display/api';
|
||||||
import { isNodeJS } from '../../src/shared/util';
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
|
|
||||||
function getTopLeftPixel(canvasContext) {
|
function getTopLeftPixel(canvasContext) {
|
||||||
let imgData = canvasContext.getImageData(0, 0, 1, 1);
|
let imgData = canvasContext.getImageData(0, 0, 1, 1);
|
||||||
|
@ -14,10 +14,11 @@
|
|||||||
*/
|
*/
|
||||||
/* globals __non_webpack_require__ */
|
/* globals __non_webpack_require__ */
|
||||||
|
|
||||||
import { isNodeJS, NativeImageDecoding } from '../../src/shared/util';
|
|
||||||
import { setStubs, unsetStubs } from '../../examples/node/domstubs';
|
import { setStubs, unsetStubs } from '../../examples/node/domstubs';
|
||||||
import { buildGetDocumentParams } from './test_utils';
|
import { buildGetDocumentParams } from './test_utils';
|
||||||
import { getDocument } from '../../src/display/api';
|
import { getDocument } from '../../src/display/api';
|
||||||
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
|
import { NativeImageDecoding } from '../../src/shared/util';
|
||||||
import { SVGGraphics } from '../../src/display/svg';
|
import { SVGGraphics } from '../../src/display/svg';
|
||||||
|
|
||||||
const XLINK_NS = 'http://www.w3.org/1999/xlink';
|
const XLINK_NS = 'http://www.w3.org/1999/xlink';
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import {
|
import {
|
||||||
DOMSVGFactory, getFilenameFromUrl, isExternalLinkTargetSet, LinkTarget
|
DOMSVGFactory, getFilenameFromUrl, isExternalLinkTargetSet, LinkTarget
|
||||||
} from '../../src/display/dom_utils';
|
} from '../../src/display/dom_utils';
|
||||||
import { isNodeJS } from '../../src/shared/util';
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { PDFJS } from '../../src/display/global';
|
import { PDFJS } from '../../src/display/global';
|
||||||
|
|
||||||
describe('dom_utils', function() {
|
describe('dom_utils', function() {
|
||||||
|
@ -14,7 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
/* globals __non_webpack_require__ */
|
/* globals __non_webpack_require__ */
|
||||||
|
|
||||||
import { assert, isNodeJS } from '../../src/shared/util';
|
import { assert } from '../../src/shared/util';
|
||||||
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { PDFNodeStream } from '../../src/display/node_stream';
|
import { PDFNodeStream } from '../../src/display/node_stream';
|
||||||
|
|
||||||
// Make sure that we only running this script is Node.js environments.
|
// Make sure that we only running this script is Node.js environments.
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { CMapCompressionType, isNodeJS } from '../../src/shared/util';
|
import { CMapCompressionType } from '../../src/shared/util';
|
||||||
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
import { isRef } from '../../src/core/primitives';
|
import { isRef } from '../../src/core/primitives';
|
||||||
|
|
||||||
class NodeFileReaderFactory {
|
class NodeFileReaderFactory {
|
||||||
|
@ -17,7 +17,8 @@ import {
|
|||||||
binarySearchFirstItem, EventBus, getPDFFileNameFromURL, isValidRotation,
|
binarySearchFirstItem, EventBus, getPDFFileNameFromURL, isValidRotation,
|
||||||
waitOnEventOrTimeout, WaitOnType
|
waitOnEventOrTimeout, WaitOnType
|
||||||
} from '../../web/ui_utils';
|
} from '../../web/ui_utils';
|
||||||
import { createObjectURL, isNodeJS } from '../../src/shared/util';
|
import { createObjectURL } from '../../src/shared/util';
|
||||||
|
import isNodeJS from '../../src/shared/is_node';
|
||||||
|
|
||||||
describe('ui_utils', function() {
|
describe('ui_utils', function() {
|
||||||
describe('binary search', function() {
|
describe('binary search', function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user