@@ -85,6 +85,7 @@ const {
85
85
const { FSReqCallback } = binding ;
86
86
const { toPathIfFileURL } = require ( 'internal/url' ) ;
87
87
const internalUtil = require ( 'internal/util' ) ;
88
+ const { isIterable } = require ( 'internal/streams/utils' ) ;
88
89
const {
89
90
constants : {
90
91
kIoMaxLength,
@@ -828,12 +829,12 @@ function write(fd, buffer, offset, length, position, callback) {
828
829
} else {
829
830
position = length ;
830
831
}
831
- length = 'utf8' ;
832
+ length = length || 'utf8' ;
832
833
}
833
834
834
835
const str = String ( buffer ) ;
835
836
validateEncoding ( str , length ) ;
836
- callback = maybeCallback ( position ) ;
837
+ callback = maybeCallback ( callback || position ) ;
837
838
838
839
const req = new FSReqCallback ( ) ;
839
840
req . oncomplete = wrapper ;
@@ -2039,7 +2040,8 @@ function lutimesSync(path, atime, mtime) {
2039
2040
handleErrorFromBinding ( ctx ) ;
2040
2041
}
2041
2042
2042
- function writeAll ( fd , isUserFd , buffer , offset , length , signal , callback ) {
2043
+ function writeAll (
2044
+ fd , isUserFd , buffer , offset , length , signal , encoding , callback ) {
2043
2045
if ( signal ?. aborted ) {
2044
2046
const abortError = new AbortError ( ) ;
2045
2047
if ( isUserFd ) {
@@ -2051,7 +2053,29 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
2051
2053
}
2052
2054
return ;
2053
2055
}
2054
- // write(fd, buffer, offset, length, position, callback)
2056
+ if ( isCustomIterable ( buffer ) ) {
2057
+ ( async ( ) => {
2058
+ for await ( const buf of buffer ) {
2059
+ fs . write (
2060
+ fd , buf , undefined ,
2061
+ isArrayBufferView ( buf ) ? buf . byteLength : encoding ,
2062
+ null , ( writeErr , _ ) => {
2063
+ if ( writeErr ) {
2064
+ if ( isUserFd ) {
2065
+ callback ( writeErr ) ;
2066
+ } else {
2067
+ fs . close ( fd , ( err ) => {
2068
+ callback ( aggregateTwoErrors ( err , writeErr ) ) ;
2069
+ } ) ;
2070
+ }
2071
+ }
2072
+ }
2073
+ ) ;
2074
+ }
2075
+ fs . close ( fd , callback ) ;
2076
+ } ) ( ) ;
2077
+ return ;
2078
+ }
2055
2079
fs . write ( fd , buffer , offset , length , null , ( writeErr , written ) => {
2056
2080
if ( writeErr ) {
2057
2081
if ( isUserFd ) {
@@ -2070,11 +2094,16 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
2070
2094
} else {
2071
2095
offset += written ;
2072
2096
length -= written ;
2073
- writeAll ( fd , isUserFd , buffer , offset , length , signal , callback ) ;
2097
+ writeAll (
2098
+ fd , isUserFd , buffer , offset , length , signal , encoding , callback ) ;
2074
2099
}
2075
2100
} ) ;
2076
2101
}
2077
2102
2103
+ function isCustomIterable ( obj ) {
2104
+ return isIterable ( obj ) && ! isArrayBufferView ( obj ) && typeof obj !== 'string' ;
2105
+ }
2106
+
2078
2107
/**
2079
2108
* Asynchronously writes data to the file.
2080
2109
* @param {string | Buffer | URL | number } path
@@ -2093,15 +2122,16 @@ function writeFile(path, data, options, callback) {
2093
2122
options = getOptions ( options , { encoding : 'utf8' , mode : 0o666 , flag : 'w' } ) ;
2094
2123
const flag = options . flag || 'w' ;
2095
2124
2096
- if ( ! isArrayBufferView ( data ) ) {
2125
+ if ( ! isArrayBufferView ( data ) && ! isCustomIterable ( data ) ) {
2097
2126
validateStringAfterArrayBufferView ( data , 'data' ) ;
2098
2127
data = Buffer . from ( String ( data ) , options . encoding || 'utf8' ) ;
2099
2128
}
2100
2129
2101
2130
if ( isFd ( path ) ) {
2102
2131
const isUserFd = true ;
2103
2132
const signal = options . signal ;
2104
- writeAll ( path , isUserFd , data , 0 , data . byteLength , signal , callback ) ;
2133
+ writeAll ( path , isUserFd , data ,
2134
+ 0 , data . byteLength , signal , options . encoding , callback ) ;
2105
2135
return ;
2106
2136
}
2107
2137
@@ -2114,7 +2144,8 @@ function writeFile(path, data, options, callback) {
2114
2144
} else {
2115
2145
const isUserFd = false ;
2116
2146
const signal = options . signal ;
2117
- writeAll ( fd , isUserFd , data , 0 , data . byteLength , signal , callback ) ;
2147
+ writeAll ( fd , isUserFd , data ,
2148
+ 0 , data . byteLength , signal , options . encoding , callback ) ;
2118
2149
}
2119
2150
} ) ;
2120
2151
}
0 commit comments