Skip to content

Commit

Permalink
fix(ext/fs): truncate files when a ReadableStream is passed to writeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
char committed Apr 15, 2024
1 parent f358ae6 commit 7e2a3d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/fs/30_fs.js
Expand Up @@ -926,6 +926,7 @@ async function writeFile(
append: options.append ?? false,
create: options.create ?? true,
createNew: options.createNew ?? false,
truncate: true,
write: true,
});
await data.pipeTo(file.writable, {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/write_file_test.ts
Expand Up @@ -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]));
},
);

0 comments on commit 7e2a3d1

Please sign in to comment.