Skip to main content

bitwarden_wasm_internal/
flight_recorder.rs

1//! WASM bindings for the Flight Recorder.
2
3use bitwarden_ipc::{Endpoint, SubscribeError, wasm::JsIpcClient};
4use bitwarden_logging::{
5    FlightRecorderEvent, flight_recorder_count,
6    ipc::{FlightRecorderCollectorRole, read_flight_recorder_aggregated},
7    write_flight_recorder,
8};
9use bitwarden_threading::cancellation_token::wasm::{AbortSignal, AbortSignalExt};
10
11use crate::init::{LogLevel, convert_level};
12
13/// WASM client for reading Flight Recorder logs.
14///
15/// The underlying buffer is global (initialized in [`init_sdk`](crate::init_sdk)),
16/// so this client is a stateless handle for WASM access.
17#[bitwarden_ffi::wasm_object]
18pub struct FlightRecorderClient;
19
20#[bitwarden_ffi::wasm_export]
21impl FlightRecorderClient {
22    /// Create a new `FlightRecorderClient`.
23    #[wasm_bindgen(constructor)]
24    pub fn new() -> Self {
25        Self
26    }
27
28    /// Read the Flight Recorder events for this client.
29    ///
30    /// In a context with forwarding enabled, requests the collector's events and appends this
31    /// context's IPC events and events not yet sent. If the collector cannot be reached or does
32    /// not answer before `abort_signal` fires, or within 5 seconds without a signal, only this
33    /// context's events are returned. Elsewhere, reads the local buffer.
34    pub async fn read(&self, abort_signal: Option<AbortSignal>) -> Vec<FlightRecorderEvent> {
35        read_flight_recorder_aggregated(abort_signal.map(|s| s.to_cancellation_token())).await
36    }
37
38    /// Get the number of events in this context's buffer without reading their contents.
39    pub fn count(&self) -> usize {
40        flight_recorder_count()
41    }
42
43    /// Ingest a single TypeScript-originated log event into the global buffer,
44    /// honoring the configured level floor.
45    ///
46    /// `timestamp` is milliseconds since the Unix epoch, supplied by the caller
47    /// (`Date.now()`).
48    pub fn write(&self, timestamp: f64, level: LogLevel, target: String, message: String) {
49        let level = convert_level(level);
50        let event = FlightRecorderEvent {
51            timestamp: timestamp as i64,
52            level: level.to_string(),
53            target,
54            message,
55            fields: Default::default(),
56            process: None,
57        };
58        write_flight_recorder(event, level);
59    }
60}
61
62/// Make this context a Flight Recorder forwarder: send captured events, including those captured
63/// so far, to the collector at `destination`. Events from the IPC stack itself stay local. Only
64/// the first call has effect.
65#[bitwarden_ffi::wasm_export(js_name = enableFlightRecorderForwarding)]
66pub fn enable_flight_recorder_forwarding(ipc_client: &JsIpcClient, destination: Endpoint) {
67    bitwarden_logging::ipc::enable_flight_recorder_forwarding(
68        ipc_client.client.clone(),
69        destination,
70    );
71}
72
73/// Make this context the Flight Recorder collector for its client, storing events from its
74/// forwarders and answering their read requests.
75#[bitwarden_ffi::wasm_export(js_name = enableFlightRecorderCollection)]
76pub async fn enable_flight_recorder_collection(
77    ipc_client: &JsIpcClient,
78    role: FlightRecorderCollectorRole,
79) -> Result<(), SubscribeError> {
80    bitwarden_logging::ipc::enable_flight_recorder_collection(ipc_client.client.clone(), role).await
81}
82
83impl Default for FlightRecorderClient {
84    fn default() -> Self {
85        Self::new()
86    }
87}