ferridriver 0.5.0

Browser automation in Rust with a Playwright-compatible API. Four pluggable backends: CDP pipe, CDP WebSocket, Playwright WebKit, Firefox BiDi.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! `Tracing` — the `context.tracing.*` surface.
//!
//! HAR recording (`startHar` / `stopHar`, Playwright 1.60) is implemented
//! against the context's observed network log: between `start_har` and
//! `stop_har` the context's requests are captured and serialized to a
//! HAR 1.2 archive. Mirrors `client/tracing.ts::startHar` / `stopHar`.
//!
//! The trace `.zip` recorder (`start` / `stop` / `startChunk` /
//! `stopChunk`, with DOM snapshots, screenshots and source attachments)
//! is a separate large subsystem; those methods return a typed
//! [`FerriError::Unsupported`] rather than a placeholder artifact.

use std::path::PathBuf;

use crate::context::ContextRef;
use crate::error::{FerriError, Result};
use crate::network::Request;
use crate::url_matcher::UrlMatcher;

/// `content` policy for `startHar` — whether (and how) response bodies
/// are stored. Mirrors Playwright's `'embed' | 'attach' | 'omit'`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum HarContentPolicy {
  /// Inline the body in `content.text` (base64 for binary).
  Embed,
  /// Playwright stores bodies as separate resources; ferridriver inlines
  /// them like `Embed` (no separate resources dir / zip yet).
  Attach,
  /// Drop bodies entirely.
  Omit,
}

/// `mode` for `startHar`. Mirrors Playwright's `'full' | 'minimal'`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum HarMode {
  Full,
  Minimal,
}

/// Options bag for [`Tracing::start_har`].
#[derive(Default)]
pub struct StartHarOptions {
  pub content: Option<HarContentPolicy>,
  pub mode: Option<HarMode>,
  pub url_filter: Option<UrlMatcher>,
}

/// Live recorder state, stored per-context on [`crate::state::BrowserState`]
/// between `start_har` and `stop_har`.
pub struct HarRecorder {
  pub path: PathBuf,
  pub content: HarContentPolicy,
  pub mode: HarMode,
  pub url_filter: UrlMatcher,
  /// Index into the context's `network_log` at recording start; only
  /// requests appended after this point are written.
  pub start_len: usize,
}

/// `context.tracing` handle. Cheap to construct (wraps a [`ContextRef`]).
pub struct Tracing {
  ctx: ContextRef,
}

impl Tracing {
  #[must_use]
  pub(crate) fn new(ctx: ContextRef) -> Self {
    Self { ctx }
  }

  /// Begin recording network into a HAR file. Playwright:
  /// `tracing.startHar(path, { content?, mode?, urlFilter? })`.
  ///
  /// # Errors
  ///
  /// Errors if a HAR recording is already active, the target is a `.zip`
  /// (zip HAR archives are not implemented), or the context is missing.
  pub async fn start_har(&self, path: impl Into<PathBuf>, options: StartHarOptions) -> Result<()> {
    let path = path.into();
    if path.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip")) {
      return Err(FerriError::unsupported(
        "zip HAR archives are not implemented; use a .har path",
      ));
    }
    let composite = self.ctx.composite();
    let start_len = self.network_log_len().await;
    let recorder = HarRecorder {
      path,
      content: options.content.unwrap_or(HarContentPolicy::Embed),
      mode: options.mode.unwrap_or(HarMode::Full),
      url_filter: options.url_filter.unwrap_or_else(UrlMatcher::any),
      start_len,
    };
    let recorders = self.ctx.har_recorders().await;
    let mut guard = recorders.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    if guard.contains_key(&composite) {
      return Err(FerriError::backend(
        "HAR recording has already been started".to_string(),
      ));
    }
    guard.insert(composite, recorder);
    Ok(())
  }

  /// Stop the active HAR recording and write the archive to disk.
  /// Playwright: `tracing.stopHar()`.
  ///
  /// # Errors
  ///
  /// Errors if no recording is active or the file cannot be written.
  pub async fn stop_har(&self) -> Result<()> {
    let composite = self.ctx.composite();
    let recorder = {
      let recorders = self.ctx.har_recorders().await;
      let mut guard = recorders.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
      guard.remove(&composite)
    };
    let Some(recorder) = recorder else {
      return Err(FerriError::backend("HAR recording has not been started".to_string()));
    };

    let requests = self.network_log_slice(recorder.start_len).await;
    let archive = build_har(&requests, &recorder).await;
    let json =
      serde_json::to_string_pretty(&archive).map_err(|e| FerriError::backend(format!("serialize HAR: {e}")))?;
    std::fs::write(&recorder.path, json)
      .map_err(|e| FerriError::backend(format!("write HAR {}: {e}", recorder.path.display())))?;
    Ok(())
  }

  async fn network_log_len(&self) -> usize {
    match self.ctx.network_log_handle().await {
      Some(log) => log.read().await.len(),
      None => 0,
    }
  }

  async fn network_log_slice(&self, start: usize) -> Vec<Request> {
    match self.ctx.network_log_handle().await {
      Some(log) => {
        let reqs = log.read().await;
        reqs.iter().skip(start).cloned().collect()
      },
      None => Vec::new(),
    }
  }

  /// Playwright: `tracing.start(options?)`. The trace `.zip` recorder is
  /// not implemented.
  ///
  /// # Errors
  ///
  /// Always [`FerriError::Unsupported`].
  pub async fn start(&self) -> Result<()> {
    Err(self.trace_zip_unsupported().await)
  }

  /// Playwright: `tracing.startChunk(options?)`.
  ///
  /// # Errors
  ///
  /// Always [`FerriError::Unsupported`].
  pub async fn start_chunk(&self) -> Result<()> {
    Err(self.trace_zip_unsupported().await)
  }

  /// Playwright: `tracing.stopChunk(options?)`.
  ///
  /// # Errors
  ///
  /// Always [`FerriError::Unsupported`].
  pub async fn stop_chunk(&self) -> Result<()> {
    Err(self.trace_zip_unsupported().await)
  }

  /// Playwright: `tracing.stop(options?)`.
  ///
  /// # Errors
  ///
  /// Always [`FerriError::Unsupported`].
  pub async fn stop(&self) -> Result<()> {
    Err(self.trace_zip_unsupported().await)
  }

  async fn trace_zip_unsupported(&self) -> FerriError {
    let har_active = {
      let recorders = self.ctx.har_recorders().await;
      let guard = recorders.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
      guard.contains_key(&self.ctx.composite())
    };
    let hint = if har_active {
      "a HAR recording is active on this context"
    } else {
      "use startHar/stopHar for network capture"
    };
    FerriError::unsupported(format!(
      "trace.zip recording (start/stop/startChunk/stopChunk) is not implemented; {hint}"
    ))
  }
}

