1
+ import { inspect } from 'node:util'
1
2
import sharp , { type FormatEnum , type ResizeOptions } from 'sharp'
2
3
import type { LocalImageService } from 'astro'
3
4
import { baseService } from 'astro/assets'
@@ -19,22 +20,75 @@ export interface Transform {
19
20
}
20
21
21
22
const service : LocalImageService < Config > = {
22
- propertiesToHash : baseService . propertiesToHash ?? [ ] ,
23
- validateOptions : baseService . validateOptions ,
24
- getURL : baseService . getURL ,
25
- parseURL : baseService . parseURL ,
26
- getHTMLAttributes : baseService . getHTMLAttributes ,
27
- getSrcSet : baseService . getSrcSet ,
28
- async transform ( inputBuffer , transformOptions , config ) {
23
+ propertiesToHash : [ 'src' , 'width' , 'height' , 'format' , 'quality' ] ,
24
+
25
+ validateOptions : ( options , config ) => {
26
+ if ( config . service . config . _debug )
27
+ globalThis . console . log ( `in: validateOptions(${ inspect ( { options, config } ) } )` )
28
+
29
+ const result = baseService . validateOptions ?.( options , config ) ?? options
30
+
31
+ if ( config . service . config . _debug )
32
+ globalThis . console . log ( `out: validateOptions(…) = ${ inspect ( result ) } ` )
33
+ } ,
34
+
35
+ getURL : ( options , config ) => {
36
+ if ( config . service . config . _debug )
37
+ globalThis . console . log ( `in: getURL(${ inspect ( { options, config } ) } )` )
38
+
39
+ const result = baseService . getURL ( options , config )
40
+
41
+ if ( config . service . config . _debug )
42
+ globalThis . console . log ( `out: getURL(…) = ${ inspect ( result ) } ` )
43
+ } ,
44
+
45
+ parseURL : ( url , config ) => {
46
+ if ( config . service . config . _debug )
47
+ globalThis . console . log ( `in: parseURL(${ inspect ( { url, config } ) } )` )
48
+
49
+ const result = baseService . parseURL ( url , config )
50
+
51
+ if ( config . service . config . _debug )
52
+ globalThis . console . log ( `out: parseURL(…) = ${ inspect ( result ) } ` )
53
+ } ,
54
+
55
+ getHTMLAttributes : ( options , config ) => {
29
56
if ( config . service . config . _debug )
30
- globalThis . console . log ( 'Made it to the transform function' , { transformOptions , config } )
57
+ globalThis . console . log ( `in: getHTMLAttributes( ${ inspect ( { options , config } ) } )` )
31
58
32
- const transform : Transform = transformOptions as Transform
59
+ const result = baseService . getHTMLAttributes
60
+
61
+ if ( config . service . config . _debug )
62
+ globalThis . console . log ( `out: getHTMLAttributes(…) = ${ inspect ( result ) } ` )
63
+ } ,
64
+
65
+ getSrcSet : ( options , config ) => {
66
+ if ( config . service . config . _debug )
67
+ globalThis . console . log ( `in: getSrcSet(${ inspect ( { options, config } ) } )` )
68
+
69
+ const result = baseService . getSrcSet ?.( options , config )
70
+
71
+ if ( config . service . config . _debug )
72
+ globalThis . console . log ( `out: getSrcSet(…) = ${ inspect ( result ) } ` )
73
+
74
+ return result
75
+ } ,
76
+
77
+ async transform ( inputBuffer , options , config ) {
78
+ if ( config . service . config . _debug )
79
+ globalThis . console . log ( `in: transform(${ inspect ( { options, config } ) } )` )
80
+
81
+ const transform : Transform = options as Transform
33
82
34
83
if ( transform . format === 'svg' ) {
35
84
// FIXME: Returning the input buffer here assumes it's SVG, but it could be anything.
36
85
// TODO: Add support for SVG image tracing.
37
- return { data : inputBuffer , format : 'svg' }
86
+ const result = { data : inputBuffer , format : 'svg' }
87
+
88
+ if ( config . service . config . _debug )
89
+ globalThis . console . log ( `out: transform(…) = ${ inspect ( result ) } ` )
90
+
91
+ return result
38
92
}
39
93
40
94
const result = sharp ( inputBuffer , {
@@ -65,6 +119,10 @@ const service: LocalImageService<Config> = {
65
119
}
66
120
67
121
const { data, info : { format } } = await result . toBuffer ( { resolveWithObject : true } )
122
+
123
+ if ( config . service . config . _debug )
124
+ globalThis . console . log ( `out: transform(…) = ${ inspect ( { data, format } ) } ` )
125
+
68
126
return {
69
127
data,
70
128
format,
0 commit comments