Skip to content

Commit 01337f1

Browse files
authored
wip migration to url builder package
1 parent d2a6fea commit 01337f1

File tree

11 files changed

+55
-373
lines changed

11 files changed

+55
-373
lines changed

example/package-lock.json

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

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"@bitpatty/imgproxy-url-builder": "^0.1.0",
1213
"next": "11.1.2",
1314
"react": "^17.0.2",
1415
"react-dom": "17.0.2"

example/pages/index.tsx

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1+
import pb, { GravityType, ResizeType } from '@bitpatty/imgproxy-url-builder';
12
import type { NextPage } from 'next';
23
import Head from 'next/head';
34

4-
import ProxyImage, {
5-
ImgProxyParamBuilder,
6-
buildProxyImagePath,
7-
ResizeType,
8-
GravityType,
9-
} from '../../dist';
5+
import ProxyImage, { buildProxyImagePath } from '../../dist';
106

117
const demoContent: {
128
label: string;
@@ -28,13 +24,13 @@ const demoContent: {
2824
label: 'Blurring',
2925
file: 'test-bucket/test-image.png',
3026

31-
proxyParams: new ImgProxyParamBuilder().blur(10).build(),
27+
proxyParams: pb().blur(10).build(),
3228
},
3329
{
3430
label: 'Cropping',
3531
file: 'test-bucket/test-image.png',
3632

37-
proxyParams: new ImgProxyParamBuilder()
33+
proxyParams: pb()
3834
.crop({
3935
width: 30,
4036
height: 30,
@@ -45,13 +41,13 @@ const demoContent: {
4541
label: 'Cropping 2',
4642
file: 'test-bucket/test-image.png',
4743

48-
proxyParams: new ImgProxyParamBuilder()
44+
proxyParams: pb()
4945
.crop({
5046
width: 30,
5147
height: 30,
5248
gravity: {
5349
type: GravityType.NORTHEAST,
54-
center: {
50+
offset: {
5551
x: 10,
5652
y: 10,
5753
},
@@ -63,7 +59,7 @@ const demoContent: {
6359
label: 'Trimming',
6460
file: 'test-bucket/test-image.png',
6561

66-
proxyParams: new ImgProxyParamBuilder()
62+
proxyParams: pb()
6763
.trim({
6864
threshold: 0,
6965
color: '000000',
@@ -74,52 +70,42 @@ const demoContent: {
7470
label: 'Trimming',
7571
file: 'test-bucket/test-image.png',
7672

77-
proxyParams: new ImgProxyParamBuilder().pad(50).build(),
73+
proxyParams: pb().pad({ top: 50 }).build(),
7874
},
7975
{
8076
label: 'Padding with background',
8177
file: 'test-bucket/test-image.png',
8278

83-
proxyParams: new ImgProxyParamBuilder()
84-
.pad(50)
85-
.setBackground('ff0000')
86-
.build(),
79+
proxyParams: pb().pad({ top: 50 }).background('ff0000').build(),
8780
},
8881
{
8982
label: 'Resizing',
9083
file: 'test-bucket/test-image.png',
9184
width: 100,
9285
height: 100,
93-
proxyParams: new ImgProxyParamBuilder()
86+
proxyParams: pb()
9487
.resize({
9588
type: ResizeType.FILL,
9689
width: 100,
9790
height: 100,
9891
enlarge: true,
99-
gravity: {
100-
type: GravityType.NORTH,
101-
center: {
102-
x: 10,
103-
y: 10,
104-
},
105-
},
10692
})
10793
.build(),
10894
},
10995
{
11096
label: 'Sharpen',
11197
file: 'test-bucket/test-image.png',
112-
proxyParams: new ImgProxyParamBuilder().sharpen({ sigma: 1.5 }).build(),
98+
proxyParams: pb().sharpen(1.5).build(),
11399
},
114100
{
115101
label: 'PNG to JPG',
116102
file: 'test-bucket/test-image.png',
117-
proxyParams: new ImgProxyParamBuilder().format('png').build(),
103+
proxyParams: pb().format('png').build(),
118104
},
119105
{
120106
label: 'SVG to PNG',
121107
file: 'test-bucket/test-image.svg',
122-
proxyParams: new ImgProxyParamBuilder().format('png').build(),
108+
proxyParams: pb().format('png').build(),
123109
},
124110
];
125111

@@ -151,7 +137,7 @@ const Home: NextPage = () => {
151137
backgroundImage: `url(${buildProxyImagePath(
152138
'test-bucket/test-image.png',
153139
{
154-
proxyParams: new ImgProxyParamBuilder().blur(10).build(),
140+
proxyParams: pb().blur(10).build(),
155141
},
156142
)})`,
157143
backgroundSize: 'cover',

package-lock.json

Lines changed: 15 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
},
3838
"peerDependencies": {
3939
"next": "^11.0.0"
40+
},
41+
"dependencies": {
42+
"@bitpatty/imgproxy-url-builder": "^0.1.0"
4043
}
4144
}

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export default {
2222
'http',
2323
'https',
2424
'node:querystring',
25+
'@bitpatty/imgproxy-url-builder',
2526
],
2627
};

src/enums/gravity-type.enum.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/enums/resize-type.enum.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/enums/watermark-position.enum.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/index.tsx

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import * as React from 'react';
22
import Image, { ImageLoaderProps, ImageProps } from 'next/image';
33

4-
import { createHmac } from 'crypto';
54
import { ParsedUrlQuery } from 'node:querystring';
65
import { request as httpsRequest } from 'https';
76
import { ServerResponse, request as httpRequest } from 'http';
87

9-
import ImgProxyParamBuilder from './param-builder';
10-
import GravityType from './enums/gravity-type.enum';
11-
import ResizeType from './enums/resize-type.enum';
8+
import pb from '@bitpatty/imgproxy-url-builder';
129

1310
const IMGPROXY_ENDPOINT = '/_next/imgproxy';
11+
1412
const SRC_REGEX = /^[^/.]+\/.+[^/]$/;
13+
1514
const FORWARDED_HEADERS = [
1615
'date',
1716
'expires',
@@ -21,20 +20,6 @@ const FORWARDED_HEADERS = [
2120
'content-disposition',
2221
];
2322

24-
/**
25-
* Generates the signature for the specified uery
26-
* @param key The signature key
27-
* @param salt The signature salt$
28-
* @param buff The full request path
29-
* @returns The imgproxy signature
30-
*/
31-
const generateSignature = (key: string, salt: string, buff: string): string => {
32-
const hmac = createHmac('sha256', Buffer.from(key, 'hex'));
33-
hmac.update(Buffer.from(salt, 'hex'));
34-
hmac.update(buff);
35-
return hmac.digest().toString('base64url');
36-
};
37-
3823
/**
3924
* Builds the final reuest path to retrieve
4025
* an image from the imgproxy instance
@@ -54,14 +39,13 @@ const buildRequestPath = (
5439
): string => {
5540
const { imgproxyParams, signature } = options ?? {};
5641

57-
const paramString = imgproxyParams ? `${imgproxyParams}/` : '';
58-
const requestPath = `/${paramString}plain/s3://${src}`;
42+
const paramBuilder = pb();
43+
if (imgproxyParams) paramBuilder.modifiers.push(imgproxyParams);
5944

60-
const urlSignature = signature
61-
? generateSignature(signature.key, signature.salt, requestPath)
62-
: '';
63-
64-
return `/${urlSignature}${requestPath}`;
45+
return paramBuilder.build({
46+
path: `s3://${src}`,
47+
signature,
48+
});
6549
};
6650

6751
type ImageOptimizerOptions = {
@@ -159,7 +143,6 @@ const buildProxyImagePath = (
159143
options?: Omit<ProxyImageProps, 'file'>,
160144
): string => {
161145
const { proxyParams, endpoint } = options ?? {};
162-
163146
const urlParams = new URLSearchParams();
164147

165148
urlParams.append('src', file);
@@ -199,12 +182,4 @@ const ProxyImage = ({
199182
};
200183

201184
export default ProxyImage;
202-
export {
203-
IMGPROXY_ENDPOINT,
204-
buildProxyImagePath,
205-
handle,
206-
generateSignature,
207-
ImgProxyParamBuilder,
208-
GravityType,
209-
ResizeType,
210-
};
185+
export { IMGPROXY_ENDPOINT, buildProxyImagePath, handle };

0 commit comments

Comments
 (0)