-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
40 lines (33 loc) · 1.12 KB
/
proxy.js
File metadata and controls
40 lines (33 loc) · 1.12 KB
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
export const config = { runtime: 'edge' };
const WORKER_URL = 'https://proxy-embed.nethriondev.workers.dev';
export default async function handler(request) {
const url = new URL(request.url);
const workerUrl = new URL(url.pathname + url.search, WORKER_URL);
try {
const fetchOptions = {
method: request.method,
headers: request.headers,
};
if (request.method !== 'GET' && request.method !== 'HEAD') {
fetchOptions.body = request.body;
fetchOptions.duplex = 'half';
}
const response = await fetch(workerUrl.toString(), fetchOptions);
const responseHeaders = new Headers(response.headers);
responseHeaders.set('Access-Control-Allow-Origin', '*');
responseHeaders.set('Access-Control-Expose-Headers', '*');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: responseHeaders
});
} catch (error) {
return new Response('Bad Gateway: Could not reach upstream', {
status: 502,
headers: {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*'
}
});
}
}