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

limireq

v0.2.0

Published

Limireq is a Node.js module that throttles the number of concurrent active requests at a given time. This is useful when batch processing API data without overloading smaller servers and/or the client itself.

Downloads

4

Readme

Limireq

Limireq is a Node.js module that throttles the number of concurrent active requests at a given time. This is useful when batch processing API data without overloading smaller servers and/or the client itself.

Installation

npm install limireq

Usage Example

// Require the module
var Limireq = require('limireq')

// Initialize a new instance
var lr = new Limireq(25) // max of 25 concurrent connections at any time

var usernames = [] // some array containing 50 usernames

// Push a URL or Request.js options object
for (int i = 0; i < 50; i+=2) {
    // Push a URL
    lr.push('http://api.hostname.com/users/' + usernames[i])

    // Push a request-module-compatible object
    lr.push({
        url: 'http://api.hostname.com/users/' + usernames[i]
        ,oauth: {
            consumer_key: 'your_key_here'
            ,consumer_secret: 'ex_girlfriends_dogs_name'
        }
    })
}

// Begin the processing
lr.start()

Event Handling

The responses and their body text will be emitted via the 'data' event:

lr.on('data', function(err, res, body) {
    try {
        var json = JSON.parse(console.log(body))
        doStuffToJsonInputFunction(json)
    } catch (e) {
        console.log(e)
    }
})

When all responses have been emitted, the throttle will emit an 'end' event and enable reuse of the limiter:

lr.on('end', function() {
    console.log('We finished')
})

Callbacks

If a specific request's response needs to be handled differently from the others, you can pass a callback function when you push the request to the pool. This means you will NOT receive a 'data' event for this request:

function callback(err, res, body) {
    // Do stuff
}

lr.push('http://api.hostname.com/users/' + usernames[i], callback)
lr.push({
    url: 'http://api.hostname.com/users/' + usernames[i]
    ,oauth: {
        consumer_key: 'your_key_here'
        ,consumer_secret: 'ex_girlfriends_dogs_name'
    }}, callback)

Reuse

Once the limiter has emitted it's 'end' event, it can reused immediately:

function startSecondJob() {
    lr.push()
    /// push more requests...
    lr.start()
    lr.on('data', function(err, res, body) {
        // do stuff with response
    }).on('end', function() {
        console.log('Completed two batch jobs')
    })
}

lr.on('end', startSecondJob)

Or it can be reinitialized with a new value for the maximum allowed simultaneous connections:

function startSecondJob() {
    lr.init(100) // raise parallel connection limit to 100
    lr.push()
    /// push more requests...
    lr.start()
    lr.on('data', function(err, res, body) {
        // do stuff with response
    }).on('end', function() {
        console.log('Completed two batch jobs')
    })
}

lr.on('end', startSecondJob)