Skip to content

Commit e906103

Browse files
committed
adding most of the missing fields in CfProperties, mostly with appropriate Rust + bindgen representations; plus some remaining TODOs and a review comment on related code
1 parent cd400f8 commit e906103

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

worker/src/request_init.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pub struct CfProperties {
114114
/// zero and negative integers. A value of 0 indicates that the cache asset expires immediately.
115115
/// Any negative value instructs Cloudflare not to cache at all.
116116
pub cache_ttl_by_status: Option<HashMap<String, i32>>,
117+
// TODO docs
118+
pub image: Option<ResizeConfig>,
117119
/// Enables or disables AutoMinify for various file types.
118120
/// For example: `{ javascript: true, css: true, html: false }`.
119121
pub minify: Option<MinifyConfig>,
@@ -233,6 +235,27 @@ impl From<&CfProperties> for JsValue {
233235
),
234236
);
235237

238+
// TODO shouldn't the above calls to set_prop also use a pattern like the one below,
239+
// such as to avoid needless work when those properties are not actually set in Rust
240+
// code and thus should also not be passed on to the JS side;
241+
// there may also not be a clear default for each (sub-)property, but even if there
242+
// officially is, there may be discrepancies between official docs, official JS and
243+
// Rust libraries introduced because they behave differently -- I'm assuming here
244+
// that the JS library / API will simply not set default values for all fields
245+
// that are simply not provided by the user, thus allowing the underlying runtime
246+
// to set the true defaults, whether or not the docs agree what those are
247+
// side benefit being that a lot less work needs to be done, such as constructing
248+
// defaults, cloning & copying, calls across the wasm/JS bridge
249+
if let Some(image) = &props.image {
250+
set_prop(
251+
&obj,
252+
&JsValue::from("image"),
253+
&JsValue::from(
254+
image.clone(),
255+
),
256+
);
257+
}
258+
236259
obj.into()
237260
}
238261
}
@@ -262,6 +285,7 @@ impl Default for CfProperties {
262285
cache_ttl_by_status: None,
263286
minify: None,
264287
mirage: Some(true),
288+
image: None,
265289
polish: None,
266290
resolve_override: None,
267291
scrape_shield: Some(true),
@@ -305,6 +329,98 @@ impl From<PolishConfig> for &str {
305329
}
306330
}
307331

332+
/// Configuration options for Cloudflare's image resizing feature:
333+
/// <https://developers.cloudflare.com/images/image-resizing/>
334+
#[wasm_bindgen]
335+
#[derive(Clone, Default)]
336+
pub struct ResizeConfig {
337+
pub anim: Option<bool>,
338+
#[wasm_bindgen(skip)]
339+
pub background: Option<String>,
340+
#[wasm_bindgen(skip)]
341+
pub blur: Option<String>,
342+
pub brightness: Option<f64>,
343+
pub contrast: Option<f64>,
344+
pub dpr: Option<f64>,
345+
pub fit: Option<ResizeFit>,
346+
pub format: Option<ResizeFormat>,
347+
pub gamma: Option<f64>,
348+
// TODO
349+
// #[wasm_bindgen(skip)]
350+
// pub gravity: Option<ResizeGravity>,
351+
pub height: Option<usize>,
352+
pub metadata: Option<ResizeMetadata>,
353+
pub onerror: Option<ResizeOnerror>,
354+
pub quality: Option<usize>,
355+
pub sharpen: Option<usize>,
356+
pub trim: Option<ResizeTrim>,
357+
pub width: Option<usize>,
358+
}
359+
360+
#[wasm_bindgen]
361+
impl ResizeConfig {
362+
#[wasm_bindgen(getter)]
363+
pub fn background(&self) -> Option<String> {
364+
self.background.clone()
365+
}
366+
#[wasm_bindgen(getter)]
367+
pub fn blur(&self) -> Option<String> {
368+
self.blur.clone()
369+
}
370+
}
371+
372+
#[wasm_bindgen]
373+
#[derive(Clone, Copy)]
374+
pub enum ResizeFit {
375+
ScaleDown,
376+
Contain,
377+
Cover,
378+
Crop,
379+
Pad,
380+
}
381+
382+
#[wasm_bindgen]
383+
#[derive(Clone, Copy)]
384+
pub enum ResizeFormat {
385+
Auto,
386+
Avif,
387+
Webp,
388+
Json,
389+
}
390+
391+
// TODO implement in a wbg-compatible way
392+
// #[wasm_bindgen]
393+
// #[derive(Clone)]
394+
// pub enum ResizeGravity {
395+
// Auto,
396+
// // TODO maybe enum top/left/bottom/right?
397+
// Side(String),
398+
// Coords(f64, f64),
399+
// }
400+
401+
#[wasm_bindgen]
402+
#[derive(Clone, Copy)]
403+
pub enum ResizeMetadata {
404+
Keep,
405+
Copyright,
406+
None,
407+
}
408+
409+
#[wasm_bindgen]
410+
#[derive(Clone, Copy)]
411+
pub enum ResizeOnerror {
412+
Redirect,
413+
}
414+
415+
#[wasm_bindgen]
416+
#[derive(Clone, Copy, Default)]
417+
pub struct ResizeTrim {
418+
pub top: usize,
419+
pub bottom: usize,
420+
pub left: usize,
421+
pub right: usize,
422+
}
423+
308424
#[wasm_bindgen]
309425
#[derive(Clone, Copy)]
310426
pub enum RequestRedirect {

0 commit comments

Comments
 (0)