|
1 | 1 | import { isRemotePath } from '@astrojs/internal-helpers/path'; |
2 | | -import type { AstroConfig, RemotePattern } from 'astro'; |
| 2 | +import { |
| 3 | + matchHostname, |
| 4 | + matchPattern, |
| 5 | +} from '@astrojs/internal-helpers/remote'; |
| 6 | +import type { AstroConfig } from 'astro'; |
3 | 7 |
|
4 | | -function matchHostname(url: URL, hostname?: string, allowWildcard?: boolean) { |
5 | | - if (!hostname) { |
6 | | - return true; |
7 | | - } |
8 | | - if (!allowWildcard || !hostname.startsWith('*')) { |
9 | | - return hostname === url.hostname; |
10 | | - } |
11 | | - if (hostname.startsWith('**.')) { |
12 | | - const slicedHostname = hostname.slice(2); // ** length |
13 | | - return slicedHostname !== url.hostname && url.hostname.endsWith(slicedHostname); |
14 | | - } |
15 | | - if (hostname.startsWith('*.')) { |
16 | | - const slicedHostname = hostname.slice(1); // * length |
17 | | - const additionalSubdomains = url.hostname |
18 | | - .replace(slicedHostname, '') |
19 | | - .split('.') |
20 | | - .filter(Boolean); |
21 | | - return additionalSubdomains.length === 1; |
22 | | - } |
23 | | - |
24 | | - return false; |
25 | | -} |
26 | | -function matchPort(url: URL, port?: string) { |
27 | | - return !port || port === url.port; |
28 | | -} |
29 | | -function matchProtocol(url: URL, protocol?: string) { |
30 | | - return !protocol || protocol === url.protocol.slice(0, -1); |
31 | | -} |
32 | | -function matchPathname(url: URL, pathname?: string, allowWildcard?: boolean) { |
33 | | - if (!pathname) { |
34 | | - return true; |
35 | | - } |
36 | | - if (!allowWildcard || !pathname.endsWith('*')) { |
37 | | - return pathname === url.pathname; |
38 | | - } |
39 | | - if (pathname.endsWith('/**')) { |
40 | | - const slicedPathname = pathname.slice(0, -2); // ** length |
41 | | - return slicedPathname !== url.pathname && url.pathname.startsWith(slicedPathname); |
42 | | - } |
43 | | - if (pathname.endsWith('/*')) { |
44 | | - const slicedPathname = pathname.slice(0, -1); // * length |
45 | | - const additionalPathChunks = url.pathname |
46 | | - .replace(slicedPathname, '') |
47 | | - .split('/') |
48 | | - .filter(Boolean); |
49 | | - return additionalPathChunks.length === 1; |
50 | | - } |
51 | | - |
52 | | - return false; |
53 | | -} |
54 | | -function matchPattern(url: URL, remotePattern: RemotePattern) { |
55 | | - return ( |
56 | | - matchProtocol(url, remotePattern.protocol) && |
57 | | - matchHostname(url, remotePattern.hostname, true) && |
58 | | - matchPort(url, remotePattern.port) && |
59 | | - matchPathname(url, remotePattern.pathname, true) |
60 | | - ); |
61 | | -} |
62 | 8 | export function isRemoteAllowed( |
63 | 9 | src: string, |
64 | 10 | { |
|
0 commit comments