summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-04-12 20:32:58 +0800
committerGitHub <noreply@github.com>2021-04-12 14:32:58 +0200
commit9d53dab4df64361f00dda2534eecbd04797d56a2 (patch)
treea80694575404f5203b058222b6ce5a6702c05011
parentda9219341fe8bcf080d3ebdd03d35cec1268875e (diff)
feat(runtime): add truncate and truncateSync methods to Deno.File (#10130)
-rw-r--r--cli/dts/lib.deno.ns.d.ts2
-rw-r--r--cli/tests/unit/file_test.ts44
-rw-r--r--runtime/js/40_files.js10
3 files changed, 55 insertions, 1 deletions
diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts
index a4ef6bc68..9108f6dd6 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<number>;
writeSync(p: Uint8Array): number;
+ truncate(len?: number): void;
+ truncateSync(len?: number): Promise<void>;
read(p: Uint8Array): Promise<number | null>;
readSync(p: Uint8Array): number | null;
seek(offset: number, whence: SeekMode): Promise<number>;
diff --git a/cli/tests/unit/file_test.ts b/cli/tests/unit/file_test.ts
index 640198034..f1db0d496 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<void> {
+ 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 2ce495f8e..d552b4ba5 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);
}