[api-minor] Move the ReadableStream polyfill to the global scope

Note that most (reasonably) modern browsers have supported this for a while now, see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#Browser_compatibility

By moving the polyfill into `src/shared/compatibility.js` we can thus get rid of the need to manually export/import `ReadableStream` and simply use it directly instead.

The only change here which *could* possibly lead to a difference in behavior is in the `isFetchSupported` function. Previously we attempted to check for the existence of a global `ReadableStream` implementation, which could now pass (assuming obviously that the preceding checks also succeeded).
However I'm not sure if that's a problem, since the previous check only confirmed the existence of a native `ReadableStream` implementation and not that it actually worked correctly. Finally it *could* just as well have been a globally registered polyfill from an application embedding the PDF.js library.
This commit is contained in:
Jonas Jenwald 2019-10-14 13:19:41 +02:00
parent af4ba75f68
commit e24050fa13
8 changed files with 32 additions and 68 deletions

View File

@ -117,12 +117,6 @@
"no-catch-shadow": "error", "no-catch-shadow": "error",
"no-delete-var": "error", "no-delete-var": "error",
"no-label-var": "error", "no-label-var": "error",
"no-restricted-globals": ["error",
{
"name": "ReadableStream",
"message": "Import it from `src/shared/util.js` or `pdfjsLib` instead; outside of the `/src` and `/web` folders, the rule may be disabled as needed. ",
},
],
"no-shadow-restricted-names": "error", "no-shadow-restricted-names": "error",
"no-shadow": "off", "no-shadow": "off",
"no-undef-init": "error", "no-undef-init": "error",

View File

@ -435,7 +435,6 @@ class StatTimer {
function isFetchSupported() { function isFetchSupported() {
return (typeof fetch !== 'undefined' && return (typeof fetch !== 'undefined' &&
typeof Response !== 'undefined' && 'body' in Response.prototype && typeof Response !== 'undefined' && 'body' in Response.prototype &&
// eslint-disable-next-line no-restricted-globals
typeof ReadableStream !== 'undefined'); typeof ReadableStream !== 'undefined');
} }

View File

@ -106,7 +106,6 @@ exports.createObjectURL = pdfjsSharedUtil.createObjectURL;
exports.removeNullCharacters = pdfjsSharedUtil.removeNullCharacters; exports.removeNullCharacters = pdfjsSharedUtil.removeNullCharacters;
exports.shadow = pdfjsSharedUtil.shadow; exports.shadow = pdfjsSharedUtil.shadow;
exports.Util = pdfjsSharedUtil.Util; exports.Util = pdfjsSharedUtil.Util;
exports.ReadableStream = pdfjsSharedUtil.ReadableStream;
exports.RenderingCancelledException = exports.RenderingCancelledException =
pdfjsDisplayDisplayUtils.RenderingCancelledException; pdfjsDisplayDisplayUtils.RenderingCancelledException;
exports.getFilenameFromUrl = pdfjsDisplayDisplayUtils.getFilenameFromUrl; exports.getFilenameFromUrl = pdfjsDisplayDisplayUtils.getFilenameFromUrl;

View File

@ -249,6 +249,36 @@ const isIE = /Trident/.test(userAgent);
globalThis.URL = require('core-js/web/url'); globalThis.URL = require('core-js/web/url');
})(); })();
// Support: IE, Node.js
(function checkReadableStream() {
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('IMAGE_DECODERS')) {
// The current image decoders are synchronous, hence `ReadableStream`
// shouldn't need to be polyfilled for the IMAGE_DECODERS build target.
return;
}
let isReadableStreamSupported = false;
if (typeof ReadableStream !== 'undefined') {
// MS Edge may say it has ReadableStream but they are not up to spec yet.
try {
// eslint-disable-next-line no-new
new ReadableStream({
start(controller) {
controller.close();
},
});
isReadableStreamSupported = true;
} catch (e) {
// The ReadableStream constructor cannot be used.
}
}
if (isReadableStreamSupported) {
return;
}
globalThis.ReadableStream =
require('web-streams-polyfill/dist/ponyfill').ReadableStream;
})();
// Support: IE<11, Safari<8, Chrome<36 // Support: IE<11, Safari<8, Chrome<36
(function checkWeakMap() { (function checkWeakMap() {
if (globalThis.WeakMap) { if (globalThis.WeakMap) {

View File

@ -16,7 +16,7 @@
import { import {
AbortException, assert, createPromiseCapability, MissingPDFException, AbortException, assert, createPromiseCapability, MissingPDFException,
ReadableStream, UnexpectedResponseException, UnknownErrorException UnexpectedResponseException, UnknownErrorException
} from './util'; } from './util';
const CallbackKind = { const CallbackKind = {

View File

@ -1,55 +0,0 @@
/* Copyright 2017 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('MOZCENTRAL')) {
if (typeof ReadableStream === 'undefined') {
throw new Error('Please enable ReadableStream support by resetting the ' +
'"javascript.options.streams" preference to "true" in about:config.');
}
exports.ReadableStream = ReadableStream;
} else {
let isReadableStreamSupported = false;
if (typeof ReadableStream !== 'undefined') {
// MS Edge may say it has ReadableStream but they are not up to spec yet.
try {
// eslint-disable-next-line no-new
new ReadableStream({
start(controller) {
controller.close();
},
});
isReadableStreamSupported = true;
} catch (e) {
// The ReadableStream constructor cannot be used.
}
}
if (isReadableStreamSupported) {
exports.ReadableStream = ReadableStream;
} else if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('IMAGE_DECODERS')) {
class DummyReadableStream {
constructor() {
throw new Error('The current image decoders are synchronous, ' +
'hence `ReadableStream` shouldn\'t need to be ' +
'polyfilled for the IMAGE_DECODERS build target.');
}
}
exports.ReadableStream = DummyReadableStream;
} else {
exports.ReadableStream =
require('web-streams-polyfill/dist/ponyfill').ReadableStream;
}
}

View File

@ -15,7 +15,6 @@
/* eslint no-var: error */ /* eslint no-var: error */
import './compatibility'; import './compatibility';
import { ReadableStream } from './streams_polyfill';
const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0]; const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0]; const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
@ -930,7 +929,6 @@ export {
readUint16, readUint16,
readUint32, readUint32,
removeNullCharacters, removeNullCharacters,
ReadableStream,
setVerbosityLevel, setVerbosityLevel,
shadow, shadow,
string32, string32,

View File

@ -16,8 +16,7 @@
import { import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, isArrayBuffer, bytesToString, createPromiseCapability, createValidAbsoluteUrl, isArrayBuffer,
isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2, isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2,
ReadableStream, removeNullCharacters, string32, stringToBytes, removeNullCharacters, string32, stringToBytes, stringToPDFString
stringToPDFString
} from '../../src/shared/util'; } from '../../src/shared/util';
describe('util', function() { describe('util', function() {