Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
abort-controller
Advanced tools
The abort-controller npm package provides a way to abort one or more Web API tasks, such as fetch requests, by using an AbortController object. This package is a polyfill for the AbortController interface based on the WHATWG Fetch specification, which allows you to cancel tasks by calling the abort() method on the controller instance.
Creating an AbortController instance
This feature allows you to create a new instance of AbortController, which can be used to generate a signal that can be passed to fetch or other APIs that support aborting.
const AbortController = require('abort-controller');
const controller = new AbortController();
const signal = controller.signal;
Aborting a fetch request
This code sample demonstrates how to use the AbortController to abort an ongoing fetch request. The signal is passed to the fetch call, and the request can be aborted by calling the abort() method on the controller.
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
const controller = new AbortController();
const signal = controller.signal;
fetch('https://example.com', { signal })
.then(response => response.json())
.catch(err => {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
console.error('Fetch error:', err);
}
});
// Abort the request
controller.abort();
The p-cancelable package provides a way to create cancelable promises. It is similar to abort-controller in that it allows you to cancel asynchronous tasks, but it works by wrapping promises rather than using a signal-based approach.
Bluebird is a fully-featured promise library that includes support for cancellation. It is more comprehensive than abort-controller as it provides a full promise implementation with additional features like map, reduce, and filter for promises, but it also includes the ability to cancel promises.
Axios is a promise-based HTTP client for the browser and Node.js that includes a cancellation feature. While abort-controller is focused on providing an AbortController polyfill, axios offers a broader set of HTTP client features, including the ability to cancel requests using a CancelToken.
An implementation of WHATWG AbortController interface.
import AbortController from "abort-controller"
const controller = new AbortController()
const signal = controller.signal
signal.addEventListener("abort", () => {
console.log("aborted!")
})
controller.abort()
Use npm to install then use a bundler.
npm install abort-controller
Or download from dist
directory.
import AbortController from "abort-controller"
// or
const AbortController = require("abort-controller")
// or UMD version defines a global variable:
const AbortController = window.AbortControllerShim
If your bundler recognizes browser
field of package.json
, the imported AbortController
is the native one and it doesn't contain shim (even if the native implementation was nothing).
If you wanted to polyfill AbortController
for IE, use abort-controller/polyfill
.
Importing abort-controller/polyfill
assigns the AbortController
shim to the AbortController
global variable if the native implementation was nothing.
import "abort-controller/polyfill"
// or
require("abort-controller/polyfill")
The AbortSignal object which is associated to this controller.
Notify abort
event to listeners that the signal
has.
Contributing is welcome ❤️
Please use GitHub issues/PRs.
npm install
installs dependencies for development.npm test
runs tests and measures code coverage.npm run clean
removes temporary files of tests.npm run coverage
opens code coverage of the previous test with your default browser.npm run lint
runs ESLint.npm run build
generates dist
codes.npm run watch
runs tests on each file change.FAQs
An implementation of WHATWG AbortController interface.
The npm package abort-controller receives a total of 18,587,432 weekly downloads. As such, abort-controller popularity was classified as popular.
We found that abort-controller demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.