-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
Copy pathmatch-remote-pattern.ts
53 lines (47 loc) · 1.26 KB
/
match-remote-pattern.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { RemotePattern } from './image-config'
import { makeRe } from 'next/dist/compiled/picomatch'
// Modifying this function should also modify writeImagesManifest()
export function matchRemotePattern(
pattern: RemotePattern | URL,
url: URL
): boolean {
if (pattern.protocol !== undefined) {
if (pattern.protocol.replace(/:$/, '') !== url.protocol.replace(/:$/, '')) {
return false
}
}
if (pattern.port !== undefined) {
if (pattern.port !== url.port) {
return false
}
}
if (pattern.hostname === undefined) {
throw new Error(
`Pattern should define hostname but found\n${JSON.stringify(pattern)}`
)
} else {
if (!makeRe(pattern.hostname).test(url.hostname)) {
return false
}
}
if (pattern.search !== undefined) {
if (pattern.search !== url.search) {
return false
}
}
// Should be the same as writeImagesManifest()
if (!makeRe(pattern.pathname ?? '**', { dot: true }).test(url.pathname)) {
return false
}
return true
}
export function hasRemoteMatch(
domains: string[],
remotePatterns: Array<RemotePattern | URL>,
url: URL
): boolean {
return (
domains.some((domain) => url.hostname === domain) ||
remotePatterns.some((p) => matchRemotePattern(p, url))
)
}