From 351d9dfae45c04fec8ae7dfeac109cb98764752c Mon Sep 17 00:00:00 2001 From: "Sakthipriyan Vairamani (thefourtheye)" Date: Wed, 17 Oct 2018 12:56:13 +0530 Subject: [PATCH 1/7] doc: clarify fd behaviour with {read,write}File --- doc/api/fs.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index b3af66364b1cbb..2a01de812f20ee 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2547,10 +2547,13 @@ fs.readFile('', (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 +10 bytes and if six bytes are already read with this file descriptor, then +`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()`. @@ -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. +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])