feat(ext/http): Make http server parameters configurable (#26785)

This commit makes http server parameters configurable on the extension
initialization via two callbacks users can provide.

The main motivation behind this change is to allow `deno_http` users to
tune the HTTP/2 server to suit their needs, although Deno CLI users will
not benefit from it as no JavaScript interface is exposed to set these
parameters currently.

It is up to users whether to provide hook functions. If not provided,
the default configuration from hyper crate will be used.
This commit is contained in:
Yusuke Tanaka 2024-11-19 10:46:24 +09:00 committed by GitHub
parent df1d36324f
commit 9f26ca4509
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 97 additions and 23 deletions

View file

@ -39,6 +39,8 @@ use deno_net::raw::NetworkStream;
use deno_websocket::ws_create_server_stream;
use flate2::write::GzEncoder;
use flate2::Compression;
use hyper::server::conn::http1;
use hyper::server::conn::http2;
use hyper_util::rt::TokioIo;
use hyper_v014::body::Bytes;
use hyper_v014::body::HttpBody;
@ -96,6 +98,25 @@ pub use request_properties::HttpRequestProperties;
pub use service::UpgradeUnavailableError;
pub use websocket_upgrade::WebSocketUpgradeError;
#[derive(Debug, Default, Clone, Copy)]
pub struct Options {
/// By passing a hook function, the caller can customize various configuration
/// options for the HTTP/2 server.
/// See [`http2::Builder`] for what parameters can be customized.
///
/// If `None`, the default configuration provided by hyper will be used. Note
/// that the default configuration is subject to change in future versions.
pub http2_builder_hook:
Option<fn(http2::Builder<LocalExecutor>) -> http2::Builder<LocalExecutor>>,
/// By passing a hook function, the caller can customize various configuration
/// options for the HTTP/1 server.
/// See [`http1::Builder`] for what parameters can be customized.
///
/// If `None`, the default configuration provided by hyper will be used. Note
/// that the default configuration is subject to change in future versions.
pub http1_builder_hook: Option<fn(http1::Builder) -> http1::Builder>,
}
deno_core::extension!(
deno_http,
deps = [deno_web, deno_net, deno_fetch, deno_websocket],
@ -135,6 +156,12 @@ deno_core::extension!(
http_next::op_http_cancel,
],
esm = ["00_serve.ts", "01_http.js", "02_websocket.ts"],
options = {
options: Options,
},
state = |state, options| {
state.put::<Options>(options.options);
}
);
#[derive(Debug, thiserror::Error)]
@ -1117,7 +1144,7 @@ async fn op_http_upgrade_websocket(
// Needed so hyper can use non Send futures
#[derive(Clone)]
struct LocalExecutor;
pub struct LocalExecutor;
impl<Fut> hyper_v014::rt::Executor<Fut> for LocalExecutor
where