async fn build_har(requests: &[Request], recorder: &HarRecorder) -> HarArchive {
  let mut entries = Vec::new();
  for req in requests {
    if !recorder.url_filter.matches(req.url()) {
      continue;
    }
    entries.push(build_entry(req, recorder).await);
  }
  HarArchive {
    log: HarLogOut {
      version: "1.2",
      creator: HarCreator {
        name: "ferridriver",
        version: env!("CARGO_PKG_VERSION"),
      },
      entries,
    },
  }
}

async fn build_entry(req: &Request, recorder: &HarRecorder) -> HarEntryOut {
  let request_headers = header_pairs(&req.headers());
  let post_data = req.post_data().map(|text| HarPostData {
    mime_type: req
      .headers()
      .get("content-type")
      .cloned()
      .unwrap_or_else(|| "application/octet-stream".to_string()),
    text,
  });

  let (response_out, response_present) = match req.response().await.ok().flatten() {
    Some(resp) => {
      let headers = header_pairs(&resp.headers());
      let mime_type = resp
        .headers()
        .get("content-type")
        .cloned()
        .unwrap_or_else(|| "x-unknown".to_string());
      let content = build_content(&resp, &mime_type, recorder.content).await;
      (
        HarResponseOut {
          status: resp.status(),
          status_text: resp.status_text().to_string(),
          http_version: "HTTP/1.1".to_string(),
          headers,
          content,
          redirect_url: String::new(),
          headers_size: -1,
          body_size: -1,
        },
        true,
      )
    },
    None => (HarResponseOut::empty(), false),
  };

  // `mode: minimal` records only entries that have a response; full keeps
  // request-only entries too. Both still emit the entry shape.
  let _ = (recorder.mode, response_present);

  HarEntryOut {
    started_date_time: now_iso8601(),
    time: 0.0,
    request: HarRequestOut {
      method: req.method().to_string(),
      url: req.url().to_string(),
      http_version: "HTTP/1.1".to_string(),
      headers: request_headers,
      query_string: Vec::new(),
      post_data,
      headers_size: -1,
      body_size: -1,
    },
    response: response_out,
    cache: HarCache {},
    timings: HarTimings {
      send: 0.0,
      wait: 0.0,
      receive: 0.0,
    },
  }
}

async fn build_content(resp: &crate::network::Response, mime_type: &str, policy: HarContentPolicy) -> HarContentOut {
  if policy == HarContentPolicy::Omit {
    return HarContentOut {
      size: 0,
      mime_type: mime_type.to_string(),
      text: None,
      encoding: None,
    };
  }
  match resp.body().await {
    Ok(bytes) => {
      let size = i64::try_from(bytes.len()).unwrap_or(i64::MAX);
      if let Ok(text) = std::str::from_utf8(&bytes) {
        HarContentOut {
          size,
          mime_type: mime_type.to_string(),
          text: Some(text.to_string()),
          encoding: None,
        }
      } else {
        use base64::Engine;
        HarContentOut {
          size,
          mime_type: mime_type.to_string(),
          text: Some(base64::engine::general_purpose::STANDARD.encode(&bytes)),
          encoding: Some("base64".to_string()),
        }
      }
    },
    Err(_) => HarContentOut {
      size: 0,
      mime_type: mime_type.to_string(),
      text: None,
      encoding: None,
    },
  }
}

