mirror of
https://github.com/denoland/deno.git
synced 2024-12-27 17:49:08 -05:00
dadd8b3d66
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`.
48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// @ts-check
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
/// <reference path="../web/internal.d.ts" />
|
|
/// <reference path="../url/internal.d.ts" />
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
/// <reference path="./internal.d.ts" />
|
|
/// <reference path="../web/06_streams_types.d.ts" />
|
|
/// <reference path="./lib.deno_fetch.d.ts" />
|
|
/// <reference lib="esnext" />
|
|
|
|
const core = globalThis.Deno.core;
|
|
const ops = core.ops;
|
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
|
|
|
/**
|
|
* @param {Deno.CreateHttpClientOptions} options
|
|
* @returns {HttpClient}
|
|
*/
|
|
function createHttpClient(options) {
|
|
options.caCerts ??= [];
|
|
return new HttpClient(
|
|
ops.op_fetch_custom_client(
|
|
options,
|
|
),
|
|
);
|
|
}
|
|
|
|
class HttpClient {
|
|
/**
|
|
* @param {number} rid
|
|
*/
|
|
constructor(rid) {
|
|
this.rid = rid;
|
|
}
|
|
|
|
close() {
|
|
core.close(this.rid);
|
|
}
|
|
|
|
[SymbolDispose]() {
|
|
core.tryClose(this.rid);
|
|
}
|
|
}
|
|
const HttpClientPrototype = HttpClient.prototype;
|
|
|
|
export { createHttpClient, HttpClient, HttpClientPrototype };
|