From fdb398d716e8b7a7fa900acd5052b7389c13386a Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Wed, 27 May 2020 12:55:15 -0400 Subject: [PATCH 1/2] fs: support util.promisify for fs.readv --- lib/fs.js | 3 +++ test/parallel/test-fs-readv-promisify.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/parallel/test-fs-readv-promisify.js diff --git a/lib/fs.js b/lib/fs.js index 5883a082921c90..8d2a1e044ee08f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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); diff --git a/test/parallel/test-fs-readv-promisify.js b/test/parallel/test-fs-readv-promisify.js new file mode 100644 index 00000000000000..2af418bcc2d12b --- /dev/null +++ b/test/parallel/test-fs-readv-promisify.js @@ -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()); From 311880ce888275acafd328bd65a3361570aebe8d Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Wed, 27 May 2020 12:57:24 -0400 Subject: [PATCH 2/2] squash: doc update --- doc/api/fs.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index ec04d6cf0dd78e..da9348902428a5 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3209,6 +3209,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])`