-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
fs: add support for async iterators to fs.writeFile
#38525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
785d76d
6f21783
c0b87d6
05a5728
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Fixes: #38075
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) => { | ||
|
|
@@ -2082,20 +2077,29 @@ function writeAll( | |
|
|
||
| async function writeAllCustomIterable( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcollina Thanks for your suggestion 👍 . I'm curious about the advantages of having a promisified version of |
||
| 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) ? | ||
|
Linkgoron marked this conversation as resolved.
|
||
| 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); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would schedule this via
process.nextTick. If this throws synchronously for whatever reason, it will lead to an unhandled rejection.