Skip to content

Commit

Permalink
fixup! fs: add support for async iterators to fs.writeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
HiroyukiYagihashi committed May 9, 2021
1 parent 785d76d commit ccdb3d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2045,21 +2045,16 @@ function lutimesSync(path, atime, mtime) {
function writeAll(
fd, isUserFd, buffer, offset, length, signal, encoding, callback) {
if (signal?.aborted) {
const abortError = new AbortError();
if (isUserFd) {
callback(abortError);
} else {
fs.close(fd, (err) => {
callback(aggregateTwoErrors(err, abortError));
});
}
handleWriteAllErrorCallback(fd, isUserFd, new AbortError(), callback);
return;
}

if (isCustomIterable(buffer)) {
writeAllCustomIterable(
fd, isUserFd, buffer, offset, length, signal, encoding, callback)
.catch((reason) => { throw reason; });
.catch((reason) => {
handleWriteAllErrorCallback(fd, isUserFd, reason, callback);
});
return;
}
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
Expand All @@ -2082,20 +2077,29 @@ function writeAll(

async function writeAllCustomIterable(
fd, isUserFd, buffer, offset, length, signal, encoding, callback) {
if (signal?.aborted) {
handleWriteAllErrorCallback(fd, isUserFd, new AbortError(), callback);
return;
}

const result = await buffer.next();
if (result.done) {
fs.close(fd, callback);
if (isUserFd) {
callback(null);
} else {
fs.close(fd, callback);
}
return;
}
const resultValue = result.value.toString();
fs.write(fd, resultValue, undefined,
isArrayBufferView(buffer) ? resultValue.byteLength : encoding,
const resultValue = isArrayBufferView(result.value) ?
result.value : Buffer.from(String(result.value), encoding);
fs.write(fd, resultValue, offset, resultValue.byteLength,
null, (writeErr, _) => {
if (writeErr) {
handleWriteAllErrorCallback(fd, isUserFd, writeErr, callback);
} else {
writeAll(fd, isUserFd, buffer, offset,
length, signal, encoding, callback);
writeAllCustomIterable(fd, isUserFd, buffer, offset, length,
signal, encoding, callback);
}
}
);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fs-write-file-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ tmpdir.refresh();
);
}

{
const filenameBufferIterableWithEncoding =
join(tmpdir.path, 'testBufferIterableWithEncoding.txt');
const bufferIterableWithEncoding = {
expected: 'ümlaut sechzig',
*[Symbol.iterator]() {
yield Buffer.from('ümlaut');
yield Buffer.from(' ');
yield Buffer.from('sechzig');
}
};

fs.writeFile(
filenameBufferIterableWithEncoding,
bufferIterableWithEncoding, common.mustSucceed(() => {
const data = fs.readFileSync(filenameBufferIterableWithEncoding, 'utf-8');
assert.strictEqual(bufferIterableWithEncoding.expected, data);
})
);
}

{
const filenameAsyncIterable = join(tmpdir.path, 'testAsyncIterable.txt');
Expand Down

0 comments on commit ccdb3d1

Please sign in to comment.