Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@
"no-catch-shadow": "error",
"no-delete-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": "off",
"no-undef-init": "error",
Expand Down
1 change: 0 additions & 1 deletion src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ class StatTimer {
function isFetchSupported() {
return (typeof fetch !== 'undefined' &&
typeof Response !== 'undefined' && 'body' in Response.prototype &&
// eslint-disable-next-line no-restricted-globals
typeof ReadableStream !== 'undefined');
}

Expand Down
1 change: 0 additions & 1 deletion src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ exports.createObjectURL = pdfjsSharedUtil.createObjectURL;
exports.removeNullCharacters = pdfjsSharedUtil.removeNullCharacters;
exports.shadow = pdfjsSharedUtil.shadow;
exports.Util = pdfjsSharedUtil.Util;
exports.ReadableStream = pdfjsSharedUtil.ReadableStream;
exports.RenderingCancelledException =
pdfjsDisplayDisplayUtils.RenderingCancelledException;
exports.getFilenameFromUrl = pdfjsDisplayDisplayUtils.getFilenameFromUrl;
Expand Down
30 changes: 30 additions & 0 deletions src/shared/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,36 @@ const isIE = /Trident/.test(userAgent);
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
(function checkWeakMap() {
if (globalThis.WeakMap) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {
AbortException, assert, createPromiseCapability, MissingPDFException,
ReadableStream, UnexpectedResponseException, UnknownErrorException
UnexpectedResponseException, UnknownErrorException
} from './util';

const CallbackKind = {
Expand Down
55 changes: 0 additions & 55 deletions src/shared/streams_polyfill.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
/* eslint no-var: error */

import './compatibility';
import { ReadableStream } from './streams_polyfill';

const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
Expand Down Expand Up @@ -930,7 +929,6 @@ export {
readUint16,
readUint32,
removeNullCharacters,
ReadableStream,
setVerbosityLevel,
shadow,
string32,
Expand Down
3 changes: 1 addition & 2 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, isArrayBuffer,
isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2,
ReadableStream, removeNullCharacters, string32, stringToBytes,
stringToPDFString
removeNullCharacters, string32, stringToBytes, stringToPDFString
} from '../../src/shared/util';

describe('util', function() {
Expand Down