mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 08:39:09 -05:00
feat(runtime): add truncate and truncateSync methods to Deno.File (#10130)
This commit is contained in:
parent
da9219341f
commit
9d53dab4df
3 changed files with 55 additions and 1 deletions
2
cli/dts/lib.deno.ns.d.ts
vendored
2
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -781,6 +781,8 @@ declare namespace Deno {
|
||||||
constructor(rid: number);
|
constructor(rid: number);
|
||||||
write(p: Uint8Array): Promise<number>;
|
write(p: Uint8Array): Promise<number>;
|
||||||
writeSync(p: Uint8Array): number;
|
writeSync(p: Uint8Array): number;
|
||||||
|
truncate(len?: number): void;
|
||||||
|
truncateSync(len?: number): Promise<void>;
|
||||||
read(p: Uint8Array): Promise<number | null>;
|
read(p: Uint8Array): Promise<number | null>;
|
||||||
readSync(p: Uint8Array): number | null;
|
readSync(p: Uint8Array): number | null;
|
||||||
seek(offset: number, whence: SeekMode): Promise<number>;
|
seek(offset: number, whence: SeekMode): Promise<number>;
|
||||||
|
|
|
@ -104,6 +104,50 @@ unitTest(function fileUsingEmptyStringFileName(): void {
|
||||||
testSecondArgument("", "");
|
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 {
|
unitTest({ perms: { read: true } }, function fileStatSyncSuccess(): void {
|
||||||
const file = Deno.openSync("README.md");
|
const file = Deno.openSync("README.md");
|
||||||
const fileInfo = file.statSync();
|
const fileInfo = file.statSync();
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
((window) => {
|
((window) => {
|
||||||
const core = window.Deno.core;
|
const core = window.Deno.core;
|
||||||
const { read, readSync, write, writeSync } = window.__bootstrap.io;
|
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;
|
const { pathFromURL } = window.__bootstrap.util;
|
||||||
|
|
||||||
function seekSync(
|
function seekSync(
|
||||||
|
@ -88,6 +88,14 @@
|
||||||
return writeSync(this.rid, p);
|
return writeSync(this.rid, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
truncate(len) {
|
||||||
|
return ftruncate(this.rid, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
truncateSync(len) {
|
||||||
|
return ftruncateSync(this.rid, len);
|
||||||
|
}
|
||||||
|
|
||||||
read(p) {
|
read(p) {
|
||||||
return read(this.rid, p);
|
return read(this.rid, p);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue