Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3512c71

Browse files
author
HiroyukiYagihashi
committedMar 20, 2021
fs: writeFile support AsyncIterable, Iterable & Stream as data argument
Fixes: nodejs#37391
1 parent e427c48 commit 3512c71

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed
 

‎doc/api/fs.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,9 @@ All the [caveats][] for `fs.watch()` also apply to `fsPromises.watch()`.
12471247
<!-- YAML
12481248
added: v10.0.0
12491249
changes:
1250+
- version: REPLACEME
1251+
pr-url: https://github.com/nodejs/node/pull/37490
1252+
description: The `data` argument supports `AsyncIterable`, `Iterable` & `Stream`.
12501253
- version: v15.2.0
12511254
pr-url: https://github.com/nodejs/node/pull/35993
12521255
description: The options argument may include an AbortSignal to abort an
@@ -1262,7 +1265,8 @@ changes:
12621265
-->
12631266
12641267
* `file` {string|Buffer|URL|FileHandle} filename or `FileHandle`
1265-
* `data` {string|Buffer|Uint8Array|Object}
1268+
* `data` {string|Buffer|Uint8Array|Object|AsyncIterable|Iterable
1269+
|Stream}
12661270
* `options` {Object|string}
12671271
* `encoding` {string|null} **Default:** `'utf8'`
12681272
* `mode` {integer} **Default:** `0o666`

‎lib/internal/fs/promises.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const {
4242
},
4343
AbortError,
4444
} = require('internal/errors');
45-
const { isArrayBufferView } = require('internal/util/types');
45+
const { isArrayBuffer, isArrayBufferView } = require('internal/util/types');
4646
const { rimrafPromises } = require('internal/fs/rimraf');
4747
const {
4848
copyObject,
@@ -78,6 +78,7 @@ const pathModule = require('path');
7878
const { promisify } = require('internal/util');
7979
const { EventEmitterMixin } = require('internal/event_target');
8080
const { watch } = require('internal/fs/watchers');
81+
const { isIterable } = require('internal/streams/utils');
8182

8283
const kHandle = Symbol('kHandle');
8384
const kFd = Symbol('kFd');
@@ -273,8 +274,17 @@ function checkAborted(signal) {
273274
throw new AbortError();
274275
}
275276

276-
async function writeFileHandle(filehandle, data, signal) {
277-
// `data` could be any kind of typed array.
277+
async function writeFileHandle(filehandle, data, signal, encoding) {
278+
checkAborted(signal);
279+
if (isCustomIterable(data)) {
280+
for await (const buf of data) {
281+
checkAborted(signal);
282+
await write(
283+
filehandle, buf, undefined, isArrayBuffer(buf) ? buf.length : encoding);
284+
checkAborted(signal);
285+
}
286+
return;
287+
}
278288
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
279289
let remaining = data.length;
280290
if (remaining === 0) return;
@@ -679,20 +689,24 @@ async function writeFile(path, data, options) {
679689
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
680690
const flag = options.flag || 'w';
681691

682-
if (!isArrayBufferView(data)) {
692+
if (!isArrayBufferView(data) && !isCustomIterable(data)) {
683693
validateStringAfterArrayBufferView(data, 'data');
684694
data = Buffer.from(data, options.encoding || 'utf8');
685695
}
686696

687697
validateAbortSignal(options.signal);
688698
if (path instanceof FileHandle)
689-
return writeFileHandle(path, data, options.signal);
699+
return writeFileHandle(path, data, options.signal, options.encoding);
690700

691701
checkAborted(options.signal);
692702

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

698712
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

+68-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,76 @@ 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 iterable = {
20+
expected: 'abc',
21+
*[Symbol.iterator]() {
22+
yield 'a';
23+
yield 'b';
24+
yield 'c';
25+
}
26+
};
27+
const asyncIterable = {
28+
expected: 'abc',
29+
async* [Symbol.asyncIterator]() {
30+
yield 'a';
31+
yield 'b';
32+
yield 'c';
33+
}
34+
};
1735

1836
async function doWrite() {
1937
await fsPromises.writeFile(dest, buffer);
2038
const data = fs.readFileSync(dest);
2139
assert.deepStrictEqual(data, buffer);
2240
}
2341

42+
async function doWriteStream() {
43+
await fsPromises.writeFile(dest, stream);
44+
const expected = 'abc';
45+
const data = fs.readFileSync(dest, 'utf-8');
46+
assert.deepStrictEqual(data, expected);
47+
}
48+
49+
async function doWriteStreamWithCancel() {
50+
const controller = new AbortController();
51+
const { signal } = controller;
52+
process.nextTick(() => controller.abort());
53+
assert.rejects(fsPromises.writeFile(otherDest, stream, { signal }), {
54+
name: 'AbortError'
55+
});
56+
}
57+
58+
async function doWriteIterable() {
59+
await fsPromises.writeFile(dest, iterable);
60+
const data = fs.readFileSync(dest, 'utf-8');
61+
assert.deepStrictEqual(data, iterable.expected);
62+
}
63+
64+
async function doWriteAsyncIterable() {
65+
await fsPromises.writeFile(dest, asyncIterable);
66+
const data = fs.readFileSync(dest, 'utf-8');
67+
assert.deepStrictEqual(data, asyncIterable.expected);
68+
}
69+
70+
async function doWriteInvalidValues() {
71+
await Promise.all(
72+
[42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) =>
73+
assert.rejects(fsPromises.writeFile(dest, value), {
74+
code: 'ERR_INVALID_ARG_TYPE',
75+
})
76+
)
77+
);
78+
}
79+
2480
async function doWriteWithCancel() {
2581
const controller = new AbortController();
2682
const { signal } = controller;
@@ -50,9 +106,15 @@ async function doReadWithEncoding() {
50106
assert.deepStrictEqual(data, syncData);
51107
}
52108

53-
doWrite()
54-
.then(doWriteWithCancel)
55-
.then(doAppend)
56-
.then(doRead)
57-
.then(doReadWithEncoding)
58-
.then(common.mustCall());
109+
(async () => {
110+
await doWrite();
111+
await doWriteWithCancel();
112+
await doAppend();
113+
await doRead();
114+
await doReadWithEncoding();
115+
await doWriteStream();
116+
await doWriteStreamWithCancel();
117+
await doWriteIterable();
118+
await doWriteAsyncIterable();
119+
await doWriteInvalidValues();
120+
})().then(common.mustCall());

0 commit comments

Comments
 (0)
Please sign in to comment.