gja / cloudflare-worker-local

Run a Cloudflare Worker Locally

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

put raw fetch in worker context?

jdanyow opened this issue Β· comments

First of all- thank you for this project! Awesome work πŸš€

I think the fetch method exposed in the worker context should be "standard fetch" for compatibility. Couple examples:

  • using the fetch(url, init) signature in the worker currently doesn't work because fetchUpstream doesn't implement the overload.
  • fetchUpstream overwrites the host on all requests

https://github.com/gja/cloudflare-worker-local/blob/master/app/worker.js#L16

  evaluateWorkerContents(workerContents) {
-   const context = { Request, Response, Headers, URL };
+   const context = { Request, Response, Headers, URL, fetch };
    const script = new Script(workerContents);
    script.runInContext(
      createContext(
        Object.assign(context, {
-         fetch: this.fetchUpstream.bind(this),
          addEventListener: this.addEventListener.bind(this),
          triggerEvent: this.triggerEvent.bind(this)
        })
      )
    );
  }

Would you accept a PR with these changes (and tests)?

Hi, Thanks for the PR.

Below is a list of functionality (as I believe CF does it). A lot of this is guesswork.

Let's say you make a request for foo.com/something, which is proxied by a cloudflare worker.

  1. If the request that is finally fetched is for foo.com/*, then it is sent to the upstream host (with the host header set, presumably)
  2. All other requests can be sent to the respective origin.

The current PR will not work in case #1. However, if you can figure out a way to get both scenarios to work, I'd be very happy to merge it. This is one of the things I had on my pending items.