-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb35c3f
commit 21a6d0d
Showing
2 changed files
with
49 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,40 @@ | ||
export default { | ||
async fetch(request) { | ||
const corsHeaders = { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", | ||
"Access-Control-Max-Age": "86400", | ||
}; | ||
|
||
const PROXY_ENDPOINT = "/"; | ||
|
||
|
||
async function handleRequest(request) { | ||
const url = new URL(request.url); | ||
let apiUrl = url.searchParams.get("api"); | ||
|
||
request = new Request(apiUrl, request); | ||
request.headers.set("Origin", new URL(apiUrl).origin); | ||
let response = await fetch(request); | ||
response = new Response(response.body, response); | ||
response.headers.set("Access-Control-Allow-Origin", url.origin); | ||
response.headers.append("Vary", "Origin"); | ||
|
||
return response; | ||
} | ||
|
||
async function handleOptions(request) { | ||
if ( | ||
request.headers.get("Origin") !== null && | ||
request.headers.get("Access-Control-Request-Method") !== null && | ||
request.headers.get("Access-Control-Request-Headers") !== null | ||
) { | ||
return new Response(null, { | ||
headers: { | ||
...corsHeaders, | ||
"Access-Control-Allow-Headers": request.headers.get( | ||
"Access-Control-Request-Headers" | ||
), | ||
}, | ||
}); | ||
} else { | ||
return new Response(null, { | ||
headers: { | ||
Allow: "GET, HEAD, POST, OPTIONS", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
const url = new URL(request.url); | ||
if (url.pathname.startsWith(PROXY_ENDPOINT)) { | ||
if (request.method === "OPTIONS") { | ||
return handleOptions(request); | ||
} else if ( | ||
request.method === "GET" || | ||
request.method === "HEAD" || | ||
request.method === "POST" | ||
) { | ||
|
||
return handleRequest(request); | ||
} else { | ||
return new Response(null, { | ||
status: 405, | ||
statusText: "Method Not Allowed", | ||
}); | ||
} | ||
} else { | ||
return; | ||
} | ||
}, | ||
}; | ||
|
||
//se modifica worker de cloudflare y se adapta para recibir solo url de la api de destino como parametro | ||
// forked from: https://github.com/chebyrash/cors | ||
addEventListener("fetch", event => { | ||
event.respondWith(handleRequest(event.request)) | ||
}) | ||
|
||
async function handleRequest(request) { | ||
try { | ||
const url = new URL(request.url); | ||
|
||
if (url.pathname === "/") { | ||
return new Response(`{"usage": "${url.origin}/<url>"}`); | ||
} | ||
|
||
function addHeaders(response) { | ||
response.headers.set("Access-Control-Allow-Origin", "*"); | ||
response.headers.set("Access-Control-Allow-Credentials", "true"); | ||
response.headers.set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT"); | ||
response.headers.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); | ||
} | ||
|
||
let response; | ||
if (request.method == "OPTIONS") { | ||
response = new Response(""); | ||
addHeaders(response); | ||
return response; | ||
} | ||
|
||
response = await fetch(request.url.slice(url.origin.length + 1), { | ||
method: request.method, | ||
headers: request.headers, | ||
redirect: "follow", | ||
body: request.body | ||
}); | ||
response = new Response(response.body, response) | ||
addHeaders(response); | ||
return response; | ||
} catch (e) { | ||
return new Response(e.stack || e, {status: 500}); | ||
} | ||
} |