
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Timer mechanism to place upper bound on rate of events.
npm install cooldown
This example reads lines of text from stdin. When 'spam' is entered, it will output 'spam' but only at a rate of once per 5 seconds. Entering 'reset' can reset the cooldown and allow 'spam' to succeed immediately after. Entering 'ready?' will display the state of the cooldown timer. The 'ready' event listener will automatically print when the timer is off cooldown.
var Cooldown = require('cooldown');
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Set limit to 5s
var cd = new Cooldown(5000);
cd.on('ready', console.log.bind('console', 'off cooldown'));
rl.on('line', function (line) {
switch (line) {
case 'spam':
if (cd.fire()) {
console.log('have some spam');
} else {
console.log('not yet');
}
break;
case 'ready?':
console.log(cd.ready ? 'yep' : 'nope');
break;
case 'reset':
// reset the cooldown
cd.reset();
break;
case 'quit':
cd.destroy();
rl.close();
break;
}
});
A cooldown timer with two states:
Return true if the timer was ready (and puts it on-cooldown).
If timer is on-cooldown, reset it back to ready.
Clear any timeouts and set timer to on-cooldown. It will never enter the 'ready' state unless it is reset.
Contains true if the timer is off cooldown and available to fire, else false.
Emitted whenever the timer comes back off cooldown.
Emitted whenever the timer goes on cooldown.
MIT
v0.2.0
FAQs
Cooldown timer for rate-limiting events
The npm package cooldown receives a total of 1 weekly downloads. As such, cooldown popularity was classified as not popular.
We found that cooldown 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.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.