1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

feat: deprecate Deno.ftruncate() and Deno.ftruncateSync() (#22069)

For removal in Deno 2.0.
This commit is contained in:
Asher Gomez 2024-01-25 01:12:22 +11:00 committed by GitHub
parent 064a6c048a
commit 4af121687c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 28 deletions

View file

@ -47,7 +47,7 @@ Deno.test(
create: true, create: true,
}); });
const size = 64; const size = 64;
Deno.ftruncateSync(file.rid, size); file.truncateSync(size);
Deno.fsyncSync(file.rid); Deno.fsyncSync(file.rid);
assertEquals(Deno.statSync(filename).size, size); assertEquals(Deno.statSync(filename).size, size);
Deno.close(file.rid); Deno.close(file.rid);
@ -65,7 +65,7 @@ Deno.test(
create: true, create: true,
}); });
const size = 64; const size = 64;
await Deno.ftruncate(file.rid, size); await file.truncate(size);
await Deno.fsync(file.rid); await Deno.fsync(file.rid);
assertEquals((await Deno.stat(filename)).size, size); assertEquals((await Deno.stat(filename)).size, size);
Deno.close(file.rid); Deno.close(file.rid);

View file

@ -11,11 +11,11 @@ Deno.test(
write: true, write: true,
}); });
Deno.ftruncateSync(file.rid, 20); file.truncateSync(20);
assertEquals(Deno.readFileSync(filename).byteLength, 20); assertEquals(Deno.readFileSync(filename).byteLength, 20);
Deno.ftruncateSync(file.rid, 5); file.truncateSync(5);
assertEquals(Deno.readFileSync(filename).byteLength, 5); assertEquals(Deno.readFileSync(filename).byteLength, 5);
Deno.ftruncateSync(file.rid, -5); file.truncateSync(-5);
assertEquals(Deno.readFileSync(filename).byteLength, 0); assertEquals(Deno.readFileSync(filename).byteLength, 0);
Deno.close(file.rid); Deno.close(file.rid);
@ -33,11 +33,11 @@ Deno.test(
write: true, write: true,
}); });
await Deno.ftruncate(file.rid, 20); await file.truncate(20);
assertEquals((await Deno.readFile(filename)).byteLength, 20); assertEquals((await Deno.readFile(filename)).byteLength, 20);
await Deno.ftruncate(file.rid, 5); await file.truncate(5);
assertEquals((await Deno.readFile(filename)).byteLength, 5); assertEquals((await Deno.readFile(filename)).byteLength, 5);
await Deno.ftruncate(file.rid, -5); await file.truncate(-5);
assertEquals((await Deno.readFile(filename)).byteLength, 0); assertEquals((await Deno.readFile(filename)).byteLength, 0);
Deno.close(file.rid); Deno.close(file.rid);

View file

