use crate::{module_loader::ImportProvider, Error, RuntimeOptions};
pub struct RuntimeBuilder(RuntimeOptions);
impl RuntimeBuilder {
#[must_use]
pub fn new() -> Self {
Self(RuntimeOptions::default())
}
#[must_use]
pub fn with_extension(mut self, extension: deno_core::Extension) -> Self {
self.0.extensions.push(extension);
self
}
#[must_use]
pub fn with_extensions(mut self, extensions: Vec<deno_core::Extension>) -> Self {
self.0.extensions.extend(extensions);
self
}
#[must_use]
pub fn with_default_entrypoint(mut self, entrypoint: String) -> Self {
self.0.default_entrypoint = Some(entrypoint);
self
}
#[must_use]
pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
self.0.timeout = timeout;
self
}
#[must_use]
pub fn with_max_heap_size(mut self, max_heap_size: usize) -> Self {
self.0.max_heap_size = Some(max_heap_size);
self
}
#[must_use]
pub fn with_import_provider(mut self, import_provider: Box<dyn ImportProvider>) -> Self {
self.0.import_provider = Some(import_provider);
self
}
#[must_use]
pub fn with_startup_snapshot(mut self, snapshot: &'static [u8]) -> Self {
self.0.startup_snapshot = Some(snapshot);
self
}
#[must_use]
pub fn with_isolate_params(mut self, params: deno_core::v8::CreateParams) -> Self {
self.0.isolate_params = Some(params);
self
}
#[must_use]
pub fn with_shared_array_buffer_store(
mut self,
store: deno_core::SharedArrayBufferStore,
) -> Self {
self.0.shared_array_buffer_store = Some(store);
self
}
#[must_use]
pub fn with_schema(mut self, schema: impl ToString) -> Self {
self.0.schema_whlist.insert(schema.to_string());
self
}
#[cfg(feature = "crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
#[must_use]
pub fn with_cryto_seed(mut self, seed: u64) -> Self {
self.0.extension_options.crypto_seed = Some(seed);
self
}
#[cfg(feature = "io")]
#[cfg_attr(docsrs, doc(cfg(feature = "io")))]
#[must_use]
pub fn with_io_pipes(mut self, pipes: deno_io::Stdio) -> Self {
self.0.extension_options.io_pipes = Some(pipes);
self
}
#[cfg(feature = "webstorage")]
#[cfg_attr(docsrs, doc(cfg(feature = "webstorage")))]
#[must_use]
pub fn with_webstorage_origin_storage_dir(mut self, dir: std::path::PathBuf) -> Self {
self.0.extension_options.webstorage_origin_storage_dir = Some(dir);
self
}
#[cfg(feature = "cache")]
#[cfg_attr(docsrs, doc(cfg(feature = "cache")))]
#[must_use]
pub fn with_cache(mut self, cache: deno_cache::CreateCache) -> Self {
self.0.extension_options.cache = Some(cache);
self
}
#[cfg(feature = "broadcast_channel")]
#[cfg_attr(docsrs, doc(cfg(feature = "broadcast_channel")))]
#[must_use]
pub fn with_broadcast_channel(
mut self,
channel: deno_broadcast_channel::InMemoryBroadcastChannel,
) -> Self {
self.0.extension_options.broadcast_channel = channel;
self
}
#[cfg(feature = "kv")]
#[cfg_attr(docsrs, doc(cfg(feature = "kv")))]
#[must_use]
pub fn with_kv_store(mut self, kv_store: crate::KvStore) -> Self {
self.0.extension_options.kv_store = kv_store;
self
}
#[cfg(feature = "node_experimental")]
#[cfg_attr(docsrs, doc(cfg(feature = "node_experimental")))]
#[must_use]
pub fn with_node_resolver(mut self, resolver: std::sync::Arc<crate::RustyResolver>) -> Self {
self.0.extension_options.node_resolver = resolver;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_base_url(mut self, base_url: deno_core::ModuleSpecifier) -> Self {
self.0.extension_options.web.base_url = Some(base_url);
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_user_agent(mut self, user_agent: String) -> Self {
self.0.extension_options.web.user_agent = user_agent;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_root_cert_store_provider(
mut self,
root_cert_store_provider: std::sync::Arc<dyn deno_tls::RootCertStoreProvider>,
) -> Self {
self.0.extension_options.web.root_cert_store_provider = Some(root_cert_store_provider);
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_proxy(mut self, proxy: deno_tls::Proxy) -> Self {
self.0.extension_options.web.proxy = Some(proxy);
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_request_builder_hook(
mut self,
hook: fn(&mut http::Request<deno_fetch::ReqBody>) -> Result<(), deno_error::JsErrorBox>,
) -> Self {
self.0.extension_options.web.request_builder_hook = Some(hook);
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_unsafely_ignored_certificate_errors(mut self, domain: impl ToString) -> Self {
match &mut self
.0
.extension_options
.web
.unsafely_ignore_certificate_errors
{
Some(vec) => vec.push(domain.to_string()),
None => {
self.0
.extension_options
.web
.unsafely_ignore_certificate_errors = Some(vec![domain.to_string()]);
}
}
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_client_cert_chain_and_key(mut self, keys: deno_tls::TlsKeys) -> Self {
self.0.extension_options.web.client_cert_chain_and_key = keys;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_file_fetch_handler(
mut self,
handler: std::rc::Rc<dyn deno_fetch::FetchHandler>,
) -> Self {
self.0.extension_options.web.file_fetch_handler = handler;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_permissions(
mut self,
permissions: std::sync::Arc<dyn crate::ext::web::WebPermissions>,
) -> Self {
self.0.extension_options.web.permissions = permissions;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_blob_store(mut self, blob_store: std::sync::Arc<deno_web::BlobStore>) -> Self {
self.0.extension_options.web.blob_store = blob_store;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_client_builder_hook(
mut self,
hook: Option<
fn(hyper_util::client::legacy::Builder) -> hyper_util::client::legacy::Builder,
>,
) -> Self {
self.0.extension_options.web.client_builder_hook = hook;
self
}
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
#[must_use]
pub fn with_web_resolver(mut self, resolver: deno_fetch::dns::Resolver) -> Self {
self.0.extension_options.web.resolver = resolver;
self
}
pub fn build(self) -> Result<crate::Runtime, Error> {
crate::Runtime::new(self.0)
}
#[cfg(feature = "snapshot_builder")]
#[cfg_attr(docsrs, doc(cfg(feature = "snapshot_builder")))]
pub fn build_snapshot(self) -> Result<crate::SnapshotBuilder, Error> {
crate::SnapshotBuilder::new(self.0)
}
}
impl Default for RuntimeBuilder {
fn default() -> Self {
Self::new()
}
}