Skip to content

Commit

Permalink
Merge pull request #17714 from Snuffleupagus/Node-fs-promise
Browse files Browse the repository at this point in the history
Use `fs/promises` in the Node.js-specific code in the `src/`-folder
  • Loading branch information
Snuffleupagus authored Mar 12, 2024
2 parents a7d47af + db2849c commit e650b95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,22 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, "");
}

fs.lstat(path, (error, stat) => {
if (error) {
fs.promises.lstat(path).then(
stat => {
// Setting right content length.
this._contentLength = stat.size;

this._setReadableStream(fs.createReadStream(path));
this._headersCapability.resolve();
},
error => {
if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${path}".`);
}
this._storedError = error;
this._headersCapability.reject(error);
return;
}
// Setting right content length.
this._contentLength = stat.size;

this._setReadableStream(fs.createReadStream(path));
this._headersCapability.resolve();
});
);
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
}

const fetchData = function (url) {
return new Promise((resolve, reject) => {
fs.readFile(url, (error, data) => {
if (error || !data) {
reject(new Error(error));
return;
}
resolve(new Uint8Array(data));
});
});
return fs.promises.readFile(url).then(data => new Uint8Array(data));
};

class NodeFilterFactory extends BaseFilterFactory {}
Expand Down

0 comments on commit e650b95

Please sign in to comment.