Skip to content

Omission in the fs doc #7099

Closed
Closed
@vsemozhetbyt

Description

@vsemozhetbyt
  • Version: 6.2.0
  • Platform: Windows 7
  • Subsystem: doc, fs

Sometimes I use the same file descriptor for writing and then reading by readline (possible case: write some data line by line, then reread it into an array to sort and rewrite).

It seems fs doc lacks for some point concerning these cases.

fs.createReadStream(path[, options]):

options can include start and end values to read a range of bytes from the file instead of the entire file. Both start and end are inclusive and start at 0.

Maybe it should be mentioned that start is set by default to the file end (or to the write position) for fd that has been previously written into.

Test:

const fs = require('fs');
const rl = require('readline');

const file = fs.openSync('test.txt', 'w+');

fs.writeSync(file, '1\n2\n3\n');

rl.createInterface({
  input: fs.createReadStream(null, {fd: file})
}).on('line', line => {
  console.log(line);
}).on('close', () => {
  console.log('Closed');
});

The output is only

Closed
const fs = require('fs');
const rl = require('readline');

const file = fs.openSync('test.txt', 'w+');

fs.writeSync(file, '1\n2\n3\n');

rl.createInterface({
  input: fs.createReadStream(null, {fd: file, start: 0})
}).on('line', line => {
  console.log(line);
}).on('close', () => {
  console.log('Closed');
});

The output contains all the file:

1
2
3
Closed

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions