-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add cloudflare worker compatibility #216
Conversation
See/Fixes dnsimple#215 In existing code, this lib detects the `fetch` API by checking for `window` - however, there are a few more environments where `fetch` is now the native http handlingmethod and this library could work automatically. Namely cloudflare workers (v8 isolates) and Web/Service Workers. This PR changes the feature detection to also include WorkerGlobalScope environments, which will detect the above cases. In addition to the detection change, cloudflare workers appear to hoist `require` calls, so this PR also manually hoists the `require` calls that were previously gated behind an early return in the browser case, but wraps them in a try/catch so that if we incorrectly feature detect node, it will preserve the original error and re-throw it once we've passed the feature gate.
Hey @ggalmazor! Curious if you're currently maintainer and if you could take a look! I've self-published this for now so will move on to other things, but I suspect this will help others. |
Hi @jesseditson, Thanks for working on this! We really appreciate it :) I will review it and get back to you as soon as possible. |
Hi @jesseditson! I just noticed that the CI build didn't run for this PR yet. Our security policies require manual approval to run workflows for PRs from public repos like yours. Apologies for that! While reviewing your changes on my computer, I noticed the tests are failing. It looks like Did this happen in your setup, too? I'm afraid we can't merge the PR with failing tests otherwise. After considering your PR, we're also evaluating the option of simplifying this library by removing the feature detection altogether and letting end-users provide a |
Nice to have CI here! I thought I ran the tests locally to make sure I didn't regress, but lemme see if I can expand the suite locally to see what's going on with the CI failure. An injected fetch API would also solve this for me so I'd be happy to move to that API instead if that route is preferable on your end! |
We also see this as a desirable simplification, but we need to work to introduce the change in the safest way possible. In the meantime, your PR would be a valuable addition. We would happily ship this PR if you can fix the issues anyway. |
Hey, @jesseditson, after working for a bit on the code to inject a fetch, I realized that we have an issue with I'm migrating the tests to |
Sounds good! Should I take a look at getting this PR to pass CI still or hold off until the nock changes land? |
Yes, we can punt on that for now. I just created #217 with the migration to |
@jesseditson , I just created #218 using your suggestions from this PR. Let me know what you think :) Ultimately, we decided to leave the On the other hand, end-users can always inject a |
Superceded by #216 ! |
Fixes #215 This PR is based on the work of @jesseditson in #216 and addresses two issues: - We use dynamic imports to build the default `Fetcher` instance, but some JS runtimes don't support them. - We are being too specific when detecting the presence of a `fetch` function, and different JS runtimes might expose it in different locations. To address these issues, this PR: - Eagerly tries to import all the built-in `Fetcher` implementations while tracking any import errors to provide actionable feedback to end-users as early as possible. - Detects the presence of `fetch` in a looser way to avoid differences across specific JS runtimes.
See/Fixes #215
In existing code, this lib detects the
fetch
API by checking forwindow
- however, there are a few more environments wherefetch
is now the native http handlingmethod and this library could work automatically. Namely cloudflare workers (v8 isolates) and Web/Service Workers.This PR changes the feature detection to also include WorkerGlobalScope environments, which will detect the above cases.
In addition to the detection change, cloudflare workers appear to hoist
require
calls, so this PR also manually hoists therequire
calls that were previously gated behind an early return in the browser case, but wraps them in a try/catch so that if we incorrectly feature detect node, it will preserve the original error and re-throw it once we've passed the feature gate.