mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
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`.
This commit is contained in:
parent
65993e5efa
commit
dadd8b3d66
3 changed files with 34 additions and 1 deletions
|
@ -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() {
|
Deno.test({ permissions: { read: false } }, async function fetchFilePerm() {
|
||||||
await assertRejects(async () => {
|
await assertRejects(async () => {
|
||||||
await fetch(import.meta.resolve("../testdata/subdir/json_1.json"));
|
await fetch(import.meta.resolve("../testdata/subdir/json_1.json"));
|
||||||
|
|
2
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
2
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
|
@ -847,7 +847,7 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* @category Fetch API
|
* @category Fetch API
|
||||||
*/
|
*/
|
||||||
export interface HttpClient {
|
export interface HttpClient extends Disposable {
|
||||||
/** The resource ID associated with the client. */
|
/** The resource ID associated with the client. */
|
||||||
rid: number;
|
rid: number;
|
||||||
/** Close the HTTP client. */
|
/** Close the HTTP client. */
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
const core = globalThis.Deno.core;
|
const core = globalThis.Deno.core;
|
||||||
const ops = core.ops;
|
const ops = core.ops;
|
||||||
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Deno.CreateHttpClientOptions} options
|
* @param {Deno.CreateHttpClientOptions} options
|
||||||
|
@ -33,9 +34,14 @@ class HttpClient {
|
||||||
constructor(rid) {
|
constructor(rid) {
|
||||||
this.rid = rid;
|
this.rid = rid;
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
core.close(this.rid);
|
core.close(this.rid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[SymbolDispose]() {
|
||||||
|
core.tryClose(this.rid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const HttpClientPrototype = HttpClient.prototype;
|
const HttpClientPrototype = HttpClient.prototype;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue