0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -9,7 +9,7 @@ Deno.test({
name: "Data is written to the file with the correct length", name: "Data is written to the file with the correct length",
async fn() { async fn() {
const tempFile: string = await Deno.makeTempFile(); const tempFile: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(tempFile, { using file = await Deno.open(tempFile, {
create: true, create: true,
write: true, write: true,
read: true, read: true,
@ -21,7 +21,6 @@ Deno.test({
resolve(nwritten); resolve(nwritten);
}); });
}); });
Deno.close(file.rid);
const data = await Deno.readFile(tempFile); const data = await Deno.readFile(tempFile);
await Deno.remove(tempFile); await Deno.remove(tempFile);
@ -35,14 +34,13 @@ Deno.test({
name: "Data is written synchronously to the file with the correct length", name: "Data is written synchronously to the file with the correct length",
fn() { fn() {
const tempFile: string = Deno.makeTempFileSync(); const tempFile: string = Deno.makeTempFileSync();
const file: Deno.FsFile = Deno.openSync(tempFile, { using file = Deno.openSync(tempFile, {
create: true, create: true,
write: true, write: true,
read: true, read: true,
}); });
const buffer = Buffer.from("hello world"); const buffer = Buffer.from("hello world");
const bytesWrite = writeSync(file.rid, buffer, 0, 5); const bytesWrite = writeSync(file.rid, buffer, 0, 5);
Deno.close(file.rid);
const data = Deno.readFileSync(tempFile); const data = Deno.readFileSync(tempFile);
Deno.removeSync(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.stdout.isTerminal() === isatty(Deno.stdout.rid));
assert(Deno.stderr.isTerminal() === isatty(Deno.stderr.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)); assert(!isatty(file.rid));
Deno.close(file.rid);
}); });
Deno.test("[node/tty isatty] returns false for irrelevant values", () => { Deno.test("[node/tty isatty] returns false for irrelevant values", () => {

View file

@ -1977,11 +1977,10 @@ declare namespace Deno {
* *
* ```ts * ```ts
* // if "/foo/bar.txt" contains the text "hello world": * // 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 buf = new Uint8Array(100);
* const numberOfBytesRead = await Deno.read(file.rid, buf); // 11 bytes * const numberOfBytesRead = await Deno.read(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world" * const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
* ``` * ```
* *
* @deprecated Use `reader.read()` instead. {@linkcode Deno.read} will be * @deprecated Use `reader.read()` instead. {@linkcode Deno.read} will be
@ -2010,11 +2009,10 @@ declare namespace Deno {
* *
* ```ts * ```ts
* // if "/foo/bar.txt" contains the text "hello world": * // 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 buf = new Uint8Array(100);
* const numberOfBytesRead = Deno.readSync(file.rid, buf); // 11 bytes * const numberOfBytesRead = Deno.readSync(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world" * const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
* ``` * ```
* *
* @deprecated Use `reader.readSync()` instead. {@linkcode Deno.readSync} * @deprecated Use `reader.readSync()` instead. {@linkcode Deno.readSync}
@ -2037,9 +2035,8 @@ declare namespace Deno {
* ```ts * ```ts
* const encoder = new TextEncoder(); * const encoder = new TextEncoder();
* const data = encoder.encode("Hello world"); * 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 * const bytesWritten = await Deno.write(file.rid, data); // 11
* Deno.close(file.rid);
* ``` * ```
* *
* @category I/O * @category I/O
@ -2060,9 +2057,8 @@ declare namespace Deno {
* ```ts * ```ts
* const encoder = new TextEncoder(); * const encoder = new TextEncoder();
* const data = encoder.encode("Hello world"); * 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 * const bytesWritten = Deno.writeSync(file.rid, data); // 11
* Deno.close(file.rid);
* ``` * ```
* *
* @category I/O * @category I/O
@ -2268,6 +2264,9 @@ declare namespace Deno {
* // do work with "file" object * // 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 * @category I/O
*/ */
export function close(rid: number): void; export function close(rid: number): void;
@ -2860,8 +2859,6 @@ declare namespace Deno {
* const ttyRid = Deno.openSync("/dev/tty6").rid; * const ttyRid = Deno.openSync("/dev/tty6").rid;
* console.log(Deno.isatty(nonTTYRid)); // false * console.log(Deno.isatty(nonTTYRid)); // false
* console.log(Deno.isatty(ttyRid)); // true * console.log(Deno.isatty(ttyRid)); // true
* Deno.close(nonTTYRid);
* Deno.close(ttyRid);
* ``` * ```
* *
* @deprecated Use `Deno.stdin.isTerminal()`, `Deno.stdout.isTerminal()` or * @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 type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
import { getValidatedFd } from "ext:deno_node/internal/fs/utils.mjs"; import { getValidatedFd } from "ext:deno_node/internal/fs/utils.mjs";
import { core } from "ext:core/mod.js";
export function close(fd: number, callback: CallbackWithError) { export function close(fd: number, callback: CallbackWithError) {
fd = getValidatedFd(fd); fd = getValidatedFd(fd);
setTimeout(() => { setTimeout(() => {
let error = null; let error = null;
try { 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) { } catch (err) {
error = err instanceof Error ? err : new Error("[non-error thrown]"); 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) { export function closeSync(fd: number) {
fd = getValidatedFd(fd); 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, ReadOptions,
TextOptionsArgument, TextOptionsArgument,
} from "ext:deno_node/_fs/_fs_common.ts"; } from "ext:deno_node/_fs/_fs_common.ts";
import { core } from "ext:core/mod.js";
interface WriteResult { interface WriteResult {
bytesWritten: number; bytesWritten: number;
@ -134,7 +135,7 @@ export class FileHandle extends EventEmitter {
close(): Promise<void> { close(): Promise<void> {
// Note that Deno.close is not async // 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); internals.warnOnDeprecatedApi("Deno.resources()", new Error().stack);
return core.resources(); return core.resources();
}, },
close: core.close, close(rid) {
internals.warnOnDeprecatedApi(
"Deno.close()",
new Error().stack,
"Use `closer.close()` instead.",
);
core.close(rid);
},
...denoNs, ...denoNs,
// Deno.test and Deno.bench are noops here, but kept for compatibility; so // 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` // that they don't cause errors when used outside of `deno test`/`deno bench`