/
route.ts
74 lines (59 loc) · 1.5 KB
/
route.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { isUrlAllowed } from '@superside-oss/partytown-gtm/util';
const PASS_THROUGH_HEADERS = [
'content-type',
'cache-control',
'pragma',
'expires',
'date',
'x-gtm-versionid',
'content-security-policy',
'x-xss-protection',
'x-frame-options',
'x-content-type-options'
];
function parseUrl(url: any): URL | null {
try {
return new URL(url);
} catch (err) {}
return null;
}
async function getResponse(maybeUrl: string | null) {
if (!maybeUrl) {
throw new Error('URL is not set');
}
const url = parseUrl(decodeURIComponent(maybeUrl));
if (!(url && isUrlAllowed(url))) {
throw new Error('URL is not on the whitelist');
}
const response = await fetch(url);
if (response.status >= 400) {
throw new Error('Invalid response status code');
}
const data = await response.text();
const headers: { [key: string]: string } = {};
PASS_THROUGH_HEADERS.forEach((headerName) => {
const headerValue = response.headers.get(headerName);
if (headerValue) {
headers[headerName] = headerValue;
}
});
return new Response(data, {
status: response.status,
headers
})
}
export async function GET(req: Request) {
const originalUrl = new URL(req.url);
const forwardUrl = originalUrl.searchParams.get('forward');
try {
const response = await getResponse(forwardUrl);
return response;
} catch (err) {
return new Response('Not found', {
status: 404,
headers: {
'Content-Type': 'text/plain'
}
});
}
}