Skip to content

Instantly share code, notes, and snippets.

@pkieltyka
Last active November 30, 2022 14:37
Show Gist options
  • Save pkieltyka/d6830df1f95ba5d2fbdef89847804060 to your computer and use it in GitHub Desktop.
Save pkieltyka/d6830df1f95ba5d2fbdef89847804060 to your computer and use it in GitHub Desktop.

Revisions

  1. pkieltyka revised this gist Apr 2, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions cors-anywhere.js
    Original file line number Diff line number Diff line change
    @@ -71,8 +71,7 @@ async function handleRequest(event, request) {
    resp.headers.set('content-type', v)
    }

    // Cache API respects Cache-Control headers, so by setting max-age to 10
    // the response will only live in cache for max of 61 seconds
    // set caching and cors headers
    resp.headers.set('X-Worker', (new Date()).toUTCString())
    resp.headers.set('Cache-Control', 'public, max-age=61')
    resp.headers.set('Access-Control-Allow-Origin', '*')
  2. pkieltyka revised this gist Apr 2, 2020. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions cors-anywhere.js
    Original file line number Diff line number Diff line change
    @@ -28,14 +28,10 @@ async function handleRequest(event, request) {
    param = param.substr(1)
    }
    if (param === "") {
    return respondWithText(
    "welcome"
    )
    return respondWithText("welcome")
    }
    if (!param.startsWith("http")) {
    return respondWithText(
    "Oops! invalid use, speak to dev team for correct usage."
    )
    return respondWithText("Oops! invalid use, speak to dev team for correct usage.")
    }
    if (param.indexOf("://") < 0) {
    param = param.replace("http:/", "http://")
  3. pkieltyka created this gist Apr 2, 2020.
    121 changes: 121 additions & 0 deletions cors-anywhere.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    //
    // CORS Anywhere -- Cloudflare Workers edition!
    //
    // try: https://cors-anywhere.0xhorizon.workers.dev/https://discordapp.com/api/guilds/444586810765475860/widget.json
    //

    addEventListener('fetch', event => {
    event.respondWith(handleRequest(event, event.request))
    })

    async function handleRequest(event, request) {
    // Handle OPTIONS request quickly
    if (request.method === "OPTIONS") {
    return handleCORSPreflight(request)
    } else if (request.method !== "GET" && request.method !== "HEAD") { //&& request.method !== "POST") {
    return new Response(null, {
    status: 405,
    statusText: "Method Not Allowed",
    })
    }

    // Handle normal GET/HEAD request
    let url = new URL(request.url)
    let host = url.host
    let param = url.pathname

    if (param.length >= 1) {
    param = param.substr(1)
    }
    if (param === "") {
    return respondWithText(
    "welcome"
    )
    }
    if (!param.startsWith("http")) {
    return respondWithText(
    "Oops! invalid use, speak to dev team for correct usage."
    )
    }
    if (param.indexOf("://") < 0) {
    param = param.replace("http:/", "http://")
    param = param.replace("https:/", "https://")
    }

    // Proxy-cache the request (with ttl of 60 seconds)
    let cacheUrl = new URL(param)
    let cacheKey = new Request(cacheUrl)
    let cache = caches.default

    // Get this request from this zone's cache
    let resp = await cache.match(cacheKey)

    if (!resp) {
    //if not in cache, grab it from the origin
    originResp = await fetch(cacheUrl, {
    method: request.method,
    headers: {
    ...request.headers
    },
    cf: {
    cacheEverything: true,
    cacheTtl: 61,
    scrapeShield: false
    }
    })

    // must use Response constructor to inherit all of response's fields
    resp = new Response(originResp.body, {
    status: originResp.status
    })

    // copy over certain headers, but we don't want all..
    let v = originResp.headers.get('content-type')
    if (v && v !== '') {
    resp.headers.set('content-type', v)
    }

    // Cache API respects Cache-Control headers, so by setting max-age to 10
    // the response will only live in cache for max of 61 seconds
    resp.headers.set('X-Worker', (new Date()).toUTCString())
    resp.headers.set('Cache-Control', 'public, max-age=61')
    resp.headers.set('Access-Control-Allow-Origin', '*')
    resp.headers.set('Access-Control-Allow-Methods', 'GET, HEAD')

    // store the fetched response as cacheKey
    await cache.put(cacheKey, resp.clone())
    }

    return resp
    }

    function respondWithText(text) {
    return new Response(text, { status: 200, headers: { "Content-Type": "text/html" } })
    }

    // We support the GET, POST, HEAD, and OPTIONS methods from any origin,
    // and accept the Content-Type header on requests. These headers must be
    // present on all responses to all CORS requests. In practice, this means
    // all responses to OPTIONS requests.
    const corsHeaders = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type",
    }

    function handleCORSPreflight(request) {
    if (request.headers.get("Origin") !== null &&
    request.headers.get("Access-Control-Request-Method") !== null) {
    // Handle CORS pre-flight request.
    return new Response(null, {
    headers: corsHeaders
    })
    } else {
    // Handle standard OPTIONS request.
    return new Response(null, {
    headers: {
    "Allow": "GET, HEAD, POST, OPTIONS",
    }
    })
    }
    }