diff --git a/README.md b/README.md index 7bd1e66..51e79a9 100644 --- a/README.md +++ b/README.md @@ -17,21 +17,26 @@ Usar FixCors es tan sencillo como: Coloca la siguiente url, antes de la url de la api que deseas consumir. ```html -https://fixcors.site/?api= +https://fixcors.site/ ``` Ejemplo, La url completa debe quedar de la siguiente manera. ```html -https://fixcors.site/?api=https://apps.bolchile.com/api/v1/dolarstatd2 +https://fixcors.site/https://apps.bolchile.com/api/v1/dolarstatd2 ``` ## Licencia -[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/) +[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/) -## Autor +## Fork From + +- [@chebyrash](https://www.github.com/chebyrash/cors) + + +## FixCors Service - [@arielhernandezcl](https://www.github.com/arielhernandezcl) diff --git a/worker.js b/worker.js index a0e66ae..ba42179 100644 --- a/worker.js +++ b/worker.js @@ -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 \ No newline at end of file +// 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}/"}`); + } + + 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}); + } + } \ No newline at end of file