Skip to content

Commit

Permalink
fs: writeFile support AsyncIterable, Iterable & Stream as `da…
Browse files Browse the repository at this point in the history
…ta` argument

Fixes: #37391
  • Loading branch information
HiroyukiYagihashi committed Feb 24, 2021
1 parent 75cc41e commit 4c51e71
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
6 changes: 5 additions & 1 deletion doc/api/fs.md
Expand Up @@ -3833,6 +3833,9 @@ details.
<!-- YAML
added: v0.1.29
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37490
description: The `data` argument supports `AsyncIterable`, `Iterable` & `Stream`.
- version: v15.2.0
pr-url: https://github.com/nodejs/node/pull/35993
description: The options argument may include an AbortSignal to abort an
Expand Down Expand Up @@ -3866,7 +3869,8 @@ changes:
-->
* `file` {string|Buffer|URL|integer} filename or file descriptor
* `data` {string|Buffer|TypedArray|DataView|Object}
* `data` {string|Buffer|TypedArray|DataView|Object|AsyncIterable|Iterable
|Stream}
* `options` {Object|string}
* `encoding` {string|null} **Default:** `'utf8'`
* `mode` {integer} **Default:** `0o666`
Expand Down
35 changes: 32 additions & 3 deletions lib/internal/fs/promises.js
Expand Up @@ -10,6 +10,7 @@ const kReadFileMaxChunkSize = 2 ** 14;
const kWriteFileMaxChunkSize = 2 ** 14;

const {
ArrayIsArray,
ArrayPrototypePush,
Error,
MathMax,
Expand All @@ -21,6 +22,8 @@ const {
PromiseResolve,
SafeArrayIterator,
Symbol,
SymbolAsyncIterator,
SymbolIterator,
Uint8Array,
} = primordials;

Expand All @@ -41,7 +44,7 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED,
} = codes;
const { isArrayBufferView } = require('internal/util/types');
const { isArrayBufferView, isTypedArray } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
const {
copyObject,
Expand Down Expand Up @@ -273,7 +276,21 @@ async function fsCall(fn, handle, ...args) {
}

async function writeFileHandle(filehandle, data, signal) {
// `data` could be any kind of typed array.
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
if (isIterable(data)) {
for await (const buf of data) {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
await filehandle.write(buf);
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
}
return;
}
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
let remaining = data.length;
if (remaining === 0) return;
Expand Down Expand Up @@ -663,7 +680,7 @@ async function writeFile(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

if (!isArrayBufferView(data)) {
if (!isArrayBufferView(data) && !isIterable(data)) {
validateStringAfterArrayBufferView(data, 'data');
data = Buffer.from(data, options.encoding || 'utf8');
}
Expand All @@ -678,6 +695,18 @@ async function writeFile(path, data, options) {
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
}

function isIterable(obj) {
if (obj == null) {
return false;
}

return SymbolAsyncIterator in obj || (
SymbolIterator in obj &&
typeof obj !== 'string' &&
!ArrayIsArray(obj) &&
!isTypedArray(obj));
}

async function appendFile(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' });
options = copyObject(options);
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-fs-promises-writefile.js
Expand Up @@ -7,20 +7,70 @@ const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;
const { Readable } = require('stream');

tmpdir.refresh();

const dest = path.resolve(tmpDir, 'tmp.txt');
const otherDest = path.resolve(tmpDir, 'tmp-2.txt');
const buffer = Buffer.from('abc'.repeat(1000));
const buffer2 = Buffer.from('xyz'.repeat(1000));
const stream = Readable.from(['a', 'b', 'c']);
const stream2 = Readable.from(['a', 'b', 'c']);
const iterable = {
[Symbol.iterator]: function*() {
yield 'a';
yield 'b';
yield 'c';
}
};
const asyncIterable = {
async* [Symbol.asyncIterator]() {
yield 'a';
yield 'b';
yield 'c';
}
};

async function doWrite() {
await fsPromises.writeFile(dest, buffer);
const data = fs.readFileSync(dest);
assert.deepStrictEqual(data, buffer);
}

async function doWriteStream() {
await fsPromises.writeFile(dest, stream);
let expected = '';
for await (const v of stream2) expected += v;
const data = fs.readFileSync(dest, 'utf-8');
assert.deepStrictEqual(data, expected);
}

async function doWriteStreamWithCancel() {
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
assert.rejects(fsPromises.writeFile(otherDest, stream, { signal }), {
name: 'AbortError'
});
}

async function doWriteIterable() {
await fsPromises.writeFile(dest, iterable);
let expected = '';
for await (const v of iterable) expected += v;
const data = fs.readFileSync(dest, 'utf-8');
assert.deepStrictEqual(data, expected);
}

async function doWriteAsyncIterable() {
await fsPromises.writeFile(dest, asyncIterable);
let expected = '';
for await (const v of asyncIterable) expected += v;
const data = fs.readFileSync(dest, 'utf-8');
assert.deepStrictEqual(data, expected);
}

async function doWriteWithCancel() {
const controller = new AbortController();
const { signal } = controller;
Expand Down Expand Up @@ -55,4 +105,8 @@ doWrite()
.then(doAppend)
.then(doRead)
.then(doReadWithEncoding)
.then(doWriteStream)
.then(doWriteStreamWithCancel)
.then(doWriteIterable)
.then(doWriteAsyncIterable)
.then(common.mustCall());

0 comments on commit 4c51e71

Please sign in to comment.