Skip to content

Commit b6f4901

Browse files
HiroyukiYagihashiMylesBorins
HiroyukiYagihashi
authored andcommitted
fs: add support for async iterators to fsPromises.writeFile
Fixes: #37391 PR-URL: #37490 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e61cc0b commit b6f4901

File tree

4 files changed

+138
-15
lines changed

4 files changed

+138
-15
lines changed

doc/api/fs.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,9 @@ All the [caveats][] for `fs.watch()` also apply to `fsPromises.watch()`.
12411241
<!-- YAML
12421242
added: v10.0.0
12431243
changes:
1244+
- version: REPLACEME
1245+
pr-url: https://github.com/nodejs/node/pull/37490
1246+
description: The `data` argument supports `AsyncIterable`, `Iterable` & `Stream`.
12441247
- version: v15.2.0
12451248
pr-url: https://github.com/nodejs/node/pull/35993
12461249
description: The options argument may include an AbortSignal to abort an
@@ -1256,7 +1259,8 @@ changes:
12561259
-->
12571260
12581261
* `file` {string|Buffer|URL|FileHandle} filename or `FileHandle`
1259-
* `data` {string|Buffer|Uint8Array|Object}
1262+
* `data` {string|Buffer|Uint8Array|Object|AsyncIterable|Iterable
1263+
|Stream}
12601264
* `options` {Object|string}
12611265
* `encoding` {string|null} **Default:** `'utf8'`
12621266
* `mode` {integer} **Default:** `0o666`

lib/internal/fs/promises.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const pathModule = require('path');
7979
const { promisify } = require('internal/util');
8080
const { EventEmitterMixin } = require('internal/event_target');
8181
const { watch } = require('internal/fs/watchers');
82+
const { isIterable } = require('internal/streams/utils');
8283

8384
const kHandle = Symbol('kHandle');
8485
const kFd = Symbol('kFd');
@@ -274,8 +275,18 @@ function checkAborted(signal) {
274275
throw new AbortError();
275276
}
276277

