cow-sdk-core 0.1.0-alpha.10

Shared CoW Protocol core types and validation primitives
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
//! Browser [`HttpTransport`] backed by the realm's global `fetch`.
//!
//! [`FetchTransport`] dispatches each request through the global `fetch` —
//! resolved from [`js_sys::global`], so it runs on a `Window` or a worker — and
//! bridges the returned `Promise` to a `Future`. Failures surface through
//! [`TransportError`] with the same [`TransportErrorClass`] taxonomy as the
//! native `ReqwestTransport`; a non-2xx response surfaces through
//! [`TransportError::HttpStatus`], carrying the status, headers, and body.
//!
//! A per-call timeout wires an [`web_sys::AbortController`] into the request and
//! bounds the whole request-response lifecycle, including the body read; an
//! exceeded timeout surfaces as [`TransportErrorClass::Timeout`]. The base URL
//! is held in [`crate::Redacted`] so it never reaches `Debug`, `Display`, or
//! serde output. Redirects follow the browser default.

use std::time::Duration;

use async_trait::async_trait;
use js_sys::{Array, Function, Object, Promise, Reflect};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{AbortController, Headers, Request, RequestInit, Response};

use crate::{
    DEFAULT_MAX_RESPONSE_BYTES, HttpTransport, Redacted, TransportError, TransportErrorClass,
    TransportResponse,
};

/// Configuration bundle for [`FetchTransport`].
///
/// The base URL is wrapped in [`Redacted`] so it is never emitted through
/// debug, display, or serde representations of the configuration value.
#[derive(Debug, Clone)]
pub struct FetchTransportConfig {
    base_url: Redacted<String>,
    timeout: Option<Duration>,
    max_response_bytes: usize,
}

impl FetchTransportConfig {
    /// Creates a configuration bundle with the supplied base URL and no
    /// request timeout.
    #[must_use]
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: Redacted::new(base_url.into()),
            timeout: None,
            max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES,
        }
    }

    /// Returns a copy of this configuration with an explicit timeout.
    ///
    /// A non-zero timeout wires an [`AbortController`] into the in-flight
    /// request. The resulting `AbortError` surfaces as
    /// [`TransportErrorClass::Timeout`].
    #[must_use]
    pub const fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Returns a copy of this configuration with an explicit maximum
    /// response-body size, in bytes.
    #[must_use]
    pub const fn with_max_response_bytes(mut self, max_response_bytes: usize) -> Self {
        self.max_response_bytes = max_response_bytes;
        self
    }

    /// Returns the configured base URL for deliberate inspection.
    #[must_use]
    pub fn base_url(&self) -> &str {
        self.base_url.as_inner()
    }

    /// Returns the configured request timeout if one is set.
    #[must_use]
    pub const fn timeout(&self) -> Option<Duration> {
        self.timeout
    }

    /// Returns the configured maximum response-body size, in bytes.
    #[must_use]
    pub const fn max_response_bytes(&self) -> usize {
        self.max_response_bytes
    }
}

/// Browser fetch-based [`HttpTransport`].
///
/// Cheap to clone: each dispatch re-reads the global `fetch` from the current
/// realm, so an instance can be cached per client and used on a `Window` or a
/// worker alike.
#[derive(Debug, Clone)]
pub struct FetchTransport {
    base_url: Redacted<String>,
    timeout: Option<Duration>,
    max_response_bytes: usize,
}

impl FetchTransport {
    /// Builds a transport from the supplied configuration.
    #[must_use]
    pub fn new(config: &FetchTransportConfig) -> Self {
        let trimmed = config.base_url.as_inner().trim_end_matches('/').to_owned();
        Self {
            base_url: Redacted::new(trimmed),
            timeout: config.timeout,
            max_response_bytes: config.max_response_bytes,
        }
    }

    /// Returns the configured base URL for deliberate inspection.
    #[must_use]
    pub fn base_url(&self) -> &str {
        self.base_url.as_inner()
    }

    fn resolve_url(&self, path: &str) -> String {
        super::join_request_url(self.base_url.as_inner(), path)
    }

