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

Workaround cloudflare bugs #1123

Merged
merged 3 commits into from
Apr 19, 2021
Merged

Workaround cloudflare bugs #1123

merged 3 commits into from
Apr 19, 2021

Conversation

Rich-Harris
Copy link
Member

Encountered some subtle but fatal issues trying to run SvelteKit in a cloudflare worker. For some reason this code...

return Reflect.get(response, key, receiver);
...results in the following error:

TypeError: Method ok called on incompatible receiver [object Object]

I don't understand how, but changing it to Reflect.get(response, key, response) seems to do the trick.

Separately, the Headers object (in the global scope, or instantiated as the headers property of a Request instance) doesn't appear to follow the standard. In a browser, Headers.prototype contains a forEach method; in a worker, that method is missing, but there's an additional getAll method which isn't present in browsers (perhaps because the only header that needs to be represented with multiple values, rather than being comma-separated, is the set-cookie header which can never appear in a browser).

Regardless of the origin of the bug, this PR appears to fix it.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts

@vercel
Copy link

vercel bot commented Apr 19, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/sveltejs/kit-demo/67DX69nM73aawXEz7XfapBgzDfad
✅ Preview: https://kit-demo-git-workaround-cloudflare-bugs-sveltejs1.vercel.app

@Rich-Harris Rich-Harris merged commit a4a1075 into master Apr 19, 2021
@Rich-Harris Rich-Harris deleted the workaround-cloudflare-bugs branch April 19, 2021 02:45
@@ -210,7 +210,7 @@ export async function load_node({

// TODO arrayBuffer?

return Reflect.get(response, key, receiver);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not Workers-specific. It's an illegal invocation in a browser runtime, too. For example:

let res = new Response('hello', { status: 201 });
let proxy1 = new Proxy(res, {
    get(r, k, rec) {
        console.log({ r, k, rec });
        return Reflect.get(r, k, rec);
    }
});

proxy1.status;
// logs: {r: Response, k: "status", rec: Proxy}
// throws: Uncaught TypeError: Illegal invocation

let proxy2 = new Proxy(res, {
    get(r, k) {
        console.log({ r, k });
        return Reflect.get(r, k, r);
        // or return Reflect.get(r, k);    
    }
});

proxy2.status;
// logs: {r: Response, k: "status" }
//=> 201

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting, thanks. I guess node-fetch is the thing with aberrant behaviour. The Headers issue stands though AFAICT

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I forwarded the report. Thanks again for raising it 👍

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

Successfully merging this pull request may close these issues.

None yet

2 participants