Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: clarify fd behaviour with {read,write}File #23706

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2547,10 +2547,13 @@ fs.readFile('<directory>', (err, data) => {
});
```

Any specified file descriptor has to support reading.

If a file descriptor is specified as the `path`, it will not be closed
### File Descriptors
1. Any specified file descriptor has to support reading.
2. If a file descriptor is specified as the `path`, it will not be closed
automatically.
3. The reading will begin at the current position. If the file size is
Copy link
Member

@ChALkeR ChALkeR Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify that the If ... is an example?
Something like E.g., if ... or For example, if ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

10 bytes and if six bytes are already read with this file descriptor, then
`readFile` will return only the rest of the four bytes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
`readFile` will return only the rest of the four bytes.
`readFile()` will return only the rest of the four bytes.


The `fs.readFile()` function buffers the entire file. To minimize memory costs,
when possible prefer streaming via `fs.createReadStream()`.
Expand Down Expand Up @@ -3540,14 +3543,21 @@ If `options` is a string, then it specifies the encoding:
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
```

Any specified file descriptor has to support writing.
### File Descriptors
1. Any specified file descriptor has to support writing.
2. If a file descriptor is specified as the `file`, it will not be closed
automatically.
3. The writing will begin at the beginning of the file. If the file size
is 10 bytes and if six bytes are written with this file descriptor, then
`writeFile` will return six bytes newly written and four bytes from the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit confusing as writeFileSync() returns undefined and writeFile() transfers only error to the calback. Maybe something like this this?

Suggested change
`writeFile` will return six bytes newly written and four bytes from the file.
the file contents would be six bytes newly written and four bytes from the file.

For example, if the file already had `'Hello World'` and the newly written
content is `'Aloha'`, then the contents of the file would be `'Aloha World'`,
rather than just `'Aloha'`.

It is unsafe to use `fs.writeFile()` multiple times on the same file without
waiting for the callback. For this scenario, [`fs.createWriteStream()`][] is
recommended.

If a file descriptor is specified as the `file`, it will not be closed
automatically.

## fs.writeFileSync(file, data[, options])
<!-- YAML
Expand Down