3.1.1 • Published 2 years ago

node-abort-controller v3.1.1

Weekly downloads
91,636
License
MIT
Repository
github
Last release
2 years ago

node-abort-controller

AbortController Polyfill for Node.JS based on EventEmitter for Node v14.6.x and below.

Are you using Node 14.7.0 or above? You don't need this! Node has AbortController and AbortSignal as builtin globals. In Node versions >=14.7.0 and <15.4.0 you can access the experimental implementation using --experimental-abortcontroller.

Example Usage

Timing out fetch

import fetch from "node-fetch";
import { AbortController } from "node-abort-controller";

const controller = new AbortController();
const signal = controller.signal;

await fetch("https:/www.google.com", { signal });

// Abort fetch after 500ms. Effectively a timeout
setTimeout(() => controller.abort(), 500);

Re-usable fetch function with a built in timeout

import { AbortController } from "node-abort-controller";
import fetch from "node-fetch";

const fetchWithTimeout = async (url = "") => {
  const controller = new AbortController();
  const { signal } = controller;

  const timeout = setTimeout(() => {
    controller.abort();
  }, 5000);

  const request = await fetch(url, { signal });

  clearTimeout(timeout);

  const result = await req.json();

  return result;
};

Why would I need this?

You might not need to! Generally speaking, there are three environments your JavaScript code can run in:

  • Node
  • Modern Browsers (Not Internet Explorer)
  • Legacy Browsers (Mostly Internet Explorer)

For modern JS APIs, each environment would ideally get a polyfill:

  • only if it needs one
  • specific to the platform.

In practice, this is hard. Tooling such as webpack and browserify are great at making sure stuff works out of the box in all environments. But it is quite easy to fail on both points above. In all likelyhood, you end up shipping less than ideal polyfills on platforms that don't even need them. So what is a developer to do? In the case of fetch and AbortController I've done the work for you. This is a guide to that work.

If you are building a ...

NodeJS library only supports Node 16 or above

You don't need this library! AbortController is now built into nodeJS . Use that instead.

Web Application running only in modern browsers

You don't need a library! Close this tab. Uninstall this package.

Web Application running in modern browsers AND NodeJS (such as a server side rendered JS app)

Use this package and node-fetch. It is minimally what you need.

Web Application supporting legacy browsers AND NOT NodeJS

Use abort-controller and whatwg-fetch. These are more complete polyfills that will work in all browser environments.

Web Application supporting legacy browsers AND NodeJS

Use abort-controller and cross-fetch. Same as above, except cross-fetch will polyfill correctly in both the browser and node.js

NodeJS Library being consumed by other applications and using fetch internally

Use this package and node-fetch. It is the smallest and least opinionated combination for your end users. Application developers targeting Internet Exploer will need to polyfill AbortController and fetch on their own. But your library won't be forcing unecessary polyfills on developers who only target modern browsers.

Goals

With the above guide in mind, this library has a very specific set of goals:

  1. Provide a minimal polyfill in node.js
  2. Do not provide a polyfill in any browser environment

This is the ideal for library authors who use fetch and AbortController internally and target both browser and node developers.

Prior Art

Thank you @mysticatea for https://github.com/mysticatea/abort-controller. It is a fantastic AbortController polyfill and ideal for many use cases.

fork-ts-checker-webpack-pluginapollo-server-coreeasy-select-rn@mangadex/homeswagger-parameter-extactorkeat-node@leni-msft/oav@infinitebrahmanuniverse/nolb-node-a@deltadao/libflowiseailuminos-ui-corelamlha@everything-registry/sub-chunk-2297jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkg@1xtr/nodejs-sdkposthog-corereact-native-jawwy_samplestepctl-legacy@wacoco/ydd-trace-tsswagger-clientwio-spotifytrn-currency-formatterscptre-tediousreactnativelyrn-check-btnrn-circular-chartrn-horizontal-listreact_native_pandey_marqueern-keyboard-avoiding-viewredlockredlock-temp-fixredlock2tedious-better-data-typestemp-nft-hookssteadybitrouter-loggerrenu-packreact-native-withframework-checktest-zeo-collectswagger-parameters-extractorvision-camera-plugin-face-detectorvantiq-reactverdaccio-redis-search-patchviteflarejawwy_library_newjawy_library_v1gamification-jawwy-libraryframework_test_library_sixdee_new_jawwyreact-native-experts-sdktest_lib_module_aarquickcapture_react_nativern-agora-ios-m@e-hentai/homemetro-file-maplibrarian-nodejs-sdklogarygraphql-language-service-serverjesh-calculationjambotichatyipfs-raceircbloq-resourceipsamasperioresmoleculer-1c-esbkokonahttptoolkit-serverjordy-frijters-test-libnanomanan-react-native-expo-authorize-netnode-openlproptimadeopenblock-resourceori-bot-react-nativeouterbridgepolyglot-component-librarynode-double-takepnm-yph-react-native-custom-componentsnode-module-benchmarkerreact-native-build-vesion-getterreact-native-dimensions-layoutreact-native-bottom-tab-designsreact-native-components-designreact-native-conekta-card-tokenizerreact-native-ctp-odpreact-native-escapereact-native-custome-component-demoreact-native-dummy-viewreact-native-fedlight-dsmreact-native-dff-components-demoreact-native-custom-poccomponentreact-native-custom-poccomponent-nextreact-native-custom-image-carouselnovacap-componentsnon-hostile-nut-js
3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago