Skip to content

Commit fbcb4f5

Browse files
committed
fs: support util.promisify for fs.read/fs.write
PR-URL: #12442 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: William Kapke <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
1 parent e7c5145 commit fbcb4f5

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

doc/api/fs.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,9 @@ If `position` is `null`, data will be read from the current file position.
16121612

16131613
The callback is given the three arguments, `(err, bytesRead, buffer)`.
16141614

1615+
If this method is invoked as its [`util.promisify()`][]ed version, it returns
1616+
a Promise for an object with `bytesRead` and `buffer` properties.
1617+
16151618
## fs.readdir(path[, options], callback)
16161619
<!-- YAML
16171620
added: v0.1.8
@@ -2393,8 +2396,11 @@ an integer specifying the number of bytes to write.
23932396
should be written. If `typeof position !== 'number'`, the data will be written
23942397
at the current position. See pwrite(2).
23952398

2396-
The callback will be given three arguments `(err, written, buffer)` where
2397-
`written` specifies how many _bytes_ were written from `buffer`.
2399+
The callback will be given three arguments `(err, bytesWritten, buffer)` where
2400+
`bytesWritten` specifies how many _bytes_ were written from `buffer`.
2401+
2402+
If this method is invoked as its [`util.promisify()`][]ed version, it returns
2403+
a Promise for an object with `bytesWritten` and `buffer` properties.
23982404

23992405
Note that it is unsafe to use `fs.write` multiple times on the same file
24002406
without waiting for the callback. For this scenario,
@@ -2810,6 +2816,7 @@ The following constants are meant for use with the [`fs.Stats`][] object's
28102816
[`net.Socket`]: net.html#net_class_net_socket
28112817
[`stat()`]: fs.html#fs_fs_stat_path_callback
28122818
[`util.inspect(stats)`]: util.html#util_util_inspect_object_options
2819+
[`util.promisify()`]: util.html#util_util_promisify_original
28132820
[Caveats]: #fs_caveats
28142821
[Common System Errors]: errors.html#errors_common_system_errors
28152822
[FS Constants]: #fs_fs_constants_1

lib/fs.js

+6
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
656656
binding.read(fd, buffer, offset, length, position, req);
657657
};
658658

659+
Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
660+
{ value: ['bytesRead', 'buffer'], enumerable: false });
661+
659662
fs.readSync = function(fd, buffer, offset, length, position) {
660663
if (length === 0) {
661664
return 0;
@@ -706,6 +709,9 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
706709
return binding.writeString(fd, buffer, offset, length, req);
707710
};
708711

712+
Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
713+
{ value: ['bytesWritten', 'buffer'], enumerable: false });
714+
709715
// usage:
710716
// fs.writeSync(fd, buffer[, offset[, length[, position]]]);
711717
// OR

test/parallel/test-fs-promisified.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const { promisify } = require('util');
7+
8+
common.crashOnUnhandledRejection();
9+
10+
const read = promisify(fs.read);
11+
const write = promisify(fs.write);
12+
13+
{
14+
const fd = fs.openSync(__filename, 'r');
15+
read(fd, Buffer.alloc(1024), 0, 1024, null).then(common.mustCall((obj) => {
16+
assert.strictEqual(typeof obj.bytesRead, 'number');
17+
assert(obj.buffer instanceof Buffer);
18+
fs.closeSync(fd);
19+
}));
20+
}
21+
22+
common.refreshTmpDir();
23+
{
24+
const filename = path.join(common.tmpDir, 'write-promise.txt');
25+
const fd = fs.openSync(filename, 'w');
26+
write(fd, Buffer.from('foobar')).then(common.mustCall((obj) => {
27+
assert.strictEqual(typeof obj.bytesWritten, 'number');
28+
assert.strictEqual(obj.buffer.toString(), 'foobar');
29+
fs.closeSync(fd);
30+
}));
31+
}

0 commit comments

Comments
 (0)