-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Changes from 5 commits
351d9df
5370c14
b5a440c
d44bf9f
121e1dd
9975e9a
ef290a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2547,14 +2547,17 @@ 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 | ||
automatically. | ||
|
||
The `fs.readFile()` function buffers the entire file. To minimize memory costs, | ||
when possible prefer streaming via `fs.createReadStream()`. | ||
|
||
### 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 | ||
10 bytes and if six bytes are already read with this file descriptor, then | ||
`readFile()` will return only the rest of the four bytes. | ||
|
||
## fs.readFileSync(path[, options]) | ||
<!-- YAML | ||
added: v0.1.8 | ||
|
@@ -3540,14 +3543,32 @@ 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. | ||
|
||
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 | ||
### 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 current position. Basically, if there are | ||
multiple write calls to a file descriptor and then a `writeFile()` call is | ||
made with the same file descriptor, the data would have been appended. | ||
For example, if we did something like this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this line can be omitted as self-evident. |
||
```js | ||
fs.writeFile(fd, 'Hello', function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFile(fd, 'World', function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
// At this point, file will have `HelloWorld`. | ||
}); | ||
}); | ||
``` | ||
|
||
|
||
## fs.writeFileSync(file, data[, options]) | ||
<!-- YAML | ||
|
@@ -3780,6 +3801,11 @@ returned. | |
|
||
The `FileHandle` has to support reading. | ||
|
||
**Note:** If one or more `filehandle.read()` calls are made on a file handle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We deliberately don't use See #18592. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not official, but there has definitely been an effort to use it much less. In this case, I think It can be dropped and you can just say what you have to say: If one or more `filehandle.read()` calls are made on a file handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. I removed the Note now. |
||
and then a `filehandle.readFile()` call is made, the data will be read from | ||
the current position till the end of the file. It doesn't always read from the | ||
beginning of the file. | ||
|
||
#### filehandle.stat([options]) | ||
<!-- YAML | ||
added: v10.0.0 | ||
|
@@ -3942,6 +3968,11 @@ The `FileHandle` has to support writing. | |
It is unsafe to use `filehandle.writeFile()` multiple times on the same file | ||
without waiting for the `Promise` to be resolved (or rejected). | ||
|
||
**Note:** If one or more `filehandle.write()` calls are made on a file handle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
and then a `filehandle.writeFile()` call is made, the data will be written from | ||
the current position till the end of the file. It doesn't always write from the | ||
beginning of the file. | ||
|
||
### fsPromises.access(path[, mode]) | ||
<!-- YAML | ||
added: v10.0.0 | ||
|
There was a problem hiding this comment.
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 ...
orFor example, if ...
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack.