Skip to content

Commit

Permalink
Let env.ASSETS.fetch also work with URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed May 28, 2024
1 parent 0b42abc commit 46d0d4b
Showing 1 changed file with 55 additions and 21 deletions.
76 changes: 55 additions & 21 deletions src/request_handlers/cloudflare/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ impl Env {
}
}

#[derive(FromValue)]
pub enum FetchInput<'cx> {
#[ion(inherit)]
Request(&'cx FetchRequest),
#[ion(inherit)]
Url(&'cx runtime::globals::url::URL),
#[ion(inherit)]
String(String),
}

enum FetchInputHeap {
Request(TracedHeap<*mut JSObject>),
Url(String),
}

#[js_class]
pub struct EnvAssets {
reflector: Reflector,
Expand All @@ -66,32 +81,51 @@ impl EnvAssets {
ion_err!("Cannot construct this type", Type)
}

pub fn fetch(&self, cx: &Context, request: &FetchRequest) -> Option<Promise> {
let request_heap = TracedHeap::new(request.reflector().get());
pub fn fetch<'cx>(&self, cx: &'cx Context, input: FetchInput<'cx>) -> Option<Promise> {
let input_heap = match input {
FetchInput::Request(req) => {
FetchInputHeap::Request(TracedHeap::new(req.reflector().get()))
}
FetchInput::Url(url) => FetchInputHeap::Url(url.to_string()),
FetchInput::String(url) => FetchInputHeap::Url(url),
};

unsafe {
future_to_promise::<_, _, _, Exception>(cx, move |cx| async move {
let request =
FetchRequest::get_mut_private(&cx, &request_heap.root(&cx).into()).unwrap();

let mut http_req = http::Request::builder()
.uri(request.get_url())
.method(request.method());

for header in request.headers(&cx) {
http_req = http_req.header(header.0.clone(), header.1.clone())
}

let request_body = request.take_body()?;
let (cx, body_bytes) = cx.await_native_cx(|cx| request_body.into_bytes(cx)).await;
let body_bytes = body_bytes?;
let body = match body_bytes {
Some(bytes) => hyper::Body::from(bytes),
None => hyper::Body::empty(),
let (cx, http_req) = match input_heap {
FetchInputHeap::Request(request_heap) => {
let request =
FetchRequest::get_mut_private(&cx, &request_heap.root(&cx).into())
.unwrap();

let mut http_req = http::Request::builder()
.uri(request.get_url())
.method(request.method());

for header in request.headers(&cx) {
http_req = http_req.header(header.0.clone(), header.1.clone())
}

let request_body = request.take_body()?;
let (cx, body_bytes) =
cx.await_native_cx(|cx| request_body.into_bytes(cx)).await;
let body_bytes = body_bytes?;
let body = match body_bytes {
Some(bytes) => hyper::Body::from(bytes),
None => hyper::Body::empty(),
};

(cx, http_req.body(body)?)
}
FetchInputHeap::Url(url) => (
cx,
http::Request::builder()
.uri(url)
.method(http::Method::GET)
.body(hyper::Body::empty())?,
),
};

let http_req = http_req.body(body)?;

let (parts, body) = http_req.into_parts();
let request = super::super::Request { parts, body };

Expand Down

0 comments on commit 46d0d4b

Please sign in to comment.