277-
async function writeFileHandle(filehandle, data, signal) {
278-
// `data` could be any kind of typed array.
278+
async function writeFileHandle(filehandle, data, signal, encoding) {
279+
checkAborted(signal);
280+
if (isCustomIterable(data)) {
281+
for await (const buf of data) {
282+
checkAborted(signal);
283+
await write(
284+
filehandle, buf, undefined,
285+
isArrayBufferView(buf) ? buf.length : encoding);
286+
checkAborted(signal);
287+
}
288+
return;
289+
}
279290
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
280291
let remaining = data.length;
281292
if (remaining === 0) return;
@@ -438,7 +449,7 @@ async function readv(handle, buffers, position) {
438449
}
439450

440451
async function write(handle, buffer, offset, length, position) {
441-
if (buffer.length === 0)
452+
if (buffer?.length === 0)
442453
return { bytesWritten: 0, buffer };
443454

444455
if (isArrayBufferView(buffer)) {
@@ -679,20 +690,24 @@ async function writeFile(path, data, options) {
679690
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
680691
const flag = options.flag || 'w';
681692

682-
if (!isArrayBufferView(data)) {
693+
if (!isArrayBufferView(data) && !isCustomIterable(data)) {
683694
validateStringAfterArrayBufferView(data, 'data');
684695
data = Buffer.from(data, options.encoding || 'utf8');
685696
}
686697

687698
validateAbortSignal(options.signal);
688699
if (path instanceof FileHandle)
689-
return writeFileHandle(path, data, options.signal);
700+
return writeFileHandle(path, data, options.signal, options.encoding);
690701

691702
checkAborted(options.signal);
692703

693704
const fd = await open(path, flag, options.mode);
694-
const { signal } = options;
695-
return PromisePrototypeFinally(writeFileHandle(fd, data, signal), fd.close);
705+
return PromisePrototypeFinally(
706+
writeFileHandle(fd, data, options.signal, options.encoding), fd.close);
707+
}
708+
709+
function isCustomIterable(obj) {
710+
return isIterable(obj) && !isArrayBufferView(obj) && typeof obj !== 'string';
696711
}
697712

698713
async function appendFile(path, data, options) {

test/parallel/test-fs-append-file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
121121
}
122122

123123
// Test that appendFile does not accept invalid data type (callback API).
124-
[false, 5, {}, [], null, undefined].forEach(async (data) => {
124+
[false, 5, {}, null, undefined].forEach(async (data) => {
125125
const errObj = {
126126
code: 'ERR_INVALID_ARG_TYPE',
127127
message: /"data"|"buffer"/

test/parallel/test-fs-promises-writefile.js

+110-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,115 @@ const path = require('path');
77
const tmpdir = require('../common/tmpdir');
88
const assert = require('assert');
99
const tmpDir = tmpdir.path;
10+
const { Readable } = require('stream');
1011

1112
tmpdir.refresh();
1213

1314
const dest = path.resolve(tmpDir, 'tmp.txt');
1415
const otherDest = path.resolve(tmpDir, 'tmp-2.txt');
1516
const buffer = Buffer.from('abc'.repeat(1000));
1617
const buffer2 = Buffer.from('xyz'.repeat(1000));
18+
const stream = Readable.from(['a', 'b', 'c']);
19+
const stream2 = Readable.from(['ümlaut', ' ', 'sechzig']);
20+
const iterable = {
21+
expected: 'abc',
22+
*[Symbol.iterator]() {
23+
yield 'a';
24+
yield 'b';
25+
yield 'c';
26+
}
27+
};
28+
function iterableWith(value) {
29+
return {
30+
*[Symbol.iterator]() {
31+
yield value;
32+
}
33+
};
34+
}
35+
const bufferIterable = {
36+
expected: 'abc',
37+
*[Symbol.iterator]() {
38+
yield Buffer.from('a');
39+
yield Buffer.from('b');
40+
yield Buffer.from('c');
41+
}
42+
};
43+
const asyncIterable = {
44+
expected: 'abc',
45+
async* [Symbol.asyncIterator]() {
46+
yield 'a';
47+
yield 'b';
48+
yield 'c';
49+
}
50+
};
1751

1852
async function doWrite() {
1953
await fsPromises.writeFile(dest, buffer);
2054
const data = fs.readFileSync(dest);
2155
assert.deepStrictEqual(data, buffer);
2256
}
2357

58+
async function doWriteStream() {
59+
await fsPromises.writeFile(dest, stream);
60+
const expected = 'abc';
61+
const data = fs.readFileSync(dest, 'utf-8');
62+
assert.deepStrictEqual(data, expected);
63+
}
64+
65+
async function doWriteStreamWithCancel() {
66+
const controller = new AbortController();
67+
const { signal } = controller;
68+
process.nextTick(() => controller.abort());
69+
assert.rejects(fsPromises.writeFile(otherDest, stream, { signal }), {
70+
name: 'AbortError'
71+
});
72+
}
73+
74+
async function doWriteIterable() {
75+
await fsPromises.writeFile(dest, iterable);
76+
const data = fs.readFileSync(dest, 'utf-8');
77+
assert.deepStrictEqual(data, iterable.expected);
78+
}
79+
80+
async function doWriteInvalidIterable() {
81+
await Promise.all(
82+
[42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) =>
83+
assert.rejects(fsPromises.writeFile(dest, iterableWith(value)), {
84+
code: 'ERR_INVALID_ARG_TYPE',
85+
})
86+
)
87+
);
88+
}
89+
90+
async function doWriteIterableWithEncoding() {
91+
await fsPromises.writeFile(dest, stream2, 'latin1');
92+
const expected = 'ümlaut sechzig';
93+
const data = fs.readFileSync(dest, 'latin1');
94+
assert.deepStrictEqual(data, expected);
95+
}
96+
97+
async function doWriteBufferIterable() {
98+
await fsPromises.writeFile(dest, bufferIterable);
99+
const data = fs.readFileSync(dest, 'utf-8');
100+
assert.deepStrictEqual(data, bufferIterable.expected);
101+
}
102+
103+
async function doWriteAsyncIterable() {
104+
await fsPromises.writeFile(dest, asyncIterable);
105+
const data = fs.readFileSync(dest, 'utf-8');
106+
assert.deepStrictEqual(data, asyncIterable.expected);
107+
}
108+
109+
async function doWriteInvalidValues() {
110+
await Promise.all(
111+
[42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) =>
112+
assert.rejects(fsPromises.writeFile(dest, value), {
113+
code: 'ERR_INVALID_ARG_TYPE',
114+
})
115+
)
116+
);
117+
}
118+
24119
async function doWriteWithCancel() {
25120
const controller = new AbortController();
26121
const { signal } = controller;
@@ -50,9 +145,18 @@ async function doReadWithEncoding() {
50145
assert.deepStrictEqual(data, syncData);
51146
}
52147

53-
doWrite()
54-
.then(doWriteWithCancel)
55-
.then(doAppend)
56-
.then(doRead)
57-
.then(doReadWithEncoding)
58-
.then(common.mustCall());
148+
(async () => {
149+
await doWrite();
150+
await doWriteWithCancel();
151+
await doAppend();
152+
await doRead();
153+
await doReadWithEncoding();
154+
await doWriteStream();
155+
await doWriteStreamWithCancel();
156+
await doWriteIterable();
157+
await doWriteInvalidIterable();
158+
await doWriteIterableWithEncoding();
159+
await doWriteBufferIterable();
160+
await doWriteAsyncIterable();
161+
await doWriteInvalidValues();
162+
})().then(common.mustCall());

0 commit comments

Comments
 (0)