From c3236428e02b00a416858e9f76ce287923595266 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Fri, 1 Dec 2023 03:45:10 -0300 Subject: [PATCH] fs: use default w flag for writeFileSync with utf8 encoding --- lib/fs.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 8761ab4f9f00a2..e1a959bae4804d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2343,6 +2343,8 @@ function writeFileSync(path, data, options) { validateBoolean(flush, 'options.flush'); + const flag = options.flag || 'w'; + // C++ fast path for string data and UTF8 encoding if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) { if (!isInt32(path)) { @@ -2351,7 +2353,7 @@ function writeFileSync(path, data, options) { return binding.writeFileUtf8( path, data, - stringToFlags(options.flag), + stringToFlags(flag), parseFileMode(options.mode, 'mode', 0o666), ); } @@ -2361,8 +2363,6 @@ function writeFileSync(path, data, options) { data = Buffer.from(data, options.encoding || 'utf8'); } - const flag = options.flag || 'w'; - const isUserFd = isFd(path); // File descriptor ownership const fd = isUserFd ? path : fs.openSync(path, flag, options.mode);