Skip to content

Commit

Permalink
fs: change default value of position in read and readSync
Browse files Browse the repository at this point in the history
Since the docs mention that `position` in `read` and `readSync` should
be an `integer` or a `bigint`, the functions should rather accept what
is actually fed into `read`, i.e., `-1` instead of any nullish value.
  • Loading branch information
RaisinTen committed Mar 31, 2021
1 parent 01d8b39 commit 446bdb6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
12 changes: 9 additions & 3 deletions doc/api/fs.md
Expand Up @@ -2704,6 +2704,9 @@ directory and subsequent read operations.
<!-- YAML
added: v0.0.2
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37101
description: Runtime description.
- version: v10.10.0
pr-url: https://github.com/nodejs/node/pull/22150
description: The `buffer` parameter can now be any `TypedArray`, or a
Expand All @@ -2722,8 +2725,8 @@ changes:
* `offset` {integer} The position in `buffer` to write the data to.
* `length` {integer} The number of bytes to read.
* `position` {integer|bigint} Specifies where to begin reading from in the
file. If `position` is `null` or `-1 `, data will be read from the current
file position, and the file position will be updated. If `position` is an
file. If `position` is `-1 `, data will be read from the current file
position, and the file position will be updated. If `position` is any other
integer, the file position will be unchanged.
* `callback` {Function}
* `err` {Error}
Expand All @@ -2746,6 +2749,9 @@ added:
- v13.11.0
- v12.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37101
description: Runtime description.
- version:
- v13.11.0
- v12.17.0
Expand All @@ -2758,7 +2764,7 @@ changes:
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
* `offset` {integer} **Default:** `0`
* `length` {integer} **Default:** `buffer.length`
* `position` {integer|bigint} **Default:** `null`
* `position` {integer|bigint} **Default:** `-1`
* `callback` {Function}
* `err` {Error}
* `bytesRead` {integer}
Expand Down
22 changes: 18 additions & 4 deletions lib/fs.js
Expand Up @@ -534,7 +534,7 @@ function read(fd, buffer, offset, length, position, callback) {
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.length,
position
position = -1,
} = options);
}

Expand Down Expand Up @@ -562,8 +562,13 @@ function read(fd, buffer, offset, length, position, callback) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (position == null)
if (position === null) {
process.emitWarning(
`The provided ${position} is not a valid position, and is supported ` +
'in the fs module solely for compatibility.',
'DeprecationWarning', 'DEP01149');
position = -1;
}

validatePosition(position, 'position');

Expand Down Expand Up @@ -592,7 +597,11 @@ function readSync(fd, buffer, offset, length, position) {
// Assume fs.read(fd, buffer, options)
const options = offset || {};

({ offset = 0, length = buffer.length, position } = options);
({
offset = 0,
length = buffer.length,
position = -1,
} = options);
}

validateBuffer(buffer);
Expand All @@ -616,8 +625,13 @@ function readSync(fd, buffer, offset, length, position) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (position == null)
if (position === null) {
process.emitWarning(
`The provided ${position} is not a valid position, and is supported ` +
'in the fs module solely for compatibility.',
'DeprecationWarning', 'DEP0149');
position = -1;
}

validatePosition(position, 'position');

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-promisified.js
Expand Up @@ -11,7 +11,7 @@ const exists = promisify(fs.exists);

{
const fd = fs.openSync(__filename, 'r');
read(fd, Buffer.alloc(1024), 0, 1024, null).then(common.mustCall((obj) => {
read(fd, Buffer.alloc(1024), 0, 1024, -1).then(common.mustCall((obj) => {
assert.strictEqual(typeof obj.bytesRead, 'number');
assert(obj.buffer instanceof Buffer);
fs.closeSync(fd);
Expand Down
30 changes: 29 additions & 1 deletion test/parallel/test-fs-read-type.js
Expand Up @@ -8,7 +8,6 @@ const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');
const expected = 'xyz\n';


// Error must be thrown with string
assert.throws(
() => fs.read(fd, expected.length, 0, 'utf-8', common.mustNotCall()),
Expand Down Expand Up @@ -90,6 +89,21 @@ assert.throws(() => {
});
});

{
fs.read(fd,
Buffer.allocUnsafe(expected.length),
0,
expected.length,
null,
common.mustCall());
common.expectWarning(
'DeprecationWarning',
'The provided null is not a valid position, and is supported ' +
'in the fs module solely for compatibility.',
'DEP01149',
);
}

[0.5, 2 ** 53, 2n ** 63n].forEach((value) => {
assert.throws(() => {
fs.read(fd,
Expand Down Expand Up @@ -210,6 +224,20 @@ assert.throws(() => {
});
});

{
fs.readSync(fd,
Buffer.allocUnsafe(expected.length),
0,
expected.length,
null);
common.expectWarning(
'DeprecationWarning',
'The provided null is not a valid position, and is supported ' +
'in the fs module solely for compatibility.',
'DEP01149',
);
}

[0.5, 2 ** 53, 2n ** 63n].forEach((value) => {
assert.throws(() => {
fs.readSync(fd,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-readfile-fd.js
Expand Up @@ -79,7 +79,7 @@ function tempFdSync(callback) {
const buf = Buffer.alloc(5);

// Read only five bytes, so that the position moves to five.
fs.read(fd, buf, 0, 5, null, common.mustSucceed((bytes) => {
fs.read(fd, buf, 0, 5, -1, common.mustSucceed((bytes) => {
assert.strictEqual(bytes, 5);
assert.deepStrictEqual(buf.toString(), 'Hello');

Expand Down

0 comments on commit 446bdb6

Please sign in to comment.