@ -5,32 +5,31 @@ import { fsync, fsyncSync } from "node:fs";
Deno.test({ Deno.test({
name: "ASYNC: flush any pending data of the given file stream to disk", name: "ASYNC: flush any pending data of the given file stream to disk",
async fn() { async fn() {
const file: string = await Deno.makeTempFile(); const filePath = await Deno.makeTempFile();
const { rid } = await Deno.open(file, { using file = await Deno.open(filePath, {
read: true, read: true,
write: true, write: true,
create: true, create: true,
}); });
const size = 64; const size = 64;
await Deno.ftruncate(rid, size); await file.truncate(size);
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
fsync(rid, (err: Error | null) => { fsync(file.rid, (err: Error | null) => {
if (err !== null) reject(); if (err !== null) reject();
else resolve(); else resolve();
}); });
}) })
.then( .then(
async () => { async () => {
assertEquals((await Deno.stat(file)).size, size); assertEquals((await Deno.stat(filePath)).size, size);
}, },
() => { () => {
fail("No error expected"); fail("No error expected");
}, },
) )
.finally(async () => { .finally(async () => {
await Deno.remove(file); await Deno.remove(filePath);
Deno.close(rid);
}); });
}, },
}); });
@ -38,21 +37,20 @@ Deno.test({
Deno.test({ Deno.test({
name: "SYNC: flush any pending data the given file stream to disk", name: "SYNC: flush any pending data the given file stream to disk",
fn() { fn() {
const file: string = Deno.makeTempFileSync(); const filePath = Deno.makeTempFileSync();
const { rid } = Deno.openSync(file, { using file = Deno.openSync(filePath, {
read: true, read: true,
write: true, write: true,
create: true, create: true,
}); });
const size = 64; const size = 64;
Deno.ftruncateSync(rid, size); file.truncateSync(size);
try { try {
fsyncSync(rid); fsyncSync(file.rid);
assertEquals(Deno.statSync(file).size, size); assertEquals(Deno.statSync(filePath).size, size);
} finally { } finally {
Deno.removeSync(file); Deno.removeSync(filePath);
Deno.close(rid);
} }
}, },
}); });

View file

@ -2175,7 +2175,7 @@ declare namespace Deno {
* { read: true, write: true, create: true }, * { read: true, write: true, create: true },
* ); * );
* await Deno.write(file.rid, new TextEncoder().encode("Hello World")); * await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await Deno.ftruncate(file.rid, 1); * await file.truncate(1);
* await Deno.fsync(file.rid); * await Deno.fsync(file.rid);
* console.log(await Deno.readTextFile("my_file.txt")); // H * console.log(await Deno.readTextFile("my_file.txt")); // H
* ``` * ```
@ -2197,7 +2197,7 @@ declare namespace Deno {
* { read: true, write: true, create: true }, * { read: true, write: true, create: true },
* ); * );
* Deno.writeSync(file.rid, new TextEncoder().encode("Hello World")); * Deno.writeSync(file.rid, new TextEncoder().encode("Hello World"));
* Deno.ftruncateSync(file.rid, 1); * file.truncateSync(1);
* Deno.fsyncSync(file.rid); * Deno.fsyncSync(file.rid);
* console.log(Deno.readTextFileSync("my_file.txt")); // H * console.log(Deno.readTextFileSync("my_file.txt")); // H
* ``` * ```
@ -5314,6 +5314,9 @@ declare namespace Deno {
* console.log(new TextDecoder().decode(data)); // Hello W * console.log(new TextDecoder().decode(data)); // Hello W
* ``` * ```
* *
* @deprecated Use {@linkcode Deno.FsFile.truncate} instead.
* {@linkcode Deno.ftruncate} will be removed in Deno 2.0.
*
* @category File System * @category File System
*/ */
export function ftruncate(rid: number, len?: number): Promise<void>; export function ftruncate(rid: number, len?: number): Promise<void>;
@ -5356,6 +5359,9 @@ declare namespace Deno {
* console.log(new TextDecoder().decode(data)); // Hello W * console.log(new TextDecoder().decode(data)); // Hello W
* ``` * ```
* *
* @deprecated Use {@linkcode Deno.FsFile.truncateSync} instead.
* {@linkcode Deno.ftruncateSync} will be removed in Deno 2.0.
*
* @category File System * @category File System
*/ */
export function ftruncateSync(rid: number, len?: number): void; export function ftruncateSync(rid: number, len?: number): void;

View file

@ -4,6 +4,7 @@
// deno-lint-ignore-file prefer-primordials // deno-lint-ignore-file prefer-primordials
import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
import { FsFile } from "ext:deno_fs/30_fs.js";
export function ftruncate( export function ftruncate(
fd: number, fd: number,
@ -19,9 +20,9 @@ export function ftruncate(
if (!callback) throw new Error("No callback function supplied"); if (!callback) throw new Error("No callback function supplied");
Deno.ftruncate(fd, len).then(() => callback(null), callback); new FsFile(fd).truncate(len).then(() => callback(null), callback);
} }
export function ftruncateSync(fd: number, len?: number) { export function ftruncateSync(fd: number, len?: number) {
Deno.ftruncateSync(fd, len); new FsFile(fd).truncateSync(len);
} }

View file

@ -92,8 +92,22 @@ const denoNs = {
lstat: fs.lstat, lstat: fs.lstat,
truncateSync: fs.truncateSync, truncateSync: fs.truncateSync,
truncate: fs.truncate, truncate: fs.truncate,
ftruncateSync: fs.ftruncateSync, ftruncateSync(rid, len) {
ftruncate: fs.ftruncate, internals.warnOnDeprecatedApi(
"Deno.ftruncateSync()",
new Error().stack,
"Use `Deno.FsFile.truncateSync()` instead.",
);
return fs.ftruncateSync(rid, len);
},
ftruncate(rid, len) {
internals.warnOnDeprecatedApi(
"Deno.ftruncate()",
new Error().stack,
"Use `Deno.FsFile.truncate()` instead.",
);
return fs.ftruncate(rid, len);
},
futime: fs.futime, futime: fs.futime,
futimeSync: fs.futimeSync, futimeSync: fs.futimeSync,
errors: errors.errors, errors: errors.errors,