fn header_pairs(headers: &crate::network::Headers) -> Vec<HarHeaderOut> {
  headers
    .iter()
    .map(|(name, value)| HarHeaderOut {
      name: name.clone(),
      value: value.clone(),
    })
    .collect()
}

/// Format the current wall-clock time as an ISO-8601 UTC string with
/// millisecond precision (`YYYY-MM-DDTHH:MM:SS.mmmZ`). Uses the civil-
/// from-days algorithm so no date dependency is needed.
fn now_iso8601() -> String {
  let now = std::time::SystemTime::now()
    .duration_since(std::time::UNIX_EPOCH)
    .unwrap_or_default();
  let total_ms = i64::try_from(now.as_millis()).unwrap_or(i64::MAX);
  let secs = total_ms.div_euclid(1000);
  let ms = total_ms.rem_euclid(1000);
  let days = secs.div_euclid(86_400);
  let tod = secs.rem_euclid(86_400);
  let (hour, minute, second) = (tod / 3600, (tod % 3600) / 60, tod % 60);
  let (year, month, day) = civil_from_days(days);
  format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{ms:03}Z")
}

/// Howard Hinnant's `civil_from_days`: convert a day count since the Unix
/// epoch into a `(year, month, day)` Gregorian date.
fn civil_from_days(z: i64) -> (i64, u32, u32) {
  let z = z + 719_468;
  let era = z.div_euclid(146_097);
  let doe = z.rem_euclid(146_097);
  let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
  let y = yoe + era * 400;
  let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
  let mp = (5 * doy + 2) / 153;
  let d = u32::try_from(doy - (153 * mp + 2) / 5 + 1).unwrap_or(1);
  let m = u32::try_from(if mp < 10 { mp + 3 } else { mp - 9 }).unwrap_or(1);
  (if m <= 2 { y + 1 } else { y }, m, d)
}

// ── HAR 1.2 serialization shapes ──────────────────────────────────────

#[derive(serde::Serialize)]
struct HarArchive {
  log: HarLogOut,
}

#[derive(serde::Serialize)]
struct HarLogOut {
  version: &'static str,
  creator: HarCreator,
  entries: Vec<HarEntryOut>,
}

#[derive(serde::Serialize)]
struct HarCreator {
  name: &'static str,
  version: &'static str,
}

#[derive(serde::Serialize)]
struct HarEntryOut {
  #[serde(rename = "startedDateTime")]
  started_date_time: String,
  time: f64,
  request: HarRequestOut,
  response: HarResponseOut,
  cache: HarCache,
  timings: HarTimings,
}

#[derive(serde::Serialize)]
struct HarRequestOut {
  method: String,
  url: String,
  #[serde(rename = "httpVersion")]
  http_version: String,
  headers: Vec<HarHeaderOut>,
  #[serde(rename = "queryString")]
  query_string: Vec<HarHeaderOut>,
  #[serde(rename = "postData", skip_serializing_if = "Option::is_none")]
  post_data: Option<HarPostData>,
  #[serde(rename = "headersSize")]
  headers_size: i64,
  #[serde(rename = "bodySize")]
  body_size: i64,
}

#[derive(serde::Serialize)]
struct HarPostData {
  #[serde(rename = "mimeType")]
  mime_type: String,
  text: String,
}

#[derive(serde::Serialize)]
struct HarResponseOut {
  status: i64,
  #[serde(rename = "statusText")]
  status_text: String,
  #[serde(rename = "httpVersion")]
  http_version: String,
  headers: Vec<HarHeaderOut>,
  content: HarContentOut,
  #[serde(rename = "redirectURL")]
  redirect_url: String,
  #[serde(rename = "headersSize")]
  headers_size: i64,
  #[serde(rename = "bodySize")]
  body_size: i64,
}

impl HarResponseOut {
  fn empty() -> Self {
    Self {
      status: 0,
      status_text: String::new(),
      http_version: "HTTP/1.1".to_string(),
      headers: Vec::new(),
      content: HarContentOut {
        size: 0,
        mime_type: "x-unknown".to_string(),
        text: None,
        encoding: None,
      },
      redirect_url: String::new(),
      headers_size: -1,
      body_size: -1,
    }
  }
}

#[derive(serde::Serialize)]
struct HarContentOut {
  size: i64,
  #[serde(rename = "mimeType")]
  mime_type: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  text: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  encoding: Option<String>,
}

#[derive(serde::Serialize)]
struct HarHeaderOut {
  name: String,
  value: String,
}

#[derive(serde::Serialize)]
struct HarCache {}

#[derive(serde::Serialize)]
struct HarTimings {
  send: f64,
  wait: f64,
  receive: f64,
}