    async fn dispatch(
        &self,
        method: &str,
        path: &str,
        body: Option<&str>,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        #[cfg(feature = "tracing")]
        {
            use tracing::Instrument as _;

            let endpoint = super::span_endpoint(path);
            let bytes_sent = body.map_or(0, str::len);
            let span = tracing::info_span!(
                target: "cow_sdk::transport",
                "transport.dispatch",
                chain = "wasm32",
                method = method,
                endpoint = endpoint,
                bytes_sent = bytes_sent as u64,
                bytes_received = tracing::field::Empty,
            );
            let recorder = span.clone();
            async move {
                let result = self
                    .dispatch_request(method, path, body, headers, timeout)
                    .await;
                if let Some(bytes_received) = bytes_received(&result) {
                    recorder.record("bytes_received", bytes_received as u64);
                }
                result
            }
            .instrument(span)
            .await
        }

        #[cfg(not(feature = "tracing"))]
        {
            self.dispatch_request(method, path, body, headers, timeout)
                .await
        }
    }

    async fn dispatch_request(
        &self,
        method: &str,
        path: &str,
        body: Option<&str>,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        let url = self.resolve_url(path);
        let (global, fetch) = global_fetch_or_configuration_error()?;
        let init = build_request_init(method, body, headers)?;
        // RAII guard: dropping it cancels the timer (gloo calls `clearTimeout`)
        // on every exit path, so no timeout callback outlives its request.
        let _abort_timeout = match timeout.or(self.timeout) {
            Some(timeout) => Some(install_abort_timeout(&init, timeout)?),
            None => None,
        };
        let request = Request::new_with_str_and_init(&url, &init)
            .map_err(|error| configuration_error("could not build fetch request", &error))?;
        let fetch_invocation = fetch
            .call1(&global, request.as_ref())
            .map_err(|error| classify_fetch_rejection(&error))?;
        let response_value = JsFuture::from(Promise::resolve(&fetch_invocation))
            .await
            .map_err(|error| classify_fetch_rejection(&error))?;
        let response: Response = response_value
            .dyn_into()
            .map_err(|_| decode_error("fetch returned a value that was not a Response"))?;
        let status = response.status();
        let headers = response_headers(&response.headers());
        let text_promise = response
            .text()
            .map_err(|error| body_error("could not read response body", &error))?;
        let body_text = JsFuture::from(text_promise)
            .await
            .map_err(|error| body_error("could not decode response body", &error))?
            .as_string()
            .ok_or_else(|| decode_error("response body was not a string"))?;
        // The body is already fully materialized in JS here, so the cap rejects
        // an oversized body rather than capping mid-stream; it bounds decoded bytes.
        if body_text.len() > self.max_response_bytes {
            return Err(TransportError::Transport {
                class: TransportErrorClass::ResponseTooLarge,
                detail: Redacted::new(format!(
                    "response body exceeded {} byte limit",
                    self.max_response_bytes
                )),
            });
        }
        if (200..300).contains(&status) {
            Ok(TransportResponse::new(status, headers, body_text))
        } else {
            Err(TransportError::HttpStatus {
                status,
                headers,
                body: Redacted::new(body_text),
            })
        }
    }
}

#[cfg(feature = "tracing")]
fn bytes_received(result: &Result<TransportResponse, TransportError>) -> Option<usize> {
    match result {
        Ok(response) => Some(response.body().len()),
        Err(TransportError::HttpStatus { body, .. }) => Some(body.as_inner().len()),
        Err(_) => None,
    }
}

#[async_trait(?Send)]
impl HttpTransport for FetchTransport {
    async fn get(
        &self,
        path: &str,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        self.dispatch("GET", path, None, headers, timeout).await
    }

    async fn post(
        &self,
        path: &str,
        body: &str,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        self.dispatch("POST", path, Some(body), headers, timeout)
            .await
    }

    async fn put(
        &self,
        path: &str,
        body: &str,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        self.dispatch("PUT", path, Some(body), headers, timeout)
            .await
    }

    async fn delete(
        &self,
        path: &str,
        body: &str,
        headers: &[(String, String)],
        timeout: Option<Duration>,
    ) -> Result<TransportResponse, TransportError> {
        self.dispatch("DELETE", path, Some(body), headers, timeout)
            .await
    }
}

/// Resolves the global `fetch` from the active realm via [`js_sys::global`], so
/// it serves a `Window` or a worker alike, and invokes it with the global as
/// `this`. Returns a typed [`TransportError::Configuration`] when no global
/// `fetch` exists.
fn global_fetch_or_configuration_error() -> Result<(JsValue, Function), TransportError> {
    let global: JsValue = js_sys::global().into();
    let fetch = Reflect::get(&global, &JsValue::from_str("fetch"))
        .ok()
        .and_then(|value| value.dyn_into::<Function>().ok())
        .ok_or_else(|| TransportError::Configuration {
            message: Redacted::new(
                "no global `fetch` function is available in this JavaScript realm".to_owned(),
            ),
        })?;
    Ok((global, fetch))
}

