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

Support for AbortController #7

Merged
merged 6 commits into from
Nov 2, 2018

Conversation

pimterry
Copy link
Contributor

@pimterry pimterry commented Jul 5, 2018

Fixes #6.

This PR makes requests through this library abortable. The tests should explain the target behaviour pretty clearly I think.

I've got these tests passing locally in Firefox 62 & Chrome 67 - some help testing in more environments would be great!

I'm going to do some downstream integration testing in our usage of this library elsewhere to confirm it works nicely there too, I'll update this once that's done.

I'm not confident that this works in 100% of cases, and it definitely doesn't cover 100% of the spec, so I've left some caveats in the readme. It should cover the vast majority though, will work fine anywhere with full native readable-stream + abortcontroller support (for now just new Chrome) and it shouldn't ever break anything, so it's definitely a significant improvement.

Copy link
Owner

@jonnyreeves jonnyreeves left a comment

Choose a reason for hiding this comment

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

Some lint issues, otherwise lgtm

src/xhr.js Outdated
@@ -1,5 +1,19 @@
import { Headers as HeadersPolyfill } from './polyfill/Headers';

function getAbortError() {
Copy link
Owner

Choose a reason for hiding this comment

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

Nit pick: as this constructs a new instance could we please prefix using make or create instead of get which, IMHO is ambiguous in this regard

README.md Outdated
This is fully in any environment that supports both ReadableStreams & AbortController directly, and has basic support in most other environments, though you may need [a polyfill](https://www.npmjs.com/package/abortcontroller-polyfill) in your own code to use it. To abort a request:

```js
let controller = new AbortController();
Copy link
Owner

Choose a reason for hiding this comment

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

Please prefer const

@pimterry
Copy link
Contributor Author

pimterry commented Jul 5, 2018

@jonnyreeves Linting etc all now fixed, but it now seems to fail because the tests can't start browsers in SauceLabs. Any idea?

@jonnyreeves
Copy link
Owner

Hmm no idea, but this project hasn't been built in quite a long time so it could be due to out of date testrunner dependencies.

I'm on holiday until next weekend so won't be able to take a look before then. If you are happy to take a look then please be my guest 😀

@pimterry
Copy link
Contributor Author

@jonnyreeves sorry, clearly I haven't got around to looking at this. I suspect either you're right and it's a dependency issue, or it's something to do with your SauceLabs config (you might need to update the password or something) but it's hard to debug without any access to the SauceLabs account myself. I'd love to get this merged anyway though, let me know if there's anything I can do.

@jonnyreeves
Copy link
Owner

@pimterry sorry for the delay; I've fixed the tests on master as you can see here - if you rebase you should get a green tick and I'll merge.

@pimterry
Copy link
Contributor Author

pimterry commented Sep 4, 2018

I've rebased.

The new tests fail in SauceLabs though because that's running Chrome 48, which is pretty out of date (January 2016). I think it should definitely pass in 66+, would you be happy for me to update the tests to run in that? In theory it should run in many older versions too, and it'll never break any existing behaviour either way, but as noted in the readme there'll be some differences and only basic support with some older browsers.

Note that these tests are passing happily in IE11 & Firefox (via polyfills), it's just Chrome 48 that has trouble.

README.md Outdated
@@ -56,6 +56,23 @@ fetchStream('/endpoint')
.then(chunks => console.dir(chunks))
```

`AbortController` is also supported in many environments, allowing you to abort ongoing requests.

This is fully in any environment that supports both ReadableStreams & AbortController directly, and has basic support in most other environments, though you may need [a polyfill](https://www.npmjs.com/package/abortcontroller-polyfill) in your own code to use it. To abort a request:
Copy link
Owner

Choose a reason for hiding this comment

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

This is fully in any environment that supports both ReadableStreams & AbortController directly

Is the first sentence missing a word? It doesn't quite read correctly

@jonnyreeves
Copy link
Owner

Thanks for rebasing and investigating @pimterry

From looking at the code, my guess for the failure is because Chrome 48 does not support AbortController (google suggests it was introduced in Chrome 66).

If updating Chrome to v66 makes the tests pass then I'm happy to include that change, however please can you mention that aborting is only supported natively in Chrome 66+ in the README?

Thanks!

@pimterry
Copy link
Contributor Author

pimterry commented Nov 2, 2018

Hi @jonnyreeves sorry for the delay. I looked into this further, because while Chrome 48 doesn't support AbortController, it also shouldn't be breaking like this. The tests do pass in IE11 for example, which has even less support for that.

It turns out this was actually caused by some incorrect polyfill setup in the tests for certain environments (any that do have fetch & readable stream, but have no abort controller), which caused this problem, so I've now fixed that. In these cases, abort still isn't totally supported (unsent and unread requests can be aborted successfully, but requests that are actively being read from can't), but now nothing breaks, the tests pass, and I think this is sufficient for the few older browsers in this situation.

I'v also updated the readme to clarify things a little, and link to caniuse for detailed info on browser support. I think that's everything, let me know if you'd like me to look at anything else.

Copy link
Owner

@jonnyreeves jonnyreeves left a comment

Choose a reason for hiding this comment

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

Fantastic work @pimterry; really appreciate your following up on this and diving deep into why the tests were failing 🎉

@jonnyreeves jonnyreeves merged commit d7e7e1e into jonnyreeves:master Nov 2, 2018
@pimterry
Copy link
Contributor Author

pimterry commented Nov 8, 2018

@jonnyreeves would you mind releasing this to npm, so we can start using it elsewhere? Thanks!

@jonnyreeves
Copy link
Owner

pimterry added a commit to balena-io-modules/balena-request that referenced this pull request Nov 13, 2018
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