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: nodejs#37391
  • Loading branch information
HiroyukiYagihashi committed Feb 24, 2021
1 parent 75cc41e commit e880436
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
6 changes: 5 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 (isCustomIterable(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) && !isCustomIterable(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 isCustomIterable(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
50 changes: 50 additions & 0 deletions test/parallel/test-fs-promises-writefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,66 @@ 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 iterable = {
expected: 'abc',
*[Symbol.iterator]() {
yield 'a';
yield 'b';
yield 'c';
}
};
const asyncIterable = {
expected: 'abc',
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);
const expected = 'abc';
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);
const data = fs.readFileSync(dest, 'utf-8');
assert.deepStrictEqual(data, iterable.expected);
}

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

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

0 comments on commit e880436

Please sign in to comment.