Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/tests/unit/sync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Deno.test(
create: true,
});
const size = 64;
Deno.ftruncateSync(file.rid, size);
file.truncateSync(size);
Deno.fsyncSync(file.rid);
assertEquals(Deno.statSync(filename).size, size);
Deno.close(file.rid);
Expand All @@ -65,7 +65,7 @@ Deno.test(
create: true,
});
const size = 64;
await Deno.ftruncate(file.rid, size);
await file.truncate(size);
await Deno.fsync(file.rid);
assertEquals((await Deno.stat(filename)).size, size);
Deno.close(file.rid);
Expand Down
12 changes: 6 additions & 6 deletions cli/tests/unit/truncate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Deno.test(
write: true,
});

Deno.ftruncateSync(file.rid, 20);
file.truncateSync(20);
assertEquals(Deno.readFileSync(filename).byteLength, 20);
Deno.ftruncateSync(file.rid, 5);
file.truncateSync(5);
assertEquals(Deno.readFileSync(filename).byteLength, 5);
Deno.ftruncateSync(file.rid, -5);
file.truncateSync(-5);
assertEquals(Deno.readFileSync(filename).byteLength, 0);

Deno.close(file.rid);
Expand All @@ -33,11 +33,11 @@ Deno.test(
write: true,
});

await Deno.ftruncate(file.rid, 20);
await file.truncate(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);
await Deno.ftruncate(file.rid, -5);
await file.truncate(-5);
assertEquals((await Deno.readFile(filename)).byteLength, 0);

Deno.close(file.rid);
Expand Down
26 changes: 12 additions & 14 deletions cli/tests/unit_node/_fs/_fs_fsync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,52 @@ import { fsync, fsyncSync } from "node:fs";
Deno.test({
name: "ASYNC: flush any pending data of the given file stream to disk",
async fn() {
const file: string = await Deno.makeTempFile();
const { rid } = await Deno.open(file, {
const filePath = await Deno.makeTempFile();
using file = await Deno.open(filePath, {
read: true,
write: true,
create: true,
});
const size = 64;
await Deno.ftruncate(rid, size);
await file.truncate(size);

await new Promise<void>((resolve, reject) => {
fsync(rid, (err: Error | null) => {
fsync(file.rid, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
async () => {
assertEquals((await Deno.stat(file)).size, size);
assertEquals((await Deno.stat(filePath)).size, size);
},
() => {
fail("No error expected");
},
)
.finally(async () => {
await Deno.remove(file);
Deno.close(rid);
await Deno.remove(filePath);
});
},
});

Deno.test({
name: "SYNC: flush any pending data the given file stream to disk",
fn() {
const file: string = Deno.makeTempFileSync();
const { rid } = Deno.openSync(file, {
const filePath = Deno.makeTempFileSync();
using file = Deno.openSync(filePath, {
read: true,
write: true,
create: true,
});
const size = 64;
Deno.ftruncateSync(rid, size);
file.truncateSync(size);

try {
fsyncSync(rid);
assertEquals(Deno.statSync(file).size, size);
fsyncSync(file.rid);
assertEquals(Deno.statSync(filePath).size, size);
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});
10 changes: 8 additions & 2 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ declare namespace Deno {
* { read: true, write: true, create: true },
* );
* 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);
* console.log(await Deno.readTextFile("my_file.txt")); // H
* ```
Expand All @@ -2185,7 +2185,7 @@ declare namespace Deno {
* { read: true, write: true, create: true },
* );
* Deno.writeSync(file.rid, new TextEncoder().encode("Hello World"));
* Deno.ftruncateSync(file.rid, 1);
* file.truncateSync(1);
* Deno.fsyncSync(file.rid);
* console.log(Deno.readTextFileSync("my_file.txt")); // H
* ```
Expand Down Expand Up @@ -5291,6 +5291,9 @@ declare namespace Deno {
* 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
*/
export function ftruncate(rid: number, len?: number): Promise<void>;
Expand Down Expand Up @@ -5333,6 +5336,9 @@ declare namespace Deno {
* 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
*/
export function ftruncateSync(rid: number, len?: number): void;
Expand Down
5 changes: 3 additions & 2 deletions ext/node/polyfills/_fs/_fs_ftruncate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// deno-lint-ignore-file prefer-primordials

import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
import { FsFile } from "ext:deno_fs/30_fs.js";

export function ftruncate(
fd: number,
Expand All @@ -19,9 +20,9 @@ export function ftruncate(

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) {
Deno.ftruncateSync(fd, len);
new FsFile(fd).truncateSync(len);
}
20 changes: 17 additions & 3 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { core } from "ext:core/mod.js";
import { core, internals } from "ext:core/mod.js";
const {
op_net_listen_udp,
op_net_listen_unixpacket,
Expand Down Expand Up @@ -78,8 +78,22 @@ const denoNs = {
lstat: fs.lstat,
truncateSync: fs.truncateSync,
truncate: fs.truncate,
ftruncateSync: fs.ftruncateSync,
ftruncate: fs.ftruncate,
ftruncateSync(rid, len) {
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,
futimeSync: fs.futimeSync,
errors: errors.errors,
Expand Down