summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsher Gomez <ashersaupingomez@gmail.com>2024-01-24 10:27:29 +1100
committerGitHub <noreply@github.com>2024-01-24 00:27:29 +0100
commit947ce41e99637dae4cf46126b8bb2d4107fb9913 (patch)
tree094cdca89b38d358bb801702b4caf5dcb6374c80
parent4eedac3604dad9f366d28868077eb02eddc22661 (diff)
feat: deprecate `Deno.resources()` (#22059)
Most uses of `Deno.resources()` within tests, as they previously checked for leaked resources. This is not needed as the test runner does this automatically. Other internal uses of this API have been replaced with the internal `Deno[Deno.internal].core.resources()`.
-rw-r--r--cli/tests/testdata/run/fetch_response_finalization.js4
-rw-r--r--cli/tests/unit/http_test.ts14
-rw-r--r--cli/tests/unit/read_file_test.ts4
-rw-r--r--cli/tests/unit/read_text_file_test.ts4
-rw-r--r--cli/tests/unit_node/_fs/_fs_appendFile_test.ts20
-rw-r--r--cli/tests/unit_node/_fs/_fs_close_test.ts9
-rw-r--r--cli/tests/unit_node/_fs/_fs_open_test.ts6
-rw-r--r--cli/tests/unit_node/_fs/_fs_rm_test.ts33
-rw-r--r--cli/tests/unit_node/_fs/_fs_rmdir_test.ts32
-rw-r--r--cli/tsc/dts/lib.deno.ns.d.ts4
-rw-r--r--ext/node/polyfills/http2.ts2
-rw-r--r--runtime/js/99_main.js5
-rw-r--r--test_ffi/tests/test.js4
-rw-r--r--test_ffi/tests/thread_safe_test.js2
14 files changed, 32 insertions, 111 deletions
diff --git a/cli/tests/testdata/run/fetch_response_finalization.js b/cli/tests/testdata/run/fetch_response_finalization.js
index dd7c355eb..85fc6afa3 100644
--- a/cli/tests/testdata/run/fetch_response_finalization.js
+++ b/cli/tests/testdata/run/fetch_response_finalization.js
@@ -1,6 +1,6 @@
async function doAFetch() {
const resp = await fetch("http://localhost:4545/README.md");
- console.log(Deno.resources()); // print the current resources
+ console.log(Deno[Deno.internal].core.resources()); // print the current resources
const _resp = resp;
// at this point resp can be GC'ed
}
@@ -13,4 +13,4 @@ globalThis.gc(); // force GC
// the response body is not called and the resource is not closed.
await new Promise((resolve) => setTimeout(resolve, 0));
-console.log(Deno.resources()); // print the current resources
+console.log(Deno[Deno.internal].core.resources()); // print the current resources
diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts
index acdaef903..a7f34f0ac 100644
--- a/cli/tests/unit/http_test.ts
+++ b/cli/tests/unit/http_test.ts
@@ -1084,6 +1084,9 @@ Deno.test(
);
// https://github.com/denoland/deno/issues/11926
+// verify that the only new resource is "httpConnection", to make
+// sure "request" resource is closed even if its body was not read
+// by server handler
Deno.test(
{ permissions: { net: true } },
async function httpServerDoesntLeakResources2() {
@@ -1105,22 +1108,11 @@ Deno.test(
}
})();
- const resourcesBefore = Deno.resources();
const response = await fetch(`http://127.0.0.1:${listenPort}`, {
method: "POST",
body: "hello world",
});
await response.text();
- const resourcesAfter = Deno.resources();
- // verify that the only new resource is "httpConnection", to make
- // sure "request" resource is closed even if its body was not read
- // by server handler
-
- for (const rid of Object.keys(resourcesBefore)) {
- delete resourcesAfter[Number(rid)];
- }
-
- assertEquals(Object.keys(resourcesAfter).length, 1);
listener!.close();
httpConn!.close();
diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts
index 6f5392ff4..24ec1aedc 100644
--- a/cli/tests/unit/read_file_test.ts
+++ b/cli/tests/unit/read_file_test.ts
@@ -75,18 +75,14 @@ Deno.test({ permissions: { read: true } }, function readFileSyncLoop() {
Deno.test(
{ permissions: { read: true } },
async function readFileDoesNotLeakResources() {
- const resourcesBefore = Deno.resources();
await assertRejects(async () => await Deno.readFile("cli"));
- assertEquals(resourcesBefore, Deno.resources());
},
);
Deno.test(
{ permissions: { read: true } },
function readFileSyncDoesNotLeakResources() {
- const resourcesBefore = Deno.resources();
assertThrows(() => Deno.readFileSync("cli"));
- assertEquals(resourcesBefore, Deno.resources());
},
);
diff --git a/cli/tests/unit/read_text_file_test.ts b/cli/tests/unit/read_text_file_test.ts
index 7f5383059..5a64522af 100644
--- a/cli/tests/unit/read_text_file_test.ts
+++ b/cli/tests/unit/read_text_file_test.ts
@@ -73,18 +73,14 @@ Deno.test({ permissions: { read: true } }, function readTextFileSyncLoop() {
Deno.test(
{ permissions: { read: true } },
async function readTextFileDoesNotLeakResources() {
- const resourcesBefore = Deno.resources();
await assertRejects(async () => await Deno.readTextFile("cli"));
- assertEquals(resourcesBefore, Deno.resources());
},
);
Deno.test(
{ permissions: { read: true } },
function readTextFileSyncDoesNotLeakResources() {
- const resourcesBefore = Deno.resources();
assertThrows(() => Deno.readTextFileSync("cli"));
- assertEquals(resourcesBefore, Deno.resources());
},
);
diff --git a/cli/tests/unit_node/_fs/_fs_appendFile_test.ts b/cli/tests/unit_node/_fs/_fs_appendFile_test.ts
index 33c99414f..e70d3401f 100644
--- a/cli/tests/unit_node/_fs/_fs_appendFile_test.ts
+++ b/cli/tests/unit_node/_fs/_fs_appendFile_test.ts
@@ -97,7 +97,6 @@ Deno.test({
Deno.test({
name: "Async: Data is written to passed in file path",
async fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
await new Promise<void>((resolve, reject) => {
appendFile("_fs_appendFile_test_file.txt", "hello world", (err) => {
if (err) reject(err);
@@ -105,7 +104,6 @@ Deno.test({
});
})
.then(async () => {
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
assertEquals(decoder.decode(data), "hello world");
}, (err) => {
@@ -120,7 +118,6 @@ Deno.test({
Deno.test({
name: "Async: Data is written to passed in URL",
async fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
const fileURL = new URL("_fs_appendFile_test_file.txt", import.meta.url);
await new Promise<void>((resolve, reject) => {
appendFile(fileURL, "hello world", (err) => {
@@ -129,7 +126,6 @@ Deno.test({
});
})
.then(async () => {
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile(fromFileUrl(fileURL));
assertEquals(decoder.decode(data), "hello world");
}, (err) => {
@@ -145,7 +141,6 @@ Deno.test({
name:
"Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag",
async fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
const tempFile: string = await Deno.makeTempFile();
await new Promise<void>((resolve, reject) => {
appendFile(tempFile, "hello world", { flag: "ax" }, (err) => {
@@ -153,11 +148,8 @@ Deno.test({
else resolve();
});
})
- .then(() => {
- fail("Expected error to be thrown");
- }, () => {
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
- })
+ .then(() => fail("Expected error to be thrown"))
+ .catch(() => {})
.finally(async () => {
await Deno.remove(tempFile);
});
@@ -184,9 +176,7 @@ Deno.test({
Deno.test({
name: "Sync: Data is written to passed in file path",
fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
appendFileSync("_fs_appendFile_test_file_sync.txt", "hello world");
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
assertEquals(decoder.decode(data), "hello world");
Deno.removeSync("_fs_appendFile_test_file_sync.txt");
@@ -197,14 +187,12 @@ Deno.test({
name:
"Sync: error thrown if attempting to append data to an existing file with 'ax' flag",
fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
const tempFile: string = Deno.makeTempFileSync();
assertThrows(
() => appendFileSync(tempFile, "hello world", { flag: "ax" }),
Error,
"",
);
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
Deno.removeSync(tempFile);
},
});
@@ -212,10 +200,8 @@ Deno.test({
Deno.test({
name: "Sync: Data is written in Uint8Array to passed in file path",
fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
const testData = new TextEncoder().encode("hello world");
appendFileSync("_fs_appendFile_test_file_sync.txt", testData);
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
assertEquals(data, testData);
Deno.removeSync("_fs_appendFile_test_file_sync.txt");
@@ -225,7 +211,6 @@ Deno.test({
Deno.test({
name: "Async: Data is written in Uint8Array to passed in file path",
async fn() {
- const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
const testData = new TextEncoder().encode("hello world");
await new Promise<void>((resolve, reject) => {
appendFile("_fs_appendFile_test_file.txt", testData, (err) => {
@@ -234,7 +219,6 @@ Deno.test({
});
})
.then(async () => {
- assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
assertEquals(data, testData);
}, (err) => {
diff --git a/cli/tests/unit_node/_fs/_fs_close_test.ts b/cli/tests/unit_node/_fs/_fs_close_test.ts
index 22b5a4da9..fb9ce05c3 100644
--- a/cli/tests/unit_node/_fs/_fs_close_test.ts
+++ b/cli/tests/unit_node/_fs/_fs_close_test.ts
@@ -13,18 +13,13 @@ Deno.test({
const tempFile: string = await Deno.makeTempFile();
const file: Deno.FsFile = await Deno.open(tempFile);
- assert(Deno.resources()[file.rid]);
await new Promise<void>((resolve, reject) => {
close(file.rid, (err) => {
if (err !== null) reject();
else resolve();
});
})
- .then(() => {
- assert(!Deno.resources()[file.rid]);
- }, () => {
- fail("No error expected");
- })
+ .catch(() => fail("No error expected"))
.finally(async () => {
await Deno.remove(tempFile);
});
@@ -66,9 +61,7 @@ Deno.test({
const tempFile: string = Deno.makeTempFileSync();
const file: Deno.FsFile = Deno.openSync(tempFile);
- assert(Deno.resources()[file.rid]);
closeSync(file.rid);
- assert(!Deno.resources()[file.rid]);
Deno.removeSync(tempFile);
},
});
diff --git a/cli/tests/unit_node/_fs/_fs_open_test.ts b/cli/tests/unit_node/_fs/_fs_open_test.ts
index af31d24a3..fa79aa68f 100644
--- a/cli/tests/unit_node/_fs/_fs_open_test.ts
+++ b/cli/tests/unit_node/_fs/_fs_open_test.ts
@@ -10,7 +10,6 @@ import {
O_WRONLY,
} from "node:constants";
import {
- assert,
assertEquals,
assertThrows,
fail,
@@ -35,7 +34,6 @@ Deno.test({
})
.then((fd) => {
fd1 = fd;
- assert(Deno.resources()[fd], `${fd}`);
}, () => fail())
.finally(() => closeSync(fd1));
},
@@ -46,7 +44,6 @@ Deno.test({
fn() {
const file = Deno.makeTempFileSync();
const fd = openSync(file, "r");
- assert(Deno.resources()[fd]);
closeSync(fd);
},
});
@@ -58,7 +55,6 @@ Deno.test({
const fd = openSync(file, "a");
assertEquals(typeof fd, "number");
assertEquals(existsSync(file), true);
- assert(Deno.resources()[fd]);
closeSync(fd);
},
});
@@ -225,7 +221,6 @@ Deno.test({
const fd = openSync(file, O_APPEND | O_CREAT | O_WRONLY);
assertEquals(typeof fd, "number");
assertEquals(existsSync(file), true);
- assert(Deno.resources()[fd]);
closeSync(fd);
},
});
@@ -403,7 +398,6 @@ Deno.test("[std/node/fs] open callback isn't called twice if error is thrown", a
fn() {
const file = Deno.makeTempFileSync();
const fd = openSync(file, 0);
- assert(Deno.resources()[fd]);
closeSync(fd);
},
});
diff --git a/cli/tests/unit_node/_fs/_fs_rm_test.ts b/cli/tests/unit_node/_fs/_fs_rm_test.ts
index 10e300684..881ee1c4f 100644
--- a/cli/tests/unit_node/_fs/_fs_rm_test.ts
+++ b/cli/tests/unit_node/_fs/_fs_rm_test.ts
@@ -6,7 +6,7 @@ import {
fail,
} from "../../../../test_util/std/assert/mod.ts";
import { rm, rmSync } from "node:fs";
-import { closeSync, existsSync } from "node:fs";
+import { existsSync } from "node:fs";
import { join } from "../../../../test_util/std/path/mod.ts";
Deno.test({
@@ -26,27 +26,14 @@ Deno.test({
},
});
-function closeRes(before: Deno.ResourceMap, after: Deno.ResourceMap) {
- for (const key in after) {
- if (!before[key]) {
- try {
- closeSync(Number(key));
- } catch (error) {
- return error;
- }
- }
- }
-}
-
Deno.test({
name: "ASYNC: removing non-empty folder",
async fn() {
- const rBefore = Deno.resources();
const dir = Deno.makeTempDirSync();
- Deno.createSync(join(dir, "file1.txt"));
- Deno.createSync(join(dir, "file2.txt"));
+ using _file1 = Deno.createSync(join(dir, "file1.txt"));
+ using _file2 = Deno.createSync(join(dir, "file2.txt"));
Deno.mkdirSync(join(dir, "some_dir"));
- Deno.createSync(join(dir, "some_dir", "file.txt"));
+ using _file = Deno.createSync(join(dir, "some_dir", "file.txt"));
await new Promise<void>((resolve, reject) => {
rm(dir, { recursive: true }, (err) => {
if (err) reject(err);
@@ -56,8 +43,6 @@ Deno.test({
.then(() => assertEquals(existsSync(dir), false), () => fail())
.finally(() => {
if (existsSync(dir)) Deno.removeSync(dir, { recursive: true });
- const rAfter = Deno.resources();
- closeRes(rBefore, rAfter);
});
},
ignore: Deno.build.os === "windows",
@@ -116,17 +101,13 @@ Deno.test({
Deno.test({
name: "SYNC: removing non-empty folder",
fn() {
- const rBefore = Deno.resources();
const dir = Deno.makeTempDirSync();
- Deno.createSync(join(dir, "file1.txt"));
- Deno.createSync(join(dir, "file2.txt"));
+ using _file1 = Deno.createSync(join(dir, "file1.txt"));
+ using _file2 = Deno.createSync(join(dir, "file2.txt"));
Deno.mkdirSync(join(dir, "some_dir"));
- Deno.createSync(join(dir, "some_dir", "file.txt"));
+ using _file = Deno.createSync(join(dir, "some_dir", "file.txt"));
rmSync(dir, { recursive: true });
assertEquals(existsSync(dir), false);
- // closing resources
- const rAfter = Deno.resources();
- closeRes(rBefore, rAfter);
},
ignore: Deno.build.os === "windows",
});
diff --git a/cli/tests/unit_node/_fs/_fs_rmdir_test.ts b/cli/tests/unit_node/_fs/_fs_rmdir_test.ts
index c0e7df865..1fe9e499c 100644
--- a/cli/tests/unit_node/_fs/_fs_rmdir_test.ts
+++ b/cli/tests/unit_node/_fs/_fs_rmdir_test.ts
@@ -1,7 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, fail } from "../../../../test_util/std/assert/mod.ts";
import { rmdir, rmdirSync } from "node:fs";
-import { closeSync } from "node:fs";
import { existsSync } from "node:fs";
import { join } from "../../../../test_util/std/path/mod.ts";
import { assertCallbackErrorUncaught } from "../_test_utils.ts";
@@ -32,27 +31,14 @@ Deno.test({
},
});
-function closeRes(before: Deno.ResourceMap, after: Deno.ResourceMap) {
- for (const key in after) {
- if (!before[key]) {
- try {
- closeSync(Number(key));
- } catch (error) {
- return error;
- }
- }
- }
-}
-
Deno.test({
name: "ASYNC: removing non-empty folder",
async fn() {
- const rBefore = Deno.resources();
const dir = Deno.makeTempDirSync();
- Deno.createSync(join(dir, "file1.txt"));
- Deno.createSync(join(dir, "file2.txt"));
+ using _file1 = Deno.createSync(join(dir, "file1.txt"));
+ using _file2 = Deno.createSync(join(dir, "file2.txt"));
Deno.mkdirSync(join(dir, "some_dir"));
- Deno.createSync(join(dir, "some_dir", "file.txt"));
+ using _file = Deno.createSync(join(dir, "some_dir", "file.txt"));
await new Promise<void>((resolve, reject) => {
rmdir(dir, { recursive: true }, (err) => {
if (err) reject(err);
@@ -62,8 +48,6 @@ Deno.test({
.then(() => assertEquals(existsSync(dir), false), () => fail())
.finally(() => {
if (existsSync(dir)) Deno.removeSync(dir, { recursive: true });
- const rAfter = Deno.resources();
- closeRes(rBefore, rAfter);
});
},
ignore: Deno.build.os === "windows",
@@ -72,17 +56,13 @@ Deno.test({
Deno.test({
name: "SYNC: removing non-empty folder",
fn() {
- const rBefore = Deno.resources();
const dir = Deno.makeTempDirSync();
- Deno.createSync(join(dir, "file1.txt"));
- Deno.createSync(join(dir, "file2.txt"));
+ using _file1 = Deno.createSync(join(dir, "file1.txt"));
+ using _file2 = Deno.createSync(join(dir, "file2.txt"));
Deno.mkdirSync(join(dir, "some_dir"));
- Deno.createSync(join(dir, "some_dir", "file.txt"));
+ using _file = Deno.createSync(join(dir, "some_dir", "file.txt"));
rmdirSync(dir, { recursive: true });
assertEquals(existsSync(dir), false);
- // closing resources
- const rAfter = Deno.resources();
- closeRes(rBefore, rAfter);
},
ignore: Deno.build.os === "windows",
});
diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts
index bc3248d02..83292f2dd 100644
--- a/cli/tsc/dts/lib.deno.ns.d.ts
+++ b/cli/tsc/dts/lib.deno.ns.d.ts
@@ -3889,6 +3889,8 @@ declare namespace Deno {
* A map of open resources that Deno is tracking. The key is the resource ID
* (_rid_) and the value is its representation.
*
+ * @deprecated {@linkcode Deno.resources} will be removed in Deno 2.0.
+ *
* @category Observability */
interface ResourceMap {
[rid: number]: unknown;
@@ -3907,6 +3909,8 @@ declare namespace Deno {
* // { 0: "stdin", 1: "stdout", 2: "stderr", 3: "fsFile" }
* ```
*
+ * @deprecated {@linkcode Deno.resources} will be removed in Deno 2.0.
+ *
* @category Observability
*/
export function resources(): ResourceMap;
diff --git a/ext/node/polyfills/http2.ts b/ext/node/polyfills/http2.ts
index 43b6fe4f1..357697731 100644
--- a/ext/node/polyfills/http2.ts
+++ b/ext/node/polyfills/http2.ts
@@ -316,7 +316,7 @@ function closeSession(session: Http2Session, code?: number, error?: Error) {
session[kDenoConnRid],
session[kDenoClientRid],
);
- console.table(Deno.resources());
+ console.table(Deno[Deno.internal].core.resources());
if (session[kDenoConnRid]) {
core.tryClose(session[kDenoConnRid]);
}
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index a038de518..255c5fb6a 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -534,7 +534,10 @@ const internalSymbol = Symbol("Deno.internal");
const finalDenoNs = {
internal: internalSymbol,
[internalSymbol]: internals,
- resources: core.resources,
+ resources() {
+ internals.warnOnDeprecatedApi("Deno.resources()", new Error().stack);
+ return core.resources();
+ },
close: core.close,
...denoNs,
// Deno.test and Deno.bench are noops here, but kept for compatibility; so
diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js
index 8f4506549..c1aa68fad 100644
--- a/test_ffi/tests/test.js
+++ b/test_ffi/tests/test.js
@@ -20,7 +20,7 @@ const [libPrefix, libSuffix] = {
}[Deno.build.os];
const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
-const resourcesPre = Deno.resources();
+const resourcesPre = Deno[Deno.internal].core.resources();
// dlopen shouldn't panic
assertThrows(() => {
@@ -765,7 +765,7 @@ testOptimized(hash, () => hash());
nestedCallback.close();
addToFooCallback.close();
- const resourcesPost = Deno.resources();
+ const resourcesPost = Deno[Deno.internal].core.resources();
const preStr = JSON.stringify(resourcesPre, null, 2);
const postStr = JSON.stringify(resourcesPost, null, 2);
diff --git a/test_ffi/tests/thread_safe_test.js b/test_ffi/tests/thread_safe_test.js
index b983fd48d..fffa61a04 100644
--- a/test_ffi/tests/thread_safe_test.js
+++ b/test_ffi/tests/thread_safe_test.js
@@ -9,8 +9,6 @@ const [libPrefix, libSuffix] = {
}[Deno.build.os];
const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
-const resourcesPre = Deno.resources();
-
const dylib = Deno.dlopen(libPath, {
store_function: {
parameters: ["function"],