Skip to content

Commit 09d2a8c

Browse files
authored
feat(cname-prefix): add isPrefixedCdnBase helper (#547)
Co-authored-by: nd0ut <[email protected]>
1 parent b58b1cf commit 09d2a8c

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { getPrefixedCdnBaseAsync } from './getPrefixedCdnBaseAsync'
2+
export { isPrefixedCdnBase } from '../common/isPrefixedCdnBase'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { isPrefixedCdnBase } from './isPrefixedCdnBase'
2+
import { describe, it, expect } from 'vitest'
3+
4+
describe('isPrefixedCdnBase', () => {
5+
it('should return true for a prefixed CDN base', () => {
6+
const cdnBase = 'https://prefix.ucarecd.net'
7+
const prefixCdnBase = 'https://ucarecd.net'
8+
expect(isPrefixedCdnBase(cdnBase, prefixCdnBase)).toBe(true)
9+
})
10+
11+
it('should return false for a non-prefixed CDN base', () => {
12+
const cdnBase = 'https://ucarecdn.com'
13+
const prefixCdnBase = 'https://ucarecd.net'
14+
expect(isPrefixedCdnBase(cdnBase, prefixCdnBase)).toBe(false)
15+
})
16+
17+
it('should return false for an invalid URL', () => {
18+
const cdnBase = 'invalid-url'
19+
const prefixCdnBase = 'https://ucarecd.net'
20+
expect(isPrefixedCdnBase(cdnBase, prefixCdnBase)).toBe(false)
21+
})
22+
23+
it('should return false for a CDN base with an unexpected format', () => {
24+
const cdnBase = 'https://ucarecdn.com/some/path'
25+
const prefixCdnBase = 'https://ucarecd.net'
26+
expect(isPrefixedCdnBase(cdnBase, prefixCdnBase)).toBe(false)
27+
})
28+
29+
it('should return false for a prefix that does not match the CDN base origin', () => {
30+
const cdnBase = 'https://prefix.ucarecdn.com'
31+
const prefixCdnBase = 'https://ucarecd.net'
32+
expect(isPrefixedCdnBase(cdnBase, prefixCdnBase)).toBe(false)
33+
})
34+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const isPrefixedCdnBase = (cdnBase: string, prefixCdnBase: string) => {
2+
try {
3+
const cdnBaseUrl = new URL(cdnBase)
4+
const prefixCdnBaseUrl = new URL(prefixCdnBase)
5+
return cdnBaseUrl.hostname.endsWith(prefixCdnBaseUrl.hostname)
6+
} catch (_err) {
7+
return false
8+
}
9+
}

packages/cname-prefix/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { getPrefixedCdnBaseAsync } from './async'
22
export { getPrefixedCdnBaseSync } from './sync'
3+
export { isPrefixedCdnBase } from './common/isPrefixedCdnBase'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { getPrefixedCdnBaseSync } from './getPrefixedCdnBaseSync'
2+
export { isPrefixedCdnBase } from '../common/isPrefixedCdnBase'

0 commit comments

Comments
 (0)