Skip to content

Commit 9dbf173

Browse files
authoredOct 31, 2022
Add promise support for fs.readv (#970)
1 parent 5623ba3 commit 9dbf173

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
 

‎lib/fs/__tests__/multi-param.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,70 @@ describe('fs.write()', () => {
149149
})
150150
})
151151

152+
describe('fs.readv()', () => {
153+
let TEST_FILE
154+
let TEST_DATA
155+
let TEST_FD
156+
157+
beforeEach(() => {
158+
TEST_FILE = path.join(os.tmpdir(), 'fs-extra', 'readv-test-file')
159+
TEST_DATA = crypto.randomBytes(SIZE)
160+
fs.writeFileSync(TEST_FILE, TEST_DATA)
161+
TEST_FD = fs.openSync(TEST_FILE, 'r')
162+
})
163+
164+
afterEach(() => {
165+
return fs.close(TEST_FD)
166+
.then(() => fs.remove(TEST_FILE))
167+
})
168+
169+
describe('with promises', () => {
170+
it('returns an object', () => {
171+
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
172+
return fs.readv(TEST_FD, bufferArray, 0)
173+
.then(({ bytesRead, buffers }) => {
174+
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
175+
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
176+
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
177+
})
178+
})
179+
180+
it('returns an object when minimal arguments are passed', () => {
181+
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
182+
return fs.readv(TEST_FD, bufferArray)
183+
.then(({ bytesRead, buffers }) => {
184+
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
185+
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
186+
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
187+
})
188+
})
189+
})
190+
191+
describe('with callbacks', () => {
192+
it('works', done => {
193+
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
194+
fs.readv(TEST_FD, bufferArray, 0, (err, bytesRead, buffers) => {
195+
assert.ifError(err)
196+
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
197+
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
198+
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
199+
done()
200+
})
201+
})
202+
203+
it('works when minimal arguments are passed', done => {
204+
const bufferArray = [Buffer.alloc(SIZE / 2), Buffer.alloc(SIZE / 2)]
205+
fs.readv(TEST_FD, bufferArray, (err, bytesRead, buffers) => {
206+
assert.ifError(err)
207+
assert.strictEqual(bytesRead, SIZE, 'bytesRead is correct')
208+
assert.deepStrictEqual(buffers, bufferArray, 'returned data matches mutated input param')
209+
assert.deepStrictEqual(Buffer.concat(buffers), TEST_DATA, 'data is correct')
210+
done()
211+
})
212+
})
213+
})
214+
})
215+
152216
describe('fs.writev()', () => {
153217
let TEST_FILE
154218
let TEST_DATA

‎lib/fs/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ exports.exists = function (filename, callback) {
6565
})
6666
}
6767

68-
// fs.read(), fs.write(), & fs.writev() need special treatment due to multiple callback args
68+
// fs.read(), fs.write(), fs.readv(), & fs.writev() need special treatment due to multiple callback args
6969

7070
exports.read = function (fd, buffer, offset, length, position, callback) {
7171
if (typeof callback === 'function') {
@@ -97,6 +97,22 @@ exports.write = function (fd, buffer, ...args) {
9797
})
9898
}
9999

100+
// Function signature is
101+
// s.readv(fd, buffers[, position], callback)
102+
// We need to handle the optional arg, so we use ...args
103+
exports.readv = function (fd, buffers, ...args) {
104+
if (typeof args[args.length - 1] === 'function') {
105+
return fs.readv(fd, buffers, ...args)
106+
}
107+
108+
return new Promise((resolve, reject) => {
109+
fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {
110+
if (err) return reject(err)
111+
resolve({ bytesRead, buffers })
112+
})
113+
})
114+
}
115+
100116
// Function signature is
101117
// s.writev(fd, buffers[, position], callback)
102118
// We need to handle the optional arg, so we use ...args

0 commit comments

Comments
 (0)
Please sign in to comment.