npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

limits.js

v0.1.4

Published

limits.js helps you to handle API limitations

Downloads

8

Readme

limits.js

Most Web services have certain limitations regarding how many requests your client application can do in specific timeranges. Dealing with these limitations can be tough, especially if the limitations are more complex like for example: "You can do one request in a second but only a maximum of 20 requests in a minute and a maximum of 100 requests in an hour". This is where limits.js comes to help. limits.js makes sure you don't exceed the given limitations. limits.js works with Node.JS and in the Browser.

Sample


limits = require('limits.js');

queue = limits({
    secondly: 1, // allow 1 call per second
    minutely: 2  // allow 2 calls per minute
});

queue.push(function() {
    // this function will run instantly
});

queue.push(function() {
    // this function will run after
    // a 1 second delay
});

queue.push(function() {
    // this function will run
    // after a 60 second delay
    // since we already exceeded
    // the two calls per minute restriction
});

queue.push(function() {
    // this function will not get executed
    // because we have defined an conditional
    // function which defines that this function
    // should only get called if the delay is
    // under 10 seconds.
}, function(delay) {
    return delay < 10000;
});

Installation

Node.JS

npm install limits.js

Browser

There is minified version availible in the build directory.

Usage


var service = limits({

                           // The Number of calls permitted...
    secondly:   Number,    // ...in one second
    minutely:   Number,    // ...in one minute
    quarterly:  Number,    // ...in an quarter of an hour
    hourly:     Number,    // ...in an hour
    daily:      Number,    // ...in one day
    weekly:     Number,    // ...in one week

    history:    Array,     // Array of timestamps ( Date.now() )
                           // from previous calls

    onCall:     Function,  // Callback Function which gets fired
                           // everytime a call gets executed. Passed
                           // in as first argument you get the delay
                           // in milliseconds with which the function
                           // was called.
                           // Can be used to persist the call history.

    onClear:    Function,  // Callback Function which gets fired
                           // everytime a part of the backlog can be cleared.
                           // First parameter is a Timestamp which indicates
                           // from where on to the past it's save to delete
                           // the history.
});

// You can also specify the number of calls permitted
// in an certain timerange like this:

service.secondly(Numeric);
service.minutely(Numeric);
service.quarterly(Numeric);
service.hourly(Numeric);
service.daily(Numeric);
service.weekly(Numeric);

// The predefined ranges don't fit to your requirements?
// No problem, try this:

service.within(timerange, maxcalls);

// With the 'push' method you are able to push a function
// into the execution stack.
// If the call doesn't get aborted by the second conditional function
// you will get an object in return containing an 'delay' property, which indicates
// with which delay the function will get called and an 'timer' property which
// holds the return of the setTimeout function.

var myCall = service.push(function() {
    // Here we do our API call.
    // The execution of this function will get
    // delayed if we have reached the limitations.

}, function(delay) {
    // This is an optional conditional function.
    // It gets an parameter 'delay' passed in which
    // indicates when the call will get executed.
    // If delay is 0 the call will get executed immediately (although asynchronily).

    // Returning 'false' will prevent the call from being executed.
});

License

Copyright (c) 2014 Simon Kusterer Licensed under the MIT license.