Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Intercept requests made by the worker #32

Closed
wtuzza opened this issue Aug 16, 2021 · 3 comments
Closed

[Feature Request]: Intercept requests made by the worker #32

wtuzza opened this issue Aug 16, 2021 · 3 comments

Comments

@wtuzza
Copy link

wtuzza commented Aug 16, 2021

Hey! First thanks for this awesome project, We are currently using the one from dollar shave club but that project died :-(

We need a way to strip the added Cloudflare headers added in MiniFlare as it causes Cloudflare to block the request. We need to route to our upstream via cloudflare as there is no direct access to the servers.

So one thought was a way to have some sort of middleware we can write to strip the needed headers to make it work
Or maybe there can be an option to not add any CF headers when the worker makes fetch requests.

Looking forward to hearing your opinion!

@mrbbot
Copy link
Contributor

mrbbot commented Aug 16, 2021

Hey! 👋 Workers themselves are basically middleware. You could do the header stripping yourself in your worker script:

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

async function handleRequest(request) {
  const headers = new Headers(request.headers);
  headers.delete("CF-Ray");
  // ...and other headers
  return fetch(request, { headers });
}

If you only wanted to remove headers during development, you could either use the define option (e.g. https://webpack.js.org/plugins/define-plugin/, https://esbuild.github.io/api/#define) of your build tool (if you're using one) or add a custom binding:

$ miniflare --binding __MINIFLARE=true
addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const headers = new Headers(request.headers);
  if (globalThis.__MINIFLARE) {
    headers.delete("CF-Ray");
    // ...and other headers
  }
  return fetch(request, { headers });
}

We create a new Headers object as event.request.headers is immutable (well it should be, Miniflare's isn't but don't tell anyone 🤫).

@wtuzza
Copy link
Author

wtuzza commented Aug 16, 2021

Should have thought of that haha. Let me do some testing :-)

Btw do you have github sponsors or something similar?

@mrbbot
Copy link
Contributor

mrbbot commented Aug 18, 2021

Not at the moment, but I might look into it in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants