New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

event-horizon

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-horizon - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

46

event-horizon.js

@@ -5,20 +5,40 @@ /*global module*/

module.exports.instance = function (conf) {
var counter = 0,
startTime = new Date().getTime();
var timeArray = [];
function register(time) {
timeArray.push(time);
}
function gc(time) {
var max = timeArray.length,
index = 0;
for (index = 0; index < max; index += 1) {
if (time - timeArray[index] < conf.max) {
break; // we reached the window again
}
}
return timeArray.slice(index);
}
return {
run: function (func, eatenCB) {
var diff = new Date().getTime() - startTime;
if (diff < conf.window) {
if (counter < conf.max) {
func();
} else {
eatenCB();
}
} else {
startTime = new Date().getTime();
counter = 0;
var time = new Date().getTime();
if (timeArray.length === 0) {
register(time);
func();
return;
}
counter += 1;
timeArray = gc(time);
if (timeArray.length >= conf.max) {
eatenCB();
return;
}
register(time);
func();
}

@@ -25,0 +45,0 @@ };

{
"name": "event-horizon",
"version": "0.0.5",
"version": "0.1.0",
"description": "Stay below a certain number of things per second",

@@ -5,0 +5,0 @@ "main": "event-horizon.js",

# event-horizon
Eats things that happen too many times per time window. A time window in this case a clock based window, not a scrolling span.
Eats things that happen too many times per time window.
A time window in this case is a sliding window.
# Example

@@ -7,0 +7,0 @@

@@ -1,2 +0,2 @@

/*global require, describe, it, console*/
/*global require, describe, it, console, setTimeout*/
'use strict';

@@ -7,9 +7,11 @@

function loop (max, t, i, eaten, run) {
function funcFactory(cb) {
return function () {
cb();
};
}
function loop(max, t, i, eaten, run) {
for (i = 0; i < max; i += 1) {
t.run(function () {
run();
}, function () {
eaten();
});
t.run(funcFactory(run), funcFactory(eaten));
}

@@ -25,3 +27,3 @@ }

loop(100, t, i, function () {eaten += 1;}, function () { run += 1; });
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(5, run);

@@ -31,2 +33,22 @@ assert.equal(95, eaten);

it('should use a sliding window', function (done) {
var t = horizon.instance({ window: 20, max: 10 }),
run = 0,
eaten = 0,
i = 0;
this.timeout(500);
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
}, 18);
setTimeout(function () {
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(10, run);
assert.equal(190, eaten);
done();
}, 21);
});
it('should reset when I go over window', function (done) {

@@ -40,7 +62,7 @@ var t = horizon.instance({ window: 10, max: 10 }),

loop(100, t, i, function () {eaten += 1;}, function () { run += 1; });
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
setTimeout(function () {
loop(100, t, i, function () {eaten += 1;}, function () { run += 1; });
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
setTimeout(function () {
loop(100, t, i, function () {eaten += 1;}, function () { run += 1; });
loop(100, t, i, function () {eaten += 1; }, function () { run += 1; });
assert.equal(30, run);

@@ -47,0 +69,0 @@ assert.equal(270, eaten);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc