Skip to content

Commit b1c548e

Browse files
committed
chore: use native URL API to detect whether URL is on our CDN
1 parent 997658d commit b1c548e

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/__tests__/is-cdn-url.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import { isCdnUrl } from '../utils/helpers';
5+
6+
describe('isCdnUrl', () => {
7+
it('should return true in cases when provided URL matches provided domain', () => {
8+
expect(
9+
isCdnUrl('https://ucarecdn.com/:uuid/:filename', 'ucarecdn.com')
10+
).toBeTruthy();
11+
expect(isCdnUrl('http://ucarecdn.com/', 'ucarecdn.com')).toBeTruthy();
12+
expect(isCdnUrl('http://ucarecdn.com', 'ucarecdn.com')).toBeTruthy();
13+
expect(isCdnUrl('http://ucarecdn.com:8080', 'ucarecdn.com')).toBeTruthy();
14+
expect(
15+
isCdnUrl('http://custom.domain.com', 'custom.domain.com')
16+
).toBeTruthy();
17+
expect(isCdnUrl('//ucarecdn.com/', 'ucarecdn.com')).toBeTruthy();
18+
});
19+
20+
it("should return false in cases when provided URL doesn't matches provided domain", () => {
21+
expect(
22+
isCdnUrl('https://ucarecdn.com/:uuid/:filename', 'other.com')
23+
).toBeFalsy();
24+
expect(isCdnUrl('http://custom.domain.com', 'ucarecdn.com')).toBeFalsy();
25+
expect(isCdnUrl('//custom.domain.com/', 'ucarecdn.com')).toBeFalsy();
26+
});
27+
28+
it('should throw a error if invalid URL provided', () => {
29+
expect(() => isCdnUrl('ucarecdn', 'ucarecdn.com')).toThrowError(
30+
/Invalid URL/
31+
);
32+
});
33+
});

src/__tests__/uploadcare-loader.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('uploadcareLoader', () => {
6767

6868
const t = () => {
6969
uploadcareLoader({
70-
src: '',
70+
src: 'https://example.com',
7171
width: 0,
7272
quality: 80
7373
});
@@ -95,7 +95,7 @@ describe('uploadcareLoader', () => {
9595
});
9696

9797
test('The loader parses user paramters properly', () => {
98-
const src = 'https:/example.com/image.jpg';
98+
const src = 'https://example.com/image.jpg';
9999

100100
addEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY', 'test-public-key');
101101

@@ -108,7 +108,7 @@ describe('uploadcareLoader', () => {
108108
});
109109

110110
expect(result).toBe(
111-
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/500x/-/quality/normal/https:/example.com/image.jpg'
111+
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/500x/-/quality/normal/https://example.com/image.jpg'
112112
);
113113

114114
// Override default params, including resize and quality.
@@ -125,7 +125,7 @@ describe('uploadcareLoader', () => {
125125
});
126126

127127
expect(result).toBe(
128-
'https://test-public-key.ucr.io/-/format/jpg/-/stretch/on/-/progressive/no/-/resize/1x/-/quality/smart_retina/https:/example.com/image.jpg'
128+
'https://test-public-key.ucr.io/-/format/jpg/-/stretch/on/-/progressive/no/-/resize/1x/-/quality/smart_retina/https://example.com/image.jpg'
129129
);
130130

131131
// Add a new parameter (no defaults).
@@ -142,15 +142,15 @@ describe('uploadcareLoader', () => {
142142
});
143143

144144
expect(result).toBe(
145-
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/500x/-/quality/normal/-/new/parameter/https:/example.com/image.jpg'
145+
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/500x/-/quality/normal/-/new/parameter/https://example.com/image.jpg'
146146
);
147147

148148
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');
149149
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_TRANSFORMATION_PARAMETERS');
150150
});
151151

152152
test("The loader doesn't process SVG and GIF (absolute url)", () => {
153-
const src = 'https:/example.com/image.svg';
153+
const src = 'https://example.com/image.svg';
154154

155155
addEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY', 'test-public-key');
156156

@@ -242,7 +242,7 @@ describe('uploadcareLoader', () => {
242242

243243
// Not a jpg image. Should be max 3000 width.
244244

245-
let src = 'https:/example.com/image.png';
245+
let src = 'https://example.com/image.png';
246246

247247
addEnvVar(
248248
'NEXT_PUBLIC_UPLOADCARE_TRANSFORMATION_PARAMETERS',
@@ -256,7 +256,7 @@ describe('uploadcareLoader', () => {
256256
});
257257

258258
expect(result).toBe(
259-
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/3000x/-/quality/normal/https:/example.com/image.png'
259+
'https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/3000x/-/quality/normal/https://example.com/image.png'
260260
);
261261

262262
// Jpg image by format. Should be max 5000 width.
@@ -273,13 +273,13 @@ describe('uploadcareLoader', () => {
273273
});
274274

275275
expect(result).toBe(
276-
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.png'
276+
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https://example.com/image.png'
277277
);
278278

279279
// // Jpg image by extension with auto format.
280280
// // Should be max 5000 width with /format/jpeg/ forced.
281281

282-
src = 'https:/example.com/image.jpg';
282+
src = 'https://example.com/image.jpg';
283283

284284
addEnvVar(
285285
'NEXT_PUBLIC_UPLOADCARE_TRANSFORMATION_PARAMETERS',
@@ -293,7 +293,7 @@ describe('uploadcareLoader', () => {
293293
});
294294

295295
expect(result).toBe(
296-
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https:/example.com/image.jpg'
296+
'https://test-public-key.ucr.io/-/format/jpeg/-/stretch/off/-/progressive/yes/-/resize/5000x/-/quality/normal/https://example.com/image.jpg'
297297
);
298298

299299
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');

src/utils/helpers.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ export function generateCustomProxyEndpoint(customProxyDomain: string): string {
114114
}
115115

116116
export function isCdnUrl(url: string, cdnDomain: string): boolean {
117-
//eslint-disable-next-line
118-
const escapedCdnDomain = cdnDomain.replace('.', '.');
119-
120-
//eslint-disable-next-line
121-
const regexp = new RegExp(`^https?:\/\/${escapedCdnDomain}`);
117+
url = url.trim();
118+
if (url.startsWith('//')) {
119+
url = location.protocol + url;
120+
} else if (url.startsWith('/')) {
121+
return false;
122+
}
122123

123-
return regexp.test(url);
124+
return new URL(url).hostname === cdnDomain.trim();
124125
}
125126

126127
export function isProduction(): boolean {

0 commit comments

Comments
 (0)