fn build_request_init(
    method: &str,
    body: Option<&str>,
    headers: &[(String, String)],
) -> Result<RequestInit, TransportError> {
    let init = RequestInit::new();
    init.set_method(method);
    let header_object = Headers::new()
        .map_err(|error| configuration_error("could not build request headers", &error))?;
    let mut has_content_type = false;
    for (name, value) in headers {
        if name.eq_ignore_ascii_case("content-type") {
            has_content_type = true;
        }
        header_object
            .set(name, value)
            .map_err(|error| configuration_error("could not set request header", &error))?;
    }
    if let Some(body) = body {
        if !has_content_type {
            header_object
                .set("Content-Type", "application/json")
                .map_err(|error| configuration_error("could not set Content-Type", &error))?;
        }
        init.set_body(&JsValue::from_str(body));
    }
    init.set_headers(&header_object);
    Ok(init)
}

fn install_abort_timeout(
    init: &RequestInit,
    timeout: Duration,
) -> Result<gloo_timers::callback::Timeout, TransportError> {
    let controller = AbortController::new().map_err(|error| {
        configuration_error("could not build an AbortController for the timeout", &error)
    })?;
    init.set_signal(Some(&controller.signal()));
    let ms_u128 = timeout.as_millis();
    let ms = u32::try_from(ms_u128).map_err(|_| TransportError::Configuration {
        message: Redacted::new(format!(
            "timeout {ms_u128} ms exceeds the supported browser setTimeout range"
        )),
    })?;
    // The `Timeout` owns the closure, which owns the controller, so both live
    // until the caller drops the guard; gloo's `Timeout::drop` then calls
    // `clearTimeout`, cancelling the pending abort.
    Ok(gloo_timers::callback::Timeout::new(ms, move || {
        controller.abort();
    }))
}

fn classify_fetch_rejection(error: &JsValue) -> TransportError {
    let (class, detail) = classify_dom_exception(error);
    TransportError::Transport {
        class,
        detail: Redacted::new(detail),
    }
}

fn classify_dom_exception(error: &JsValue) -> (TransportErrorClass, String) {
    let name = reflect_string(error, "name").unwrap_or_default();
    let message = reflect_string(error, "message").unwrap_or_else(|| redacted_error_render(error));
    let class = match name.as_str() {
        "AbortError" | "TimeoutError" => TransportErrorClass::Timeout,
        "NetworkError" | "TypeError" => TransportErrorClass::Connect,
        "SyntaxError" => TransportErrorClass::Decode,
        _ => TransportErrorClass::Other,
    };
    (
        class,
        format!("{name}: {message}")
            .trim_start_matches(": ")
            .to_owned(),
    )
}

fn configuration_error(context: &str, error: &JsValue) -> TransportError {
    TransportError::Configuration {
        message: Redacted::new(format!("{context}: {}", redacted_error_render(error))),
    }
}

fn body_error(context: &str, error: &JsValue) -> TransportError {
    let (class, detail) = classify_dom_exception(error);
    let class = if matches!(class, TransportErrorClass::Timeout) {
        TransportErrorClass::Timeout
    } else {
        TransportErrorClass::Body
    };
    TransportError::Transport {
        class,
        detail: Redacted::new(format!("{context}: {detail}")),
    }
}

fn decode_error(context: &str) -> TransportError {
    TransportError::Transport {
        class: TransportErrorClass::Decode,
        detail: Redacted::new(context.to_owned()),
    }
}

fn redacted_error_render(error: &JsValue) -> String {
    reflect_string(error, "message")
        .or_else(|| error.as_string())
        .unwrap_or_else(|| "<opaque JsValue>".to_owned())
}

fn reflect_string(source: &JsValue, key: &str) -> Option<String> {
    let key_value = JsValue::from_str(key);
    Reflect::get(source, &key_value)
        .ok()
        .and_then(|value| value.as_string())
        .or_else(|| {
            source
                .dyn_ref::<Object>()
                .and_then(|object| Reflect::get(object, &key_value).ok())
                .and_then(|value| value.as_string())
        })
}

fn response_headers(headers: &Headers) -> Vec<(String, Redacted<String>)> {
    let entries = Array::from(headers.as_ref());
    let mut collected = Vec::with_capacity(entries.length() as usize);

    for index in 0..entries.length() {
        let pair = Array::from(&entries.get(index));
        let Some(name) = pair.get(0).as_string() else {
            continue;
        };
        let Some(value) = pair.get(1).as_string() else {
            continue;
        };
        collected.push((name, Redacted::new(value)));
    }

    collected
}