Skip to content

Commit

Permalink
Update fix worker
Browse files Browse the repository at this point in the history
  • Loading branch information
arielhernandezcl committed Feb 26, 2024
1 parent bb35c3f commit 21a6d0d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 76 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
112 changes: 40 additions & 72 deletions worker.js
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});
}
}

0 comments on commit 21a6d0d

Please sign in to comment.