diff options
author | Nitzan Uziely <linkgoron@gmail.com> | 2021-05-10 01:45:52 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2021-05-17 22:16:27 +0300 |
commit | 3612229d4433a2f9913f3363830999ca7c7a3c7e (patch) | |
tree | 150dbbddb885add96849f050e7a41daec56fd412 /lib | |
parent | 61a67c16253fb975e97451c2d4847965e8e73efa (diff) |
fs: fix async iterator partial writes
fix an issue where chunks might be partially written when
using writeFile with an async iterator.
PR-URL: https://github.com/nodejs/node/pull/38615
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/fs/promises.js | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 7a575e69491..0253a29c7d5 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -296,10 +296,16 @@ async function writeFileHandle(filehandle, data, signal, encoding) { if (isCustomIterable(data)) { for await (const buf of data) { checkAborted(signal); - await write( - filehandle, buf, undefined, - isArrayBufferView(buf) ? buf.byteLength : encoding); - checkAborted(signal); + const toWrite = + isArrayBufferView(buf) ? buf : Buffer.from(buf, encoding || 'utf8'); + let remaining = toWrite.byteLength; + while (remaining > 0) { + const writeSize = MathMin(kWriteFileMaxChunkSize, remaining); + const { bytesWritten } = await write( + filehandle, toWrite, toWrite.byteLength - remaining, writeSize); + remaining -= bytesWritten; + checkAborted(signal); + } } return; } |