Skip to content

Commit b89595f

Browse files
committed
[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.
1 parent e9fd67a commit b89595f

File tree

5 files changed

+19
-25
lines changed

5 files changed

+19
-25
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mo
2424
### Online demo
2525

2626
Please note that the "Modern browsers" version assumes native support for
27-
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining,
28-
nullish coalescing, and private `class` fields/methods.
27+
features such as `async`/`await`, optional chaining, nullish coalescing,
28+
and private `class` fields/methods.
2929

3030
+ Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html
3131

external/dist/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ This is a pre-built version of the PDF.js source code. It is automatically
88
generated by the build scripts.
99

1010
For usage with older browsers or environments, without support for modern
11-
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining,
12-
nullish coalescing, and private `class` fields/methods; please see the `legacy`
13-
folder.
11+
features such as `async`/`await`, optional chaining, nullish coalescing,
12+
and private `class` fields/methods; please see the `legacy/` folder.
1413

1514
See https://github.com/mozilla/pdf.js for learning and contributing.

gulpfile.js

+3
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,9 @@ function packageBowerJson() {
20422042
homepage: DIST_HOMEPAGE,
20432043
bugs: DIST_BUGS_URL,
20442044
license: DIST_LICENSE,
2045+
dependencies: {
2046+
"web-streams-polyfill": "^3.2.0",
2047+
},
20452048
peerDependencies: {
20462049
"worker-loader": "^3.0.8", // Used in `external/dist/webpack.js`.
20472050
},

src/core/worker.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ class WorkerMessageHandler {
133133
// Ensure that (primarily) Node.js users won't accidentally attempt to use
134134
// a non-translated/non-polyfilled build of the library, since that would
135135
// quickly fail anyway because of missing functionality.
136-
if (
137-
(typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) &&
138-
typeof ReadableStream === "undefined"
139-
) {
140-
throw new Error(
136+
if (typeof ReadableStream === "undefined") {
137+
const partialMsg =
141138
"The browser/environment lacks native support for critical " +
142-
"functionality used by the PDF.js library (e.g. `ReadableStream`); " +
143-
"please use a `legacy`-build instead."
144-
);
139+
"functionality used by the PDF.js library (e.g. `ReadableStream`); ";
140+
141+
if (isNodeJS) {
142+
throw new Error(partialMsg + "please use a `legacy`-build instead.");
143+
}
144+
throw new Error(partialMsg + "please update to a supported browser.");
145145
}
146146
}
147147

src/shared/compatibility.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15+
/* globals __non_webpack_require__ */
1516

1617
import { isNodeJS } from "./is_node.js";
1718

@@ -69,21 +70,12 @@ if (
6970

7071
// Support: Node.js
7172
(function checkReadableStream() {
72-
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
73-
// The current image decoders are synchronous, hence `ReadableStream`
74-
// shouldn't need to be polyfilled for the IMAGE_DECODERS build target.
75-
return;
76-
}
77-
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
78-
// Slightly reduce the size of the Chromium-extension, given
79-
// that `ReadableStream` has been supported since Chrome 43.
80-
return;
81-
}
8273
if (globalThis.ReadableStream || !isNodeJS) {
8374
return;
8475
}
85-
globalThis.ReadableStream =
86-
require("web-streams-polyfill/dist/ponyfill.js").ReadableStream;
76+
globalThis.ReadableStream = __non_webpack_require__(
77+
"web-streams-polyfill/dist/ponyfill.js"
78+
).ReadableStream;
8779
})();
8880

8981
// Support: Firefox<94, Chrome<98, Safari, Node.js<17.0.0

0 commit comments

Comments
 (0)