You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Open the file descriptor. */
const fd = fs.openSync(__filename, 'r');
/* Read only the first five characters. */
assert.deepStrictEqual(fs.readSync(fd, buffer, 0, 5), 5);
assert.deepStrictEqual(buffer.toString('UTF-8'), '\'use ');
/* As per the docs, both of these should be the same. */
assert.deepStrictEqual(
fs.readFileSync(fd).toString(),
fs.readFileSync(__filename).toString()
);
writeFile
Writes from the beginning of the file, without replacing the contents of the file. For example, if the existing content in the file is ABCD and the newly written data is 12, then the actual contents of the file after both the writeFiles is 12CD.
Its intuitive for people to expect read/write from the current position till the end of the file. (For example, read a header from a file and then decide whether to read the rest of the file or not.)
Make the readFile and writeFile to work from the beginning of the file. (semver-major)
a. If the file descriptor is not seekable, an error will be thrown from libuv.
Variant of 2, if the file descriptor doesn't correspond to a regular file (basically a non-seekable file descriptor), throw an error. Otherwise, try to work from the beginning of the file. (semver-major)
Remove fd support, its just a helper function for a not so oftenly used use-case, and there are valid reasons to support both seeking and non-seeking, so whatever we choose someone will need to write their own wrapper to make functions that work as they expect. Let there be an npm package to do this, or several. (semver-major)
This problem has been discussed multiple times without reaching any conclusion. That is why opening a separate issue to discuss.
Documented Expectations
As per our docs
readFileis defined asand
writeFileis defined asCurrent State of
readFile&writeFile:When a regular file path is passed:
The file is either read from the beginning or completely overwritten. This complies with the documentation.
When a file descriptor is passed:
readFileThe file is read from the current position in the file (the current position is maintained by the file descriptor itself).
readFile problem reproduction
writeFileWrites from the beginning of the file, without replacing the contents of the file. For example, if the existing content in the file is
ABCDand the newly written data is12, then the actual contents of the file after both thewriteFiles is12CD.writeFile problem reproduction
These two behaviours deviate from the documented expectations.
Cases for changing the behaviour to work from the beginning
#9671 (It presents the case with
readFile)Cases for keeping the current behaviour
Its intuitive for people to expect read/write from the current position till the end of the file. (For example, read a header from a file and then decide whether to read the rest of the file or not.)
Proposed courses of action
writeFilealso to write from the current file position just likereadFilereads from the current file position, and be done with it. (semver-major)readFileandwriteFileto work from the beginning of the file. (semver-major)a. If the file descriptor is not seekable, an error will be thrown from libuv.
This is not an exhaustive list. Please feel free to propose more ideas.
PS: This comment by @sam-github summarises my personal expectations from the API.
XXXXXXXXXX+ warn *XXXXXXX91124cc @nodejs/fs
Tagging @nodejs/tsc (Might be a bit early, but this has been discussed before. So tagging to get more inputs)
Tagging @seishun @vsemozhetbyt @sam-github @jorangreef @addaleax from the previous discussions.
If needed we can tag the collaborators to get their expectations.
Previous discussions on this topic happened in #9671, #10809, #10853, #13572,
#22554