mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
fs: use default w flag for writeFileSync with utf8 encoding
PR-URL: https://github.com/nodejs/node/pull/50990 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
This commit is contained in:
1 parent
23031d9b0a
commit
7bfb087727
2 files changed
+19
-3
No files matched your search
@@ -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);
|
||||
|
||||
|
||||
@@ -101,6 +101,22 @@ tmpdir.refresh();
|
||||
assert.strictEqual(content, 'hello world!');
|
||||
}
|
||||
|
||||
// Test writeFileSync with no flags
|
||||
{
|
||||
const utf8Data = 'hello world!';
|
||||
for (const test of [
|
||||
{ data: utf8Data },
|
||||
{ data: utf8Data, options: { encoding: 'utf8' } },
|
||||
{ data: Buffer.from(utf8Data, 'utf8').toString('hex'), options: { encoding: 'hex' } },
|
||||
]) {
|
||||
const file = tmpdir.resolve(`testWriteFileSyncNewFile_${Math.random()}.txt`);
|
||||
fs.writeFileSync(file, test.data, test.options);
|
||||
|
||||
const content = fs.readFileSync(file, { encoding: 'utf-8' });
|
||||
assert.strictEqual(content, utf8Data);
|
||||
}
|
||||
}
|
||||
|
||||
// Test writeFileSync with an invalid input
|
||||
{
|
||||
const file = tmpdir.resolve('testWriteFileSyncInvalid.txt');
|
||||
|
||||
Reference in new issue
Block a user