Skip to content

Commit

Permalink
fs: support util.promisify for fs.readv
Browse files Browse the repository at this point in the history
PR-URL: #33590
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
lholmquist authored and codebytere committed Jun 18, 2020
1 parent cb8b9ec commit b280c86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3191,6 +3191,9 @@ from the current position.
The callback will be given three arguments: `err`, `bytesRead`, and
`buffers`. `bytesRead` is how many bytes were read from the file.

If this method is invoked as its [`util.promisify()`][]ed version, it returns
a `Promise` for an `Object` with `bytesRead` and `buffers` properties.

## `fs.readvSync(fd, buffers[, position])`
<!-- YAML
added: v14.0.0
Expand Down
3 changes: 3 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ function readv(fd, buffers, position, callback) {
return binding.readBuffers(fd, buffers, position, req);
}

ObjectDefineProperty(readv, internalUtil.customPromisifyArgs,
{ value: ['bytesRead', 'buffers'], enumerable: false });

function readvSync(fd, buffers, position) {
validateInt32(fd, 'fd', 0);
validateBufferArray(buffers);
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-fs-readv-promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const fs = require('fs');
const readv = require('util').promisify(fs.readv);
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = [Buffer.from('xyz\n')];

readv(fd, expected)
.then(function({ bytesRead, buffers }) {
assert.deepStrictEqual(bytesRead, expected[0].length);
assert.deepStrictEqual(buffers, expected);
})
.then(common.mustCall());

0 comments on commit b280c86

Please sign in to comment.