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

Error trying to use in a Cloudflare Worker #207

Closed
jaymakes11 opened this issue Nov 28, 2019 · 13 comments
Closed

Error trying to use in a Cloudflare Worker #207

jaymakes11 opened this issue Nov 28, 2019 · 13 comments

Comments

@jaymakes11
Copy link

jaymakes11 commented Nov 28, 2019

I'm seeing the following error while trying to use the driver in a Cloudflare Worker:

ReferenceError: XMLHttpRequest is not defined

This is making it impossible to use Fauna from Cloudflare Workers, unless there's some workaround that I haven't yet considered.

@aequasi Sounds like you're using Workers, are you seeing this issue?

Note: Originally opened this issue on the closed #182 but then realized it's likely best to create a new issue for it.

@BrunoQuaresma
Copy link
Contributor

Hi @lolcoolkat thanks for the fix.

  1. We used cross-fetch because some environments was having compatibility issues. So we need to find another solution instead of just remove it.

  2. I have to check what it is doing.

@BrunoQuaresma
Copy link
Contributor

@lolcoolkat NodeJS does not support fetch() natively so we need to use some polyfill like cross-fetch to make driver works on both environments (browser and nodejs). Unfortunately, we didn't find out a polyfill that was not using XMLHttpRequest however we are brainstorming to see what we can do about this issue so please, let us know if you have any ideas on your mind.

@eigilsagafos
Copy link

@BrunoQuaresma We use Axios in the browser and on our node backend. Have you considered using that?

@BrunoQuaresma
Copy link
Contributor

BrunoQuaresma commented Dec 11, 2019

@eigilsagafos I think axios uses XMLHttpRequests right?

@eigilsagafos
Copy link

Yeah... you are right. Was just looking at that.

@eigilsagafos
Copy link

I know some packages build for different environments and allow you to require like require("fauna/browser") and require("fauna/node"). Maybe you could even provide a barebones version where you could provide your own http adapter?

@BrunoQuaresma
Copy link
Contributor

Yeah, we are thinking on this solution as well but it adds some extra effort to define a common API interface that should work for these environments. Sounds strange to me a serverless environment does not support core NodeJS stuff. 🤔

@BrunoQuaresma
Copy link
Contributor

BrunoQuaresma commented Dec 11, 2019

Yeah Cloudflare Workers run ontop of V8 and without all the NodeJS overhead.. that's why they are super quick and performant!

Cool, that makes sense.

Doesn't have to be exactly like that, but hopefully you get the idea :D And that config would allow you to specify whether to use cross-fetch or not?

I think it is a nice suggestion. We will evaluate that. Thanks for the contribution @lolcoolkat

@Ayplow
Copy link

Ayplow commented Dec 18, 2019

Is there any reason cross-fetch doesn't just use the global fetch when it exists?

@BrunoQuaresma
Copy link
Contributor

@lolcoolkat yes it is! We just need to add some tests and an extra environment in our pipeline to check things on V8 environments. Thanks for the suggestion.

@BrunoQuaresma
Copy link
Contributor

Fixes on https://www.npmjs.com/package/faunadb/v/2.11.2-beta. We are releasing this soon on the main version.

@fireridlle
Copy link
Contributor

just a small note. Cloudflare doesn't implement AbortController which we need to terminate requests as well as streams. so it better to pass customized fetch. for example:

            const client = new faunadb.Client({
                'secret': 'your secret',
                fetch: (url, params) => {
                    const signal = params.signal
                    delete params.signal
                    const abortPromise = new Promise(resolve => {
                        if(signal) {
                            signal.onabort = resolve
                        }
                    })
                    return Promise.race([abortPromise, fetch(url, params)])
                }
            })

@Mhmdrza
Copy link

Mhmdrza commented Mar 14, 2021

just a small note. Cloudflare doesn't implement AbortController which we need to terminate requests as well as streams. so it better to pass customized fetch. for example:

            const client = new faunadb.Client({
                'secret': 'your secret',
                fetch: (url, params) => {
                    const signal = params.signal
                    delete params.signal
                    const abortPromise = new Promise(resolve => {
                        if(signal) {
                            signal.onabort = resolve
                        }
                    })
                    return Promise.race([abortPromise, fetch(url, params)])
                }
            })

this one actually works :O) previous solution fetch.bind(globalThis) Isn't work with fauna v4.x.x

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

6 participants