cloudworker icon indicating copy to clipboard operation
cloudworker copied to clipboard

Throw an error for unimplemented `fetch` options

Open iameli opened this issue 5 years ago • 0 comments

CloudFlare Workers don't implement a variety of fields in the fetch options dict. The ones I know about are credentials, referrer, referrerPolicy, and mode. Here's a minimal example:

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

async function doFetch(request) {
  try {
    const res = await fetch(
    "https://gist.githubusercontent.com/iameli/2d9fd917a1235bb5854cca4b962a7c09/raw/f37c19b8fe5e57340ea7a874effb6b575f6b28ad/example-content.txt",
      {
        credentials: "include",
        referrer: "http://example.com",
        referrerPolicy: "no-referrer-when-downgrade",
        mode: "cors"
      }
    );
    const text = await res.text();
    return new Response(text);
  } catch (e) {
    return new Response(e.stack, { status: 500 });
  }
}

This example runs in cloudworker but fails in the CloudFlare worker itself, with an error like

Error: The 'mode' field on 'RequestInitializerDict' is not implemented.
    at doFetch (worker.js:7:23)
    at worker.js:2:21

Usually this happens when I'm being lazy and copying a fetch() from the V8 inspector — I don't actually need the fields for anything. But we should probably copy CF's behavior here.

iameli avatar Dec 29 '19 23:12 iameli