diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-04-29 03:30:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-28 21:30:48 -0400 |
commit | 0703431ec23efbde59669cc0568ec740909e47d7 (patch) | |
tree | c4de3bd13cb5ac3032dfbd1eb265430391f5d8bb | |
parent | 640f6878f61204bf7211cd9f0750b3ab6dee57b8 (diff) |
fix: bug in Deno.copy (#4977)
-rw-r--r-- | cli/js/io.ts | 6 | ||||
-rw-r--r-- | cli/js/tests/io_test.ts | 16 |
2 files changed, 21 insertions, 1 deletions
diff --git a/cli/js/io.ts b/cli/js/io.ts index 72c90d047..bf86d55b1 100644 --- a/cli/js/io.ts +++ b/cli/js/io.ts @@ -65,7 +65,11 @@ export async function copy( if (result === null) { gotEOF = true; } else { - n += await dst.write(b.subarray(0, result)); + let nwritten = 0; + while (nwritten < result) { + nwritten += await dst.write(b.subarray(nwritten, result)); + } + n += nwritten; } } return n; diff --git a/cli/js/tests/io_test.ts b/cli/js/tests/io_test.ts index d6d6e35d1..0ccd83ea2 100644 --- a/cli/js/tests/io_test.ts +++ b/cli/js/tests/io_test.ts @@ -55,3 +55,19 @@ unitTest(async function copyWithCustomBufferSize() { assertEquals(write.length, xBytes.length); assertEquals(readSpy.calls, DEFAULT_BUF_SIZE / bufSize + 1); }); + +unitTest({ perms: { write: true } }, async function copyBufferToFile() { + const filePath = "test-file.txt"; + // bigger than max File possible buffer 16kb + const bufSize = 32 * 1024; + const xBytes = repeat("b", bufSize); + const reader = new Deno.Buffer(xBytes.buffer as ArrayBuffer); + const write = await Deno.open(filePath, { write: true, create: true }); + + const n = await Deno.copy(reader, write, { bufSize }); + + assertEquals(n, xBytes.length); + + write.close(); + await Deno.remove(filePath); +}); |