255 lines
9.6 KiB
Rust
255 lines
9.6 KiB
Rust
use std::cell::RefCell;
|
|
use std::pin::Pin;
|
|
use std::rc::{Rc, Weak};
|
|
use std::sync::Arc;
|
|
|
|
use deno_core::error::ModuleLoaderError;
|
|
use deno_core::ModuleLoadResponse;
|
|
use deno_core::ModuleLoader;
|
|
use deno_core::ModuleSource;
|
|
use deno_core::ModuleSourceCode;
|
|
use deno_core::ModuleSpecifier;
|
|
use deno_core::RequestedModuleType;
|
|
|
|
use crate::import_map::ImportMap;
|
|
use crate::ops::ObscuraState;
|
|
|
|
/// Observable network activity for ES-module graphs.
|
|
///
|
|
/// deno_core keeps dynamic-import state inside its private module map. The
|
|
/// browser lifecycle still needs to distinguish a genuinely idle page from a
|
|
/// graph whose fetch future is being advanced in short event-loop slices. A
|
|
/// loader-owned counter provides that signal without treating unrelated
|
|
/// fetch/XHR analytics as render-blocking work.
|
|
#[derive(Debug, Default)]
|
|
pub(crate) struct ModuleLoadActivity {
|
|
pending: std::sync::atomic::AtomicUsize,
|
|
last_activity: std::sync::Mutex<Option<std::time::Instant>>,
|
|
}
|
|
|
|
impl ModuleLoadActivity {
|
|
fn begin(self: &Arc<Self>) -> ModuleLoadGuard {
|
|
self.pending
|
|
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
|
*self
|
|
.last_activity
|
|
.lock()
|
|
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(std::time::Instant::now());
|
|
ModuleLoadGuard(self.clone())
|
|
}
|
|
|
|
pub(crate) fn is_pending_or_recent(&self, grace: std::time::Duration) -> bool {
|
|
if self.pending.load(std::sync::atomic::Ordering::Relaxed) != 0 {
|
|
return true;
|
|
}
|
|
self.last_activity
|
|
.lock()
|
|
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
|
.is_some_and(|last| last.elapsed() <= grace)
|
|
}
|
|
}
|
|
|
|
struct ModuleLoadGuard(Arc<ModuleLoadActivity>);
|
|
|
|
impl Drop for ModuleLoadGuard {
|
|
fn drop(&mut self) {
|
|
let previous = self
|
|
.0
|
|
.pending
|
|
.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
|
|
debug_assert!(previous > 0, "module load activity counter underflow");
|
|
*self
|
|
.0
|
|
.last_activity
|
|
.lock()
|
|
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(std::time::Instant::now());
|
|
}
|
|
}
|
|
|
|
pub struct ObscuraModuleLoader {
|
|
pub base_url: String,
|
|
/// Proxy URL threaded through to every dynamic ES-module fetch (#139).
|
|
/// `None` keeps the pre-#139 direct-connection behaviour for callers
|
|
/// that haven't been updated.
|
|
pub proxy_url: Option<String>,
|
|
/// The owning page's network context. Production runtimes always install
|
|
/// this so every module in a graph uses the same cookie jar, configured
|
|
/// identity, redirect/security policy, interception, and callbacks as the
|
|
/// entry module. Directly-constructed standalone loaders remain supported.
|
|
page_state: Option<Weak<RefCell<ObscuraState>>>,
|
|
/// Directly-constructed loaders still use Obscura's network policy and
|
|
/// connection pool; they simply have an isolated cookie jar.
|
|
standalone_client: Option<Arc<obscura_net::ObscuraHttpClient>>,
|
|
import_map: Rc<RefCell<ImportMap>>,
|
|
activity: Arc<ModuleLoadActivity>,
|
|
}
|
|
|
|
impl ObscuraModuleLoader {
|
|
pub fn new(base_url: &str) -> Self {
|
|
Self::with_proxy(base_url, None)
|
|
}
|
|
|
|
pub fn with_proxy(base_url: &str, proxy_url: Option<String>) -> Self {
|
|
let import_map = Rc::new(RefCell::new(ImportMap::default()));
|
|
Self::with_proxy_and_import_map(base_url, proxy_url, import_map)
|
|
}
|
|
|
|
fn with_proxy_and_import_map(
|
|
base_url: &str,
|
|
proxy_url: Option<String>,
|
|
import_map: Rc<RefCell<ImportMap>>,
|
|
) -> Self {
|
|
let standalone_client = Arc::new(obscura_net::ObscuraHttpClient::with_options(
|
|
Arc::new(obscura_net::CookieJar::new()),
|
|
proxy_url.as_deref(),
|
|
));
|
|
ObscuraModuleLoader {
|
|
base_url: base_url.to_string(),
|
|
proxy_url,
|
|
page_state: None,
|
|
standalone_client: Some(standalone_client),
|
|
import_map,
|
|
activity: Arc::new(ModuleLoadActivity::default()),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn with_page_state(
|
|
base_url: &str,
|
|
proxy_url: Option<String>,
|
|
page_state: &Rc<RefCell<ObscuraState>>,
|
|
import_map: Rc<RefCell<ImportMap>>,
|
|
) -> Self {
|
|
ObscuraModuleLoader {
|
|
base_url: base_url.to_string(),
|
|
proxy_url,
|
|
page_state: Some(Rc::downgrade(page_state)),
|
|
standalone_client: None,
|
|
import_map,
|
|
activity: Arc::new(ModuleLoadActivity::default()),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn activity(&self) -> Arc<ModuleLoadActivity> {
|
|
self.activity.clone()
|
|
}
|
|
}
|
|
|
|
fn io_err(msg: String) -> ModuleLoaderError {
|
|
std::io::Error::new(std::io::ErrorKind::Other, msg).into()
|
|
}
|
|
|
|
impl ModuleLoader for ObscuraModuleLoader {
|
|
fn resolve(
|
|
&self,
|
|
specifier: &str,
|
|
referrer: &str,
|
|
_kind: deno_core::ResolutionKind,
|
|
) -> Result<ModuleSpecifier, ModuleLoaderError> {
|
|
// deno_core represents the root passed to load_side_es_module with a
|
|
// synthetic "." referrer. A browser resolves <script type=module src>
|
|
// as a resource URL before it starts a graph; the document import map
|
|
// must not remap that root URL.
|
|
if referrer == "." {
|
|
return deno_core::resolve_import(specifier, &self.base_url)
|
|
.map_err(|error| error.into());
|
|
}
|
|
|
|
let base = if referrer.is_empty()
|
|
|| referrer.starts_with('<')
|
|
|| referrer == "about:blank"
|
|
{
|
|
&self.base_url
|
|
} else {
|
|
referrer
|
|
};
|
|
|
|
let base = ModuleSpecifier::parse(base)
|
|
.map_err(|e| io_err(format!("Invalid module referrer {}: {}", base, e)))?;
|
|
self.import_map
|
|
.try_borrow_mut()
|
|
.map_err(|_| io_err("Import map is already borrowed".to_string()))?
|
|
.resolve(specifier, &base)
|
|
.map_err(io_err)
|
|
}
|
|
|
|
fn load(
|
|
&self,
|
|
module_specifier: &ModuleSpecifier,
|
|
_maybe_referrer: Option<&ModuleSpecifier>,
|
|
is_dyn_import: bool,
|
|
_requested_module_type: RequestedModuleType,
|
|
) -> ModuleLoadResponse {
|
|
let url = module_specifier.to_string();
|
|
// Capture the loader's proxy here so the async closure below owns a
|
|
// plain Option<String> rather than borrowing &self across an `await`.
|
|
let proxy_url = self.proxy_url.clone();
|
|
let activity = self.activity.clone();
|
|
// Register before returning the future. The lifecycle can inspect the
|
|
// runtime between deno_core accepting the load and first polling it.
|
|
// Keeping the guard inside the future makes cancellation/navigation
|
|
// decrement the count through Drop as well as success and failure.
|
|
let activity_guard = is_dyn_import.then(|| activity.begin());
|
|
let page_network = match self.page_state.as_ref() {
|
|
Some(weak) => (|| {
|
|
let state = weak
|
|
.upgrade()
|
|
.ok_or_else(|| "Module loader page state was dropped".to_string())?;
|
|
let state = state
|
|
.try_borrow()
|
|
.map_err(|_| "Module loader page state is already borrowed".to_string())?;
|
|
let client = state
|
|
.http_client
|
|
.clone()
|
|
.ok_or_else(|| "No http_client wired to module loader".to_string())?;
|
|
Ok((client, state.callbacks.clone()))
|
|
})(),
|
|
None => self
|
|
.standalone_client
|
|
.clone()
|
|
.map(|client| (client, None))
|
|
.ok_or_else(|| "No network context wired to module loader".to_string()),
|
|
};
|
|
|
|
ModuleLoadResponse::Async(Pin::from(Box::new(async move {
|
|
// deno_core propagates `is_dyn_import` to every dependency edge in
|
|
// the recursive graph, so this excludes parser-discovered/static
|
|
// graphs without losing descendant fetches of a lazy graph.
|
|
let _activity_guard = activity_guard;
|
|
tracing::debug!(
|
|
"Loading ES module: {} (proxy: {})",
|
|
url,
|
|
proxy_url.as_deref().unwrap_or("direct")
|
|
);
|
|
|
|
match page_network {
|
|
Ok((client, callbacks)) => {
|
|
let requested = ModuleSpecifier::parse(&url)
|
|
.map_err(|e| io_err(format!("Invalid module URL {}: {}", url, e)))?;
|
|
let resp = client
|
|
.fetch_with_callbacks(&requested, callbacks.as_deref())
|
|
.await
|
|
.map_err(|e| io_err(format!("Failed to fetch module {}: {}", url, e)))?;
|
|
if !(200..=299).contains(&resp.status) {
|
|
return Err(io_err(format!(
|
|
"Module {} returned HTTP {}",
|
|
url, resp.status
|
|
)));
|
|
}
|
|
let found = ModuleSpecifier::parse(resp.url.as_str()).map_err(|e| {
|
|
io_err(format!("Invalid final module URL {}: {}", resp.url, e))
|
|
})?;
|
|
let code = obscura_net::decode_non_html(&resp.body, resp.content_type());
|
|
Ok(ModuleSource::new_with_redirect(
|
|
deno_core::ModuleType::JavaScript,
|
|
ModuleSourceCode::String(code.into()),
|
|
&requested,
|
|
&found,
|
|
None,
|
|
))
|
|
}
|
|
Err(error) => Err(io_err(error)),
|
|
}
|
|
})))
|
|
}
|
|
}
|