Skip to content

Commit

Permalink
Add fs.exists[util.promisify.custom] (#8936)
Browse files Browse the repository at this point in the history
* Add fs.exists[util.promisify.custom]

fs.exists doesn't follow the error-first-callback convention, so it
needs a custom implementation for util.promisify.

* Simplify

---------

Co-authored-by: John-David Dalton <john.david.dalton@gmail.com>
  • Loading branch information
dmitri-gb and jdalton authored Feb 17, 2024
1 parent 27eed54 commit fe8ec29
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/js/node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ var access = function access(...args) {
// TODO: make symbols a separate export somewhere
var kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");

exists[kCustomPromisifiedSymbol] = path => new Promise(resolve => exists(path, resolve));

read[kCustomPromisifiedSymbol] = async function (fd, bufferOrOptions, ...rest) {
const { isArrayBufferView } = require("node:util/types");
let buffer;
Expand Down
8 changes: 8 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,14 @@ describe("fs.exists", () => {
}
});
});
it("should work with util.promisify when path exists", async () => {
const fsexists = promisify(fs.exists);
expect(await fsexists(import.meta.path)).toBe(true);
});
it("should work with util.promisify when path doesn't exist", async () => {
const fsexists = promisify(fs.exists);
expect(await fsexists(`${tmpdir()}/test-fs-exists-${Date.now()}`)).toBe(false);
});
});

describe("rm", () => {
Expand Down

0 comments on commit fe8ec29

Please # to comment.