use super::*;
thread_local! {
static CONTENT_WORKSPACE: std::cell::RefCell<pagesift_content::ProcessWorkspace> =
std::cell::RefCell::new(pagesift_content::ProcessWorkspace::default());
}
pub(super) fn fetch_request(url: &str, opts: &ScrapeOptions) -> Result<FetchRequest, ApiError> {
let mut req = FetchRequest::new(url);
for (k, v) in &opts.headers {
req = req.header(k.clone(), v.clone());
}
if let Some(etag) = &opts.if_none_match {
req = req.if_none_match(etag.clone());
}
if let Some(t) = &opts.if_modified_since {
let t = httpdate::parse_http_date(t).map_err(|_| {
ApiError::invalid(format!("ifModifiedSince is not an HTTP date: {t:?}"))
})?;
req = req.if_modified_since(t);
}
req.timeout = opts.timeout.map(Duration::from_millis);
Ok(req)
}
pub(crate) fn chunk_markdown(
md: &str,
spec: &serde_json::Value,
) -> Result<Vec<pagesift_api::types::DocChunk>, ApiError> {
let snake = match spec {
serde_json::Value::Object(m) => serde_json::Value::Object(
m.iter()
.map(|(k, v)| {
let mut out = String::new();
for c in k.chars() {
if c.is_ascii_uppercase() {
out.push('_');
out.push(c.to_ascii_lowercase());
} else {
out.push(c);
}
}
(out, v.clone())
})
.collect(),
),
other => other.clone(),
};
let strategy: pagesift_extract::ChunkStrategy = serde_json::from_value(snake)
.map_err(|e| ApiError::invalid(format!("invalid chunking: {e}")))?;
let chunks = pagesift_extract::chunk(md, &strategy)
.map_err(|e| ApiError::invalid(format!("invalid chunking: {e}")))?;
Ok(chunks
.into_iter()
.map(|c| pagesift_api::types::DocChunk {
text: c.text,
heading_path: c.heading_path,
start: c.start,
end: c.end,
tokens: c.tokens,
})
.collect())
}
pub(super) async fn apply_query_filter(
doc: &mut Document,
q: &pagesift_api::types::QueryFilter,
) -> Result<(), ApiError> {
let Some(md) = doc.markdown.take() else {
return Ok(());
};
let fo = pagesift_extract::QueryFilterOptions {
threshold: q.threshold,
relative: q.relative,
top_k: q.top_k,
max_tokens: q.max_tokens,
min_tokens: q.min_tokens,
headings: q.headings,
};
let query = q.query.clone();
let (f, before) = cpu_task(move || {
let f = pagesift_extract::filter_markdown_by_query(&md, &query, &fo);
(f, pagesift_content::estimate_tokens(&md))
})
.await?;
let ext = doc.metadata.pagesift.get_or_insert_with(Default::default);
if f.kept == 0 && f.total > 0 {
ext.warnings.push("query_filter_no_match".into());
}
ext.query_filter = Some(pagesift_api::types::QueryFilterStats {
kept_blocks: f.kept,
total_blocks: f.total,
tokens_before: before,
tokens_after: pagesift_content::estimate_tokens(&f.markdown),
});
doc.markdown = Some(f.markdown);
Ok(())
}
pub(super) async fn process_html(
html: String,
base: &Url,
status: Option<u16>,
opts: &ScrapeOptions,
) -> Result<(pagesift_content::Document, String), ApiError> {
let copts = pagesift_content::Options {
base_url: Some(base.clone()),
only_main_content: opts.only_main_content,
status,
detect_barrier: true,
clean_html: opts.wants("html"),
links: opts.wants("links"),
images: opts.wants("images"),
fingerprints: opts.wants("changeTracking"),
..Default::default()
};
cpu_task(move || {
let document = CONTENT_WORKSPACE.with(|workspace| {
pagesift_content::process_with_workspace(&html, &copts, &mut workspace.borrow_mut())
});
(document, html)
})
.await
}
pub(super) static CPU_SLOTS: std::sync::LazyLock<tokio::sync::Semaphore> =
std::sync::LazyLock::new(|| {
tokio::sync::Semaphore::new(std::thread::available_parallelism().map_or(4, |n| n.get()))
});
pub(super) async fn cpu_task<T: Send + 'static>(
f: impl FnOnce() -> T + Send + 'static,
) -> Result<T, ApiError> {
let _slot = CPU_SLOTS.acquire().await.expect("semaphore never closed");
tokio::task::spawn_blocking(f)
.await
.map_err(|e| ApiError::new(ErrorCode::Internal, e.to_string()))
}
pub(super) fn check_barrier(doc: &pagesift_content::Document) -> Result<(), ApiError> {
let Some(b) = &doc.barrier else { return Ok(()) };
Err(ApiError::new(
ErrorCode::AccessBarrier,
format!(
"the site returned an access barrier ({} {}); pagesift does not bypass it",
b.vendor,
b.kind.as_str()
),
)
.with_details(serde_json::json!({
"vendor": b.vendor,
"kind": b.kind.as_str(),
"confidence": b.confidence,
"ruleId": b.rule_id,
})))
}
pub(super) fn document(
doc: pagesift_content::Document,
opts: &ScrapeOptions,
raw_html: String,
) -> Document {
Document {
markdown: opts.wants("markdown").then_some(doc.markdown),
html: doc.html,
raw_html: opts.wants("rawHtml").then_some(raw_html),
links: doc.links.map(|l| l.into_iter().map(|l| l.url).collect()),
images: doc.images.map(|i| i.into_iter().map(|i| i.src).collect()),
..Default::default()
}
}
pub(super) fn fill_fingerprints(ext: &mut PagesiftMeta, doc: &pagesift_content::Document) {
match &doc.fingerprints {
Some(f) => {
ext.content_hash = Some(f.markdown.clone());
ext.raw_hash = Some(f.raw_html.clone());
ext.text_hash = Some(f.text.clone());
}
None => ext.content_hash = Some(pagesift_content::hash_hex(doc.markdown.as_bytes())),
}
}
pub(super) fn fill_metadata(meta: &mut Metadata, m: &pagesift_content::Metadata) {
meta.title = m.title.clone();
meta.description = m.description.clone();
meta.language = m.language.clone();
meta.keywords = m.keywords.clone();
meta.robots = m.robots.clone();
meta.favicon = m.favicon.clone();
meta.published_time = m.published_time.clone();
meta.author = m.author.clone();
meta.modified_time = m.modified_time.clone();
for (k, v) in &m.extra {
meta.extra
.entry(firecrawl_meta_key(k))
.or_insert_with(|| v.clone().into());
}
}
pub(super) fn firecrawl_meta_key(raw: &str) -> String {
let mut out = String::with_capacity(raw.len());
let mut upper = false;
for c in raw.chars() {
if matches!(c, ':' | '_' | '-' | '.') {
upper = true;
} else if upper {
out.extend(c.to_uppercase());
upper = false;
} else {
out.push(c);
}
}
out
}
pub(super) fn finish(
mut doc: Document,
mut meta: Metadata,
mut ext: PagesiftMeta,
mut timings: BTreeMap<String, f64>,
warnings: Vec<String>,
started: Instant,
) -> Document {
timings.insert("total".into(), ms(started.elapsed()));
ext.timings = timings;
ext.warnings = warnings;
meta.pagesift = Some(ext);
doc.metadata = meta;
doc
}
pub(super) fn ms(d: Duration) -> f64 {
(d.as_secs_f64() * 1000.0 * 100.0).round() / 100.0
}
pub(super) fn is_pdf(body: &[u8], content_type: Option<&str>) -> bool {
body.starts_with(b"%PDF-")
|| content_type.is_some_and(|c| c.to_ascii_lowercase().contains("application/pdf"))
}
pub(crate) fn policy_error(e: pagesift_policy::Error) -> ApiError {
use pagesift_policy::ErrorCode as P;
let code = match e.code() {
P::RobotsDisallowed => ErrorCode::RobotsDisallowed,
P::AccessBarrier => ErrorCode::AccessBarrier,
P::RateLimited => ErrorCode::RateLimited,
P::HostCooldown => ErrorCode::HostCooldown,
P::Timeout => ErrorCode::Timeout,
P::SsrfBlocked => ErrorCode::SsrfBlocked,
P::TooLarge => ErrorCode::TooLarge,
P::UpstreamError => ErrorCode::UpstreamError,
P::UnsupportedOption => ErrorCode::UnsupportedOption,
P::InvalidRequest => ErrorCode::InvalidRequest,
P::DnsFailure => ErrorCode::DnsFailure,
P::NetworkError => ErrorCode::NetworkError,
_ => ErrorCode::Internal,
};
let retry = e.retry_after().map(|d| d.as_secs().max(1));
let mut err = ApiError::new(code, e.to_string());
if let Some(s) = retry {
err = err.with_retry_after(s);
}
err
}