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 b5538ff

Browse files
author
HiroyukiYagihashi
committedFeb 23, 2021
fs: writeFile support AsyncIterable, Iterable & Stream as data argument
Fixes: nodejs#37391
1 parent 75cc41e commit b5538ff

File tree

3 files changed

+107
-13
lines changed

3 files changed

+107
-13
lines changed
 

‎doc/api/fs.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3866,7 +3866,8 @@ changes:
38663866
-->
38673867
38683868
* `file` {string|Buffer|URL|integer} filename or file descriptor
3869-
* `data` {string|Buffer|TypedArray|DataView|Object}
3869+
* `data` {string|Buffer|TypedArray|DataView|Object|AsyncIterable|Iterable
3870+
|Stream}
38703871
* `options` {Object|string}
38713872
* `encoding` {string|null} **Default:** `'utf8'`
38723873
* `mode` {integer} **Default:** `0o666`

‎lib/internal/fs/promises.js

+51-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const kReadFileMaxChunkSize = 2 ** 14;
1010
const kWriteFileMaxChunkSize = 2 ** 14;
1111

1212
const {
13+
ArrayIsArray,
1314
ArrayPrototypePush,
1415
Error,
1516
MathMax,
@@ -21,7 +22,9 @@ const {
2122
PromiseResolve,
2223
SafeArrayIterator,
2324
Symbol,
24-
Uint8Array,
25+
SymbolAsyncIterator,
26+
SymbolIterator,
27+
Uint8Array
2528
} = primordials;
2629

2730
const {
@@ -41,7 +44,7 @@ const {
4144
ERR_INVALID_ARG_VALUE,
4245
ERR_METHOD_NOT_IMPLEMENTED,
4346
} = codes;
44-
const { isArrayBufferView } = require('internal/util/types');
47+
const { isArrayBufferView, isTypedArray } = require('internal/util/types');
4548
const { rimrafPromises } = require('internal/fs/rimraf');
4649
const {
4750
copyObject,
@@ -663,19 +666,55 @@ async function writeFile(path, data, options) {
663666
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
664667
const flag = options.flag || 'w';
665668

666-
if (!isArrayBufferView(data)) {
667-
validateStringAfterArrayBufferView(data, 'data');
668-
data = Buffer.from(data, options.encoding || 'utf8');
669-
}
669+
if (isIterable(data)) {
670+
if (options.signal?.aborted) {
671+
throw lazyDOMException('The operation was aborted', 'AbortError');
672+
}
673+
const fd = await open(path, flag, options.mode);
674+
try {
675+
if (options.signal?.aborted) {
676+
throw lazyDOMException('The operation was aborted', 'AbortError');
677+
}
678+
for await (const buf of data) {
679+
if (options.signal?.aborted) {
680+
throw lazyDOMException('The operation was aborted', 'AbortError');
681+
}
682+
await fd.write(buf);
683+
if (options.signal?.aborted) {
684+
throw lazyDOMException('The operation was aborted', 'AbortError');
685+
}
686+
}
687+
} finally {
688+
await fd.close();
689+
}
690+
} else {
691+
if (!isArrayBufferView(data)) {
692+
validateStringAfterArrayBufferView(data, 'data');
693+
data = Buffer.from(data, options.encoding || 'utf8');
694+
}
670695

671-
if (path instanceof FileHandle)
672-
return writeFileHandle(path, data, options.signal);
696+
if (path instanceof FileHandle) {
697+
return writeFileHandle(path, data, options.signal);
698+
}
673699

674-
const fd = await open(path, flag, options.mode);
675-
if (options.signal?.aborted) {
676-
throw lazyDOMException('The operation was aborted', 'AbortError');
700+
const fd = await open(path, flag, options.mode);
701+
if (options.signal?.aborted) {
702+
throw lazyDOMException('The operation was aborted', 'AbortError');
703+
}
704+
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
677705
}
678-
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
706+
}
707+
708+
function isIterable(obj) {
709+
if (obj == null) {
710+
return false;
711+
}
712+
713+
return SymbolAsyncIterator in obj || (
714+
SymbolIterator in obj &&
715+
typeof obj !== 'string' &&
716+
!ArrayIsArray(obj) &&
717+
!isTypedArray(obj));
679718
}
680719

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

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

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

1835
async function doWrite() {
1936
await fsPromises.writeFile(dest, buffer);
2037
const data = fs.readFileSync(dest);
2138
assert.deepStrictEqual(data, buffer);
2239
}
2340

41+
async function doWriteStream() {
42+
await fsPromises.writeFile(dest, stream);
43+
let expected = '';
44+
for await (const v of stream2) expected += v;
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+
let expected = '';
61+
for await (const v of iterable) expected += v;
62+
const data = fs.readFileSync(dest, 'utf-8');
63+
assert.deepStrictEqual(data, expected);
64+
}
65+
66+
async function doWriteAsyncIterable() {
67+
await fsPromises.writeFile(dest, asyncIterable);
68+
let expected = '';
69+
for await (const v of asyncIterable) expected += v;
70+
const data = fs.readFileSync(dest, 'utf-8');
71+
assert.deepStrictEqual(data, expected);
72+
}
73+
2474
async function doWriteWithCancel() {
2575
const controller = new AbortController();
2676
const { signal } = controller;
@@ -55,4 +105,8 @@ doWrite()
55105
.then(doAppend)
56106
.then(doRead)
57107
.then(doReadWithEncoding)
108+
.then(doWriteStream)
109+
.then(doWriteStreamWithCancel)
110+
.then(doWriteIterable)
111+
.then(doWriteAsyncIterable)
58112
.then(common.mustCall());

0 commit comments

Comments
 (0)
Please sign in to comment.