| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2343,6 +2343,8 @@ function writeFileSync(path, data, options) { | |||
| 2343 | 2343 | ||
| 2344 | 2344 | validateBoolean(flush, 'options.flush'); | |
| 2345 | 2345 | ||
| 2346 | + const flag = options.flag || 'w'; | ||
| 2347 | + | ||
| 2346 | 2348 | // C++ fast path for string data and UTF8 encoding | |
| 2347 | 2349 | if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) { | |
| 2348 | 2350 | if (!isInt32(path)) { | |
@@ -2351,7 +2353,7 @@ function writeFileSync(path, data, options) { | |||
| 2351 | 2353 | ||
| 2352 | 2354 | return binding.writeFileUtf8( | |
| 2353 | 2355 | path, data, | |
| 2354 | - stringToFlags(options.flag), | ||
| 2356 | + stringToFlags(flag), | ||
| 2355 | 2357 | parseFileMode(options.mode, 'mode', 0o666), | |
| 2356 | 2358 | ); | |
| 2357 | 2359 | } | |
@@ -2361,8 +2363,6 @@ function writeFileSync(path, data, options) { | |||
| 2361 | 2363 | data = Buffer.from(data, options.encoding || 'utf8'); | |
| 2362 | 2364 | } | |
| 2363 | 2365 | ||
| 2364 | - const flag = options.flag || 'w'; | ||
| 2365 | - | ||
| 2366 | 2366 | const isUserFd = isFd(path); // File descriptor ownership | |
| 2367 | 2367 | const fd = isUserFd ? path : fs.openSync(path, flag, options.mode); | |
| 2368 | 2368 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,6 +101,22 @@ tmpdir.refresh(); | |||
| 101 | 101 | assert.strictEqual(content, 'hello world!'); | |
| 102 | 102 | } | |
| 103 | 103 | ||
| 104 | + // Test writeFileSync with no flags | ||
| 105 | + { | ||
| 106 | + const utf8Data = 'hello world!'; | ||
| 107 | + for (const test of [ | ||
| 108 | + { data: utf8Data }, | ||
| 109 | + { data: utf8Data, options: { encoding: 'utf8' } }, | ||
| 110 | + { data: Buffer.from(utf8Data, 'utf8').toString('hex'), options: { encoding: 'hex' } }, | ||
| 111 | + ]) { | ||
| 112 | + const file = tmpdir.resolve(`testWriteFileSyncNewFile_${Math.random()}.txt`); | ||
| 113 | + fs.writeFileSync(file, test.data, test.options); | ||
| 114 | + | ||
| 115 | + const content = fs.readFileSync(file, { encoding: 'utf-8' }); | ||
| 116 | + assert.strictEqual(content, utf8Data); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + | ||
| 104 | 120 | // Test writeFileSync with an invalid input | |
| 105 | 121 | { | |
| 106 | 122 | const file = tmpdir.resolve('testWriteFileSyncInvalid.txt'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments