Giter Site home page Giter Site logo

Comments (4)

geelen avatar geelen commented on July 28, 2024

Hmm, what would be the equivalent Workers code here? You'd have to actually be calling fetch on the new Request object to fire the subrequest, right?

from fab.

waltonseymour avatar waltonseymour commented on July 28, 2024

very similar code for cloudflare workers:

addEventListener("fetch", event => {
  try {
    event.respondWith(handleEvent(event));
  } catch (e) {
    event.respondWith(new Response("Internal Error", { status: 500 }));
  }
});

async function handleEvent(event) {
   const url = new URL(event.request.url);
   const forwardedRequest = new Request(event.request);
   forwardedRequest.headers.set("host", "httpbin.org");
   return fetch(new Request(`https://httpbin.org${url.pathname}${url.search}`, forwardedRequest));
}

deployed here: https://mute-queen-572f.vanta-preview.workers.dev/cookies and can be tested via https://mute-queen-572f.vanta-preview.workers.dev/cookies/set?freeform=testing123

from fab.

geelen avatar geelen commented on July 28, 2024

I think if you port that logic more directly to the FAB it should work:

  const forwarded_request = new Request(request);
  forwarded_request.headers.set("host", "httpbin.org");
  return fetch(new Request(
   `https://httpbin.org${url.pathname}${url.search}`,
    forwarded_request
  ));

Note the addition of fetch. The whole pattern of returning a Request object was something that was mainly useful on AWS Lambda, so on Cloudflare I'd recommend always returning Responses

from fab.

weberjm avatar weberjm commented on July 28, 2024

@geelen Quick question on this: is this documented anywhere? I asked a question about the Cloudflare behavior back in June on Discord, with no response. Is this something that was supposed to already be known?

from fab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.