Skip to content

Commit

Permalink
fixup! fixup! fixup! fs: change default value of position in read and…
Browse files Browse the repository at this point in the history
… readSync
  • Loading branch information
RaisinTen committed Jan 29, 2021
1 parent a0cb2fb commit 89f3abf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
12 changes: 12 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,9 @@ this API: [`fs.open()`][].
<!-- 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 Down Expand Up @@ -2970,6 +2973,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 Down Expand Up @@ -3288,6 +3294,9 @@ the link path returned will be passed as a `Buffer` object.
<!-- YAML
added: v0.1.21
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 Down Expand Up @@ -3315,6 +3324,9 @@ added:
- v13.13.0
- v12.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37101
description: Runtime description
- version:
- v13.13.0
- v12.17.0
Expand Down
16 changes: 16 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,14 @@ function read(fd, buffer, offset, length, position = -1, callback) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

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');

function wrapper(err, bytesRead) {
Expand Down Expand Up @@ -602,6 +610,14 @@ function readSync(fd, buffer, offset, length, position = -1) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

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');

const ctx = {};
Expand Down
34 changes: 31 additions & 3 deletions test/parallel/test-fs-read-type.js
Original file line number Diff line number Diff line change
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 @@ -76,7 +75,7 @@ assert.throws(() => {
'It must be >= 0. Received -1'
});

[true, () => {}, {}, '', null].forEach((value) => {
[true, () => {}, {}, ''].forEach((value) => {
assert.throws(() => {
fs.read(fd,
Buffer.allocUnsafe(expected.length),
Expand All @@ -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 @@ -197,7 +211,7 @@ assert.throws(() => {
'It must be <= 4. Received 5'
});

[true, () => {}, {}, '', null].forEach((value) => {
[true, () => {}, {}, ''].forEach((value) => {
assert.throws(() => {
fs.readSync(fd,
Buffer.allocUnsafe(expected.length),
Expand All @@ -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

0 comments on commit 89f3abf

Please sign in to comment.