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
use std::future::Future;
use crate::worker_sys::Context as JsContext;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::future_to_promise;
/// A context bound to a `fetch` event.
#[derive(Debug)]
pub struct Context {
inner: JsContext,
}
unsafe impl Send for Context {}
unsafe impl Sync for Context {}
impl Context {
/// Constructs a context from an underlying JavaScript context object.
pub fn new(inner: JsContext) -> Self {
Self { inner }
}
/// Extends the lifetime of the "fetch" event which this context is bound to,
/// until the given future has been completed. The future is executed before the handler
/// terminates but does not block the response. For example, this is ideal for caching
/// responses or handling logging.
/// ```no_run
/// context.wait_until(async move {
/// let _ = cache.put(request, response).await;
/// });
/// ```
pub fn wait_until<F>(&self, future: F)
where
F: Future<Output = ()> + 'static,
{
self.inner
.wait_until(&future_to_promise(async {
future.await;
Ok(JsValue::UNDEFINED)
}))
.unwrap()
}
/// Prevents a runtime error response when the Worker script throws an unhandled exception.
/// Instead, the script will "fail open", which will proxy the request to the origin server
/// as though the Worker was never invoked.
pub fn pass_through_on_exception(&self) {
self.inner.pass_through_on_exception().unwrap()
}
}
impl AsRef<JsContext> for Context {
fn as_ref(&self) -> &JsContext {
&self.inner
}
}