From 9d53dab4df64361f00dda2534eecbd04797d56a2 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Mon, 12 Apr 2021 20:32:58 +0800 Subject: [PATCH] feat(runtime): add truncate and truncateSync methods to Deno.File (#10130) --- cli/dts/lib.deno.ns.d.ts | 2 ++ cli/tests/unit/file_test.ts | 44 +++++++++++++++++++++++++++++++++++++ runtime/js/40_files.js | 10 ++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index a4ef6bc68c..9108f6dd6f 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -781,6 +781,8 @@ declare namespace Deno { constructor(rid: number); write(p: Uint8Array): Promise; writeSync(p: Uint8Array): number; + truncate(len?: number): void; + truncateSync(len?: number): Promise; read(p: Uint8Array): Promise; readSync(p: Uint8Array): number | null; seek(offset: number, whence: SeekMode): Promise; diff --git a/cli/tests/unit/file_test.ts b/cli/tests/unit/file_test.ts index 6401980347..f1db0d4965 100644 --- a/cli/tests/unit/file_test.ts +++ b/cli/tests/unit/file_test.ts @@ -104,6 +104,50 @@ unitTest(function fileUsingEmptyStringFileName(): void { testSecondArgument("", ""); }); +unitTest( + { perms: { read: true, write: true } }, + function fileTruncateSyncSuccess(): void { + const filename = Deno.makeTempDirSync() + "/test_fileTruncateSync.txt"; + const file = Deno.openSync(filename, { + create: true, + read: true, + write: true, + }); + + file.truncateSync(20); + assertEquals(Deno.readFileSync(filename).byteLength, 20); + file.truncateSync(5); + assertEquals(Deno.readFileSync(filename).byteLength, 5); + file.truncateSync(-5); + assertEquals(Deno.readFileSync(filename).byteLength, 0); + + file.close(); + Deno.removeSync(filename); + }, +); + +unitTest( + { perms: { read: true, write: true } }, + async function fileTruncateSuccess(): Promise { + const filename = Deno.makeTempDirSync() + "/test_fileTruncate.txt"; + const file = await Deno.open(filename, { + create: true, + read: true, + write: true, + }); + + await file.truncate(20); + assertEquals((await Deno.readFile(filename)).byteLength, 20); + await file.truncate(5); + assertEquals((await Deno.readFile(filename)).byteLength, 5); + await file.truncate(-5); + assertEquals((await Deno.readFile(filename)).byteLength, 0); + + file.close(); + await Deno.remove(filename); + }, +); + unitTest({ perms: { read: true } }, function fileStatSyncSuccess(): void { const file = Deno.openSync("README.md"); const fileInfo = file.statSync(); diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js index 2ce495f8e4..d552b4ba50 100644 --- a/runtime/js/40_files.js +++ b/runtime/js/40_files.js @@ -4,7 +4,7 @@ ((window) => { const core = window.Deno.core; const { read, readSync, write, writeSync } = window.__bootstrap.io; - const { fstat, fstatSync } = window.__bootstrap.fs; + const { ftruncate, ftruncateSync, fstat, fstatSync } = window.__bootstrap.fs; const { pathFromURL } = window.__bootstrap.util; function seekSync( @@ -88,6 +88,14 @@ return writeSync(this.rid, p); } + truncate(len) { + return ftruncate(this.rid, len); + } + + truncateSync(len) { + return ftruncateSync(this.rid, len); + } + read(p) { return read(this.rid, p); }