Use fs/promises in the Node.js-specific code in the src/-folder

This is available in all Node.js versions that we currently support, and using it allows us to remove callback-functions; please see https://nodejs.org/docs/latest-v18.x/api/fs.html#promises-api
This commit is contained in:
Jonas Jenwald 2024-02-22 16:50:13 +01:00
parent 72b8b29147
commit db2849cc17
2 changed files with 11 additions and 18 deletions

View File

@ -424,21 +424,22 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, ""); path = path.replace(/^\//, "");
} }
fs.lstat(path, (error, stat) => { fs.promises.lstat(path).then(
if (error) { stat => {
// Setting right content length.
this._contentLength = stat.size;
this._setReadableStream(fs.createReadStream(path));
this._headersCapability.resolve();
},
error => {
if (error.code === "ENOENT") { if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${path}".`); error = new MissingPDFException(`Missing PDF "${path}".`);
} }
this._storedError = error; this._storedError = error;
this._headersCapability.reject(error); this._headersCapability.reject(error);
return;
} }
// Setting right content length. );
this._contentLength = stat.size;
this._setReadableStream(fs.createReadStream(path));
this._headersCapability.resolve();
});
} }
} }

View File

@ -71,15 +71,7 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
} }
const fetchData = function (url) { const fetchData = function (url) {
return new Promise((resolve, reject) => { return fs.promises.readFile(url).then(data => new Uint8Array(data));
fs.readFile(url, (error, data) => {
if (error || !data) {
reject(new Error(error));
return;
}
resolve(new Uint8Array(data));
});
});
}; };
class NodeFilterFactory extends BaseFilterFactory {} class NodeFilterFactory extends BaseFilterFactory {}