Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Yet another cancelable Promise wrapper, inspired by this article from React blog.
# npm
npm install cancelbl
# yarn
yarn add cancelbl
Or you can copy-paste it from here if ES6 sounds good to you.
For React components, that use fetch to update the state, unmounting can lead to the following issue:
setState(…): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op
The correct way to fix this issue, according to the article, is to cancel any callbacks in componentWillUnmount
, prior to unmounting.
Suggested makeCancelable
wraps target promise and returns cancel()
function, which can be called in componentWillUnmount
.
The advantage of this implementation is that it wraps and handles the promise in a single statement. Check out Example section below to see how simple it is.
For complete specification, see cancelbl.test.js.
A typical example of two subsequent fetches without taking care of the method setState
:
constructor()
{
super();
this.state = {
item: {},
relatedItems: {}
};
}
componentDidMount() {
fetchResource(this.props.url)
.then(item => {
this.setState({item})
fetchRelatedResources(item)
.then(relatedItems => {
this.setState({item});
})
.catch(error => { /* handle */ })
})
.catch(error => { /* handle */ });
}
Let's import cancelbl:
import cancelbl from 'cancelbl';
And update the component:
constructor()
{
super();
this.state = {
item: {},
relatedItems: {}
};
// set initial cancel object:
this.cancel = cancelbl.default;
}
componentDidMount() {
// wrap promises with .make and .with:
this.cancel = cancelbl.make(
fetchResource(this.props.url),
item => {
this.setState({item});
this.cancel.with(
fetchRelatedResources(item),
relatedItems => this.setState({relatedItems}),
error => { /* handle */ }
);
},
error => { /* handle */ }
);
}
componentWillUnmount() {
// the magic happens here:
this.cancel.do();
}
Here we go.
# with npm
npm test
# with yarn
yarn test
Please check out react-unplug
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
SemVer is used for versioning. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
Yet another make-cancelable Promise wrapper
The npm package cancelbl receives a total of 4 weekly downloads. As such, cancelbl popularity was classified as not popular.
We found that cancelbl 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 researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.