1 | import escapeStringRegexp from 'escape-string-regexp';
|
2 |
|
3 | export default function extractPathFromURL(prefixes: string[], url: string) {
|
4 | for (const prefix of prefixes) {
|
5 | const protocol = prefix.match(/^[^:]+:/)?.[0] ?? '';
|
6 | const host = prefix
|
7 | .replace(new RegExp(`^${escapeStringRegexp(protocol)}`), '')
|
8 | .replace(/\/+/g, '/')
|
9 | .replace(/^\//, '');
|
10 |
|
11 | const prefixRegex = new RegExp(
|
12 | `^${escapeStringRegexp(protocol)}(/)*${host
|
13 | .split('.')
|
14 | .map((it) => (it === '*' ? '[^/]+' : escapeStringRegexp(it)))
|
15 | .join('\\.')}`
|
16 | );
|
17 |
|
18 | const normalizedURL = url.replace(/\/+/g, '/');
|
19 |
|
20 | if (prefixRegex.test(normalizedURL)) {
|
21 | return normalizedURL.replace(prefixRegex, '');
|
22 | }
|
23 | }
|
24 |
|
25 | return undefined;
|
26 | }
|