diff options
author | charlotte ✨ <charlotte@som.codes> | 2024-05-27 23:14:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-28 00:14:35 +0200 |
commit | 506c275053c880a4c6c3f49921f99fd41759064f (patch) | |
tree | 71aa767b906bb857791c2c93026eef620dcde996 /tests/unit/write_file_test.ts | |
parent | 35e5159c8d5987497b8980c1cf3996d241612957 (diff) |
fix(ext/fs): truncate files when a ReadableStream is passed to writeFile (#23330)
Closes #19697. This fixes a bug where the writeFile API can create
partially-overwritten files which may lead to invalid / corrupt files or
data leakage. It also aligns the behavior of writing a ReadableStream
and writing a Uint8Array to the disk.
Diffstat (limited to 'tests/unit/write_file_test.ts')
-rw-r--r-- | tests/unit/write_file_test.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/unit/write_file_test.ts b/tests/unit/write_file_test.ts index 6cd08e2d1..29780446c 100644 --- a/tests/unit/write_file_test.ts +++ b/tests/unit/write_file_test.ts @@ -425,3 +425,21 @@ Deno.test( assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2])); }, ); + +Deno.test( + { permissions: { read: true, write: true } }, + async function overwriteFileWithStream() { + const filename = Deno.makeTempDirSync() + "/test.txt"; + await Deno.writeFile(filename, new Uint8Array([1, 2, 3, 4])); + + const stream = new ReadableStream({ + pull(controller) { + controller.enqueue(new Uint8Array([1])); + controller.enqueue(new Uint8Array([2])); + controller.close(); + }, + }); + await Deno.writeFile(filename, stream); + assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2])); + }, +); |