Gathering detailed insights and metrics for abortcontroller-polyfill
Gathering detailed insights and metrics for abortcontroller-polyfill
Gathering detailed insights and metrics for abortcontroller-polyfill
Gathering detailed insights and metrics for abortcontroller-polyfill
Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
npm install abortcontroller-polyfill
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
330 Stars
181 Commits
24 Forks
7 Watching
10 Branches
13 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
JavaScript (92.96%)
HTML (3.87%)
Shell (3.17%)
Cumulative downloads
Total Downloads
Last day
2%
278,765
Compared to previous day
Last week
3.8%
1,475,460
Compared to previous week
Last month
9%
6,235,119
Compared to previous month
Last year
-5.2%
74,504,119
Compared to previous year
Minimal stubs so that the AbortController DOM API for terminating fetch()
requests can be used
in browsers that doesn't yet implement it. This "polyfill" doesn't actually close the connection
when the request is aborted, but it will call .catch()
with err.name == 'AbortError'
instead of .then()
.
1const controller = new AbortController(); 2const signal = controller.signal; 3fetch('/some/url', {signal}) 4 .then(res => res.json()) 5 .then(data => { 6 // do something with "data" 7 }).catch(err => { 8 if (err.name == 'AbortError') { 9 return; 10 } 11 }); 12// controller.abort(); // can be called at any time
You can read about the AbortController API in the DOM specification.
1$ npm install --save abortcontroller-polyfill
If you're using webpack or similar, you then import it early in your client entrypoint .js file using
1import 'abortcontroller-polyfill/dist/polyfill-patch-fetch' 2// or: 3require('abortcontroller-polyfill/dist/polyfill-patch-fetch')
If you need to support browsers where fetch is not available at all (for example
Internet Explorer 11), you first need to install a fetch polyfill and then
import the abortcontroller-polyfill
afterwards.
The unfetch npm package offers a minimal fetch()
implementation (though it does not offer for example a Request
class). If you need a polyfill that
implements the full Fetch specification, use the
whatwg-fetch npm package instead. Typically you will
also need to load a polyfill that implements ES6 promises, for example
promise-polyfill, and of course you need to avoid
ES6 arrow functions and template literals.
Example projects showing abortable fetch setup so that it works even in Internet Explorer 11, using both unfetch and GitHub fetch, is available here.
create-react-app enforces the no-undef eslint rule at compile time so if your
version of eslint does not list AbortController
etc as a known global for
the browser
environment, then you might run into an compile error like:
'AbortController' is not defined no-undef
This can be worked around by (temporarily, details here) adding a declaration like:
1 const AbortController = window.AbortController;
If you just want to polyfill AbortController/AbortSignal without patching fetch you can use:
1import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
You can either import it as a ponyfill without modifying globals:
1const { AbortController, abortableFetch } = require('abortcontroller-polyfill/dist/cjs-ponyfill'); 2const { fetch } = abortableFetch(require('node-fetch')); 3// or 4// import { AbortController, abortableFetch } from 'abortcontroller-polyfill/dist/cjs-ponyfill'; 5// import _fetch from 'node-fetch'; 6// const { fetch } = abortableFetch(_fetch);
or if you're lazy
1global.fetch = require('node-fetch'); 2require('abortcontroller-polyfill/dist/polyfill-patch-fetch');
If you also need a Request
class with support for aborting you can do:
1const { AbortController, abortableFetch } = require('abortcontroller-polyfill/dist/cjs-ponyfill'); 2const _nodeFetch = require('node-fetch'); 3const { fetch, Request } = abortableFetch({fetch: _nodeFetch, Request: _nodeFetch.Request}); 4 5const controller = new AbortController(); 6const signal = controller.signal; 7controller.abort(); 8fetch(Request("http://api.github.com", {signal})) 9 .then(r => r.json()) 10 .then(j => console.log(j)) 11 .catch(err => { 12 if (err.name === 'AbortError') { 13 console.log('aborted'); 14 } 15 })
See also Node.js examples here
The abortcontroller-polyfill
works on Internet Explorer 11. However, to use it you must first
install separate polyfills for promises and for fetch()
. For the promise polyfill, you can
use the promise-polyfill
package from npm, and for fetch()
you can use either the whatwg-fetch
npm package (complete fetch implementation) or the unfetch
npm package (not a complete polyfill but it's only 500 bytes large and covers a lot of the basic use cases).
If you choose unfetch
, the imports should be done in this order for example:
1import 'promise-polyfill/src/polyfill'; 2import 'unfetch/polyfill'; 3import 'abortcontroller-polyfill';
See example code here.
The abortcontroller-polyfill
works on Internet Explorer 8. However, since github-fetch
only supports IE 10+ you need to use the fetch-ie8
npm package instead and also note that IE 8 only
implements ES 3 so you need to use the es5-shim
package (or similar). Finally, just like with
IE 11 you also need to polyfill promises. One caveat is that CORS requests will not work out of the box on IE 8.
Here is a basic example of abortable fetch running in IE 8.
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
7 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 7
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More