pdf.js/src/shared/url_polyfill.js
Jonas Jenwald a9ce4e8417 Stop exposing the URL polyfill in the global scope
This moves/exposes the `URL` polyfill similarily to the existing `ReadableStream` polyfill, rather than exposing it globally, to avoid interfering with any "outside" code.
Both the `URL` and `ReadableStream` polyfills are now exposed on the `pdfjsLib` object, such that they are accessible to the viewer components.
Furthermore, the `no-restricted-globals` ESLint rule is also enabled to prevent accidental usage of the native `URL`/`ReadableStream` implementations directly in the `src/` and `web/` folders; see also https://eslint.org/docs/rules/no-restricted-globals

Addresses the remaining TODO in https://github.com/mozilla/pdf.js/projects/6
2018-07-04 09:16:28 +02:00

64 lines
2.3 KiB
JavaScript

/* 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.
*/
/* eslint-disable no-restricted-globals */
if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
// The `URL` constructor is assumed to be available in the extension builds.
exports.URL = URL;
} else {
let isURLSupported = false;
try {
if (typeof URL === 'function' && typeof URL.prototype === 'object' &&
('origin' in URL.prototype)) {
const u = new URL('b', 'http://a');
u.pathname = 'c%20d';
isURLSupported = (u.href === 'http://a/c%20d');
}
} catch (ex) {
// The `URL` constructor cannot be used.
}
if (isURLSupported) {
exports.URL = URL;
} else {
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('IMAGE_DECODERS')) {
class DummyURL {
constructor() {
throw new Error('The current image decoders doesn\'t utilize the ' +
'`URL` constructor, hence it shouldn\'t need to be ' +
'polyfilled for the IMAGE_DECODERS build target.');
}
}
exports.URL = DummyURL;
} else {
const PolyfillURL = require('../../external/url/url-lib').URL;
// Attempt to copy over the static methods.
const OriginalURL = require('./global_scope').URL;
if (OriginalURL) {
PolyfillURL.createObjectURL = function(blob) {
// IE extension allows a second optional options argument, see
// http://msdn.microsoft.com/en-us/library/ie/hh772302(v=vs.85).aspx
return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
};
PolyfillURL.revokeObjectURL = function(url) {
OriginalURL.revokeObjectURL(url);
};
}
exports.URL = PolyfillURL;
}
}
}