[api-minor] Remove the, in legacy builds, bundled ReadableStream polyfill

According to the MDN compatibility data, see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility, all browsers that we support have native `ReadableStream` implementations (since quite some time too).

Hence only Node.js is now lagging behind w.r.t. `ReadableStream` support, and its experimental implementation doesn't really help us given the life-span of the LTS releases (see https://en.wikipedia.org/wiki/Node.js#Releases).
It seems quite unfortunate to bundle a `ReadableStream` polyfill in the `legacy` builds when it's unnecessary in browsers, given its overall size, but fortunately we can avoid that by simply listing `web-streams-polyfill` as a dependency for the `pdfjs-dist` library.
This commit is contained in:
Jonas Jenwald 2022-02-12 17:30:39 +01:00
parent e9fd67a3f6
commit b89595fd20
5 changed files with 19 additions and 25 deletions

View File

@ -24,8 +24,8 @@ Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mo
### Online demo ### Online demo
Please note that the "Modern browsers" version assumes native support for Please note that the "Modern browsers" version assumes native support for
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining, features such as `async`/`await`, optional chaining, nullish coalescing,
nullish coalescing, and private `class` fields/methods. and private `class` fields/methods.
+ Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html + Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html

View File

@ -8,8 +8,7 @@ This is a pre-built version of the PDF.js source code. It is automatically
generated by the build scripts. generated by the build scripts.
For usage with older browsers or environments, without support for modern For usage with older browsers or environments, without support for modern
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining, features such as `async`/`await`, optional chaining, nullish coalescing,
nullish coalescing, and private `class` fields/methods; please see the `legacy` and private `class` fields/methods; please see the `legacy/` folder.
folder.
See https://github.com/mozilla/pdf.js for learning and contributing. See https://github.com/mozilla/pdf.js for learning and contributing.

View File

@ -2042,6 +2042,9 @@ function packageBowerJson() {
homepage: DIST_HOMEPAGE, homepage: DIST_HOMEPAGE,
bugs: DIST_BUGS_URL, bugs: DIST_BUGS_URL,
license: DIST_LICENSE, license: DIST_LICENSE,
dependencies: {
"web-streams-polyfill": "^3.2.0",
},
peerDependencies: { peerDependencies: {
"worker-loader": "^3.0.8", // Used in `external/dist/webpack.js`. "worker-loader": "^3.0.8", // Used in `external/dist/webpack.js`.
}, },

View File

@ -133,15 +133,15 @@ class WorkerMessageHandler {
// Ensure that (primarily) Node.js users won't accidentally attempt to use // Ensure that (primarily) Node.js users won't accidentally attempt to use
// a non-translated/non-polyfilled build of the library, since that would // a non-translated/non-polyfilled build of the library, since that would
// quickly fail anyway because of missing functionality. // quickly fail anyway because of missing functionality.
if ( if (typeof ReadableStream === "undefined") {
(typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) && const partialMsg =
typeof ReadableStream === "undefined"
) {
throw new Error(
"The browser/environment lacks native support for critical " + "The browser/environment lacks native support for critical " +
"functionality used by the PDF.js library (e.g. `ReadableStream`); " + "functionality used by the PDF.js library (e.g. `ReadableStream`); ";
"please use a `legacy`-build instead."
); if (isNodeJS) {
throw new Error(partialMsg + "please use a `legacy`-build instead.");
}
throw new Error(partialMsg + "please update to a supported browser.");
} }
} }

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals __non_webpack_require__ */
import { isNodeJS } from "./is_node.js"; import { isNodeJS } from "./is_node.js";
@ -69,21 +70,12 @@ if (
// Support: Node.js // Support: Node.js
(function checkReadableStream() { (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;
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
// Slightly reduce the size of the Chromium-extension, given
// that `ReadableStream` has been supported since Chrome 43.
return;
}
if (globalThis.ReadableStream || !isNodeJS) { if (globalThis.ReadableStream || !isNodeJS) {
return; return;
} }
globalThis.ReadableStream = globalThis.ReadableStream = __non_webpack_require__(
require("web-streams-polyfill/dist/ponyfill.js").ReadableStream; "web-streams-polyfill/dist/ponyfill.js"
).ReadableStream;
})(); })();
// Support: Firefox<94, Chrome<98, Safari, Node.js<17.0.0 // Support: Firefox<94, Chrome<98, Safari, Node.js<17.0.0