From c91f09e3cd640def5148eefec33d2749184bf489 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Tue, 24 Mar 2020 11:23:33 -0400 Subject: [PATCH 1/4] fs: Make parameters optional for readSync * This makes the offset, length and position parameters optional by passing in an options object. --- lib/fs.js | 14 ++++++++ .../test-fs-readSync-optional-params.js | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/parallel/test-fs-readSync-optional-params.js diff --git a/lib/fs.js b/lib/fs.js index 4bf2b81b008dd3..4cb242b05439dd 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -533,8 +533,22 @@ function read(fd, buffer, offset, length, position, callback) { ObjectDefineProperty(read, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffer'], enumerable: false }); +// usage: +// fs.readSync(fd, buffer, offset, length, position); +// OR +// fs.readSync(fd, buffer, {}) or fs.readSync(fd, buffer) function readSync(fd, buffer, offset, length, position) { validateInt32(fd, 'fd', 0); + + if (arguments.length <= 3) { + // Assume fs.read(fd, buffer, options) + const options = offset || {}; + + offset = options.offset || 0; + length = options.length || buffer.length; + position = options.position; + } + validateBuffer(buffer); if (offset == null) { diff --git a/test/parallel/test-fs-readSync-optional-params.js b/test/parallel/test-fs-readSync-optional-params.js new file mode 100644 index 00000000000000..afe777902c2bed --- /dev/null +++ b/test/parallel/test-fs-readSync-optional-params.js @@ -0,0 +1,34 @@ +'use strict'; + +require('../common'); +const fixtures = require('../common/fixtures'); +const fs = require('fs'); +const assert = require('assert'); +const filepath = fixtures.path('x.txt'); +const fd = fs.openSync(filepath, 'r'); + +const expected = Buffer.from('xyz\n'); +const defaultBuffer1 = Buffer.allocUnsafe(expected.length); +const defaultBuffer2 = Buffer.allocUnsafe(expected.length); +const defaultBuffer3 = Buffer.allocUnsafe(expected.length); + +// Test passing in an empty options object +const result1 = fs.readSync(fd, defaultBuffer1, { position: 0 }); +assert.strictEqual(result1, expected.length); +assert.deepStrictEqual(defaultBuffer1, expected); + +// Test not passing in any options object +const result2 = fs.readSync(fd, defaultBuffer2); +assert.strictEqual(result2, expected.length); +assert.deepStrictEqual(defaultBuffer2, expected); + + +// Test passing in options +const result3 = fs.readSync(fd, defaultBuffer3, { + offset: 0, + lenght: defaultBuffer3.length, + position: 0 +}); + +assert.strictEqual(result3, expected.length); +assert.deepStrictEqual(defaultBuffer3, expected); From 7115ba958faf3e3cef021a4d28540e547489df23 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Tue, 24 Mar 2020 12:13:38 -0400 Subject: [PATCH 2/4] squash: fix typo --- test/parallel/test-fs-readSync-optional-params.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-readSync-optional-params.js b/test/parallel/test-fs-readSync-optional-params.js index afe777902c2bed..60cbc4aaf3e203 100644 --- a/test/parallel/test-fs-readSync-optional-params.js +++ b/test/parallel/test-fs-readSync-optional-params.js @@ -26,7 +26,7 @@ assert.deepStrictEqual(defaultBuffer2, expected); // Test passing in options const result3 = fs.readSync(fd, defaultBuffer3, { offset: 0, - lenght: defaultBuffer3.length, + length: defaultBuffer3.length, position: 0 }); From c2bb51bd281941d947d29ccfdb743dbd9651e0b5 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Wed, 25 Mar 2020 12:54:12 -0400 Subject: [PATCH 3/4] squash: fix up tests --- lib/fs.js | 4 +-- .../test-fs-readSync-optional-params.js | 29 +++++++------------ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4cb242b05439dd..b0581927514888 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -544,9 +544,7 @@ function readSync(fd, buffer, offset, length, position) { // Assume fs.read(fd, buffer, options) const options = offset || {}; - offset = options.offset || 0; - length = options.length || buffer.length; - position = options.position; + ({ offset = 0, length = buffer.length, position } = options); } validateBuffer(buffer); diff --git a/test/parallel/test-fs-readSync-optional-params.js b/test/parallel/test-fs-readSync-optional-params.js index 60cbc4aaf3e203..37d3d24911db51 100644 --- a/test/parallel/test-fs-readSync-optional-params.js +++ b/test/parallel/test-fs-readSync-optional-params.js @@ -8,27 +8,20 @@ const filepath = fixtures.path('x.txt'); const fd = fs.openSync(filepath, 'r'); const expected = Buffer.from('xyz\n'); -const defaultBuffer1 = Buffer.allocUnsafe(expected.length); -const defaultBuffer2 = Buffer.allocUnsafe(expected.length); -const defaultBuffer3 = Buffer.allocUnsafe(expected.length); + +function runTest(defaultBuffer, options) { + const result = fs.readSync(fd, defaultBuffer, options); + assert.strictEqual(result, expected.length); + assert.deepStrictEqual(defaultBuffer, expected); +} // Test passing in an empty options object -const result1 = fs.readSync(fd, defaultBuffer1, { position: 0 }); -assert.strictEqual(result1, expected.length); -assert.deepStrictEqual(defaultBuffer1, expected); +runTest(Buffer.allocUnsafe(expected.length), { position: 0 }); // Test not passing in any options object -const result2 = fs.readSync(fd, defaultBuffer2); -assert.strictEqual(result2, expected.length); -assert.deepStrictEqual(defaultBuffer2, expected); - +runTest(Buffer.allocUnsafe(expected.length)); // Test passing in options -const result3 = fs.readSync(fd, defaultBuffer3, { - offset: 0, - length: defaultBuffer3.length, - position: 0 -}); - -assert.strictEqual(result3, expected.length); -assert.deepStrictEqual(defaultBuffer3, expected); +runTest(Buffer.allocUnsafe(expected.length), { offset: 0, + length: expected.length, + position: 0 }); From 7f68e77636959639efd9157bf5fc30b6d2eb8fb9 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Thu, 26 Mar 2020 09:22:50 -0400 Subject: [PATCH 4/4] squash: update docs --- doc/api/fs.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index 5e6cfb31786c6d..dc511268880543 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3069,6 +3069,32 @@ Returns the number of `bytesRead`. For detailed information, see the documentation of the asynchronous version of this API: [`fs.read()`][]. +## `fs.readSync(fd, buffer, [options])` + + +* `fd` {integer} +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.length` + * `position` {integer} **Default:** `null` +* Returns: {number} + +Returns the number of `bytesRead`. + +Similar to the above `fs.readSync` function, this version takes an optional `options` object. +If no `options` object is specified, it will default with the above values. + +For detailed information, see the documentation of the asynchronous version of +this API: [`fs.read()`][]. + ## `fs.readv(fd, buffers[, position], callback)`