Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(fetch): pass through raw responses by default

ldm0c84cbebdfe37
diff --git a/moli/src/app.rs b/moli/src/app.rs
index 49252a740..f5be12a0b 100644
--- a/moli/src/app.rs
+++ b/moli/src/app.rs
@@ -96,7 +96,7 @@ pub async fn run_cli_with_config<W: Write>(
));
}
let rendered =
- fetch_dump::render_raw_document_dump(&raw_document, &config.fetch)
+ fetch_dump::render_raw_document_output(&raw_document, &config.fetch)
.map_err(|error| with_fetch_context(error, &args.url))?;
stdout
.write_all(&rendered)
diff --git a/moli/src/cli.rs b/moli/src/cli.rs
index 4e1b7cac2..3d83eb407 100644
--- a/moli/src/cli.rs
+++ b/moli/src/cli.rs
@@ -37,10 +37,12 @@ pub enum Commands {
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct FetchArgs {
- /// Select the fetch output format. `screenshot` writes a viewport PNG,
- /// `screenshot_full` writes a full-document PNG, and `pdf` writes a
- /// paginated PDF directly to stdout; all three require layout. Dump output
- /// is quiet by default; pass `--log-level` to opt into diagnostic logs.
+ /// Select the fetch output format. When omitted, rendered HTML documents
+ /// are serialized and raw responses such as PDFs or videos are written
+ /// verbatim. `screenshot` writes a viewport PNG, `screenshot_full` writes a
+ /// full-document PNG, and `pdf` writes a paginated PDF directly to stdout;
+ /// all three require layout. Dump output is quiet by default; pass
+ /// `--log-level` to opt into diagnostic logs.
#[arg(short, long, value_enum)]
pub dump: Option<DumpFormat>,
diff --git a/moli/src/fetch_dump/mod.rs b/moli/src/fetch_dump/mod.rs
index 6e4258696..e0f3c07d1 100644
--- a/moli/src/fetch_dump/mod.rs
+++ b/moli/src/fetch_dump/mod.rs
@@ -64,13 +64,16 @@ pub async fn render_page_dump_async(
.await
}
-pub fn render_raw_document_dump(
+pub fn render_raw_document_output(
raw: &RawDocument,
command: &FetchCommandConfig,
) -> Result<Vec<u8>> {
- match command.dump_mode.unwrap_or(DumpFormat::Html) {
- DumpFormat::Html => Ok(raw.body_bytes().to_vec()),
- DumpFormat::Json => {
+ match command.dump_mode {
+ None => Ok(raw.body_bytes().to_vec()),
+ Some(DumpFormat::Html) => {
+ bail!("--dump html requires a renderable HTML document, not a raw download")
+ }
+ Some(DumpFormat::Json) => {
let html = String::from_utf8_lossy(raw.body_bytes());
let redirect_chain = raw
.navigation_redirect_chain()
@@ -89,13 +92,15 @@ pub fn render_raw_document_dump(
)?;
Ok(payload.into_bytes())
}
- DumpFormat::Markdown
- | DumpFormat::Screenshot
- | DumpFormat::ScreenshotFull
- | DumpFormat::Pdf
- | DumpFormat::SemanticTree
- | DumpFormat::SemanticTreeText => {
- anyhow::bail!("raw non-HTML document output only supports --dump html or --dump json")
+ Some(
+ DumpFormat::Markdown
+ | DumpFormat::Screenshot
+ | DumpFormat::ScreenshotFull
+ | DumpFormat::Pdf
+ | DumpFormat::SemanticTree
+ | DumpFormat::SemanticTreeText,
+ ) => {
+ bail!("raw download output only supports automatic output or --dump json")
}
}
}
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index 8d68632c6..5fdfcd61d 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -155,6 +155,18 @@ fn run_fetch_cli_with_default_wait_and_dump(url: &str, dump: &str) -> Result<Out
])
}
+fn run_fetch_cli_without_dump(url: &str) -> Result<Output> {
+ run_moli([
+ "moli",
+ "fetch",
+ "--log-level",
+ "error",
+ "--http-no-proxy",
+ "*",
+ url,
+ ])
+}
+
fn run_moli<I, S>(args: I) -> Result<Output>
where
I: IntoIterator<Item = S>,
@@ -266,6 +278,7 @@ struct CacheableFixtureServer {
const PDF_FIXTURE_BODY: &[u8] =
b"%PDF-1.7\n% moli raw document fixture\n1 0 obj\n<<>>\nendobj\n%%EOF\n";
+const VIDEO_FIXTURE_BODY: &[u8] = b"\0\0\0\x18ftypmp42\xff\0moli-video-fixture";
struct BinaryDocumentFixtureServer {
base_url: String,
@@ -467,10 +480,18 @@ impl BinaryDocumentFixtureServer {
)
}
+ async fn inline_video() -> impl IntoResponse {
+ (
+ [("content-type", "video/mp4; codecs=\"avc1.42E01E\"")],
+ VIDEO_FIXTURE_BODY.to_vec(),
+ )
+ }
+
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
let app = Router::new()
.route("/inline.pdf", get(inline_pdf))
+ .route("/clip.mp4", get(inline_video))
.route("/attachment.html", get(attachment_pdf));
let task = tokio::spawn(async move {
axum::serve(listener, app)
@@ -1001,11 +1022,11 @@ fn binary_dump_modes_require_layout() -> Result<()> {
}
#[test]
-fn cli_fetch_inline_pdf_dump_html_bypasses_dcl_page_lifecycle() -> Result<()> {
+fn cli_fetch_without_dump_writes_pdf_verbatim() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;
let server = runtime.block_on(BinaryDocumentFixtureServer::spawn())?;
let url = server.url("/inline.pdf");
- let output = run_fetch_cli_with_wait_until(&url, "domcontentloaded")?;
+ let output = run_fetch_cli_without_dump(&url)?;
runtime.block_on(server.shutdown());
assert!(
@@ -1019,11 +1040,29 @@ fn cli_fetch_inline_pdf_dump_html_bypasses_dcl_page_lifecycle() -> Result<()> {
}
#[test]
-fn cli_fetch_attachment_dump_html_bypasses_dcl_page_lifecycle() -> Result<()> {
+fn cli_fetch_without_dump_writes_video_verbatim() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(BinaryDocumentFixtureServer::spawn())?;
+ let url = server.url("/clip.mp4");
+ let output = run_fetch_cli_without_dump(&url)?;
+ runtime.block_on(server.shutdown());
+
+ assert!(
+ output.status.success(),
+ "moli fetch failed: stdout={:?}\nstderr={}",
+ output.stdout,
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert_eq!(output.stdout, VIDEO_FIXTURE_BODY);
+ Ok(())
+}
+
+#[test]
+fn cli_fetch_without_dump_writes_attachment_verbatim() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;
let server = runtime.block_on(BinaryDocumentFixtureServer::spawn())?;
let url = server.url("/attachment.html");
- let output = run_fetch_cli_with_wait_until(&url, "domcontentloaded")?;
+ let output = run_fetch_cli_without_dump(&url)?;
runtime.block_on(server.shutdown());
assert!(
@@ -1036,6 +1075,29 @@ fn cli_fetch_attachment_dump_html_bypasses_dcl_page_lifecycle() -> Result<()> {
Ok(())
}
+#[test]
+fn cli_dump_html_rejects_raw_documents_without_partial_output() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(BinaryDocumentFixtureServer::spawn())?;
+
+ for path in ["/inline.pdf", "/clip.mp4", "/attachment.html"] {
+ let url = server.url(path);
+ let output = run_fetch_cli_with_dump_and_args(&url, "html", &[])?;
+ let stdout = clean_output(&output.stdout);
+ let stderr = clean_output(&output.stderr);
+ assert!(!output.status.success(), "path={path} stdout={stdout}");
+ assert!(stdout.is_empty(), "path={path} stdout={stdout}");
+ assert_single_fetch_failure_reason(
+ &stderr,
+ &url,
+ "--dump html requires a renderable HTML document, not a raw download",
+ );
+ }
+
+ runtime.block_on(server.shutdown());
+ Ok(())
+}
+
#[test]
fn cli_fetch_raw_document_with_page_wait_uses_fetch_failure_report() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;