Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/api/fs.md26
-rw-r--r--lib/fs.js12
-rw-r--r--test/parallel/test-fs-readSync-optional-params.js27
3 files changed, 65 insertions, 0 deletions
diff --git a/doc/api/fs.md b/doc/api/fs.md
index b4f429d6f4e..a0dff1c3008 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])`
+<!-- YAML
+added: REPLACEME
+changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/32460
+ description: Options object can be passed in
+ to make offset, length and position optional
+-->
+
+* `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)`
<!-- YAML
added: REPLACEME
diff --git a/lib/fs.js b/lib/fs.js
index 4bf2b81b008..b0581927514 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -533,8 +533,20 @@ 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 = 0, length = buffer.length, position } = options);
+ }
+
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 00000000000..37d3d24911d
--- /dev/null
+++ b/test/parallel/test-fs-readSync-optional-params.js
@@ -0,0 +1,27 @@
+'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');
+
+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
+runTest(Buffer.allocUnsafe(expected.length), { position: 0 });
+
+// Test not passing in any options object
+runTest(Buffer.allocUnsafe(expected.length));
+
+// Test passing in options
+runTest(Buffer.allocUnsafe(expected.length), { offset: 0,
+ length: expected.length,
+ position: 0 });