@@ -21,6 +21,16 @@ const FORWARDED_HEADERS = [
2121 'cache-control' ,
2222] ;
2323
24+ type ImageOptimizerOptions = {
25+ signature ?: {
26+ key : string ;
27+ salt : string ;
28+ } ;
29+ authToken ?: string ;
30+ bucketWhitelist ?: string [ ] ;
31+ forwardedHeaders ?: string [ ] ;
32+ } ;
33+
2434const generateSignature = ( key : string , salt : string , buff : string ) : string => {
2535 const hmac = createHmac ( 'sha256' , Buffer . from ( key , 'hex' ) ) ;
2636 hmac . update ( Buffer . from ( salt , 'hex' ) ) ;
@@ -32,16 +42,11 @@ const imageOptimizer = (
3242 imgproxyBaseUrl : URL ,
3343 query : ParsedUrlQuery ,
3444 res : ServerResponse ,
35- options ?: {
36- signature ?: {
37- key : string ;
38- salt : string ;
39- } ;
40- bucketWhitelist ?: string [ ] ;
41- } ,
45+ options ?: ImageOptimizerOptions ,
4246) => {
4347 const { src, params, format } = query ;
44- const { signature, bucketWhitelist } = options ?? { } ;
48+ const { authToken, bucketWhitelist, forwardedHeaders, signature } =
49+ options ?? { } ;
4550
4651 // If the source is not set of fails the
4752 // regex check throw a 400
@@ -76,9 +81,12 @@ const imageOptimizer = (
7681 ...( imgproxyBaseUrl . port ? { port : imgproxyBaseUrl . port } : { } ) ,
7782 path : `/${ urlSignature } ${ requestPath } ` ,
7883 method : 'GET' ,
84+ headers : {
85+ ...( authToken ? { Authorization : `Bearer ${ authToken } ` } : { } ) ,
86+ } ,
7987 } ,
8088 ( r ) => {
81- FORWARDED_HEADERS . forEach ( ( h ) => {
89+ ( forwardedHeaders ?? FORWARDED_HEADERS ) . forEach ( ( h ) => {
8290 if ( r . headers [ h ] ) res . setHeader ( h , r . headers [ h ] as string ) ;
8391 } ) ;
8492
@@ -103,6 +111,7 @@ type ProxyImageProps = {
103111 file : string ;
104112 format ?: string ;
105113 proxyParams ?: string ;
114+ endpoint ?: string ;
106115} ;
107116
108117const buildProxyImagePath = (
@@ -117,13 +126,14 @@ const buildProxyImagePath = (
117126 if ( proxyParams ) urlParams . append ( 'params' , proxyParams ) ;
118127 if ( format ) urlParams . append ( 'format' , format ) ;
119128
120- return `${ IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
129+ return `${ options ?. endpoint ?? IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
121130} ;
122131
123132const ProxyImage = ( {
124133 file,
125134 proxyParams,
126135 format,
136+ endpoint,
127137 ...props
128138} : ProxyImageProps &
129139 Omit < ImageProps , 'src' | 'quality' | 'unoptimized' | 'loader' > ) => {
@@ -138,7 +148,7 @@ const ProxyImage = ({
138148 if ( width ) urlParams . append ( 'width' , width . toString ( ) ) ;
139149
140150 // will return /_next/imgproxy?src=...¶ms=...&width=...
141- return `${ IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
151+ return `${ endpoint ?? IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
142152 } ;
143153
144154 return (
0 commit comments