Merge pull request #18959 from Snuffleupagus/Node-20
[api-minor] Update the minimum supported Node.js version to 20, and only support the Fetch API for "remote" PDF documents in Node.js
This commit is contained in:
commit
cefd1ebcd2
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node-version: [18, lts/*, 22, 23]
|
node-version: [20, 22, 23]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
|
|||||||
@ -84,7 +84,7 @@ const ENV_TARGETS = [
|
|||||||
"Chrome >= 103",
|
"Chrome >= 103",
|
||||||
"Firefox ESR",
|
"Firefox ESR",
|
||||||
"Safari >= 16.4",
|
"Safari >= 16.4",
|
||||||
"Node >= 18",
|
"Node >= 20",
|
||||||
"> 1%",
|
"> 1%",
|
||||||
"not IE > 0",
|
"not IE > 0",
|
||||||
"not dead",
|
"not dead",
|
||||||
@ -2271,7 +2271,7 @@ function packageJson() {
|
|||||||
url: `git+${DIST_GIT_URL}`,
|
url: `git+${DIST_GIT_URL}`,
|
||||||
},
|
},
|
||||||
engines: {
|
engines: {
|
||||||
node: ">=18",
|
node: ">=20",
|
||||||
},
|
},
|
||||||
scripts: {},
|
scripts: {},
|
||||||
};
|
};
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@ -64,7 +64,7 @@
|
|||||||
"yargs": "^17.7.2"
|
"yargs": "^17.7.2"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18"
|
"node": ">=20"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@aashutoshrathi/word-wrap": {
|
"node_modules/@aashutoshrathi/word-wrap": {
|
||||||
|
|||||||
@ -63,7 +63,7 @@
|
|||||||
"url": "git://github.com/mozilla/pdf.js.git"
|
"url": "git://github.com/mozilla/pdf.js.git"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18"
|
"node": ">=20"
|
||||||
},
|
},
|
||||||
"license": "Apache-2.0"
|
"license": "Apache-2.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,7 +56,6 @@ import {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
NodePackages,
|
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
} from "display-node_utils";
|
} from "display-node_utils";
|
||||||
import { CanvasGraphics } from "./canvas.js";
|
import { CanvasGraphics } from "./canvas.js";
|
||||||
@ -451,15 +450,20 @@ function getDocument(src = {}) {
|
|||||||
PDFJSDev.test("GENERIC") &&
|
PDFJSDev.test("GENERIC") &&
|
||||||
isNodeJS
|
isNodeJS
|
||||||
) {
|
) {
|
||||||
const isFetchSupported =
|
if (isValidFetchUrl(url)) {
|
||||||
typeof fetch !== "undefined" &&
|
if (
|
||||||
typeof Response !== "undefined" &&
|
typeof fetch === "undefined" ||
|
||||||
"body" in Response.prototype;
|
typeof Response === "undefined" ||
|
||||||
|
!("body" in Response.prototype)
|
||||||
NetworkStream =
|
) {
|
||||||
isFetchSupported && isValidFetchUrl(url)
|
throw new Error(
|
||||||
? PDFFetchStream
|
"getDocument - the Fetch API was disabled in Node.js, see `--no-experimental-fetch`."
|
||||||
: PDFNodeStream;
|
);
|
||||||
|
}
|
||||||
|
NetworkStream = PDFFetchStream;
|
||||||
|
} else {
|
||||||
|
NetworkStream = PDFNodeStream;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
NetworkStream = isValidFetchUrl(url)
|
NetworkStream = isValidFetchUrl(url)
|
||||||
? PDFFetchStream
|
? PDFFetchStream
|
||||||
@ -2137,14 +2141,6 @@ class PDFWorker {
|
|||||||
* @type {Promise<void>}
|
* @type {Promise<void>}
|
||||||
*/
|
*/
|
||||||
get promise() {
|
get promise() {
|
||||||
if (
|
|
||||||
typeof PDFJSDev !== "undefined" &&
|
|
||||||
PDFJSDev.test("GENERIC") &&
|
|
||||||
isNodeJS
|
|
||||||
) {
|
|
||||||
// Ensure that all Node.js packages/polyfills have loaded.
|
|
||||||
return Promise.all([NodePackages.promise, this._readyCapability.promise]);
|
|
||||||
}
|
|
||||||
return this._readyCapability.promise;
|
return this._readyCapability.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,14 +12,9 @@
|
|||||||
* 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 process */
|
||||||
|
|
||||||
import { AbortException, assert, MissingPDFException } from "../shared/util.js";
|
import { AbortException, assert, MissingPDFException } from "../shared/util.js";
|
||||||
import {
|
|
||||||
createHeaders,
|
|
||||||
extractFilenameFromHeader,
|
|
||||||
validateRangeRequestCapabilities,
|
|
||||||
} from "./network_utils.js";
|
|
||||||
import { NodePackages } from "./node_utils.js";
|
|
||||||
|
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -33,28 +28,18 @@ function parseUrlOrPath(sourceUrl) {
|
|||||||
if (urlRegex.test(sourceUrl)) {
|
if (urlRegex.test(sourceUrl)) {
|
||||||
return new URL(sourceUrl);
|
return new URL(sourceUrl);
|
||||||
}
|
}
|
||||||
const url = NodePackages.get("url");
|
const url = process.getBuiltinModule("url");
|
||||||
return new URL(url.pathToFileURL(sourceUrl));
|
return new URL(url.pathToFileURL(sourceUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRequest(url, headers, callback) {
|
|
||||||
if (url.protocol === "http:") {
|
|
||||||
const http = NodePackages.get("http");
|
|
||||||
return http.request(url, { headers }, callback);
|
|
||||||
}
|
|
||||||
const https = NodePackages.get("https");
|
|
||||||
return https.request(url, { headers }, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
class PDFNodeStream {
|
class PDFNodeStream {
|
||||||
constructor(source) {
|
constructor(source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.url = parseUrlOrPath(source.url);
|
this.url = parseUrlOrPath(source.url);
|
||||||
this.isHttp =
|
assert(
|
||||||
this.url.protocol === "http:" || this.url.protocol === "https:";
|
this.url.protocol === "file:",
|
||||||
// Check if url refers to filesystem.
|
"PDFNodeStream only supports file:// URLs."
|
||||||
this.isFsUrl = this.url.protocol === "file:";
|
);
|
||||||
this.headers = createHeaders(this.isHttp, source.httpHeaders);
|
|
||||||
|
|
||||||
this._fullRequestReader = null;
|
this._fullRequestReader = null;
|
||||||
this._rangeRequestReaders = [];
|
this._rangeRequestReaders = [];
|
||||||
@ -69,9 +54,7 @@ class PDFNodeStream {
|
|||||||
!this._fullRequestReader,
|
!this._fullRequestReader,
|
||||||
"PDFNodeStream.getFullReader can only be called once."
|
"PDFNodeStream.getFullReader can only be called once."
|
||||||
);
|
);
|
||||||
this._fullRequestReader = this.isFsUrl
|
this._fullRequestReader = new PDFNodeStreamFsFullReader(this);
|
||||||
? new PDFNodeStreamFsFullReader(this)
|
|
||||||
: new PDFNodeStreamFullReader(this);
|
|
||||||
return this._fullRequestReader;
|
return this._fullRequestReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,9 +62,7 @@ class PDFNodeStream {
|
|||||||
if (end <= this._progressiveDataLength) {
|
if (end <= this._progressiveDataLength) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const rangeReader = this.isFsUrl
|
const rangeReader = new PDFNodeStreamFsRangeReader(this, start, end);
|
||||||
? new PDFNodeStreamFsRangeReader(this, start, end)
|
|
||||||
: new PDFNodeStreamRangeReader(this, start, end);
|
|
||||||
this._rangeRequestReaders.push(rangeReader);
|
this._rangeRequestReaders.push(rangeReader);
|
||||||
return rangeReader;
|
return rangeReader;
|
||||||
}
|
}
|
||||||
@ -95,7 +76,7 @@ class PDFNodeStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BaseFullReader {
|
class PDFNodeStreamFsFullReader {
|
||||||
constructor(stream) {
|
constructor(stream) {
|
||||||
this._url = stream.url;
|
this._url = stream.url;
|
||||||
this._done = false;
|
this._done = false;
|
||||||
@ -118,6 +99,24 @@ class BaseFullReader {
|
|||||||
this._readableStream = null;
|
this._readableStream = null;
|
||||||
this._readCapability = Promise.withResolvers();
|
this._readCapability = Promise.withResolvers();
|
||||||
this._headersCapability = Promise.withResolvers();
|
this._headersCapability = Promise.withResolvers();
|
||||||
|
|
||||||
|
const fs = process.getBuiltinModule("fs");
|
||||||
|
fs.promises.lstat(this._url).then(
|
||||||
|
stat => {
|
||||||
|
// Setting right content length.
|
||||||
|
this._contentLength = stat.size;
|
||||||
|
|
||||||
|
this._setReadableStream(fs.createReadStream(this._url));
|
||||||
|
this._headersCapability.resolve();
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
if (error.code === "ENOENT") {
|
||||||
|
error = new MissingPDFException(`Missing PDF "${this._url}".`);
|
||||||
|
}
|
||||||
|
this._storedError = error;
|
||||||
|
this._headersCapability.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
get headersReady() {
|
get headersReady() {
|
||||||
@ -210,8 +209,8 @@ class BaseFullReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BaseRangeReader {
|
class PDFNodeStreamFsRangeReader {
|
||||||
constructor(stream) {
|
constructor(stream, start, end) {
|
||||||
this._url = stream.url;
|
this._url = stream.url;
|
||||||
this._done = false;
|
this._done = false;
|
||||||
this._storedError = null;
|
this._storedError = null;
|
||||||
@ -221,6 +220,11 @@ class BaseRangeReader {
|
|||||||
this._readCapability = Promise.withResolvers();
|
this._readCapability = Promise.withResolvers();
|
||||||
const source = stream.source;
|
const source = stream.source;
|
||||||
this._isStreamingSupported = !source.disableStream;
|
this._isStreamingSupported = !source.disableStream;
|
||||||
|
|
||||||
|
const fs = process.getBuiltinModule("fs");
|
||||||
|
this._setReadableStream(
|
||||||
|
fs.createReadStream(this._url, { start, end: end - 1 })
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
get isStreamingSupported() {
|
get isStreamingSupported() {
|
||||||
@ -288,112 +292,4 @@ class BaseRangeReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PDFNodeStreamFullReader extends BaseFullReader {
|
|
||||||
constructor(stream) {
|
|
||||||
super(stream);
|
|
||||||
|
|
||||||
// Node.js requires the `headers` to be a regular Object.
|
|
||||||
const headers = Object.fromEntries(stream.headers);
|
|
||||||
|
|
||||||
const handleResponse = response => {
|
|
||||||
if (response.statusCode === 404) {
|
|
||||||
const error = new MissingPDFException(`Missing PDF "${this._url}".`);
|
|
||||||
this._storedError = error;
|
|
||||||
this._headersCapability.reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this._headersCapability.resolve();
|
|
||||||
this._setReadableStream(response);
|
|
||||||
|
|
||||||
const responseHeaders = new Headers(this._readableStream.headers);
|
|
||||||
|
|
||||||
const { allowRangeRequests, suggestedLength } =
|
|
||||||
validateRangeRequestCapabilities({
|
|
||||||
responseHeaders,
|
|
||||||
isHttp: stream.isHttp,
|
|
||||||
rangeChunkSize: this._rangeChunkSize,
|
|
||||||
disableRange: this._disableRange,
|
|
||||||
});
|
|
||||||
|
|
||||||
this._isRangeSupported = allowRangeRequests;
|
|
||||||
// Setting right content length.
|
|
||||||
this._contentLength = suggestedLength || this._contentLength;
|
|
||||||
|
|
||||||
this._filename = extractFilenameFromHeader(responseHeaders);
|
|
||||||
};
|
|
||||||
|
|
||||||
this._request = createRequest(this._url, headers, handleResponse);
|
|
||||||
|
|
||||||
this._request.on("error", reason => {
|
|
||||||
this._storedError = reason;
|
|
||||||
this._headersCapability.reject(reason);
|
|
||||||
});
|
|
||||||
// Note: `request.end(data)` is used to write `data` to request body
|
|
||||||
// and notify end of request. But one should always call `request.end()`
|
|
||||||
// even if there is no data to write -- (to notify the end of request).
|
|
||||||
this._request.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class PDFNodeStreamRangeReader extends BaseRangeReader {
|
|
||||||
constructor(stream, start, end) {
|
|
||||||
super(stream);
|
|
||||||
|
|
||||||
// Node.js requires the `headers` to be a regular Object.
|
|
||||||
const headers = Object.fromEntries(stream.headers);
|
|
||||||
headers.Range = `bytes=${start}-${end - 1}`;
|
|
||||||
|
|
||||||
const handleResponse = response => {
|
|
||||||
if (response.statusCode === 404) {
|
|
||||||
const error = new MissingPDFException(`Missing PDF "${this._url}".`);
|
|
||||||
this._storedError = error;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this._setReadableStream(response);
|
|
||||||
};
|
|
||||||
|
|
||||||
this._request = createRequest(this._url, headers, handleResponse);
|
|
||||||
|
|
||||||
this._request.on("error", reason => {
|
|
||||||
this._storedError = reason;
|
|
||||||
});
|
|
||||||
this._request.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class PDFNodeStreamFsFullReader extends BaseFullReader {
|
|
||||||
constructor(stream) {
|
|
||||||
super(stream);
|
|
||||||
|
|
||||||
const fs = NodePackages.get("fs");
|
|
||||||
fs.promises.lstat(this._url).then(
|
|
||||||
stat => {
|
|
||||||
// Setting right content length.
|
|
||||||
this._contentLength = stat.size;
|
|
||||||
|
|
||||||
this._setReadableStream(fs.createReadStream(this._url));
|
|
||||||
this._headersCapability.resolve();
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
if (error.code === "ENOENT") {
|
|
||||||
error = new MissingPDFException(`Missing PDF "${this._url}".`);
|
|
||||||
}
|
|
||||||
this._storedError = error;
|
|
||||||
this._headersCapability.reject(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class PDFNodeStreamFsRangeReader extends BaseRangeReader {
|
|
||||||
constructor(stream, start, end) {
|
|
||||||
super(stream);
|
|
||||||
|
|
||||||
const fs = NodePackages.get("fs");
|
|
||||||
this._setReadableStream(
|
|
||||||
fs.createReadStream(this._url, { start, end: end - 1 })
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export { PDFNodeStream };
|
export { PDFNodeStream };
|
||||||
|
|||||||
@ -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 process */
|
||||||
|
|
||||||
import { isNodeJS, warn } from "../shared/util.js";
|
import { isNodeJS, warn } from "../shared/util.js";
|
||||||
import { BaseCanvasFactory } from "./canvas_factory.js";
|
import { BaseCanvasFactory } from "./canvas_factory.js";
|
||||||
@ -25,94 +26,63 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNodeJS) {
|
if (
|
||||||
// eslint-disable-next-line no-var
|
typeof PDFJSDev !== "undefined" &&
|
||||||
var packageCapability = Promise.withResolvers();
|
!PDFJSDev.test("SKIP_BABEL") &&
|
||||||
// eslint-disable-next-line no-var
|
isNodeJS
|
||||||
var packageMap = null;
|
) {
|
||||||
|
let canvas, path2d;
|
||||||
|
try {
|
||||||
|
const require = process
|
||||||
|
.getBuiltinModule("module")
|
||||||
|
.createRequire(import.meta.url);
|
||||||
|
|
||||||
const loadPackages = async () => {
|
try {
|
||||||
// Native packages.
|
canvas = require("canvas");
|
||||||
const fs = await __non_webpack_import__("fs"),
|
} catch (ex) {
|
||||||
http = await __non_webpack_import__("http"),
|
warn(`Cannot load "canvas" package: "${ex}".`);
|
||||||
https = await __non_webpack_import__("https"),
|
|
||||||
url = await __non_webpack_import__("url");
|
|
||||||
|
|
||||||
// Optional, third-party, packages.
|
|
||||||
let canvas, path2d;
|
|
||||||
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
|
|
||||||
try {
|
|
||||||
canvas = await __non_webpack_import__("canvas");
|
|
||||||
} catch {}
|
|
||||||
try {
|
|
||||||
path2d = await __non_webpack_import__("path2d");
|
|
||||||
} catch {}
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
return new Map(Object.entries({ fs, http, https, url, canvas, path2d }));
|
path2d = require("path2d");
|
||||||
};
|
} catch (ex) {
|
||||||
|
warn(`Cannot load "path2d" package: "${ex}".`);
|
||||||
loadPackages().then(
|
|
||||||
map => {
|
|
||||||
packageMap = map;
|
|
||||||
packageCapability.resolve();
|
|
||||||
|
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!globalThis.DOMMatrix) {
|
|
||||||
const DOMMatrix = map.get("canvas")?.DOMMatrix;
|
|
||||||
|
|
||||||
if (DOMMatrix) {
|
|
||||||
globalThis.DOMMatrix = DOMMatrix;
|
|
||||||
} else {
|
|
||||||
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!globalThis.Path2D) {
|
|
||||||
const CanvasRenderingContext2D =
|
|
||||||
map.get("canvas")?.CanvasRenderingContext2D;
|
|
||||||
const applyPath2DToCanvasRenderingContext =
|
|
||||||
map.get("path2d")?.applyPath2DToCanvasRenderingContext;
|
|
||||||
const Path2D = map.get("path2d")?.Path2D;
|
|
||||||
|
|
||||||
if (
|
|
||||||
CanvasRenderingContext2D &&
|
|
||||||
applyPath2DToCanvasRenderingContext &&
|
|
||||||
Path2D
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
|
|
||||||
} catch (ex) {
|
|
||||||
warn(`applyPath2DToCanvasRenderingContext: "${ex}".`);
|
|
||||||
}
|
|
||||||
globalThis.Path2D = Path2D;
|
|
||||||
} else {
|
|
||||||
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
reason => {
|
|
||||||
warn(`loadPackages: ${reason}`);
|
|
||||||
|
|
||||||
packageMap = new Map();
|
|
||||||
packageCapability.resolve();
|
|
||||||
}
|
}
|
||||||
);
|
} catch {}
|
||||||
}
|
|
||||||
|
|
||||||
class NodePackages {
|
if (!globalThis.DOMMatrix) {
|
||||||
static get promise() {
|
const DOMMatrix = canvas?.DOMMatrix;
|
||||||
return packageCapability.promise;
|
|
||||||
|
if (DOMMatrix) {
|
||||||
|
globalThis.DOMMatrix = DOMMatrix;
|
||||||
|
} else {
|
||||||
|
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!globalThis.Path2D) {
|
||||||
|
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
|
||||||
|
const applyPath2DToCanvasRenderingContext =
|
||||||
|
path2d?.applyPath2DToCanvasRenderingContext;
|
||||||
|
const Path2D = path2d?.Path2D;
|
||||||
|
|
||||||
static get(name) {
|
if (
|
||||||
return packageMap?.get(name);
|
CanvasRenderingContext2D &&
|
||||||
|
applyPath2DToCanvasRenderingContext &&
|
||||||
|
Path2D
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
|
||||||
|
} catch (ex) {
|
||||||
|
warn(`applyPath2DToCanvasRenderingContext: "${ex}".`);
|
||||||
|
}
|
||||||
|
globalThis.Path2D = Path2D;
|
||||||
|
} else {
|
||||||
|
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchData(url) {
|
async function fetchData(url) {
|
||||||
const fs = NodePackages.get("fs");
|
const fs = process.getBuiltinModule("fs");
|
||||||
const data = await fs.promises.readFile(url);
|
const data = await fs.promises.readFile(url);
|
||||||
return new Uint8Array(data);
|
return new Uint8Array(data);
|
||||||
}
|
}
|
||||||
@ -124,7 +94,10 @@ class NodeCanvasFactory extends BaseCanvasFactory {
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
_createCanvas(width, height) {
|
_createCanvas(width, height) {
|
||||||
const canvas = NodePackages.get("canvas");
|
const require = process
|
||||||
|
.getBuiltinModule("module")
|
||||||
|
.createRequire(import.meta.url);
|
||||||
|
const canvas = require("canvas");
|
||||||
return canvas.createCanvas(width, height);
|
return canvas.createCanvas(width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +125,5 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
NodePackages,
|
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -18,7 +18,6 @@ const DOMStandardFontDataFactory = null;
|
|||||||
const NodeCanvasFactory = null;
|
const NodeCanvasFactory = null;
|
||||||
const NodeCMapReaderFactory = null;
|
const NodeCMapReaderFactory = null;
|
||||||
const NodeFilterFactory = null;
|
const NodeFilterFactory = null;
|
||||||
const NodePackages = null;
|
|
||||||
const NodeStandardFontDataFactory = null;
|
const NodeStandardFontDataFactory = null;
|
||||||
const PDFFetchStream = null;
|
const PDFFetchStream = null;
|
||||||
const PDFNetworkStream = null;
|
const PDFNetworkStream = null;
|
||||||
@ -30,7 +29,6 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
NodePackages,
|
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
PDFFetchStream,
|
PDFFetchStream,
|
||||||
PDFNetworkStream,
|
PDFNetworkStream,
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import {
|
|||||||
setVerbosityLevel,
|
setVerbosityLevel,
|
||||||
VerbosityLevel,
|
VerbosityLevel,
|
||||||
} from "../../src/shared/util.js";
|
} from "../../src/shared/util.js";
|
||||||
import { NodePackages } from "../../src/display/node_utils.js";
|
|
||||||
|
|
||||||
// Sets longer timeout, similar to `jasmine-boot.js`.
|
// Sets longer timeout, similar to `jasmine-boot.js`.
|
||||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
||||||
@ -30,9 +29,6 @@ if (!isNodeJS) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that all Node.js packages/polyfills have loaded.
|
|
||||||
await NodePackages.promise;
|
|
||||||
|
|
||||||
// Reduce the amount of console "spam", by ignoring `info`/`warn` calls,
|
// Reduce the amount of console "spam", by ignoring `info`/`warn` calls,
|
||||||
// when running the unit-tests in Node.js/Travis.
|
// when running the unit-tests in Node.js/Travis.
|
||||||
setVerbosityLevel(VerbosityLevel.ERRORS);
|
setVerbosityLevel(VerbosityLevel.ERRORS);
|
||||||
|
|||||||
@ -14,7 +14,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { AbortException, isNodeJS } from "../../src/shared/util.js";
|
import { AbortException, isNodeJS } from "../../src/shared/util.js";
|
||||||
import { createTemporaryNodeServer } from "./test_utils.js";
|
|
||||||
import { PDFNodeStream } from "../../src/display/node_stream.js";
|
import { PDFNodeStream } from "../../src/display/node_stream.js";
|
||||||
|
|
||||||
// Ensure that these tests only run in Node.js environments.
|
// Ensure that these tests only run in Node.js environments.
|
||||||
@ -24,98 +23,49 @@ if (!isNodeJS) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = await __non_webpack_import__("url");
|
|
||||||
|
|
||||||
describe("node_stream", function () {
|
describe("node_stream", function () {
|
||||||
let tempServer = null;
|
const url = process.getBuiltinModule("url");
|
||||||
|
|
||||||
const cwdURL = url.pathToFileURL(process.cwd()) + "/";
|
const cwdURL = url.pathToFileURL(process.cwd()) + "/";
|
||||||
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href;
|
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href;
|
||||||
const pdfLength = 1016315;
|
const pdfLength = 1016315;
|
||||||
|
|
||||||
beforeAll(function () {
|
it("read filesystem pdf files", async function () {
|
||||||
tempServer = createTemporaryNodeServer();
|
const stream = new PDFNodeStream({
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(function () {
|
|
||||||
// Close the server from accepting new connections after all test finishes.
|
|
||||||
const { server } = tempServer;
|
|
||||||
server.close();
|
|
||||||
|
|
||||||
tempServer = null;
|
|
||||||
});
|
|
||||||
|
|
||||||
it("read both http(s) and filesystem pdf files", async function () {
|
|
||||||
const stream1 = new PDFNodeStream({
|
|
||||||
url: `http://127.0.0.1:${tempServer.port}/tracemonkey.pdf`,
|
|
||||||
rangeChunkSize: 65536,
|
|
||||||
disableStream: true,
|
|
||||||
disableRange: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const stream2 = new PDFNodeStream({
|
|
||||||
url: pdf,
|
url: pdf,
|
||||||
rangeChunkSize: 65536,
|
rangeChunkSize: 65536,
|
||||||
disableStream: true,
|
disableStream: true,
|
||||||
disableRange: true,
|
disableRange: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const fullReader1 = stream1.getFullReader();
|
const fullReader = stream.getFullReader();
|
||||||
const fullReader2 = stream2.getFullReader();
|
|
||||||
|
|
||||||
let isStreamingSupported1, isRangeSupported1;
|
let isStreamingSupported, isRangeSupported;
|
||||||
const promise1 = fullReader1.headersReady.then(() => {
|
const promise = fullReader.headersReady.then(() => {
|
||||||
isStreamingSupported1 = fullReader1.isStreamingSupported;
|
isStreamingSupported = fullReader.isStreamingSupported;
|
||||||
isRangeSupported1 = fullReader1.isRangeSupported;
|
isRangeSupported = fullReader.isRangeSupported;
|
||||||
});
|
});
|
||||||
|
|
||||||
let isStreamingSupported2, isRangeSupported2;
|
let len = 0;
|
||||||
const promise2 = fullReader2.headersReady.then(() => {
|
const read = function () {
|
||||||
isStreamingSupported2 = fullReader2.isStreamingSupported;
|
return fullReader.read().then(function (result) {
|
||||||
isRangeSupported2 = fullReader2.isRangeSupported;
|
|
||||||
});
|
|
||||||
|
|
||||||
let len1 = 0,
|
|
||||||
len2 = 0;
|
|
||||||
const read1 = function () {
|
|
||||||
return fullReader1.read().then(function (result) {
|
|
||||||
if (result.done) {
|
if (result.done) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
len1 += result.value.byteLength;
|
len += result.value.byteLength;
|
||||||
return read1();
|
return read();
|
||||||
});
|
|
||||||
};
|
|
||||||
const read2 = function () {
|
|
||||||
return fullReader2.read().then(function (result) {
|
|
||||||
if (result.done) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
len2 += result.value.byteLength;
|
|
||||||
return read2();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
await Promise.all([read1(), read2(), promise1, promise2]);
|
await Promise.all([read(), promise]);
|
||||||
|
|
||||||
expect(isStreamingSupported1).toEqual(false);
|
expect(isStreamingSupported).toEqual(false);
|
||||||
expect(isRangeSupported1).toEqual(false);
|
expect(isRangeSupported).toEqual(false);
|
||||||
expect(isStreamingSupported2).toEqual(false);
|
expect(len).toEqual(pdfLength);
|
||||||
expect(isRangeSupported2).toEqual(false);
|
|
||||||
expect(len1).toEqual(pdfLength);
|
|
||||||
expect(len1).toEqual(len2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("read custom ranges for both http(s) and filesystem urls", async function () {
|
it("read custom ranges for filesystem urls", async function () {
|
||||||
const rangeSize = 32768;
|
const rangeSize = 32768;
|
||||||
const stream1 = new PDFNodeStream({
|
const stream = new PDFNodeStream({
|
||||||
url: `http://127.0.0.1:${tempServer.port}/tracemonkey.pdf`,
|
|
||||||
length: pdfLength,
|
|
||||||
rangeChunkSize: rangeSize,
|
|
||||||
disableStream: true,
|
|
||||||
disableRange: false,
|
|
||||||
});
|
|
||||||
const stream2 = new PDFNodeStream({
|
|
||||||
url: pdf,
|
url: pdf,
|
||||||
length: pdfLength,
|
length: pdfLength,
|
||||||
rangeChunkSize: rangeSize,
|
rangeChunkSize: rangeSize,
|
||||||
@ -123,53 +73,28 @@ describe("node_stream", function () {
|
|||||||
disableRange: false,
|
disableRange: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const fullReader1 = stream1.getFullReader();
|
const fullReader = stream.getFullReader();
|
||||||
const fullReader2 = stream2.getFullReader();
|
|
||||||
|
|
||||||
let isStreamingSupported1, isRangeSupported1, fullReaderCancelled1;
|
let isStreamingSupported, isRangeSupported, fullReaderCancelled;
|
||||||
let isStreamingSupported2, isRangeSupported2, fullReaderCancelled2;
|
const promise = fullReader.headersReady.then(function () {
|
||||||
|
isStreamingSupported = fullReader.isStreamingSupported;
|
||||||
const promise1 = fullReader1.headersReady.then(function () {
|
isRangeSupported = fullReader.isRangeSupported;
|
||||||
isStreamingSupported1 = fullReader1.isStreamingSupported;
|
|
||||||
isRangeSupported1 = fullReader1.isRangeSupported;
|
|
||||||
// we shall be able to close the full reader without issues
|
// we shall be able to close the full reader without issues
|
||||||
fullReader1.cancel(new AbortException("Don't need fullReader1."));
|
fullReader.cancel(new AbortException("Don't need fullReader."));
|
||||||
fullReaderCancelled1 = true;
|
fullReaderCancelled = true;
|
||||||
});
|
|
||||||
|
|
||||||
const promise2 = fullReader2.headersReady.then(function () {
|
|
||||||
isStreamingSupported2 = fullReader2.isStreamingSupported;
|
|
||||||
isRangeSupported2 = fullReader2.isRangeSupported;
|
|
||||||
fullReader2.cancel(new AbortException("Don't need fullReader2."));
|
|
||||||
fullReaderCancelled2 = true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Skipping fullReader results, requesting something from the PDF end.
|
// Skipping fullReader results, requesting something from the PDF end.
|
||||||
const tailSize = pdfLength % rangeSize || rangeSize;
|
const tailSize = pdfLength % rangeSize || rangeSize;
|
||||||
|
|
||||||
const range11Reader = stream1.getRangeReader(
|
const range1Reader = stream.getRangeReader(
|
||||||
pdfLength - tailSize - rangeSize,
|
pdfLength - tailSize - rangeSize,
|
||||||
pdfLength - tailSize
|
pdfLength - tailSize
|
||||||
);
|
);
|
||||||
const range12Reader = stream1.getRangeReader(
|
const range2Reader = stream.getRangeReader(pdfLength - tailSize, pdfLength);
|
||||||
pdfLength - tailSize,
|
|
||||||
pdfLength
|
|
||||||
);
|
|
||||||
|
|
||||||
const range21Reader = stream2.getRangeReader(
|
|
||||||
pdfLength - tailSize - rangeSize,
|
|
||||||
pdfLength - tailSize
|
|
||||||
);
|
|
||||||
const range22Reader = stream2.getRangeReader(
|
|
||||||
pdfLength - tailSize,
|
|
||||||
pdfLength
|
|
||||||
);
|
|
||||||
|
|
||||||
const result11 = { value: 0 },
|
|
||||||
result12 = { value: 0 };
|
|
||||||
const result21 = { value: 0 },
|
|
||||||
result22 = { value: 0 };
|
|
||||||
|
|
||||||
|
const result1 = { value: 0 },
|
||||||
|
result2 = { value: 0 };
|
||||||
const read = function (reader, lenResult) {
|
const read = function (reader, lenResult) {
|
||||||
return reader.read().then(function (result) {
|
return reader.read().then(function (result) {
|
||||||
if (result.done) {
|
if (result.done) {
|
||||||
@ -181,23 +106,15 @@ describe("node_stream", function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
read(range11Reader, result11),
|
read(range1Reader, result1),
|
||||||
read(range12Reader, result12),
|
read(range2Reader, result2),
|
||||||
read(range21Reader, result21),
|
promise,
|
||||||
read(range22Reader, result22),
|
|
||||||
promise1,
|
|
||||||
promise2,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(result11.value).toEqual(rangeSize);
|
expect(result1.value).toEqual(rangeSize);
|
||||||
expect(result12.value).toEqual(tailSize);
|
expect(result2.value).toEqual(tailSize);
|
||||||
expect(result21.value).toEqual(rangeSize);
|
expect(isStreamingSupported).toEqual(false);
|
||||||
expect(result22.value).toEqual(tailSize);
|
expect(isRangeSupported).toEqual(true);
|
||||||
expect(isStreamingSupported1).toEqual(false);
|
expect(fullReaderCancelled).toEqual(true);
|
||||||
expect(isRangeSupported1).toEqual(true);
|
|
||||||
expect(fullReaderCancelled1).toEqual(true);
|
|
||||||
expect(isStreamingSupported2).toEqual(false);
|
|
||||||
expect(isRangeSupported2).toEqual(true);
|
|
||||||
expect(fullReaderCancelled2).toEqual(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -20,13 +20,6 @@ import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js";
|
|||||||
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
|
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
|
||||||
import { Ref } from "../../src/core/primitives.js";
|
import { Ref } from "../../src/core/primitives.js";
|
||||||
|
|
||||||
let fs, http;
|
|
||||||
if (isNodeJS) {
|
|
||||||
// Native packages.
|
|
||||||
fs = await __non_webpack_import__("fs");
|
|
||||||
http = await __non_webpack_import__("http");
|
|
||||||
}
|
|
||||||
|
|
||||||
const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
|
const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
|
||||||
|
|
||||||
const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/";
|
const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/";
|
||||||
@ -132,6 +125,8 @@ function createIdFactory(pageIndex) {
|
|||||||
function createTemporaryNodeServer() {
|
function createTemporaryNodeServer() {
|
||||||
assert(isNodeJS, "Should only be used in Node.js environments.");
|
assert(isNodeJS, "Should only be used in Node.js environments.");
|
||||||
|
|
||||||
|
const fs = process.getBuiltinModule("fs"),
|
||||||
|
http = process.getBuiltinModule("http");
|
||||||
// Create http server to serve pdf data for tests.
|
// Create http server to serve pdf data for tests.
|
||||||
const server = http
|
const server = http
|
||||||
.createServer((request, response) => {
|
.createServer((request, response) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user