-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: fix fs.promises.writeFile with typed arrays
Before this change, only the first part of typed arrays which have more than 1 byte per element (e.g. Uint16Array) would be written. This also removes the use of the `slice` method to avoid unnecessary copying the data. Fixes: #35343 PR-URL: #35376 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
1 parent
70ad69b
commit 99a79e3
Showing
2 changed files
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const fsPromises = fs.promises; | ||
const path = require('path'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('assert'); | ||
const tmpDir = tmpdir.path; | ||
|
||
tmpdir.refresh(); | ||
|
||
const dest = path.resolve(tmpDir, 'tmp.txt'); | ||
// Use a file size larger than `kReadFileMaxChunkSize`. | ||
const buffer = Buffer.from('012'.repeat(2 ** 14)); | ||
|
||
(async () => { | ||
for (const Constructor of [Uint8Array, Uint16Array, Uint32Array]) { | ||
const array = new Constructor(buffer.buffer); | ||
await fsPromises.writeFile(dest, array); | ||
const data = await fsPromises.readFile(dest); | ||
assert.deepStrictEqual(data, buffer); | ||
} | ||
})().then(common.mustCall()); |