Skip to content

Commit 991aca3

Browse files
lholmquisttargos
authored andcommitted
fs: make parameters optional for readSync
This makes the offset, length and position parameters optional by passing in an options object. PR-URL: #32460 Reviewed-By: Anna Henningsen <[email protected]>
1 parent fcfde57 commit 991aca3

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

doc/api/fs.md

+26
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,32 @@ Returns the number of `bytesRead`.
30803080
For detailed information, see the documentation of the asynchronous version of
30813081
this API: [`fs.read()`][].
30823082

3083+
## `fs.readSync(fd, buffer, [options])`
3084+
<!-- YAML
3085+
added: REPLACEME
3086+
changes:
3087+
- version: REPLACEME
3088+
pr-url: https://github.com/nodejs/node/pull/32460
3089+
description: Options object can be passed in
3090+
to make offset, length and position optional
3091+
-->
3092+
3093+
* `fd` {integer}
3094+
* `buffer` {Buffer|TypedArray|DataView}
3095+
* `options` {Object}
3096+
* `offset` {integer} **Default:** `0`
3097+
* `length` {integer} **Default:** `buffer.length`
3098+
* `position` {integer} **Default:** `null`
3099+
* Returns: {number}
3100+
3101+
Returns the number of `bytesRead`.
3102+
3103+
Similar to the above `fs.readSync` function, this version takes an optional `options` object.
3104+
If no `options` object is specified, it will default with the above values.
3105+
3106+
For detailed information, see the documentation of the asynchronous version of
3107+
this API: [`fs.read()`][].
3108+
30833109
## `fs.readv(fd, buffers[, position], callback)`
30843110
<!-- YAML
30853111
added: REPLACEME

lib/fs.js

+12
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,20 @@ function read(fd, buffer, offset, length, position, callback) {
534534
ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
535535
{ value: ['bytesRead', 'buffer'], enumerable: false });
536536

537+
// usage:
538+
// fs.readSync(fd, buffer, offset, length, position);
539+
// OR
540+
// fs.readSync(fd, buffer, {}) or fs.readSync(fd, buffer)
537541
function readSync(fd, buffer, offset, length, position) {
538542
validateInt32(fd, 'fd', 0);
543+
544+
if (arguments.length <= 3) {
545+
// Assume fs.read(fd, buffer, options)
546+
const options = offset || {};
547+
548+
({ offset = 0, length = buffer.length, position } = options);
549+
}
550+
539551
validateBuffer(buffer);
540552

541553
if (offset == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const fs = require('fs');
6+
const assert = require('assert');
7+
const filepath = fixtures.path('x.txt');
8+
const fd = fs.openSync(filepath, 'r');
9+
10+
const expected = Buffer.from('xyz\n');
11+
12+
function runTest(defaultBuffer, options) {
13+
const result = fs.readSync(fd, defaultBuffer, options);
14+
assert.strictEqual(result, expected.length);
15+
assert.deepStrictEqual(defaultBuffer, expected);
16+
}
17+
18+
// Test passing in an empty options object
19+
runTest(Buffer.allocUnsafe(expected.length), { position: 0 });
20+
21+
// Test not passing in any options object
22+
runTest(Buffer.allocUnsafe(expected.length));
23+
24+
// Test passing in options
25+
runTest(Buffer.allocUnsafe(expected.length), { offset: 0,
26+
length: expected.length,
27+
position: 0 });

0 commit comments

Comments
 (0)