Skip to content

Commit dd67a76

Browse files
authored
add additional configuration options (authToken, forwardedHeaders, endpoint)
1 parent a19ea4b commit dd67a76

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ app.prepare().then(() => {
4646
// (Required) The IMGPROXY_SALT (hex encoded)
4747
salt: '<imgproxy salt>',
4848
},
49+
// (Optional) If your imgproxy instance uses
50+
// the IMGPROXY_SECRET, specify the token here
51+
authToken: '<my-token>'
4952
// (Optional) If you wanna restrict access to specific
5053
// buckets add an array of valid bucket names
5154
bucketWhitelist: ['<my-bucket>'],
55+
// (Optional) A list of imgproxy headers that should be
56+
// forwarded through the imgproxy endpoint
57+
forwardedHeaders: ['<my-header>'],
5258
},
5359
);
5460
} else {
@@ -110,3 +116,26 @@ const imagePath = buildProxyImagePath('test-bucket/test-image.png', {
110116
{/* Content */}
111117
</div>;
112118
```
119+
120+
## Overriding the endpoint
121+
122+
You can override the default endpoint address or use an absolute address in the component instead.
123+
124+
```js
125+
// server.js
126+
createServer((req, res) => {
127+
// ...
128+
if (pathname === '/my-endpoint') {
129+
// ...
130+
}
131+
}
132+
```
133+
134+
```tsx
135+
<ProxyImage file="mybucket/myfile.png" endpoint="/my-endpoint" />;
136+
137+
// Or
138+
buildProxyImagePath('test-bucket/test-image.png', {
139+
endpoint: '/my-endpoint',
140+
});
141+
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitpatty/next-image-s3-imgproxy-loader",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "imgproxy S3 extension for next/image",
55
"author": "Matteias Collet <[email protected]>",
66
"main": "dist/index.js",

src/index.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2434
const 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

108117
const 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

123132
const 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=...&params=...&width=...
141-
return `${IMGPROXY_ENDPOINT}?${urlParams.toString()}`;
151+
return `${endpoint ?? IMGPROXY_ENDPOINT}?${urlParams.toString()}`;
142152
};
143153

144154
return (

0 commit comments

Comments
 (0)