Skip to content

Commit

Permalink
Allow fs.close with no callback (#10229)
Browse files Browse the repository at this point in the history
* allow fs.close to only take one argument

* add test

* fix tests on windows
  • Loading branch information
gvilums committed Apr 13, 2024
1 parent d785d30 commit 472bd6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/js/node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ var access = function access(...args) {
appendFile = function appendFile(...args) {
callbackify(fs.appendFile, args);
},
close = function close(...args) {
callbackify(fs.close, args);
close = function close(fd, callback) {
if ($isCallable(callback)) {
fs.close(fd).then(() => callback(), callback);
} else if (callback == undefined) {
fs.close(fd).then(() => {});
} else {
const err = new TypeError("Callback must be a function");
err.code = "ERR_INVALID_ARG_TYPE";
throw err;
}
},
rm = function rm(...args) {
callbackify(fs.rm, args);
Expand Down
12 changes: 9 additions & 3 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2973,11 +2973,17 @@ describe.if(isWindows)("windows path handling", () => {
});

it("using writeFile on an fd does not truncate it", () => {
const temp = tmpdir();
const fd = fs.openSync(join(temp, "file.txt"), "w+");
const filepath = join(tmpdir(), `file-${Math.random().toString(32).slice(2)}.txt`);
const fd = fs.openSync(filepath, "w+");
fs.writeFileSync(fd, "x");
fs.writeFileSync(fd, "x");
fs.closeSync(fd);
const content = fs.readFileSync(join(temp, "file.txt"), "utf8");
const content = fs.readFileSync(filepath, "utf8");
expect(content).toBe("xx");
});

it("fs.close with one arg works", () => {
const filepath = join(tmpdir(), `file-${Math.random().toString(32).slice(2)}.txt`);
const fd = fs.openSync(filepath, "w+");
fs.close(fd);
});

0 comments on commit 472bd6c

Please sign in to comment.