feat: deprecate Deno.close() (#22066)

For removal in Deno v2.

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Asher Gomez 2024-01-25 01:59:55 +11:00 committed by GitHub
parent 4af121687c
commit 62786cfebb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 114 additions and 128 deletions

View file

@ -8,7 +8,7 @@ import {
} from "./test_util.ts";
Deno.test({ permissions: { read: true } }, function fstatSyncSuccess() {
const file = Deno.openSync("README.md");
using file = Deno.openSync("README.md");
const fileInfo = Deno.fstatSync(file.rid);
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
@ -18,12 +18,10 @@ Deno.test({ permissions: { read: true } }, function fstatSyncSuccess() {
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");
Deno.close(file.rid);
});
Deno.test({ permissions: { read: true } }, async function fstatSuccess() {
const file = await Deno.open("README.md");
using file = await Deno.open("README.md");
const fileInfo = await Deno.fstat(file.rid);
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
@ -33,8 +31,6 @@ Deno.test({ permissions: { read: true } }, async function fstatSuccess() {
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");
Deno.close(file.rid);
});
Deno.test(

View file

@ -5,7 +5,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
function fdatasyncSyncSuccess() {
const filename = Deno.makeTempDirSync() + "/test_fdatasyncSync.txt";
const file = Deno.openSync(filename, {
using file = Deno.openSync(filename, {
read: true,
write: true,
create: true,
@ -13,8 +13,6 @@ Deno.test(
const data = new Uint8Array(64);
Deno.writeSync(file.rid, data);
Deno.fdatasyncSync(file.rid);
assertEquals(Deno.readFileSync(filename), data);
Deno.close(file.rid);
Deno.removeSync(filename);
},
);
@ -23,7 +21,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
async function fdatasyncSuccess() {
const filename = (await Deno.makeTempDir()) + "/test_fdatasync.txt";
const file = await Deno.open(filename, {
using file = await Deno.open(filename, {
read: true,
write: true,
create: true,
@ -32,7 +30,6 @@ Deno.test(
await Deno.write(file.rid, data);
await Deno.fdatasync(file.rid);
assertEquals(await Deno.readFile(filename), data);
Deno.close(file.rid);
await Deno.remove(filename);
},
);
@ -41,7 +38,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
function fsyncSyncSuccess() {
const filename = Deno.makeTempDirSync() + "/test_fsyncSync.txt";
const file = Deno.openSync(filename, {
using file = Deno.openSync(filename, {
read: true,
write: true,
create: true,
@ -50,7 +47,6 @@ Deno.test(
file.truncateSync(size);
Deno.fsyncSync(file.rid);
assertEquals(Deno.statSync(filename).size, size);
Deno.close(file.rid);
Deno.removeSync(filename);
},
);
@ -59,7 +55,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
async function fsyncSuccess() {
const filename = (await Deno.makeTempDir()) + "/test_fsync.txt";
const file = await Deno.open(filename, {
using file = await Deno.open(filename, {
read: true,
write: true,
create: true,
@ -68,7 +64,6 @@ Deno.test(
await file.truncate(size);
await Deno.fsync(file.rid);
assertEquals((await Deno.stat(filename)).size, size);
Deno.close(file.rid);
await Deno.remove(filename);
},
);

View file

@ -5,7 +5,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
function ftruncateSyncSuccess() {
const filename = Deno.makeTempDirSync() + "/test_ftruncateSync.txt";
const file = Deno.openSync(filename, {
using file = Deno.openSync(filename, {
create: true,
read: true,
write: true,
@ -18,7 +18,6 @@ Deno.test(
file.truncateSync(-5);
assertEquals(Deno.readFileSync(filename).byteLength, 0);
Deno.close(file.rid);
Deno.removeSync(filename);
},
);
@ -27,7 +26,7 @@ Deno.test(
{ permissions: { read: true, write: true } },
async function ftruncateSuccess() {
const filename = Deno.makeTempDirSync() + "/test_ftruncate.txt";
const file = await Deno.open(filename, {
using file = await Deno.open(filename, {
create: true,
read: true,
write: true,
@ -40,7 +39,6 @@ Deno.test(
await file.truncate(-5);
assertEquals((await Deno.readFile(filename)).byteLength, 0);
Deno.close(file.rid);
await Deno.remove(filename);
},
);

View file

@ -70,7 +70,7 @@ Deno.test({
name: "Async: Data is written to passed in rid",
async fn() {
const tempFile: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(tempFile, {
using file = await Deno.open(tempFile, {
create: true,
write: true,
read: true,
@ -88,7 +88,6 @@ Deno.test({
fail("No error expected");
})
.finally(async () => {
Deno.close(file.rid);
await Deno.remove(tempFile);
});
},
@ -160,13 +159,12 @@ Deno.test({
name: "Sync: Data is written to passed in rid",
fn() {
const tempFile: string = Deno.makeTempFileSync();
const file: Deno.FsFile = Deno.openSync(tempFile, {
using file = Deno.openSync(tempFile, {
create: true,
write: true,
read: true,
});
appendFileSync(file.rid, "hello world");
Deno.close(file.rid);
const data = Deno.readFileSync(tempFile);
assertEquals(decoder.decode(data), "hello world");
Deno.removeSync(tempFile);

View file

@ -6,32 +6,31 @@ Deno.test({
name:
"ASYNC: flush any pending data operations 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 data = new Uint8Array(64);
await Deno.write(rid, data);
await Deno.write(file.rid, data);
await new Promise<void>((resolve, reject) => {
fdatasync(rid, (err: Error | null) => {
fdatasync(file.rid, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
async () => {
assertEquals(await Deno.readFile(file), data);
assertEquals(await Deno.readFile(filePath), data);
},
() => {
fail("No error expected");
},
)
.finally(async () => {
Deno.close(rid);
await Deno.remove(file);
await Deno.remove(filePath);
});
},
});
@ -40,21 +39,20 @@ Deno.test({
name:
"SYNC: flush any pending data operations of 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 data = new Uint8Array(64);
Deno.writeSync(rid, data);
Deno.writeSync(file.rid, data);
try {
fdatasyncSync(rid);
assertEquals(Deno.readFileSync(file), data);
fdatasyncSync(file.rid);
assertEquals(Deno.readFileSync(filePath), data);
} finally {
Deno.close(rid);
Deno.removeSync(file);
Deno.removeSync(filePath);
}
},
});

View file

@ -7,24 +7,23 @@ import type { BigIntStats, Stats } from "node:fs";
Deno.test({
name: "ASYNC: get a file Stats",
async fn() {
const file = await Deno.makeTempFile();
const { rid } = await Deno.open(file);
const filePath = await Deno.makeTempFile();
using file = await Deno.open(filePath);
await new Promise<Stats>((resolve, reject) => {
fstat(rid, (err: Error | null, stat: Stats) => {
fstat(file.rid, (err: Error | null, stat: Stats) => {
if (err) reject(err);
resolve(stat);
});
})
.then(
(stat) => {
assertStats(stat, Deno.fstatSync(rid));
assertStats(stat, Deno.fstatSync(file.rid));
},
() => fail(),
)
.finally(() => {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
});
},
});
@ -32,22 +31,25 @@ Deno.test({
Deno.test({
name: "ASYNC: get a file BigInt Stats",
async fn() {
const file = await Deno.makeTempFile();
const { rid } = await Deno.open(file);
const filePath = await Deno.makeTempFile();
using file = await Deno.open(filePath);
await new Promise<BigIntStats>((resolve, reject) => {
fstat(rid, { bigint: true }, (err: Error | null, stat: BigIntStats) => {
if (err) reject(err);
resolve(stat);
});
fstat(
file.rid,
{ bigint: true },
(err: Error | null, stat: BigIntStats) => {
if (err) reject(err);
resolve(stat);
},
);
})
.then(
(stat) => assertStatsBigInt(stat, Deno.fstatSync(rid)),
(stat) => assertStatsBigInt(stat, Deno.fstatSync(file.rid)),
() => fail(),
)
.finally(() => {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
});
},
});
@ -55,14 +57,13 @@ Deno.test({
Deno.test({
name: "SYNC: get a file Stats",
fn() {
const file = Deno.makeTempFileSync();
const { rid } = Deno.openSync(file);
const filePath = Deno.makeTempFileSync();
using file = Deno.openSync(filePath);
try {
assertStats(fstatSync(rid), Deno.fstatSync(rid));
assertStats(fstatSync(file.rid), Deno.fstatSync(file.rid));
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});
@ -70,14 +71,16 @@ Deno.test({
Deno.test({
name: "SYNC: get a file BigInt Stats",
fn() {
const file = Deno.makeTempFileSync();
const { rid } = Deno.openSync(file);
const filePath = Deno.makeTempFileSync();
using file = Deno.openSync(filePath);
try {
assertStatsBigInt(fstatSync(rid, { bigint: true }), Deno.fstatSync(rid));
assertStatsBigInt(
fstatSync(file.rid, { bigint: true }),
Deno.fstatSync(file.rid),
);
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});

View file

@ -23,23 +23,23 @@ Deno.test({
Deno.test({
name: "ASYNC: truncate entire file contents",
async fn() {
const file: string = Deno.makeTempFileSync();
await Deno.writeTextFile(file, "hello world");
const { rid } = await Deno.open(file, {
const filePath = Deno.makeTempFileSync();
await Deno.writeTextFile(filePath, "hello world");
using file = await Deno.open(filePath, {
read: true,
write: true,
create: true,
});
await new Promise<void>((resolve, reject) => {
ftruncate(rid, (err: Error | null) => {
ftruncate(file.rid, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
() => {
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.size, 0);
},
() => {
@ -47,8 +47,7 @@ Deno.test({
},
)
.finally(() => {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
});
},
});
@ -56,23 +55,23 @@ Deno.test({
Deno.test({
name: "ASYNC: truncate file to a size of precisely len bytes",
async fn() {
const file: string = Deno.makeTempFileSync();
await Deno.writeTextFile(file, "hello world");
const { rid } = await Deno.open(file, {
const filePath = Deno.makeTempFileSync();
await Deno.writeTextFile(filePath, "hello world");
using file = await Deno.open(filePath, {
read: true,
write: true,
create: true,
});
await new Promise<void>((resolve, reject) => {
ftruncate(rid, 3, (err: Error | null) => {
ftruncate(file.rid, 3, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
() => {
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.size, 3);
},
() => {
@ -80,8 +79,7 @@ Deno.test({
},
)
.finally(() => {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
});
},
});
@ -89,21 +87,20 @@ Deno.test({
Deno.test({
name: "SYNC: truncate entire file contents",
fn() {
const file: string = Deno.makeTempFileSync();
Deno.writeFileSync(file, new TextEncoder().encode("hello world"));
const { rid } = Deno.openSync(file, {
const filePath = Deno.makeTempFileSync();
Deno.writeFileSync(filePath, new TextEncoder().encode("hello world"));
using file = Deno.openSync(filePath, {
read: true,
write: true,
create: true,
});
try {
ftruncateSync(rid);
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
ftruncateSync(file.rid);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.size, 0);
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});
@ -111,21 +108,20 @@ Deno.test({
Deno.test({
name: "SYNC: truncate file to a size of precisely len bytes",
fn() {
const file: string = Deno.makeTempFileSync();
Deno.writeFileSync(file, new TextEncoder().encode("hello world"));
const { rid } = Deno.openSync(file, {
const filePath = Deno.makeTempFileSync();
Deno.writeFileSync(filePath, new TextEncoder().encode("hello world"));
using file = Deno.openSync(filePath, {
read: true,
write: true,
create: true,
});
try {
ftruncateSync(rid, 3);
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
ftruncateSync(file.rid, 3);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.size, 3);
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});

View file

@ -12,18 +12,18 @@ Deno.test({
name:
"ASYNC: change the file system timestamps of the object referenced by path",
async fn() {
const file: string = Deno.makeTempFileSync();
const { rid } = await Deno.open(file, { create: true, write: true });
const filePath = Deno.makeTempFileSync();
using file = await Deno.open(filePath, { create: true, write: true });
await new Promise<void>((resolve, reject) => {
futimes(rid, randomDate, randomDate, (err: Error | null) => {
futimes(file.rid, randomDate, randomDate, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
() => {
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.mtime, randomDate);
assertEquals(fileInfo.atime, randomDate);
},
@ -32,8 +32,7 @@ Deno.test({
},
)
.finally(() => {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
});
},
});
@ -68,19 +67,18 @@ Deno.test({
name:
"SYNC: change the file system timestamps of the object referenced by path",
fn() {
const file: string = Deno.makeTempFileSync();
const { rid } = Deno.openSync(file, { create: true, write: true });
const filePath = Deno.makeTempFileSync();
using file = Deno.openSync(filePath, { create: true, write: true });
try {
futimesSync(rid, randomDate, randomDate);
futimesSync(file.rid, randomDate, randomDate);
const fileInfo: Deno.FileInfo = Deno.lstatSync(file);
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
assertEquals(fileInfo.mtime, randomDate);
assertEquals(fileInfo.atime, randomDate);
} finally {
Deno.removeSync(file);
Deno.close(rid);
Deno.removeSync(filePath);
}
},
});

View file

@ -107,7 +107,7 @@ Deno.test(
"Data is written to correct rid",
async function testCorrectWriteUsingRid() {
const tempFile: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(tempFile, {
using file = await Deno.open(tempFile, {
create: true,
write: true,
read: true,
@ -119,7 +119,6 @@ Deno.test(
resolve();
});
});
Deno.close(file.rid);
const data = await Deno.readFile(tempFile);
await Deno.remove(tempFile);
@ -213,7 +212,7 @@ Deno.test(
if (Deno.build.os === "windows") return;
const filename: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(filename, {
using file = await Deno.open(filename, {
create: true,
write: true,
read: true,
@ -225,7 +224,6 @@ Deno.test(
resolve();
});
});
Deno.close(file.rid);
const fileInfo = await Deno.stat(filename);
await Deno.remove(filename);
@ -264,14 +262,13 @@ Deno.test(
"Data is written synchronously to correct rid",
function testCorrectWriteSyncUsingRid() {
const tempFile: string = Deno.makeTempFileSync();
const file: Deno.FsFile = Deno.openSync(tempFile, {
using file = Deno.openSync(tempFile, {
create: true,
write: true,
read: true,
});
writeFileSync(file.rid, "hello world");
Deno.close(file.rid);
const data = Deno.readFileSync(tempFile);
Deno.removeSync(tempFile);

View file

@ -9,7 +9,7 @@ Deno.test({
name: "Data is written to the file with the correct length",
async fn() {
const tempFile: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(tempFile, {
using file = await Deno.open(tempFile, {
create: true,
write: true,
read: true,
@ -21,7 +21,6 @@ Deno.test({
resolve(nwritten);
});
});
Deno.close(file.rid);
const data = await Deno.readFile(tempFile);
await Deno.remove(tempFile);
@ -35,14 +34,13 @@ Deno.test({
name: "Data is written synchronously to the file with the correct length",
fn() {
const tempFile: string = Deno.makeTempFileSync();
const file: Deno.FsFile = Deno.openSync(tempFile, {
using file = Deno.openSync(tempFile, {
create: true,
write: true,
read: true,
});
const buffer = Buffer.from("hello world");
const bytesWrite = writeSync(file.rid, buffer, 0, 5);
Deno.close(file.rid);
const data = Deno.readFileSync(tempFile);
Deno.removeSync(tempFile);

View file

@ -10,9 +10,8 @@ Deno.test("[node/tty isatty] returns true when fd is a tty, false otherwise", ()
assert(Deno.stdout.isTerminal() === isatty(Deno.stdout.rid));
assert(Deno.stderr.isTerminal() === isatty(Deno.stderr.rid));
const file = Deno.openSync("README.md");
using file = Deno.openSync("README.md");
assert(!isatty(file.rid));
Deno.close(file.rid);
});
Deno.test("[node/tty isatty] returns false for irrelevant values", () => {

View file

@ -1977,11 +1977,10 @@ declare namespace Deno {
*
* ```ts
* // if "/foo/bar.txt" contains the text "hello world":
* const file = await Deno.open("/foo/bar.txt");
* using file = await Deno.open("/foo/bar.txt");
* const buf = new Uint8Array(100);
* const numberOfBytesRead = await Deno.read(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
* ```
*
* @deprecated Use `reader.read()` instead. {@linkcode Deno.read} will be
@ -2010,11 +2009,10 @@ declare namespace Deno {
*
* ```ts
* // if "/foo/bar.txt" contains the text "hello world":
* const file = Deno.openSync("/foo/bar.txt");
* using file = Deno.openSync("/foo/bar.txt");
* const buf = new Uint8Array(100);
* const numberOfBytesRead = Deno.readSync(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
* ```
*
* @deprecated Use `reader.readSync()` instead. {@linkcode Deno.readSync}
@ -2037,9 +2035,8 @@ declare namespace Deno {
* ```ts
* const encoder = new TextEncoder();
* const data = encoder.encode("Hello world");
* const file = await Deno.open("/foo/bar.txt", { write: true });
* using file = await Deno.open("/foo/bar.txt", { write: true });
* const bytesWritten = await Deno.write(file.rid, data); // 11
* Deno.close(file.rid);
* ```
*
* @category I/O
@ -2060,9 +2057,8 @@ declare namespace Deno {
* ```ts
* const encoder = new TextEncoder();
* const data = encoder.encode("Hello world");
* const file = Deno.openSync("/foo/bar.txt", { write: true });
* using file = Deno.openSync("/foo/bar.txt", { write: true });
* const bytesWritten = Deno.writeSync(file.rid, data); // 11
* Deno.close(file.rid);
* ```
*
* @category I/O
@ -2268,6 +2264,9 @@ declare namespace Deno {
* // do work with "file" object
* ```
*
* @deprecated Use `.close()` method on the resource instead.
* {@linkcode Deno.close} will be removed in Deno 2.0.
*
* @category I/O
*/
export function close(rid: number): void;
@ -2860,8 +2859,6 @@ declare namespace Deno {
* const ttyRid = Deno.openSync("/dev/tty6").rid;
* console.log(Deno.isatty(nonTTYRid)); // false
* console.log(Deno.isatty(ttyRid)); // true
* Deno.close(nonTTYRid);
* Deno.close(ttyRid);
* ```
*
* @deprecated Use `Deno.stdin.isTerminal()`, `Deno.stdout.isTerminal()` or

View file

@ -5,13 +5,16 @@
import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
import { getValidatedFd } from "ext:deno_node/internal/fs/utils.mjs";
import { core } from "ext:core/mod.js";
export function close(fd: number, callback: CallbackWithError) {
fd = getValidatedFd(fd);
setTimeout(() => {
let error = null;
try {
Deno.close(fd);
// TODO(@littledivy): Treat `fd` as real file descriptor. `rid` is an
// implementation detail and may change.
core.close(fd);
} catch (err) {
error = err instanceof Error ? err : new Error("[non-error thrown]");
}
@ -21,5 +24,7 @@ export function close(fd: number, callback: CallbackWithError) {
export function closeSync(fd: number) {
fd = getValidatedFd(fd);
Deno.close(fd);
// TODO(@littledivy): Treat `fd` as real file descriptor. `rid` is an
// implementation detail and may change.
core.close(fd);
}

View file

@ -12,6 +12,7 @@ import {
ReadOptions,
TextOptionsArgument,
} from "ext:deno_node/_fs/_fs_common.ts";
import { core } from "ext:core/mod.js";
interface WriteResult {
bytesWritten: number;
@ -134,7 +135,7 @@ export class FileHandle extends EventEmitter {
close(): Promise<void> {
// Note that Deno.close is not async
return Promise.resolve(Deno.close(this.fd));
return Promise.resolve(core.close(this.fd));
}
}

View file

@ -542,7 +542,14 @@ const finalDenoNs = {
internals.warnOnDeprecatedApi("Deno.resources()", new Error().stack);
return core.resources();
},
close: core.close,
close(rid) {
internals.warnOnDeprecatedApi(
"Deno.close()",
new Error().stack,
"Use `closer.close()` instead.",
);
core.close(rid);
},
...denoNs,
// Deno.test and Deno.bench are noops here, but kept for compatibility; so
// that they don't cause errors when used outside of `deno test`/`deno bench`