Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stabilize Deno.createHttpClient() #25569

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,6 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
"Run again with `--unstable-cron` flag to enable this API.",
),
];
} else if msg.contains("createHttpClient is not a function") {
return vec![
FixSuggestion::info("Deno.createHttpClient() is an unstable API."),
FixSuggestion::hint(
"Run again with `--unstable-http` flag to enable this API.",
),
];
} else if msg.contains("WebSocketStream is not defined") {
return vec![
FixSuggestion::info("new WebSocketStream() is an unstable API."),
Expand Down
3 changes: 0 additions & 3 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ delete Object.prototype.__proto__;
/** @type {ReadonlySet<string>} */
const unstableDenoProps = new Set([
"AtomicOperation",
"CreateHttpClientOptions",
"DatagramConn",
"HttpClient",
"Kv",
"KvListIterator",
"KvU64",
Expand All @@ -44,7 +42,6 @@ delete Object.prototype.__proto__;
"UnsafeFnPointer",
"UnixConnectOptions",
"UnixListenOptions",
"createHttpClient",
"dlopen",
"listen",
"listenDatagram",
Expand Down
122 changes: 122 additions & 0 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6087,4 +6087,126 @@ declare namespace Deno {
filename: string | URL,
symbols: S,
): DynamicLibrary<S>;

/**
* A custom `HttpClient` for use with {@linkcode fetch} function. This is
* designed to allow custom certificates or proxies to be used with `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* const client = Deno.createHttpClient({ caCerts: [ caCert ] });
* const req = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
*/
export interface HttpClient extends Disposable {
/** Close the HTTP client. */
close(): void;
}

/**
* The options used when creating a {@linkcode Deno.HttpClient}.
*
* @category Fetch
*/
export interface CreateHttpClientOptions {
/** A list of root certificates that will be used in addition to the
* default root certificates to verify the peer's certificate.
*
* Must be in PEM format. */
caCerts?: string[];
/** A HTTP proxy to use for new connections. */
proxy?: Proxy;
/** Sets the maximum number of idle connections per host allowed in the pool. */
poolMaxIdlePerHost?: number;
/** Set an optional timeout for idle sockets being kept-alive.
* Set to false to disable the timeout. */
poolIdleTimeout?: number | false;
/**
* Whether HTTP/1.1 is allowed or not.
*
* @default {true}
*/
http1?: boolean;
/** Whether HTTP/2 is allowed or not.
*
* @default {true}
*/
http2?: boolean;
/** Whether setting the host header is allowed or not.
*
* @default {false}
*/
allowHost?: boolean;
}

/**
* The definition of a proxy when specifying
* {@linkcode Deno.CreateHttpClientOptions}.
*
* @category Fetch
*/
export interface Proxy {
/** The string URL of the proxy server to use. */
url: string;
/** The basic auth credentials to be used against the proxy server. */
basicAuth?: BasicAuth;
}

/**
* Basic authentication credentials to be used with a {@linkcode Deno.Proxy}
* server when specifying {@linkcode Deno.CreateHttpClientOptions}.
*
* @category Fetch
*/
export interface BasicAuth {
/** The username to be used against the proxy server. */
username: string;
/** The password to be used against the proxy server. */
password: string;
}

/** Create a custom HttpClient to use with {@linkcode fetch}. This is an
* extension of the web platform Fetch API which allows Deno to use custom
* TLS certificates and connect via a proxy while using `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* const client = Deno.createHttpClient({ caCerts: [ caCert ] });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @example ```ts
* const client = Deno.createHttpClient({
* proxy: { url: "http://myproxy.com:8080" }
* });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
*/
export function createHttpClient(
options: CreateHttpClientOptions,
): HttpClient;

/**
* Create a custom HttpClient to use with {@linkcode fetch}. This is an
* extension of the web platform Fetch API which allows Deno to use custom
* TLS certificates and connect via a proxy while using `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* // Load a client key and certificate that we'll use to connect
* const key = await Deno.readTextFile("./key.key");
* const cert = await Deno.readTextFile("./cert.crt");
* const client = Deno.createHttpClient({ caCerts: [ caCert ], key, cert });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
*/
export function createHttpClient(
options: CreateHttpClientOptions & TlsCertifiedKeyPem,
): HttpClient;
}
135 changes: 0 additions & 135 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,141 +36,6 @@ declare namespace Deno {
present(): void;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* A custom `HttpClient` for use with {@linkcode fetch} function. This is
* designed to allow custom certificates or proxies to be used with `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* const client = Deno.createHttpClient({ caCerts: [ caCert ] });
* const req = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
* @experimental
*/
export interface HttpClient extends Disposable {
/** Close the HTTP client. */
close(): void;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* The options used when creating a {@linkcode Deno.HttpClient}.
*
* @category Fetch
* @experimental
*/
export interface CreateHttpClientOptions {
/** A list of root certificates that will be used in addition to the
* default root certificates to verify the peer's certificate.
*
* Must be in PEM format. */
caCerts?: string[];
/** A HTTP proxy to use for new connections. */
proxy?: Proxy;
/** Sets the maximum number of idle connections per host allowed in the pool. */
poolMaxIdlePerHost?: number;
/** Set an optional timeout for idle sockets being kept-alive.
* Set to false to disable the timeout. */
poolIdleTimeout?: number | false;
/**
* Whether HTTP/1.1 is allowed or not.
*
* @default {true}
*/
http1?: boolean;
/** Whether HTTP/2 is allowed or not.
*
* @default {true}
*/
http2?: boolean;
/** Whether setting the host header is allowed or not.
*
* @default {false}
*/
allowHost?: boolean;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* The definition of a proxy when specifying
* {@linkcode Deno.CreateHttpClientOptions}.
*
* @category Fetch
* @experimental
*/
export interface Proxy {
/** The string URL of the proxy server to use. */
url: string;
/** The basic auth credentials to be used against the proxy server. */
basicAuth?: BasicAuth;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* Basic authentication credentials to be used with a {@linkcode Deno.Proxy}
* server when specifying {@linkcode Deno.CreateHttpClientOptions}.
*
* @category Fetch
* @experimental
*/
export interface BasicAuth {
/** The username to be used against the proxy server. */
username: string;
/** The password to be used against the proxy server. */
password: string;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* Create a custom HttpClient to use with {@linkcode fetch}. This is an
* extension of the web platform Fetch API which allows Deno to use custom
* TLS certificates and connect via a proxy while using `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* const client = Deno.createHttpClient({ caCerts: [ caCert ] });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @example ```ts
* const client = Deno.createHttpClient({
* proxy: { url: "http://myproxy.com:8080" }
* });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
* @experimental
*/
export function createHttpClient(
options: CreateHttpClientOptions,
): HttpClient;

/** **UNSTABLE**: New API, yet to be vetted.
*
* Create a custom HttpClient to use with {@linkcode fetch}. This is an
* extension of the web platform Fetch API which allows Deno to use custom
* TLS certificates and connect via a proxy while using `fetch()`.
*
* @example ```ts
* const caCert = await Deno.readTextFile("./ca.pem");
* // Load a client key and certificate that we'll use to connect
* const key = await Deno.readTextFile("./key.key");
* const cert = await Deno.readTextFile("./cert.crt");
* const client = Deno.createHttpClient({ caCerts: [ caCert ], key, cert });
* const response = await fetch("https://myserver.com", { client });
* ```
*
* @category Fetch
* @experimental
*/
export function createHttpClient(
options: CreateHttpClientOptions & TlsCertifiedKeyPem,
): HttpClient;

/** **UNSTABLE**: New API, yet to be vetted.
*
* Represents membership of a IPv4 multicast group.
Expand Down
7 changes: 0 additions & 7 deletions ext/http/http_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ static USE_WRITEV: Lazy<bool> = Lazy::new(|| {
false
});

// NOTE(bartlomieju): currently we don't have any unstable HTTP features,
// but let's keep this const here, because:
// a) we still need to support `--unstable-http` flag to not break user's CLI;
// b) we might add more unstable features in the future.
#[allow(dead_code)]
pub const UNSTABLE_FEATURE_NAME: &str = "http";

/// All HTTP/2 connections start with this byte string.
///
/// In HTTP/2, each endpoint is required to send a connection preface as a final confirmation
Expand Down
2 changes: 2 additions & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ const denoNs = {
uid: os.uid,
Command: process.Command,
ChildProcess: process.ChildProcess,
httpClient: httpClient.httpClient,
createHttpClient: httpClient.createHttpClient,
};

// NOTE(bartlomieju): keep IDs in sync with `cli/main.rs`
Expand Down
2 changes: 1 addition & 1 deletion runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub static UNSTABLE_GRANULAR_FLAGS: &[UnstableGranularFlag] = &[
UnstableGranularFlag {
name: ops::http::UNSTABLE_FEATURE_NAME,
help_text: "Enable unstable HTTP APIs",
show_in_help: true,
show_in_help: false,
id: 5,
},
UnstableGranularFlag {
Expand Down
2 changes: 0 additions & 2 deletions runtime/ops/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ fn op_http_start(
.resource_table
.take::<deno_net::io::UnixStreamResource>(tcp_stream_rid)
{
super::check_unstable(state, UNSTABLE_FEATURE_NAME, "Deno.serveHttp");

// This UNIX socket might be used somewhere else. If it's the case, we cannot proceed with the
// process of starting a HTTP server on top of this UNIX socket, so we just return a bad
// resource error. See also: https://github.com/denoland/deno/pull/16242
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/js_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ fn js_unit_test(test: String) {
.arg("--no-lock")
// TODO(bartlomieju): would be better if we could apply this unstable
// flag to particular files, but there's many of them that rely on unstable
// net APIs (`reusePort` in `listen` and `listenTls`; `listenDatagram`, `createHttpClient`)
// net APIs (`reusePort` in `listen` and `listenTls`; `listenDatagram`)
.arg("--unstable-net")
.arg("--unstable-http")
.arg("--location=http://127.0.0.1:4545/")
.arg("--no-prompt");

Expand Down
1 change: 0 additions & 1 deletion tests/integration/node_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ fn node_unit_test(test: String) {
.arg(deno_config_path())
.arg("--no-lock")
.arg("--unstable-broadcast-channel")
.arg("--unstable-http")
.arg("--unstable-net")
// TODO(kt3k): This option is required to pass tls_test.ts,
// but this shouldn't be necessary. tls.connect currently doesn't
Expand Down
11 changes: 0 additions & 11 deletions tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,17 +1829,6 @@ itest!(unstable_cron_enabled {
output: "run/unstable_cron.enabled.out",
});

itest!(unstable_http_disabled {
args: "run --quiet --reload --allow-read run/unstable_http.js",
output: "run/unstable_http.disabled.out",
});

itest!(unstable_http_enabled {
args:
"run --quiet --reload --allow-read --unstable-http run/unstable_http.js",
output: "run/unstable_http.enabled.out",
});

itest!(unstable_net_disabled {
args: "run --quiet --reload --allow-read run/unstable_net.js",
output: "run/unstable_net.disabled.out",
Expand Down
1 change: 0 additions & 1 deletion tests/specs/run/045_proxy/proxy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ async function testFetchProgrammaticProxy() {
"--quiet",
"--reload",
"--allow-net=localhost:4545,localhost:4555",
"--unstable-http",
"programmatic_proxy_client.ts",
],
}).output();
Expand Down
5 changes: 0 additions & 5 deletions tests/specs/run/unstable/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
"exitCode": 1,
"output": "cron.out"
},
"http": {
"args": "run http.ts",
"exitCode": 1,
"output": "http.out"
},
"http_wss": {
"args": "run http_wss.ts",
"exitCode": 1,
Expand Down
Loading