-
-
Save khru/88927ddf5fda6e43eb82e6c4b871a4f2 to your computer and use it in GitHub Desktop.
cloudflare-ngrok.worker
This file contains hidden or 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
| const validPaths = [ | |
| { | |
| path: '/zvo', | |
| redirect: 'https://www.google.com/search?q=zvonimir' | |
| }, | |
| { | |
| path: '/javi', | |
| redirect: 'https://www.google.com/search?q=javier' | |
| }, | |
| { | |
| path: '/manu', | |
| redirect: 'https://c2ad-2a0c-5a80-370f-c200-f4e9-35bb-7b8c-be1d.ngrok.io' | |
| } | |
| ]; | |
| const JSON_TYPE = 'application/json'; | |
| const HTTP_SERVER_ERROR = 500; | |
| const headerType = 0; | |
| const headerValue = 1; | |
| addEventListener("fetch", event => { | |
| const { request } = event | |
| return event.respondWith(handleRequest(request).catch( | |
| (err) => new Response(err.stack, { status: HTTP_SERVER_ERROR }) | |
| )) | |
| }); | |
| async function handleRequest(request) { | |
| const { pathname, search } = new URL(request.url); | |
| const { body, params, payload } = request; | |
| const init = { | |
| method: request.method, | |
| headers: await createHeader(request) || {}, | |
| }; | |
| if (body) { | |
| init.body = body; | |
| } | |
| for (const validPath of validPaths) { | |
| if (pathname.startsWith(validPath.path)) { | |
| const cleanPathName = pathname.split(validPath.path)[1] | |
| const destinationURL = validPath.redirect + cleanPathName + search; | |
| const response = await fetch(destinationURL, init); | |
| const results = await gatherResponse(response); | |
| const headers = await createResponseHeaders(results, response); | |
| return new Response(results, headers); | |
| } | |
| } | |
| return await notFoundResponse(); | |
| } | |
| async function notFoundResponse() { | |
| return new Response(JSON.stringify({ error: 'Redirection not found' }), { | |
| status: HTTP_SERVER_ERROR, | |
| headers: { "Content-Type": JSON_TYPE }, | |
| }); | |
| } | |
| async function createHeader(request) { | |
| const headers = { "Content-Type": JSON_TYPE }; | |
| for (const header of request.headers.entries()) { | |
| headers[header[headerType]] = header[headerValue]; | |
| } | |
| return headers; | |
| } | |
| async function createResponseHeaders(results, response) { | |
| const resultObj = JSON.parse(results); | |
| const {name, code, message, className, data, errors} = resultObj; | |
| if (!name && !code && !className) { | |
| return { | |
| status: response.status, | |
| headers: await createHeader(response) | |
| }; | |
| } | |
| return { | |
| headers: await createHeader(response), | |
| status: code, | |
| statusText: message, | |
| name, | |
| className, | |
| errors | |
| } | |
| } | |
| async function gatherResponse(response) { | |
| const { headers } = response | |
| const contentType = headers.get("content-type") || "" | |
| if (contentType.includes(JSON_TYPE)) { | |
| return JSON.stringify(await response.json()) | |
| } | |
| return response.text(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment