From 62786cfebb5c9fe36d0930582951f442bdfe9441 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 25 Jan 2024 01:59:55 +1100 Subject: [PATCH] feat: deprecate `Deno.close()` (#22066) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For removal in Deno v2. --------- Co-authored-by: Bartek IwaƄczuk --- cli/tests/unit/stat_test.ts | 8 +-- cli/tests/unit/sync_test.ts | 13 ++--- cli/tests/unit/truncate_test.ts | 6 +-- .../unit_node/_fs/_fs_appendFile_test.ts | 6 +-- cli/tests/unit_node/_fs/_fs_fdatasync_test.ts | 26 +++++---- cli/tests/unit_node/_fs/_fs_fstat_test.ts | 53 ++++++++++--------- cli/tests/unit_node/_fs/_fs_ftruncate_test.ts | 52 +++++++++--------- cli/tests/unit_node/_fs/_fs_futimes_test.ts | 22 ++++---- cli/tests/unit_node/_fs/_fs_writeFile_test.ts | 9 ++-- cli/tests/unit_node/_fs/_fs_write_test.ts | 6 +-- cli/tests/unit_node/tty_test.ts | 3 +- cli/tsc/dts/lib.deno.ns.d.ts | 17 +++--- ext/node/polyfills/_fs/_fs_close.ts | 9 +++- ext/node/polyfills/internal/fs/handle.ts | 3 +- runtime/js/99_main.js | 9 +++- 15 files changed, 114 insertions(+), 128 deletions(-) diff --git a/cli/tests/unit/stat_test.ts b/cli/tests/unit/stat_test.ts index cbc5909b5b..ccb17b164d 100644 --- a/cli/tests/unit/stat_test.ts +++ b/cli/tests/unit/stat_test.ts @@ -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( diff --git a/cli/tests/unit/sync_test.ts b/cli/tests/unit/sync_test.ts index 97624d270f..c6da9f23f5 100644 --- a/cli/tests/unit/sync_test.ts +++ b/cli/tests/unit/sync_test.ts @@ -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); }, ); diff --git a/cli/tests/unit/truncate_test.ts b/cli/tests/unit/truncate_test.ts index 97d4db62db..95b76052d3 100644 --- a/cli/tests/unit/truncate_test.ts +++ b/cli/tests/unit/truncate_test.ts @@ -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); }, ); diff --git a/cli/tests/unit_node/_fs/_fs_appendFile_test.ts b/cli/tests/unit_node/_fs/_fs_appendFile_test.ts index e70d3401f2..a7dec7e7c8 100644 --- a/cli/tests/unit_node/_fs/_fs_appendFile_test.ts +++ b/cli/tests/unit_node/_fs/_fs_appendFile_test.ts @@ -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); diff --git a/cli/tests/unit_node/_fs/_fs_fdatasync_test.ts b/cli/tests/unit_node/_fs/_fs_fdatasync_test.ts index 7ea42a512f..6a58eba129 100644 --- a/cli/tests/unit_node/_fs/_fs_fdatasync_test.ts +++ b/cli/tests/unit_node/_fs/_fs_fdatasync_test.ts @@ -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((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); } }, }); diff --git a/cli/tests/unit_node/_fs/_fs_fstat_test.ts b/cli/tests/unit_node/_fs/_fs_fstat_test.ts index a2c2ae00eb..963f79abcc 100644 --- a/cli/tests/unit_node/_fs/_fs_fstat_test.ts +++ b/cli/tests/unit_node/_fs/_fs_fstat_test.ts @@ -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((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((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); } }, }); diff --git a/cli/tests/unit_node/_fs/_fs_ftruncate_test.ts b/cli/tests/unit_node/_fs/_fs_ftruncate_test.ts index 4c2c347892..ef59f05777 100644 --- a/cli/tests/unit_node/_fs/_fs_ftruncate_test.ts +++ b/cli/tests/unit_node/_fs/_fs_ftruncate_test.ts @@ -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((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((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); } }, }); diff --git a/cli/tests/unit_node/_fs/_fs_futimes_test.ts b/cli/tests/unit_node/_fs/_fs_futimes_test.ts index 7df8be5e27..2bfe0175a5 100644 --- a/cli/tests/unit_node/_fs/_fs_futimes_test.ts +++ b/cli/tests/unit_node/_fs/_fs_futimes_test.ts @@ -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((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); } }, }); diff --git a/cli/tests/unit_node/_fs/_fs_writeFile_test.ts b/cli/tests/unit_node/_fs/_fs_writeFile_test.ts index e4bb41d9eb..0bff905946 100644 --- a/cli/tests/unit_node/_fs/_fs_writeFile_test.ts +++ b/cli/tests/unit_node/_fs/_fs_writeFile_test.ts @@ -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); diff --git a/cli/tests/unit_node/_fs/_fs_write_test.ts b/cli/tests/unit_node/_fs/_fs_write_test.ts index 2c5c3d58ec..ef538e9e46 100644 --- a/cli/tests/unit_node/_fs/_fs_write_test.ts +++ b/cli/tests/unit_node/_fs/_fs_write_test.ts @@ -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); diff --git a/cli/tests/unit_node/tty_test.ts b/cli/tests/unit_node/tty_test.ts index 8e2f66f9ef..f72eb088b8 100644 --- a/cli/tests/unit_node/tty_test.ts +++ b/cli/tests/unit_node/tty_test.ts @@ -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", () => { diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index 351610fd24..b255ea6e78 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -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 diff --git a/ext/node/polyfills/_fs/_fs_close.ts b/ext/node/polyfills/_fs/_fs_close.ts index 9938668b31..fd01a0336a 100644 --- a/ext/node/polyfills/_fs/_fs_close.ts +++ b/ext/node/polyfills/_fs/_fs_close.ts @@ -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); } diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts index b0097b4249..ce218f24ec 100644 --- a/ext/node/polyfills/internal/fs/handle.ts +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -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 { // Note that Deno.close is not async - return Promise.resolve(Deno.close(this.fd)); + return Promise.resolve(core.close(this.fd)); } } diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 7057f6d627..fe3dab58b7 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -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`