diff --git a/doc/api/fs.md b/doc/api/fs.md index 05e4ed09cda2e4..727564b8885e96 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1244,6 +1244,9 @@ All the [caveats][] for `fs.watch()` also apply to `fsPromises.watch()`. * `file` {string|Buffer|URL|FileHandle} filename or `FileHandle` -* `data` {string|Buffer|Uint8Array|Object} +* `data` {string|Buffer|Uint8Array|Object|AsyncIterable|Iterable + |Stream} * `options` {Object|string} * `encoding` {string|null} **Default:** `'utf8'` * `mode` {integer} **Default:** `0o666` diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 32ad2123d47db0..713dcc2c77004e 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -10,6 +10,7 @@ const kReadFileMaxChunkSize = 2 ** 14; const kWriteFileMaxChunkSize = 2 ** 14; const { + ArrayIsArray, ArrayPrototypePush, Error, MathMax, @@ -21,6 +22,8 @@ const { PromiseResolve, SafeArrayIterator, Symbol, + SymbolAsyncIterator, + SymbolIterator, Uint8Array, } = primordials; @@ -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, @@ -274,7 +277,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; @@ -664,7 +681,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'); } @@ -682,6 +699,17 @@ async function writeFile(path, data, options) { return PromisePrototypeFinally(writeFileHandle(fd, data, signal), fd.close); } +function isCustomIterable(obj) { + if (typeof obj !== 'object' || obj === null) { + return false; + } + + return SymbolAsyncIterator in obj || ( + SymbolIterator in obj && + !ArrayIsArray(obj) && + !isTypedArray(obj)); +} + async function appendFile(path, data, options) { options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' }); options = copyObject(options); diff --git a/test/parallel/test-fs-promises-writefile.js b/test/parallel/test-fs-promises-writefile.js index 7fbe12dda4dc2d..7c5e08ab80eb62 100644 --- a/test/parallel/test-fs-promises-writefile.js +++ b/test/parallel/test-fs-promises-writefile.js @@ -7,6 +7,7 @@ const path = require('path'); const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; +const { Readable } = require('stream'); tmpdir.refresh(); @@ -14,6 +15,23 @@ 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); @@ -21,6 +39,44 @@ async function doWrite() { 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 doWriteInvalidValues() { + await Promise.all( + [42, 42n, {}, [], Symbol('42'), true, undefined, null, NaN].map((value) => + assert.rejects(fsPromises.writeFile(dest, value), { + code: 'ERR_INVALID_ARG_TYPE', + }) + ) + ); +} + async function doWriteWithCancel() { const controller = new AbortController(); const { signal } = controller; @@ -55,4 +111,9 @@ doWrite() .then(doAppend) .then(doRead) .then(doReadWithEncoding) + .then(doWriteStream) + .then(doWriteStreamWithCancel) + .then(doWriteIterable) + .then(doWriteAsyncIterable) + .then(doWriteInvalidValues) .then(common.mustCall());