1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix(ext/node): add truncate method to the FileHandle class (#27389)

This commit is contained in:
Filip Stevanovic 2024-12-20 05:23:51 +01:00 committed by GitHub
parent feb94d09e7
commit 23f7032d56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 105 additions and 20 deletions

View file

@ -16,16 +16,24 @@ export function ftruncate(
: undefined; : undefined;
const callback: CallbackWithError = typeof lenOrCallback === "function" const callback: CallbackWithError = typeof lenOrCallback === "function"
? lenOrCallback ? lenOrCallback
: maybeCallback as CallbackWithError; : (maybeCallback as CallbackWithError);
if (!callback) throw new Error("No callback function supplied"); if (!callback) throw new Error("No callback function supplied");
new FsFile(fd, Symbol.for("Deno.internal.FsFile")).truncate(len).then( new FsFile(fd, Symbol.for("Deno.internal.FsFile"))
() => callback(null), .truncate(len)
callback, .then(() => callback(null), callback);
);
} }
export function ftruncateSync(fd: number, len?: number) { export function ftruncateSync(fd: number, len?: number) {
new FsFile(fd, Symbol.for("Deno.internal.FsFile")).truncateSync(len); new FsFile(fd, Symbol.for("Deno.internal.FsFile")).truncateSync(len);
} }
export function ftruncatePromise(fd: number, len?: number): Promise<void> {
return new Promise((resolve, reject) => {
ftruncate(fd, len, (err) => {
if (err) reject(err);
else resolve();
});
});
}

View file

@ -13,6 +13,7 @@ import {
ReadOptions, ReadOptions,
TextOptionsArgument, TextOptionsArgument,
} from "ext:deno_node/_fs/_fs_common.ts"; } from "ext:deno_node/_fs/_fs_common.ts";
import { ftruncatePromise } from "ext:deno_node/_fs/_fs_ftruncate.ts";
import { core } from "ext:core/mod.js"; import { core } from "ext:core/mod.js";
interface WriteResult { interface WriteResult {
@ -73,6 +74,10 @@ export class FileHandle extends EventEmitter {
} }
} }
truncate(len?: number): Promise<void> {
return fsCall(ftruncatePromise, this, len);
}
readFile( readFile(
opt?: TextOptionsArgument | BinaryOptionsArgument | FileOptionsArgument, opt?: TextOptionsArgument | BinaryOptionsArgument | FileOptionsArgument,
): Promise<string | Buffer> { ): Promise<string | Buffer> {
@ -85,11 +90,7 @@ export class FileHandle extends EventEmitter {
length: number, length: number,
position: number, position: number,
): Promise<WriteResult>; ): Promise<WriteResult>;
write( write(str: string, position: number, encoding: string): Promise<WriteResult>;
str: string,
position: number,
encoding: string,
): Promise<WriteResult>;
write( write(
bufferOrStr: Uint8Array | string, bufferOrStr: Uint8Array | string,
offsetOrPosition: number, offsetOrPosition: number,
@ -120,16 +121,10 @@ export class FileHandle extends EventEmitter {
const encoding = lengthOrEncoding; const encoding = lengthOrEncoding;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
write( write(this.fd, str, position, encoding, (err, bytesWritten, buffer) => {
this.fd, if (err) reject(err);
str, else resolve({ buffer, bytesWritten });
position, });
encoding,
(err, bytesWritten, buffer) => {
if (err) reject(err);
else resolve({ buffer, bytesWritten });
},
);
}); });
} }
} }

View file

@ -117,3 +117,85 @@ Deno.test("[node/fs filehandle.writeFile] Write to file", async function () {
assertEquals(decoder.decode(data), "hello world"); assertEquals(decoder.decode(data), "hello world");
}); });
Deno.test(
"[node/fs filehandle.truncate] Truncate file with length",
async function () {
const tempFile: string = await Deno.makeTempFile();
const fileHandle = await fs.open(tempFile, "w+");
await fileHandle.writeFile("hello world");
await fileHandle.truncate(5);
const data = Deno.readFileSync(tempFile);
await Deno.remove(tempFile);
await fileHandle.close();
assertEquals(decoder.decode(data), "hello");
},
);
Deno.test(
"[node/fs filehandle.truncate] Truncate file without length",
async function () {
const tempFile: string = await Deno.makeTempFile();
const fileHandle = await fs.open(tempFile, "w+");
await fileHandle.writeFile("hello world");
await fileHandle.truncate();
const data = Deno.readFileSync(tempFile);
await Deno.remove(tempFile);
await fileHandle.close();
assertEquals(decoder.decode(data), "");
},
);
Deno.test(
"[node/fs filehandle.truncate] Truncate file with extension",
async function () {
const tempFile: string = await Deno.makeTempFile();
const fileHandle = await fs.open(tempFile, "w+");
await fileHandle.writeFile("hi");
await fileHandle.truncate(5);
const data = Deno.readFileSync(tempFile);
await Deno.remove(tempFile);
await fileHandle.close();
const expected = new Uint8Array(5);
expected.set(new TextEncoder().encode("hi"));
assertEquals(data, expected);
assertEquals(data.length, 5);
assertEquals(decoder.decode(data.subarray(0, 2)), "hi");
// Verify null bytes
assertEquals(data[2], 0);
assertEquals(data[3], 0);
assertEquals(data[4], 0);
},
);
Deno.test(
"[node/fs filehandle.truncate] Truncate file with negative length",
async function () {
const tempFile: string = await Deno.makeTempFile();
const fileHandle = await fs.open(tempFile, "w+");
await fileHandle.writeFile("hello world");
await fileHandle.truncate(-1);
const data = Deno.readFileSync(tempFile);
await Deno.remove(tempFile);
await fileHandle.close();
assertEquals(decoder.decode(data), "");
assertEquals(data.length, 0);
},
);