diff --git a/worker-sys/src/images/mod.rs b/worker-sys/src/images/mod.rs new file mode 100644 index 000000000..e042c170b --- /dev/null +++ b/worker-sys/src/images/mod.rs @@ -0,0 +1,10 @@ +use js_sys::Promise; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + pub type Images; + + #[wasm_bindgen(method, js_name = fetch)] + pub fn fetch(this: &Images, input: JsValue, options: JsValue) -> Promise; +} diff --git a/worker-sys/src/lib.rs b/worker-sys/src/lib.rs index a3edbf6b9..e125ef5b6 100644 --- a/worker-sys/src/lib.rs +++ b/worker-sys/src/lib.rs @@ -1,6 +1,7 @@ #![allow(clippy::manual_non_exhaustive, clippy::empty_docs)] pub mod ext; +pub mod images; pub mod types; pub use types::*; diff --git a/worker/src/env.rs b/worker/src/env.rs index 780c2585b..ab93dd75b 100644 --- a/worker/src/env.rs +++ b/worker/src/env.rs @@ -3,6 +3,7 @@ use std::fmt::Display; use crate::analytics_engine::AnalyticsEngineDataset; #[cfg(feature = "d1")] use crate::d1::D1Database; +use crate::images::Images; use crate::kv::KvStore; use crate::rate_limit::RateLimiter; use crate::Ai; @@ -104,6 +105,10 @@ impl Env { self.get_binding(binding) } + pub fn images(&self, binding: &str) -> Result { + self.get_binding(binding) + } + /// Access a D1 Database by the binding name configured in your wrangler.toml file. #[cfg(feature = "d1")] pub fn d1(&self, binding: &str) -> Result { diff --git a/worker/src/images/mod.rs b/worker/src/images/mod.rs new file mode 100644 index 000000000..a3d1dce4a --- /dev/null +++ b/worker/src/images/mod.rs @@ -0,0 +1,62 @@ +use wasm_bindgen::{JsCast, JsValue}; +use wasm_bindgen_futures::JsFuture; + +use crate::env::EnvBinding; +use crate::{Response, Result}; +use worker_sys::images::Images as SysImages; + +pub struct Images { + inner: SysImages, +} + +impl Images { + pub(crate) fn new(inner: SysImages) -> Self { + Self { inner } + } + + pub async fn fetch( + &self, + input: impl Into, + options: Option, + ) -> Result { + let input = input.into(); + let options = options.unwrap_or(JsValue::UNDEFINED); + + let promise = self.inner.fetch(input, options); + let js_value = JsFuture::from(promise).await?; + let response: web_sys::Response = js_value.dyn_into()?; + Ok(Response::from(response)) + } +} + +impl JsCast for Images { + fn instanceof(val: &JsValue) -> bool { + SysImages::instanceof(val) + } + + fn unchecked_from_js(val: JsValue) -> Self { + Self { + inner: SysImages::unchecked_from_js(val), + } + } + + fn unchecked_from_js_ref(val: &JsValue) -> &Self { + unsafe { &*(val as *const JsValue as *const Images) } + } +} + +impl AsRef for Images { + fn as_ref(&self) -> &JsValue { + self.inner.as_ref() + } +} + +impl From for JsValue { + fn from(images: Images) -> JsValue { + images.inner.into() + } +} + +impl EnvBinding for Images { + const TYPE_NAME: &'static str = "Images"; +} \ No newline at end of file diff --git a/worker/src/lib.rs b/worker/src/lib.rs index d4f902bdf..44ce49004 100644 --- a/worker/src/lib.rs +++ b/worker/src/lib.rs @@ -223,6 +223,7 @@ mod global; mod headers; mod http; mod hyperdrive; +pub mod images; pub mod kv; #[cfg(feature = "queue")] mod queue;