mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-09 08:35:47 +02:00
Merge pull request #21007 from Snuffleupagus/Node-getReadableStream-simplify
[Node.js] Remove the `node-readable-to-web-readable-stream` polyfill
This commit is contained in:
commit
d1c3d3938e
@ -2487,7 +2487,6 @@ function packageJson() {
|
|||||||
license: DIST_LICENSE,
|
license: DIST_LICENSE,
|
||||||
optionalDependencies: {
|
optionalDependencies: {
|
||||||
"@napi-rs/canvas": "^0.1.96",
|
"@napi-rs/canvas": "^0.1.96",
|
||||||
"node-readable-to-web-readable-stream": "^0.4.2",
|
|
||||||
},
|
},
|
||||||
browser: {
|
browser: {
|
||||||
canvas: false,
|
canvas: false,
|
||||||
|
|||||||
8
package-lock.json
generated
8
package-lock.json
generated
@ -50,7 +50,6 @@
|
|||||||
"jstransformer-nunjucks": "^1.2.0",
|
"jstransformer-nunjucks": "^1.2.0",
|
||||||
"metalsmith": "^2.7.0",
|
"metalsmith": "^2.7.0",
|
||||||
"metalsmith-html-relative": "^2.0.9",
|
"metalsmith-html-relative": "^2.0.9",
|
||||||
"node-readable-to-web-readable-stream": "^0.4.2",
|
|
||||||
"ordered-read-streams": "^2.0.0",
|
"ordered-read-streams": "^2.0.0",
|
||||||
"pngjs": "^7.0.0",
|
"pngjs": "^7.0.0",
|
||||||
"postcss": "^8.5.8",
|
"postcss": "^8.5.8",
|
||||||
@ -8679,13 +8678,6 @@
|
|||||||
"node": ">= 0.4.0"
|
"node": ">= 0.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/node-readable-to-web-readable-stream": {
|
|
||||||
"version": "0.4.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-readable-to-web-readable-stream/-/node-readable-to-web-readable-stream-0.4.2.tgz",
|
|
||||||
"integrity": "sha512-/cMZNI34v//jUTrI+UIo4ieHAB5EZRY/+7OmXZgBxaWBMcW2tGdceIw06RFxWxrKZ5Jp3sI2i5TsRo+CBhtVLQ==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/node-releases": {
|
"node_modules/node-releases": {
|
||||||
"version": "2.0.36",
|
"version": "2.0.36",
|
||||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.36.tgz",
|
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.36.tgz",
|
||||||
|
|||||||
@ -45,7 +45,6 @@
|
|||||||
"jstransformer-nunjucks": "^1.2.0",
|
"jstransformer-nunjucks": "^1.2.0",
|
||||||
"metalsmith": "^2.7.0",
|
"metalsmith": "^2.7.0",
|
||||||
"metalsmith-html-relative": "^2.0.9",
|
"metalsmith-html-relative": "^2.0.9",
|
||||||
"node-readable-to-web-readable-stream": "^0.4.2",
|
|
||||||
"ordered-read-streams": "^2.0.0",
|
"ordered-read-streams": "^2.0.0",
|
||||||
"pngjs": "^7.0.0",
|
"pngjs": "^7.0.0",
|
||||||
"postcss": "^8.5.8",
|
"postcss": "^8.5.8",
|
||||||
|
|||||||
@ -29,20 +29,12 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getReadableStream(readStream) {
|
function getReadableStream(url, opts = null) {
|
||||||
|
const fs = process.getBuiltinModule("fs");
|
||||||
const { Readable } = process.getBuiltinModule("stream");
|
const { Readable } = process.getBuiltinModule("stream");
|
||||||
|
|
||||||
if (typeof Readable.toWeb === "function") {
|
const readStream = fs.createReadStream(url, opts);
|
||||||
// See https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options
|
return Readable.toWeb(readStream);
|
||||||
return Readable.toWeb(readStream);
|
|
||||||
}
|
|
||||||
// Fallback to support Node.js versions older than `24.0.0` and `22.17.0`.
|
|
||||||
const require = process
|
|
||||||
.getBuiltinModule("module")
|
|
||||||
.createRequire(import.meta.url);
|
|
||||||
|
|
||||||
const polyfill = require("node-readable-to-web-readable-stream");
|
|
||||||
return polyfill.makeDefaultReadableStreamFromNodeReadable(readStream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class PDFNodeStream extends BasePDFStream {
|
class PDFNodeStream extends BasePDFStream {
|
||||||
@ -66,13 +58,10 @@ class PDFNodeStreamReader extends BasePDFStreamReader {
|
|||||||
|
|
||||||
this._isStreamingSupported = !disableStream;
|
this._isStreamingSupported = !disableStream;
|
||||||
|
|
||||||
const fs = process.getBuiltinModule("fs");
|
const fs = process.getBuiltinModule("fs/promises");
|
||||||
fs.promises
|
fs.lstat(url)
|
||||||
.lstat(url)
|
|
||||||
.then(stat => {
|
.then(stat => {
|
||||||
const readStream = fs.createReadStream(url);
|
const readableStream = getReadableStream(url);
|
||||||
const readableStream = getReadableStream(readStream);
|
|
||||||
|
|
||||||
this._reader = readableStream.getReader();
|
this._reader = readableStream.getReader();
|
||||||
|
|
||||||
const { size } = stat;
|
const { size } = stat;
|
||||||
@ -123,14 +112,11 @@ class PDFNodeStreamRangeReader extends BasePDFStreamRangeReader {
|
|||||||
super(stream, begin, end);
|
super(stream, begin, end);
|
||||||
const { url } = stream._source;
|
const { url } = stream._source;
|
||||||
|
|
||||||
const fs = process.getBuiltinModule("fs");
|
|
||||||
try {
|
try {
|
||||||
const readStream = fs.createReadStream(url, {
|
const readableStream = getReadableStream(url, {
|
||||||
start: begin,
|
start: begin,
|
||||||
end: end - 1,
|
end: end - 1,
|
||||||
});
|
});
|
||||||
const readableStream = getReadableStream(readStream);
|
|
||||||
|
|
||||||
this._reader = readableStream.getReader();
|
this._reader = readableStream.getReader();
|
||||||
|
|
||||||
this._readCapability.resolve();
|
this._readCapability.resolve();
|
||||||
|
|||||||
@ -76,8 +76,8 @@ if (isNodeJS) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchData(url) {
|
async function fetchData(url) {
|
||||||
const fs = process.getBuiltinModule("fs");
|
const fs = process.getBuiltinModule("fs/promises");
|
||||||
const data = await fs.promises.readFile(url);
|
const data = await fs.readFile(url);
|
||||||
return new Uint8Array(data);
|
return new Uint8Array(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { bidi } from "../../src/core/bidi.js";
|
import { bidi } from "../../src/core/bidi.js";
|
||||||
|
import { fetchData } from "../../src/display/display_utils.js";
|
||||||
import { isNodeJS } from "../../src/shared/util.js";
|
import { isNodeJS } from "../../src/shared/util.js";
|
||||||
|
|
||||||
const BIDI_TEST_DATA_PATH = isNodeJS ? "./test/bidi/" : "../bidi/";
|
const BIDI_TEST_DATA_PATH = isNodeJS ? "./test/bidi/" : "../bidi/";
|
||||||
@ -21,11 +22,10 @@ const BIDI_TEST_DATA_PATH = isNodeJS ? "./test/bidi/" : "../bidi/";
|
|||||||
async function readTestFile(filename) {
|
async function readTestFile(filename) {
|
||||||
const path = BIDI_TEST_DATA_PATH + filename;
|
const path = BIDI_TEST_DATA_PATH + filename;
|
||||||
if (isNodeJS) {
|
if (isNodeJS) {
|
||||||
const fs = process.getBuiltinModule("fs");
|
const fs = process.getBuiltinModule("fs/promises");
|
||||||
return fs.promises.readFile(path, "utf8");
|
return fs.readFile(path, "utf8");
|
||||||
}
|
}
|
||||||
const response = await fetch(new URL(path, window.location));
|
return fetchData(new URL(path, window.location), /* type = */ "text");
|
||||||
return response.text();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unicode Bidirectional Algorithm tests.
|
// Unicode Bidirectional Algorithm tests.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user