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

Avoid use of globals where possible #74

Merged
merged 12 commits into from
Jan 7, 2019

Conversation

sholladay
Copy link
Collaborator

Fixes #64

This is similar to PR #69, however I've taken a different approach. Some of my goals include:

  • No regex testing of URLs.
  • Let fetch() handle URL resolution where possible.
  • Guard against ReferenceErrors by not accessing globals when we don't need them.
  • Simplify testing and mocking by treating global, window, and self as not mutually exclusive.

This should make things significantly easier in non-browser environments. In my testing on Node 10+, I was able to remove everything except for fetch and Response in test/_require.js and the test suite passed.

Previously, Ky would find the global object and then assume everything lived on that object, which makes things cumbersome in non-DOM / no-browser environments. Now, we lookup each global we need in each namespace. So for example, in Node you don't have to implement all of window anymore. If you assign a fetch implementation to window.fetch but nothing else, Ky will still be able to find URL in modern versions of Node where it lives on global and you won't have to assign it manually - it will just work.

Further, in all environments, globals are conditionally accessed to avoid unnecessary ReferenceErrors. Ky also no longer relies on the Headers class.

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test/_require.js Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

I like this approach, except for not using Headers.

@sholladay
Copy link
Collaborator Author

@sindresorhus I think this is ready to go, unless you notice anything else.

Note that I included an unrelated fix for the Travis failures. It was still running Node 8.

@sindresorhus sindresorhus changed the title Avoid usage of globals where possible Avoid use of globals where possible Jan 7, 2019
@sindresorhus sindresorhus merged commit ab86789 into sindresorhus:master Jan 7, 2019
@sindresorhus
Copy link
Owner

This is looking great! 👌

@sholladay Would you be interested in joining the project as a maintainer? You have done some really good PRs and you seem interested in the project.

@sholladay sholladay deleted the non-dom branch January 7, 2019 01:30
@sholladay
Copy link
Collaborator Author

@sindresorhus Yes, would love to join. It's an awesome project. :)

@sindresorhus
Copy link
Owner

Yay. Welcome to the project! 🎉

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.

2 participants