@@ -114,6 +114,8 @@ pub struct CfProperties {
114
114
/// zero and negative integers. A value of 0 indicates that the cache asset expires immediately.
115
115
/// Any negative value instructs Cloudflare not to cache at all.
116
116
pub cache_ttl_by_status : Option < HashMap < String , i32 > > ,
117
+ // TODO docs
118
+ pub image : Option < ResizeConfig > ,
117
119
/// Enables or disables AutoMinify for various file types.
118
120
/// For example: `{ javascript: true, css: true, html: false }`.
119
121
pub minify : Option < MinifyConfig > ,
@@ -233,6 +235,27 @@ impl From<&CfProperties> for JsValue {
233
235
) ,
234
236
) ;
235
237
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
+
236
259
obj. into ( )
237
260
}
238
261
}
@@ -262,6 +285,7 @@ impl Default for CfProperties {
262
285
cache_ttl_by_status : None ,
263
286
minify : None ,
264
287
mirage : Some ( true ) ,
288
+ image : None ,
265
289
polish : None ,
266
290
resolve_override : None ,
267
291
scrape_shield : Some ( true ) ,
@@ -305,6 +329,98 @@ impl From<PolishConfig> for &str {
305
329
}
306
330
}
307
331
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
+
308
424
#[ wasm_bindgen]
309
425
#[ derive( Clone , Copy ) ]
310
426
pub enum RequestRedirect {
0 commit comments