mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 20:38:55 +00:00
0e2f6e38e7
Some checks are pending
ci / pre-build (push) Waiting to run
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (aarch64, test, linux, debug, ubicloud-standard-16-arm) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (aarch64, test, linux, release, ubicloud-standard-16-arm, true) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (aarch64, test, macos, debug, ${{ github.repository == 'denoland/deno' && startsWith(github.ref, 'refs/tags/') && 'self-hosted' || 'macos-14' }}) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (aarch64, test, macos, release, ${{ (!contains(github.event.pull_request.labels.*.name, 'ci-full') && (github.event_name == 'pull_request')) && 'ubuntu-24.04' || github.reposit… (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, bench, linux, release, ${{ (!contains(github.event.pull_request.labels.*.name, 'ci-full') && (github.event_name == 'pull_request' && !contains(github.event.pull_reques… (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, lint, linux, debug, ubuntu-24.04) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, lint, macos, debug, macos-13) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, lint, windows, debug, windows-2022) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, linux, debug, ubuntu-24.04, true) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, linux, release, ${{ github.repository == 'denoland/deno' && 'ubuntu-24.04-xl' || 'ubuntu-24.04' }}, true, ${{ !startsWith(github.ref, 'refs/tags/') }}) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, macos, debug, macos-13) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, macos, release, ${{ (!contains(github.event.pull_request.labels.*.name, 'ci-full') && (github.event_name == 'pull_request')) && 'ubuntu-24.04' || 'macos-13' }}, … (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, windows, debug, windows-2022) (push) Blocked by required conditions
ci / ${{ matrix.job }} ${{ matrix.profile }} ${{ matrix.os }}-${{ matrix.arch }} (x86_64, test, windows, release, ${{ (!contains(github.event.pull_request.labels.*.name, 'ci-full') && (github.event_name == 'pull_request')) && 'ubuntu-24.04' || github.reposi… (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
This commit makes HTTP client parameters used in `fetch` API configurable on the extension initialization via a callback `client_builder_hook` that users can provide. The main motivation behind this change is to allow `deno_fetch` users to tune the HTTP/2 client 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 a hook function. If not provided, the default configuration from hyper crate will be used. Ref #26785
228 lines
6.2 KiB
Rust
228 lines
6.2 KiB
Rust
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use std::cell::RefCell;
|
|
use std::marker::PhantomData;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use crate::DatabaseHandler;
|
|
use anyhow::Context;
|
|
use async_trait::async_trait;
|
|
use bytes::Bytes;
|
|
use deno_core::error::type_error;
|
|
use deno_core::error::AnyError;
|
|
use deno_core::futures::Stream;
|
|
use deno_core::OpState;
|
|
use deno_fetch::create_http_client;
|
|
use deno_fetch::CreateHttpClientOptions;
|
|
use deno_permissions::PermissionCheckError;
|
|
use deno_tls::rustls::RootCertStore;
|
|
use deno_tls::Proxy;
|
|
use deno_tls::RootCertStoreProvider;
|
|
use deno_tls::TlsKeys;
|
|
use denokv_remote::MetadataEndpoint;
|
|
use denokv_remote::Remote;
|
|
use denokv_remote::RemoteResponse;
|
|
use denokv_remote::RemoteTransport;
|
|
use http_body_util::BodyExt;
|
|
use url::Url;
|
|
|
|
#[derive(Clone)]
|
|
pub struct HttpOptions {
|
|
pub user_agent: String,
|
|
pub root_cert_store_provider: Option<Arc<dyn RootCertStoreProvider>>,
|
|
pub proxy: Option<Proxy>,
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
|
pub client_cert_chain_and_key: TlsKeys,
|
|
}
|
|
|
|
impl HttpOptions {
|
|
pub fn root_cert_store(&self) -> Result<Option<RootCertStore>, AnyError> {
|
|
Ok(match &self.root_cert_store_provider {
|
|
Some(provider) => Some(provider.get_or_try_init()?.clone()),
|
|
None => None,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub trait RemoteDbHandlerPermissions {
|
|
fn check_env(&mut self, var: &str) -> Result<(), PermissionCheckError>;
|
|
fn check_net_url(
|
|
&mut self,
|
|
url: &Url,
|
|
api_name: &str,
|
|
) -> Result<(), PermissionCheckError>;
|
|
}
|
|
|
|
impl RemoteDbHandlerPermissions for deno_permissions::PermissionsContainer {
|
|
#[inline(always)]
|
|
fn check_env(&mut self, var: &str) -> Result<(), PermissionCheckError> {
|
|
deno_permissions::PermissionsContainer::check_env(self, var)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn check_net_url(
|
|
&mut self,
|
|
url: &Url,
|
|
api_name: &str,
|
|
) -> Result<(), PermissionCheckError> {
|
|
deno_permissions::PermissionsContainer::check_net_url(self, url, api_name)
|
|
}
|
|
}
|
|
|
|
pub struct RemoteDbHandler<P: RemoteDbHandlerPermissions + 'static> {
|
|
http_options: HttpOptions,
|
|
_p: std::marker::PhantomData<P>,
|
|
}
|
|
|
|
impl<P: RemoteDbHandlerPermissions> RemoteDbHandler<P> {
|
|
pub fn new(http_options: HttpOptions) -> Self {
|
|
Self {
|
|
http_options,
|
|
_p: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct PermissionChecker<P: RemoteDbHandlerPermissions> {
|
|
state: Rc<RefCell<OpState>>,
|
|
_permissions: PhantomData<P>,
|
|
}
|
|
|
|
impl<P: RemoteDbHandlerPermissions> Clone for PermissionChecker<P> {
|
|
fn clone(&self) -> Self {
|
|
Self {
|
|
state: self.state.clone(),
|
|
_permissions: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<P: RemoteDbHandlerPermissions + 'static> denokv_remote::RemotePermissions
|
|
for PermissionChecker<P>
|
|
{
|
|
fn check_net_url(&self, url: &Url) -> Result<(), anyhow::Error> {
|
|
let mut state = self.state.borrow_mut();
|
|
let permissions = state.borrow_mut::<P>();
|
|
permissions
|
|
.check_net_url(url, "Deno.openKv")
|
|
.map_err(Into::into)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct FetchClient(deno_fetch::Client);
|
|
pub struct FetchResponse(http::Response<deno_fetch::ResBody>);
|
|
|
|
impl RemoteTransport for FetchClient {
|
|
type Response = FetchResponse;
|
|
async fn post(
|
|
&self,
|
|
url: Url,
|
|
headers: http::HeaderMap,
|
|
body: Bytes,
|
|
) -> Result<(Url, http::StatusCode, Self::Response), anyhow::Error> {
|
|
let body = http_body_util::Full::new(body)
|
|
.map_err(|never| match never {})
|
|
.boxed();
|
|
let mut req = http::Request::new(body);
|
|
*req.method_mut() = http::Method::POST;
|
|
*req.uri_mut() = url.as_str().parse()?;
|
|
*req.headers_mut() = headers;
|
|
|
|
let res = self.0.clone().send(req).await?;
|
|
let status = res.status();
|
|
Ok((url, status, FetchResponse(res)))
|
|
}
|
|
}
|
|
|
|
impl RemoteResponse for FetchResponse {
|
|
async fn bytes(self) -> Result<Bytes, anyhow::Error> {
|
|
Ok(self.0.collect().await?.to_bytes())
|
|
}
|
|
fn stream(
|
|
self,
|
|
) -> impl Stream<Item = Result<Bytes, anyhow::Error>> + Send + Sync {
|
|
self.0.into_body().into_data_stream()
|
|
}
|
|
async fn text(self) -> Result<String, anyhow::Error> {
|
|
let bytes = self.bytes().await?;
|
|
Ok(std::str::from_utf8(&bytes)?.into())
|
|
}
|
|
}
|
|
|
|
#[async_trait(?Send)]
|
|
impl<P: RemoteDbHandlerPermissions + 'static> DatabaseHandler
|
|
for RemoteDbHandler<P>
|
|
{
|
|
type DB = Remote<PermissionChecker<P>, FetchClient>;
|
|
|
|
async fn open(
|
|
&self,
|
|
state: Rc<RefCell<OpState>>,
|
|
path: Option<String>,
|
|
) -> Result<Self::DB, AnyError> {
|
|
const ENV_VAR_NAME: &str = "DENO_KV_ACCESS_TOKEN";
|
|
|
|
let Some(url) = path else {
|
|
return Err(type_error("Missing database url"));
|
|
};
|
|
|
|
let Ok(parsed_url) = Url::parse(&url) else {
|
|
return Err(type_error(format!("Invalid database url: {}", url)));
|
|
};
|
|
|
|
{
|
|
let mut state = state.borrow_mut();
|
|
let permissions = state.borrow_mut::<P>();
|
|
permissions.check_env(ENV_VAR_NAME)?;
|
|
permissions.check_net_url(&parsed_url, "Deno.openKv")?;
|
|
}
|
|
|
|
let access_token = std::env::var(ENV_VAR_NAME)
|
|
.map_err(anyhow::Error::from)
|
|
.with_context(|| {
|
|
"Missing DENO_KV_ACCESS_TOKEN environment variable. Please set it to your access token from https://dash.deno.com/account."
|
|
})?;
|
|
|
|
let metadata_endpoint = MetadataEndpoint {
|
|
url: parsed_url.clone(),
|
|
access_token: access_token.clone(),
|
|
};
|
|
|
|
let options = &self.http_options;
|
|
let client = create_http_client(
|
|
&options.user_agent,
|
|
CreateHttpClientOptions {
|
|
root_cert_store: options.root_cert_store()?,
|
|
ca_certs: vec![],
|
|
proxy: options.proxy.clone(),
|
|
dns_resolver: Default::default(),
|
|
unsafely_ignore_certificate_errors: options
|
|
.unsafely_ignore_certificate_errors
|
|
.clone(),
|
|
client_cert_chain_and_key: options
|
|
.client_cert_chain_and_key
|
|
.clone()
|
|
.try_into()
|
|
.unwrap(),
|
|
pool_max_idle_per_host: None,
|
|
pool_idle_timeout: None,
|
|
http1: false,
|
|
http2: true,
|
|
client_builder_hook: None,
|
|
},
|
|
)?;
|
|
let fetch_client = FetchClient(client);
|
|
|
|
let permissions = PermissionChecker {
|
|
state: state.clone(),
|
|
_permissions: PhantomData,
|
|
};
|
|
|
|
let remote = Remote::new(fetch_client, permissions, metadata_endpoint);
|
|
|
|
Ok(remote)
|
|
}
|
|
}
|