From dadd8b3d660fd2fd56803f29e1d8b6dd7a2adde9 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Wed, 6 Dec 2023 16:52:59 +0900 Subject: [PATCH] feat(ext/fetch): allow `Deno.HttpClient` to be declared with `using` (#21453) This commit adds a method of `Symbol.dispose` to the object returned from `Deno.createHttpClient`, so we can make use of [explicit resource management](https://github.com/tc39/proposal-explicit-resource-management) by declaring it with `using`. --- cli/tests/unit/fetch_test.ts | 27 +++++++++++++++++++++++++++ cli/tsc/dts/lib.deno.unstable.d.ts | 2 +- ext/fetch/22_http_client.js | 6 ++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index d12a938670..b4d2484409 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -1618,6 +1618,33 @@ Deno.test( }, ); +Deno.test( + { permissions: { net: true } }, + async function createHttpClientExplicitResourceManagement() { + using client = Deno.createHttpClient({}); + const response = await fetch("http://localhost:4545/assets/fixture.json", { + client, + }); + const json = await response.json(); + assertEquals(json.name, "deno"); + }, +); + +Deno.test( + { permissions: { net: true } }, + async function createHttpClientExplicitResourceManagementDoubleClose() { + using client = Deno.createHttpClient({}); + const response = await fetch("http://localhost:4545/assets/fixture.json", { + client, + }); + const json = await response.json(); + assertEquals(json.name, "deno"); + // Close the client even though we declared it with `using` to confirm that + // the cleanup done as per `Symbol.dispose` will not throw any errors. + client.close(); + }, +); + Deno.test({ permissions: { read: false } }, async function fetchFilePerm() { await assertRejects(async () => { await fetch(import.meta.resolve("../testdata/subdir/json_1.json")); diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index 2890a50ff1..bdb0493b0d 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -847,7 +847,7 @@ declare namespace Deno { * * @category Fetch API */ - export interface HttpClient { + export interface HttpClient extends Disposable { /** The resource ID associated with the client. */ rid: number; /** Close the HTTP client. */ diff --git a/ext/fetch/22_http_client.js b/ext/fetch/22_http_client.js index 9d37f1b7fb..45f5de80ea 100644 --- a/ext/fetch/22_http_client.js +++ b/ext/fetch/22_http_client.js @@ -12,6 +12,7 @@ const core = globalThis.Deno.core; const ops = core.ops; +import { SymbolDispose } from "ext:deno_web/00_infra.js"; /** * @param {Deno.CreateHttpClientOptions} options @@ -33,9 +34,14 @@ class HttpClient { constructor(rid) { this.rid = rid; } + close() { core.close(this.rid); } + + [SymbolDispose]() { + core.tryClose(this.rid); + } } const HttpClientPrototype = HttpClient.prototype;