diff --git a/src/js/node/fs.js b/src/js/node/fs.js index c1fa424fd5..7f24fd8b9c 100644 --- a/src/js/node/fs.js +++ b/src/js/node/fs.js @@ -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); diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 5094633a11..c2b11cecd4 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -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); +});